Broadcast Variable Warning in MATLAB Parallel Computing Toolbox

When I ran a MATLAB code using MATLAB Parallel Computing Toolbox, I got messages like:
The entire array or structure 'a' is a broadcast variable. This might result in unnecessary communication overhead.
The entire array or structure 'b' is a broadcast variable. This might result in unnecessary communication overhead.
The entire array or structure 'L' is a broadcast variable. This might result in unnecessary communication overhead.
I then got lost. The code is still running though, but I don't know what to do exactly to avoid the messages above. Below is a description of the main part of my code:
a = (5:5:100)'; b = (-0.4:0.1:2)'; L = (0.25:0.25:10)'; la = length(a); lb = length(b); lL = length(L);
parfor jj = 1:2
err1 = zeros(la,lb,lL);
for i = 1:la
if jj == 1, bla bla bla; else, bla bla bla; end
for j = 1:lb
for k = 1:lL
err1(i,j,k) = methods(a(i),b(j),L(k));
end
end
end
end

 採用された回答

Matt J
Matt J 2022 年 4 月 15 日
編集済み: Matt J 2022 年 4 月 15 日

0 投票

In this case, the warnings can probably be ignored because a, b, and L are very small.
However, I don't really understand the parallelization stratgy, since the outer parfor loop is very short, and you seem to be doing almost nothing with the loop variable jj. If the intensive work is being done by methods(), I would think you would want to paralleilize over (i,j,k).

9 件のコメント

Kareem Elgindy
Kareem Elgindy 2022 年 4 月 15 日
Thanks @Matt J. The intensive work is on methods, but methods is running for two values of jj. One for jj = 1 and one for jj = 2. For each case, certain parameters are passed to methods but they are irrelevant to i, j, and k.
Matt J
Matt J 2022 年 4 月 15 日
編集済み: Matt J 2022 年 4 月 15 日
Your posted code doesn't show that. Nothing jj-dependent is being passed to methods(). I hope you are not doing it with global variables.
Regardless, it still doesn't explain why you would be looping over jj instead of (i,j,k). Why not as below? That way, assuming you have more than 2 cores, you can parallelize across more than 2 parpool workers.
a = (5:5:100)'; b = (-0.4:0.1:2)'; L = (0.25:0.25:10)';
la = length(a); lb = length(b); lL = length(L);
err1 = zeros(la,lb,lL);
parfor m=1:la*lb*lL
[i,j,k]=ind2sub([la,lb,lL],m);
for jj = 1:2
if jj == 1, bla bla bla; else, bla bla bla; end
err1(m) = methods(a(i),b(j),L(k));
end
end
Kareem Elgindy
Kareem Elgindy 2022 年 4 月 15 日
@Matt J Thanks again for your time and efforts. Don't worry, I'm not using global variables. I removed the parameters passed to methods from the presented code above to keep it simple and short.
Your last code looks quite interesting. Now, that we calculate err1(m) for each m, how could we recover err1(i,j,k) back?
Walter Roberson
Walter Roberson 2022 年 4 月 15 日
編集済み: Walter Roberson 2022 年 4 月 15 日
reshape(err1, la, lb, lL)
Matt J
Matt J 2022 年 4 月 15 日
Your last code looks quite interesting. Now, that we calculate err1(m) for each m, how could we recover err1(i,j,k) back?
I'm not sure what you mean by "recover" it? err1(m) is the same thing as err1(i,j,k). There shouldn't be any need to reshape() or do anything else.
Kareem Elgindy
Kareem Elgindy 2022 年 4 月 15 日
Hi @Matt J. I still need err1 as a matrix of 3 dimensions to do further postprocessing using commands like squeeze along the i dimension, etc. So, reshape here remains useful as suggested by @Walter Roberson.
Kareem Elgindy
Kareem Elgindy 2022 年 4 月 15 日
@Walter Roberson... there is a missing comma between la and lb in the command reshape; right?
Matt J
Matt J 2022 年 4 月 15 日
編集済み: Matt J 2022 年 4 月 15 日
I still need err1 as a matrix of 3 dimensions to do further postprocessing using commands like squeeze along the i dimension, etc. So, reshape here remains useful as suggested by
It should have been 3-dimensional from the beginning and remained so throughout. The shape was established in the declaration line,
err1 = zeros(la,lb,lL);
Kareem Elgindy
Kareem Elgindy 2022 年 5 月 16 日
@Matt J You are right. The size is already predefined. Thanks.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by