2

我正在运行 Mathematica 7,并且正在尝试Do使用ParallelDo. 以下标准的顺序代码可以正常工作:

len = 10;

A = Table[0, {len}];

Do[
 A[[i]] = i*10;
 , {i, 1, len}]

但是,如果我使用ParallelDo而不是 standard Do,则此代码会给出错误消息:

len = 10;

A = Table[0, {len}];

ParallelDo[
 A[[i]] = i*10;
 , {i, 1, len}]

我收到的错误消息是:

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.

我能做些什么来并行运行这个Do循环吗?

谢谢!

安德鲁·德扬

卡内基·梅隆大学

4

2 回答 2

4

有什么理由不使用 aParallelTable吗?

ParallelTable[i*10,{i, 1, len}]
于 2012-04-09T21:34:29.433 回答
4

我在 Mathematica 8.0.1.0 上没有错误。

但是,代码可能不会按照您的意图执行,因为A它被复制到每个子进程(并且更改是该子进程的本地更改)。因此

ParallelDo[A[[i]] = i*10; Print@A, {i, 1, len}]

印刷

数学结果

最终结果为A= {0, 0, ..., 0}。

相反,您应该SetSharedVariable[A]在初始化后添加A. 结果现在{10,20,30,40,50,60,70,80,90,100}正如预期的那样。

于 2012-04-09T23:05:51.873 回答