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


Groups > comp.soft-sys.math.mathematica > #9556

Re: Incorrect parallel computation

From Zach Bjornson <bjornson@stanford.edu>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Incorrect parallel computation
Date 2012-04-11 22:22 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jm506j$2nt$1@smc.vnet.net> (permalink)
References <jlrhi8$eoe$1@smc.vnet.net>

Show all headers | View raw


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

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

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

csiph-web