parfor loops and temporary variables

5 Ansichten (letzte 30 Tage)
James
James am 1 Dez. 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 Kommentar
Michael
Michael am 1 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Titus Edelhofer
Titus Edelhofer am 1 Dez. 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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by