Re: Incorrect parallel computation
- To: mathgroup at smc.vnet.net
- Subject: [mg125969] Re: Incorrect parallel computation
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Tue, 10 Apr 2012 02:29:53 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jlrhi8$eoe$1@smc.vnet.net>
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