- 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.
Broadcast / Sliced variable implementation
조회 수: 2 (최근 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!
댓글 수: 0
채택된 답변
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:
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!