reduction variables and evaluation order
Show older comments
"A reduction variable accumulates a value that depends on all the iterations together, but is independent of the iteration order. MATLAB allows reduction variables in parfor-loops."
Among the operations one have
% X = X * expr
And we all know for matrix, multiplcation order matters. So why X itcan be considered as valid reduction variable using "*"?
However when I test it it looks like parfor magically multiply in the right order as showed in this code:
A=rand(2,2,10);
% B = A(:,:,1)*A(:,:,2)*...A(:,:,10)
B=1;
for k=1:size(A,3)
B = B*A(:,:,k);
end
B
C=1;
parfor k=1:size(A,3)
pause(0.1*rand()); % This will make the order of each worker more or less random
C = C*A(:,:,k);
end
C
norm(B-C)/norm(B)
Do I missread the doc? There is a descrption in the doc about what works are done by worker and what by client and it is not totally clear to me. Is there any joint+fork occurs when such variable is updated? Order matter or not? Can someone shed a light?
Accepted Answer
More Answers (0)
Categories
Find more on Parallel Computing Toolbox 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!