# Broadcast / Sliced variable implementation

조회 수: 7(최근 30일)
Jonathan King 2019년 4월 29일
댓글: Jonathan King 2019년 4월 30일
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!

댓글을 달려면 로그인하십시오.

### 채택된 답변

Edric Ellis 2019년 4월 30일
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.
##### 댓글 수: 1표시숨기기 없음
Jonathan King 2019년 4월 30일
Got it, thank you!

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!