Broadcast / Sliced variable implementation

2 views (last 30 days)
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

Edric Ellis
Edric Ellis on 30 Apr 2019
You can use ticBytes and tocBytes to show that in your first example, the warning is accurate and R will not be sliced. This limitation is documented as follows:
  • Form of Indexing. Within the first-level of indexing for a sliced variable, exactly one indexing expression is of the form i, i+k, i-k, or k+i. The index i is the loop variable and k is a scalar integer constant or a simple (non-indexed) broadcast variable. Every other indexing expression is a positive integer constant, a simple (non-indexed) broadcast variable, a nested for-loop index variable, colon, or end.
The final sentence is the restriction you're falling foul of here - in your first case, rows is not one of those types of indexing expression.

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!