parfor loops and temporary variables

조회 수: 3 (최근 30일)
James
James 2011년 12월 1일
I don't know why MATLAB does this. When using a parfor, if we write something like this:
matlabpool(2);
a=1;
parfor b=1:30
a=a+1;
disp(a)
end
matlabpool close
We get an error:
??? Error: The variable a is perhaps intended as a reduction
variable, but is actually an uninitialized
temporary.
I find this horribly frustrating. Surely this could be made to be a warning, and not an error. It needs to be a warning because the behaviour of a is uncertain. But if you know what you are doing and you want to write your own code that interacts with the very nature of the parallel processing, then surely you should be able to do it. Does Mathworks belive it is smarter than those who use their software?
  댓글 수: 1
Michael
Michael 2011년 12월 1일
Surely both workers would read a = 1, then both would return a = 2, and your final answer would be a = 16? This is how I understand a parfor loop cannot be used to sequentially update a single variable.

댓글을 달려면 로그인하십시오.

답변 (1개)

Titus Edelhofer
Titus Edelhofer 2011년 12월 1일
Hi,
it's the disp(a) that parfor doesn't like. The problem is, that in the end a=30 and that's fine. But using a for calling another function is not O.K. (that's in fact an error) ...
Titus

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by