Can I avoid this broadcast variable?

9 次查看(过去 30 天)
Hello,
I had two nested for loops that I would like to parallelise. It looked like this
for k = 1:size(S,2)
for j = 1:length(b)
fun(S(:,k),b(j));
end
end
Here S is large (10^5 x 10^2), and b is not large (1 x 10).
Since all of the loops are independent I found the following solution
parfor i = 1:size(S,2)*length(b)
k = ceil(i/length(b)); %S
j = i-(k-1)*length(b); %b
fun(S(:,k),b(j));
end
The problem is that S and b are broadcast variables.
Is there a way to eliminate S as a broadcast variable (b does not matter much)?
Thank you!

采纳的回答

Matt J
Matt J 2019-2-22
编辑:Matt J 2019-2-22
Why not this?
parfor k = 1:size(S,2)
tmp=S(:,k);
for j = 1:length(b)
fun(tmp,b(j));
end
end
  2 个评论
Adam
Adam 2019-2-22
Hi Matt, because this way the computational time will be longer. My solution increases the number of CPUs length(b)-fold and yields overall shorter computational time. Am I correct?
Matt J
Matt J 2019-2-22
编辑:Matt J 2019-2-22
You would need a parpool with N>100=size(S,2) labs in order for the work per lab to be any less in your strategy than what I propose.
Method 1: parallelize over k only. The busiest lab will perform ceil(size(S,2)/N)*length(b) serial calls to fun.
Method 2: parallelize all calls to fun. The busiest lab will perform ceil(size(S,2)*length(b)/N) serial calls.
Both are therefore nearly the same unless N>size(S,2)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Parallel Computing Fundamentals 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by