Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #9676 > unrolled thread
| Started by | Alexei Boulbitch <Alexei.Boulbitch@iee.lu> |
|---|---|
| First post | 2012-04-17 10:06 +0000 |
| Last post | 2012-04-17 10:06 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.soft-sys.math.mathematica
Re: Accessing results in ParallelDo Alexei Boulbitch <Alexei.Boulbitch@iee.lu> - 2012-04-17 10:06 +0000
| From | Alexei Boulbitch <Alexei.Boulbitch@iee.lu> |
|---|---|
| Date | 2012-04-17 10:06 +0000 |
| Subject | Re: Accessing results in ParallelDo |
| Message-ID | <jmjfbl$8aj$1@smc.vnet.net> |
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@iee.lu
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web