parfor loops and temporary variables

5 visualizaciones (últimos 30 días)
James
James el 1 de Dic. de 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 comentario
Michael
Michael el 1 de Dic. de 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.

Iniciar sesión para comentar.

Respuestas (1)

Titus Edelhofer
Titus Edelhofer el 1 de Dic. de 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

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by