parfor loops and temporary variables
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Michael
le 1 Déc 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.
Réponses (1)
Titus Edelhofer
le 1 Déc 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
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!