parfor loops and temporary variables

5 views (last 30 days)
James
James on 1 Dec 2011
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 Comment
Michael
Michael on 1 Dec 2011
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.

Sign in to comment.

Answers (1)

Titus Edelhofer
Titus Edelhofer on 1 Dec 2011
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!