develooper Front page | perl.ithreads | Postings from September 2008

Lots of context switches with shared array

Thread Next
From:
Thomas Karcher
Date:
September 23, 2008 08:10
Subject:
Lots of context switches with shared array
Message ID:
1222182613.5444.34.camel@localhost
Hi there,

I'm writing a multithreaded merge sort that suffers from massive amounts
of context switches (as seen by vmstat). The outline of the program is
roughly as follows:

====
my @sortdata : shared;	// the values to be sorted
[...]
// starting two threads
new threads( \&mergesort_string, $begin, $half, $threaddepth - 1 );
new threads( \&mergesort_string, $half, $end, $threaddepth - 1 );
[...]

sub mergesort_string {
	my ( $begin, $end, $threaddepth ) = @_;

	// sorting "my" part of @sortdata, as delimited
	// by $begin and $end
}
====

By playing around, I discovered that the "shared" is what causes the
context switches. They take up about 40% of the duration of the program
for an array of 10,000 elements.

I don't need any locking on the elements itself because the merge sort
algorithm itself guarantees in my opinion that there is no conflicting
access to them.

Am I missing something here? How can I avoid those context switches that
make the program essentially useless?


Thanks!

Thomas


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About