Avoiding broadcast variables in parfor

8 views (last 30 days)
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

Answers (1)

Santosh Fatale
Santosh Fatale on 19 Dec 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.

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!