Broadcast / Sliced variable implementation
Show older comments
I have a question about the implementation of broadcast variables in a parfor loop. Specifically, for the following code:
% Make some fake data and preallocate output
R = rand(100,100);
output = NaN(100,1);
parfor t = 1:100
% Dynamically choose indices in dimension 1.
rows = someFunction(t);
% Do a calculation using the relevant rows.
output(t) = anotherFxn( R(rows,t) );
end
I get the code Analyzer warning: "The array or structure R is a broadcast variable. This might result in unnecessary communication overhead."
I'm curious if the entire array R is being broadcast to each worker, or if one column of R is being broadcast to each worker. Since each column of R is sliced, I would have thought that only one column of R would be sent to each worker. But the code analyzer message seems to suggest differently.
Practically, is there any difference between the above code and the following:
% Make some fake data and preallocate output
R = rand(100,100);
output = NaN(100,1);
parfor t = 1:100
% Slice a column
Rslice = R(:,t);
% Dynamically choose indices in dimension 1.
rows = someFunction(t);
% Do a calculation using the relevant rows.
output(t) = anotherFxn( Rslice(rows) );
end
with respect to how much of R is sent to each worker?
Thanks!
Accepted Answer
More Answers (0)
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!