Effacer les filtres
Effacer les filtres

Avoiding broadcast variables in parfor

7 vues (au cours des 30 derniers jours)
federico nutarelli
federico nutarelli le 6 Déc 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

Réponses (1)

Santosh Fatale
Santosh Fatale le 19 Déc 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.

Catégories

En savoir plus sur Parallel for-Loops (parfor) dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by