Why does this Mathematica 7 Do loop work sequentially, but generates errors in parallel (using ParallelDo)?
-
30-05-2021 - |
Вопрос
I am running Mathematica 7, and I am trying to run a simple Do
loop in parallel, using ParallelDo
. The following standard, sequential code works fine:
len = 10;
A = Table[0, {len}];
Do[
A[[i]] = i*10;
, {i, 1, len}]
However, if I use ParallelDo
instead of the standard Do
, this code gives error messages:
len = 10;
A = Table[0, {len}];
ParallelDo[
A[[i]] = i*10;
, {i, 1, len}]
The error messages that I get are:
Set::noval: Symbol A in part assignment does not have an immediate value.
Set::noval: Symbol A in part assignment does not have an immediate value.
Set::noval: Symbol A in part assignment does not have an immediate value.
Set::noval: Symbol A in part assignment does not have an immediate value.
Set::noval: Symbol A in part assignment does not have an immediate value.
Set::noval: Symbol A in part assignment does not have an immediate value.
General::stop: Further output of Set::noval will be suppressed during this calculation.
General::stop: Further output of Set::noval will be suppressed during this calculation.
Set::noval: Symbol A in part assignment does not have an immediate value.
Set::noval: Symbol A in part assignment does not have an immediate value.
Set::noval: Symbol A in part assignment does not have an immediate value.
Set::noval: Symbol A in part assignment does not have an immediate value.
Is there anything I can do to run this Do loop in parallel?
Thank you!
Andrew DeYoung
Carnegie Mellon University
Решение
I get no errors on Mathematica 8.0.1.0.
However, the code probably doesn't do what you intended, because A
is copied to each subprocess (and changes are local to that subprocess). Hence
ParallelDo[A[[i]] = i*10; Print@A, {i, 1, len}]
prints
and the final result is A
= {0, 0, ..., 0}.
Instead, you should add SetSharedVariable[A]
after initializing A
. The result is now {10,20,30,40,50,60,70,80,90,100}
, as expected.
Другие советы
Any reason not to use a ParallelTable
?
ParallelTable[i*10,{i, 1, len}]