# reduction variables and evaluation order

9 views (last 30 days)

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?

##### 0 Comments

### Accepted Answer

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:

C=[];

parfor k=1:10

pause(0.1*rand());

disp("k="+k)

C=[C,k];

end

C

##### 8 Comments

Raymond Norris
on 12 Aug 2022

### More Answers (0)

### See Also

### Products

### Community Treasure Hunt

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

Start Hunting!