Avoiding broadcast variables in parfor
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi all,
the following loop results in an error in C_mat and B_mat:
%previously defined
N_RIPETIZIONI=2;
K=201;
parfor n=1:N_RIPETIZIONI*K
[r,k]=ind2sub([N_RIPETIZIONI,K],n);
B=B_mat{r};
C=C_mat{r};
end
The warning says:
The entire array or structure B_mat is a broadcast variable. This might result in unnecessary communication overhead.
The same for C_mat.
How can I fix it so that the indices of B_mat and C_mat are no more broadcast variables?
Thank you
댓글 수: 0
답변 (1개)
Santosh Fatale
2022년 12월 19일
Hi Federico,
I understand that you are encountering a warning message while using "parfor" in your code.
The warning message is due to the dependency of different parallel pool workers on the same variables, which are accessed by multiple workers at the same time. It is recommended to copy these common variables into local variables in the loop and use local variables for accessing data while running the parallel for loop.
The following code demonstrates this:
% I assumed that variable B_mat and C_mat are already defined.
N_RIPETIZIONI=2;
K=201;
parfor n=1:N_RIPETIZIONI*K
matB = B_mat;
matC = C_mat;
[r,k]=ind2sub([N_RIPETIZIONI,K],n);
B=matB{r};
C=matC{r};
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!