MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Incorrect parallel computation

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

  • Prev by Date: Unable to use Combinatorica Package from Adjacent Matrix
  • Next by Date: Re: Where is ShowGraph?
  • Previous by thread: Incorrect parallel computation
  • Next by thread: Re: Incorrect parallel computation