Re: Lots of context switches with shared array

[email protected] (Thomas Karcher) Tue, 23 Sep 2008 17:32:44 +0200
Newsgroups perl.ithreads
Message-ID <1222183964.5444.38.camel@localhost>
--=-2bZbnSFG3xnUaUATbUZD
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

Hi Dean,

> > I'm writing a multithreaded merge sort that suffers from massive amount=
s
> > of context switches (as seen by vmstat). The outline of the program is
> > roughly as follows:
> >=20
> > =3D=3D=3D=3D
> > 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 );
> > [...]
> >=20
> > sub mergesort_string {
> > 	my ( $begin, $end, $threaddepth ) =3D @_;
> >=20
> > 	// sorting "my" part of @sortdata, as delimited
> > 	// by $begin and $end
> > }
> > =3D=3D=3D=3D
> >=20
> > 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.
> >=20
> > 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.
> >=20
> > Am I missing something here? How can I avoid those context switches tha=
t
> > make the program essentially useless?
> I'm afraid there isn't much you can do short of writing your own XS code =
that
> bypasses threads::shared. Due to the global lock on the shared interprett=
er (where
> shared variables are actually stored), each thread that touches the array=
 is going
> to be trying to acquire the global lock, ergo lots of lock contention, le=
ading to lots
> of probable context switches.
> You may be able to improve performance by copying parts of the array out =
to a private
> version, but that may not adapt to your algorithm.

Thanks for the enlightening answer. Do you think it is possible to
"trick" threads::shared's lock with a reference to the array, i. e. the
reference is shared but the array itself isn't?


Thanks,
Thomas



--=-2bZbnSFG3xnUaUATbUZD
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: Dies ist ein digital signierter Nachrichtenteil

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkjZDBwACgkQiMyIQtYO79wnkwCfcYb30nQJKdg55SD+z3AAKiiW
E5oAoKy/vxoGa0cvDVQGRWkZSr+Hi43P
=ZZAZ
-----END PGP SIGNATURE-----

--=-2bZbnSFG3xnUaUATbUZD--