如何嵌套多个parfor循环 [英] How to nest multiple parfor loops
问题描述
parfor
是在多个工人"之间分配密集计算的独立迭代的便捷方法.一个有意义的限制是parfor
-循环不能嵌套,并且始终是类似问题的答案,例如有 a>.
parfor
is a convenient way to distribute independent iterations of intensive computations among several "workers". One meaningful restriction is that parfor
-loops cannot be nested, and invariably, that is the answer to similar questions like there and there.
为什么需要跨循环并行化
请考虑以下代码,其中迭代在一台允许4名工人的机器上花费大量时间.这两个循环都迭代了6个值,显然很难在4个值之间共享.
Consider the following piece of code where iterations take a highly variable amount of time on a machine that allows 4 workers. Both loops iterate over 6 values, clearly hard to share among 4.
for row = 1:6
parfor col = 1:6
somefun(row, col);
end
end
为parfor
选择内部循环似乎是一个好主意,因为对somefun
的单个调用比外部循环的迭代更具可变性.但是,如果每次调用somefun
的运行时间非常相似怎么办?如果运行时有趋势并且我们有三个嵌套循环,该怎么办?这些问题会定期出现,人们去
It seems like a good idea to choose the inner loop for parfor
because individual calls to somefun
are more variable than iterations of the outer loop. But what if the run time for each call to somefun
is very similar? What if there are trends in run time and we have three nested loops? These questions come up regularly, and people go to extremes.
组合循环所需的模式
理想情况下,对于所有row
和col
对都运行somefun
,并且工作人员应该忙于工作,而无论迭代的方式如何.解决方案应该看起来像
Ideally, somefun
is run for all pairs of row
and col
, and workers should get busy irrespectively of which iterand is being varied. The solution should look like
parfor p = allpairs(1:6, 1:6)
somefun(p(1), p(2));
end
不幸的是,即使我知道哪个内置函数创建的矩阵都包含row
和col
的所有组合,MATLAB也会报错 parfor语句的范围必须是行向量.但是,for
不会抱怨,并且可以很好地遍历列.一个简单的解决方法是创建该矩阵,然后使用parfor
对其进行索引:
Unfortunately, even if I knew which builtin function creates a matrix with all combinations of row
and col
, MATLAB would complain with an error The range of a parfor statement must be a row vector. Yet, for
would not complain and nicely iterate over columns. An easy workaround would be to create that matrix and then index it with parfor
:
p = allpairs(1:6, 1:6);
parfor k = 1:size(pairs, 2)
row = p(k, 1);
col = p(k, 2);
somefun(row, col);
end
我要寻找的代替allpairs
的内置函数是什么?有人想出了一种便捷的惯用模式吗?
What is the builtin function in place of allpairs
that I am looking for? Is there a convenient idiomatic pattern that someone has come up with?
推荐答案
MrAzzman已经指出了如何线性化嵌套循环.这是线性化n个嵌套循环的一般解决方案.
MrAzzman already pointed out how to linearise nested loops. Here is a general solution to linearise n nested loops.
1)假设您有一个简单的嵌套循环结构,如下所示:
1) Assuming you have a simple nested loop structure like this:
%dummy function for demonstration purposes
f=@(a,b,c)([a,b,c]);
%three loops
X=cell(4,5,6);
for a=1:size(X,1);
for b=1:size(X,2);
for c=1:size(X,3);
X{a,b,c}=f(a,b,c);
end
end
end
2)使用for循环进行基本线性化:
2) Basic linearisation using a for loop:
%linearized conventional loop
X=cell(4,5,6);
iterations=size(X);
for ix=1:prod(iterations)
[a,b,c]=ind2sub(iterations,ix);
X{a,b,c}=f(a,b,c);
end
3)使用parfor循环进行线性化.
3) Linearisation using a parfor loop.
%linearized parfor loop
X=cell(4,5,6);
iterations=size(X);
parfor ix=1:prod(iterations)
[a,b,c]=ind2sub(iterations,ix);
X{ix}=f(a,b,c);
end
4)使用带有常规for循环的第二个版本,可以更改执行迭代的顺序.如果有什么依赖于此,则必须颠倒索引的顺序.
4) Using the second version with a conventional for loop, the order in which the iterations are executed is altered. If anything relies on this you have to reverse the order of the indices.
%linearized conventional loop
X=cell(4,5,6);
iterations=fliplr(size(X));
for ix=1:prod(iterations)
[c,b,a]=ind2sub(iterations,ix);
X{a,b,c}=f(a,b,c);
end
使用parfor
循环时反转顺序无关紧要.您完全不能依赖执行顺序.如果您认为这有所作为,则不能使用parfor
.
Reversing the order when using a parfor
loop is irrelevant. You can not rely on the order of execution at all. If you think it makes a difference, you can not use parfor
.
这篇关于如何嵌套多个parfor循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!