MATLAB中的Parfor问题 [英] Parfor in MATLAB Problem

查看:872
本文介绍了MATLAB中的Parfor问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能在这段代码中使用parfor?

Why can't I use the parfor in this piece of code?

parfor i=1:r

    for j=1:N/r

        xr(j + (N/r) * (i-1)) = x(i + r * (j-1));

    end

end

这是错误:

错误:parfor中的变量xr无法分类. 请参见MATLAB中的Parallel for Loops,概述".

Error: The variable xr in a parfor cannot be classified. See Parallel for Loops in MATLAB, "Overview".

推荐答案

此处的问题是切片数组的索引编制不正确. parfor循环异步运行,这意味着每个迭代的执行顺序是随机的.从文档:

The issue here is that of improper indexing of the sliced array. parfor loops are run asynchronously, meaning the order in which each iteration is executed is random. From the documentation:

MATLAB工作者不按特定顺序评估迭代,并且彼此独立.因为每个迭代都是独立的,所以不能保证迭代以任何方式同步,也不需要这样做.

MATLAB workers evaluate iterations in no particular order, and independently of each other. Because each iteration is independent, there is no guarantee that the iterations are synchronized in any way, nor is there any need for this.

您可以通过在命令行中键入以下内容来轻松验证上述语句:

You can easily verify the above statement by typing the following in the command line:

parfor i=1:100
    i
end

您将看到排序是任意的.因此,如果您将并行作业拆分为不同的工作程序,则一个工作程序将无法判断不同的迭代是否已完成.因此,变量索引不能取决于迭代器的过去/将来值.

You'll see that the ordering is arbitrary. Hence if you split a parallel job between different workers, one worker has no way of telling if a different iteration has finished or not. Hence, your variable indexing cannot depend on past/future values of the iterator.

让我通过一个简单的示例进行演示.考虑斐波那契数列1,1,2,3,5,8,....您可以轻松地(在简单的for循环中)生成该系列的前10个术语:

Let me demonstrate this with a simple example. Consider the Fibonacci series 1,1,2,3,5,8,.... You can generate the first 10 terms of the series easily (in a naïve for loop) as:

f=zeros(1,10);
f(1:2)=1;
for i=3:10
    f(i)=f(i-1)+f(i-2);
end

现在让我们对parfor循环进行相同的操作.

Now let's do the same with a parfor loop.

f=zeros(1,10);
f(1:2)=1;
parfor i=3:10
    f(i)=f(i-1)+f(i-2);
end

???错误:parfor中的变量f无法分类. 参见MATLAB中的Parallel for Loops,概述"

??? Error: The variable f in a parfor cannot be classified. See Parallel for Loops in MATLAB, "Overview"

但是为什么会出现错误?

But why does this give an error?

我已经展示了迭代以任意顺序执行.因此,假设一个工作程序获得了循环索引i=7和表达式f(i)=f(i-1)+f(i-2);.现在应该执行该表达式并将结果返回到主节点.现在迭代i=6完成了吗?存储在f(6)中的值可靠吗? f(5)呢?你看到我在说什么吗?假设f(5)f(6)没有完成,那么您将错误地计算出Fibonnaci系列的第七项为0!

I've shown that iterations are executed in an arbitrary order. So let's say that a worker gets the loop index i=7 and the expression f(i)=f(i-1)+f(i-2);. It is now supposed to execute the expression and return the results to the master node. Now has iteration i=6 finished? Is the value stored in f(6) reliable? What about f(5)? Do you see what I'm getting at? Supposing f(5) and f(6) are not done, then you'll incorrectly calculate that the 7th term in the Fibonnaci series is 0!

由于MATLAB无法告知您每次计算是否可以保证正确运行 并重现 same 结果,因此明确禁止此类歧义分配.

Since MATLAB has no way of telling if your calculation can be guaranteed to run correctly and reproduce the same result each time, such ambiguous assignments are explicitly disallowed.

这篇关于MATLAB中的Parfor问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆