Avoiding broadcast variables in parfor

7 ビュー (過去 30 日間)
federico nutarelli
federico nutarelli 2022 年 12 月 6 日
回答済み: Santosh Fatale 2022 年 12 月 19 日
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

回答 (1 件)

Santosh Fatale
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
Refer to the parfordocumentation for more information.

カテゴリ

Help Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by