reduction variables and evaluation order
9 views (last 30 days)
This the doc on can read
"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:
% B = A(:,:,1)*A(:,:,2)*...A(:,:,10)
B = B*A(:,:,k);
pause(0.1*rand()); % This will make the order of each worker more or less random
C = C*A(:,:,k);
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?
Matt J on 8 Aug 2022
Edited: Matt J on 8 Aug 2022
First remember that each worker is assigned some consecutive subset of loop iterations. The worker will execute the iterations within its assigned subset in the original, consecutive order. Therefore, each partial sequence of reduction operations will have a predictable result.
Then, once all iterations are complete, the partial reductions are post-consolidated. I suspect that the order in which reduction variables are post-consolidated is also done to preserve the original ordering (and why not, since the processing is sequential anyway at this point).
Note therefore that concatenation works, too, even though we can clearly see that the timing of each iteration's execution is all over the place: