Parfor unnecessary communication overhead

17 views (last 30 days)
Hi all,
I have the below parfor loop which is good when I am wanting to pass thru negative values to evaluate. I am getting the following error/warning from MATLAB which I wonder if there is a faster/optimal way to get to the same point improving the below structure.
Error: The Entire Array or Structure "yA_vals"/"yC_vals"/"yD_vals" is a broadcast variable. This might result in unnecessary communication overhead.
yA_vals = -5:5; n_yA = length(yA_vals);
yC_vals = 0:10; n_yC = length(yC_vals);
yD_vals = -10:0; n_yD = length(yD_vals);
parfor yAidx = 1:n_yA
yA = yA_vals(yAidx);
fprintf('>Job 1 %d of %d at %s...\n',yA,yA_vals(end),datestr(now));
for yCidx = 1:n_yC
yC = yC_vals(yCidx);
fprintf('>>Job 2 %d of %d...\n',yC,yC_vals(end));
for yDidx = 1:n_yD
yD = yD_vals(yDidx);
fprintf('>>>Job 3 %d of %d...\n',yD,yD_vals(end));
[~,~,Total(yAidx,yCidx,yDidx)] = Function(Input,yA,yC,yD);

Accepted Answer

Raymond Norris
Raymond Norris on 19 Apr 2022
This is a warning, not an error, displayed by the Code Analyzer. Not sure how MATLAB would "throw" this warning.
Depending on the size of the matrix/structure, this could be an issue. Specifically, for large size variables. Of course "large" is relative. But in your case, the vectors are very small -- this won't be an issue for you -- so you can use broadcast variables. To see how much data is being sent back and for, look at ticBytes and tocBytes.
If the variables are large, you could consider a couple of options
  • Create additional variables that represent the portition of the larger variable you need. The Code Analyzer gives an example of this.
  • Create temporay variables (created within the parfor). It's not how you'd write MATLAB serial code, but would avoid the transfer. For example
yA_vals = -5:5; n_yA = length(yA_vals);
parfor yAidx = 1:n_yA
% Create the temporary variable yA_vals in the parfor to avoid the
% transfer.
yA_vals = -5:5;
yA = yA_vals(yAidx);
fprintf('>Job 1 %d of %d at %s...\n',yA,yA_vals(end),datestr(now));

More Answers (0)


Find more on Parallel Computing Fundamentals in Help Center and File Exchange





Community Treasure Hunt

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

Start Hunting!