Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9522 > unrolled thread

Incorrect parallel computation

Started byYasha Gindikin <gindikin@gmail.com>
First post2012-04-08 08:17 +0000
Last post2012-04-11 22:22 +0000
Articles 4 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Incorrect parallel computation Yasha Gindikin <gindikin@gmail.com> - 2012-04-08 08:17 +0000
    Re: Incorrect parallel computation David Bailey <dave@removedbailey.co.uk> - 2012-04-10 06:31 +0000
      Re: Incorrect parallel computation Yasha Gindikin <gindikin@gmail.com> - 2012-04-11 22:20 +0000
    Re: Incorrect parallel computation Zach Bjornson <bjornson@stanford.edu> - 2012-04-11 22:22 +0000

#9522 — Incorrect parallel computation

FromYasha Gindikin <gindikin@gmail.com>
Date2012-04-08 08:17 +0000
SubjectIncorrect parallel computation
Message-ID<jlrhi8$eoe$1@smc.vnet.net>
Dear colleagues,

would you please advise me on why the following simple code executes incorrectly when parallelized?

a = Subsets[Range[1, 5], {3}];
L = Length@a;
b = Table[0, {5}];
c = Table[0, {5}];
SetSharedVariable[c];
Do[b[[a[[l, k]]]] += 1, {l, L}, {k, 3}];
ParallelDo[c[[a[[l, k]]]] += 1, {l, L}, {k, 3}];
b===c

False

Thank you!
Best regards,
Yasha Gindikin.

[toc] | [next] | [standalone]


#9541

FromDavid Bailey <dave@removedbailey.co.uk>
Date2012-04-10 06:31 +0000
Message-ID<jm0k4t$agm$1@smc.vnet.net>
In reply to#9522
On 08/04/2012 09:17, Yasha Gindikin wrote:
> Dear colleagues,
>
> would you please advise me on why the following simple code executes incorrectly when parallelized?
>
> a = Subsets[Range[1, 5], {3}];
> L = Length@a;
> b = Table[0, {5}];
> c = Table[0, {5}];
> SetSharedVariable[c];
> Do[b[[a[[l, k]]]] += 1, {l, L}, {k, 3}];
> ParallelDo[c[[a[[l, k]]]] += 1, {l, L}, {k, 3}];
> b===c
>
> False
>
> Thank you!
> Best regards,
> Yasha Gindikin.
>

I think this illustrates the pitfalls of parallel computing - which is 
why I have never used this feature for serous code!


Notice the "Possible issues" section of the help for SetSharedVariable. 
You probably thought your update was atomic, but I doubt if

c[[a[[l, k]]]] += 1

qualifies as an atomic update!

Every now and again I toy with the idea of using parallel constructs - 
so thanks for reminding me of why it is not a good idea, and saving me 
much heartache!

David Bailey
http://www.dbaileyconsultancy.co.uk

[toc] | [prev] | [next] | [standalone]


#9553

FromYasha Gindikin <gindikin@gmail.com>
Date2012-04-11 22:20 +0000
Message-ID<jm503n$2mn$1@smc.vnet.net>
In reply to#9541
On Tuesday, April 10, 2012 10:31:57 AM UTC+4, David Bailey wrote:
> I think this illustrates the pitfalls of parallel computing
I suspect it rather illustrates a bug in a SetSharedVariable realization.

> why I have never used this feature for serous code!
Actually, I wrote a modified Lanczos method for the exact diagonalization study of many-electron system in a quantum dot. I do not know, whether it is serious enough, but it successfully runs in parallel, which saves me a lot of time (literally, days). The only step that fails to parallelize is the last step of the calculation of Fourier components of the density. This is what I essentially describe here.

> You probably thought your update was atomic, but I doubt if
>
> c[[a[[l, k]]]] += 1
>
> qualifies as an atomic update!

It is indeed crucial for SetSharedVariable, however, AtomQ[c[[a[[l, k]]]] += 1] gives True.

Best,
Yasha Gindikin.

[toc] | [prev] | [next] | [standalone]


#9556

FromZach Bjornson <bjornson@stanford.edu>
Date2012-04-11 22:22 +0000
Message-ID<jm506j$2nt$1@smc.vnet.net>
In reply to#9522
On Apr 8, 1:17 am, Yasha Gindikin <gindi...@gmail.com> wrote:
> Dear colleagues,
>
> would you please advise me on why the following simple code executes incorrectly when parallelized?
>
> a = Subsets[Range[1, 5], {3}];
> L = Length@a;
> b = Table[0, {5}];
> c = Table[0, {5}];
> SetSharedVariable[c];
> Do[b[[a[[l, k]]]] += 1, {l, L}, {k, 3}];
> ParallelDo[c[[a[[l, k]]]] += 1, {l, L}, {k, 3}];
> b===c
>
> False
>
> Thank you!
> Best regards,
> Yasha Gindikin.

If you have a situation where each iteration is slow (e.g. where the
time to access the results variable c is negligible relative to the
time to compute its new value), try CriticalSection as below. In this
simplified example, it will be slower than plain Do because of
locking.

a = Subsets[Range[1, 5], {3}];
L = Length@a;
b = Table[0, {5}];
Do[b[[a[[l, k]]]] += 1, {l, L}, {k, 3}];
c = Table[0, {5}];
SetSharedVariable[c];
ParallelDo[CriticalSection[{clock}, c[[a[[l, k]]]] += 1], {l, L}, {k,
3}]
b===c

True

-Zach

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web