Azzera filtri
Azzera filtri

Avoiding broadcast variables in parfor

9 visualizzazioni (ultimi 30 giorni)
federico nutarelli
federico nutarelli il 6 Dic 2022
Risposto: Santosh Fatale il 19 Dic 2022
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

Risposte (1)

Santosh Fatale
Santosh Fatale il 19 Dic 2022
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.

Categorie

Scopri di più su Parallel for-Loops (parfor) in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by