向量值是从parfor外部还是从parfor内部访问的 [英] Vector values differ if accessed outside or from within parfor
问题描述
我已经创建了这个测试Matlab脚本文件:
I have created this test Matlab script file:
numbers = [29 37 44 54 62];
for i=1:length(numbers)
fprintf('%d\n', numbers(i));
end
fprintf('***\n');
matlabpool local 5;
parfor i=1:length(numbers)
fprintf('%d\n', numbers(i));
end % image loop
fprintf('***\n')
for i=1:length(numbers)
fprintf('%d\n', numbers(i));
end
matlabpool close;
fprintf('***\n');
for i=1:length(numbers)
fprintf('%d\n', numbers(i));
end
运行它时,始终得到以下输出:
When I run it, I get consistently the following output:
29
37
44
54
62
***
112
111
107
117
115
***
29
37
44
54
62
***
29
37
44
54
62
parfor
块中的fprintf
打印看似随机的数字集,但是它们始终是相同的(112、111、107、117、115).为什么会这样?
The fprintf
within the parfor
block prints the seemingly random set of numbers which is, however, always the same (112, 111, 107, 117, 115). Any idea as to why this is happening?
更新
有趣的是,仅当我从命令行运行脚本时,这种情况才会发生:
Interestingly enough, this only happens if I run the script from command line:
matlabR2012b -nodesktop -nosplash -nodisplay -r "run parfortest.m; exit"
如果我首先打开Matlab会话并在其中运行parfortest
,那么数字将正确打印.
If I first open a Matlab session and run parfortest
there, then the numbers are printed correctly.
推荐答案
这是运行问题,而不是nodektop问题.要验证这一点,您可以尝试
This is specifically a problem with run and not a nodesktop issue. To verify this, you can try
>> run parfortest.m
在MATLAB桌面上,您将找到相同的输出.
from the MATLAB desktop and you'll find the same output.
尽管这不是解决方案;如果您省略运行,而只需使用
Although it's not a solution as such; if you omit run, and just use
>> parfortest
错误的输出将得到纠正.
the bad output will be corrected.
这篇关于向量值是从parfor外部还是从parfor内部访问的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!