MathGroup Archive 2012

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

Search the Archive

Re: Accessing results in ParallelDo

  • To: mathgroup at smc.vnet.net
  • Subject: [mg126103] Re: Accessing results in ParallelDo
  • From: Alexei Boulbitch <Alexei.Boulbitch at iee.lu>
  • Date: Tue, 17 Apr 2012 06:05:14 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

I have been experimenting with parallel computing on Mathematica 8 on a two core MacBook Pro but I can't figure how to access to the results calculated in ParallelDo. Here is a a simple loop that builds a list b by appending random numbers. This runs fine and prints list b as it is generated in each of the two kernels. However, when I ask for b at the end of the loop I get b={}.

ClearAll["Global`*"];
CloseKernels[];
LaunchKernels[];

NoP = 4;
b = {};
ParallelDo[
  b = Append[b, RandomInteger[{1, NoP}]];
  Print["b = ", b],
  {i, 1, 4}
  ];
Print["Final result: b=", b]

b = {4}   <---kernel 2 (my annotation)
b = {3}   <---kernel 1

b = {4,2}   <---kernel 2
b = {3,3}   <---kernel 1

Final result: b={}

My question is, how do I pass the results of ParallelDo into a variable outside the loop?

Thanks

You need to allow your variable b to be distributed between the kernels with SetSharedVariable. Try this:

ClearAll["Global`*"];
CloseKernels[];
LaunchKernels[];


SetSharedVariable[b]
b = {};
ParallelDo[
 AppendTo[b, RandomInteger[{0, 10}]], {i, 1, 6}]

Print["Final result: b=", b]

Final result: b={10,10,4,10,7,7}

Have fun, Alexei

Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone :  +352-2454-2566
Office fax:       +352-2454-3566
mobile phone:  +49 151 52 40 66 44

e-mail: alexei.boulbitch at iee.lu






  • Prev by Date: Re: nonlinearmodelfit problem
  • Next by Date: Using InputField inside MapThread
  • Previous by thread: Accessing results in ParallelDo
  • Next by thread: Understanding patterns