继续在parfor循环中 [英] Continue in parfor loop
问题描述
我在Matlab中有一种奇怪的错误.
I have a kind of weird error in Matlab.
metr = cell(1,length(paths));
parfor i = 1:length(paths)
try
a = read(path{i});
catch err
continue;
end
metr{i} = dosomething(a);
end
以上代码可作为正常循环正常运行,并捕获两个错误并继续.如果我将其设置为parfor
循环,则在捕获到错误的那一刻,它将变得非常疯狂,从头开始再次运行for循环,最后由于找不到变量metr
的错误而崩溃.
The above code works fine as a normal loop and catches two errors and continues. If I make it a parfor
loop, the moment it catches an error it goes totally crazy running the for loop again from the beginning and at the end crashing with an error of not finding the variable metr
.
但是,如果我按如下方式重写它,则无论我是否离开continue
语句,我都不会收到任何错误,并且parfor
循环有效:
However, if I rewrite it as follows I don't get any error and the parfor
loop works, whether or not I leave the continue
statement:
metr = cell(1,length(paths));
parfor i = 1:length(paths)
try
a = read(path{i});
errB = 0;
catch err
errB = 1;
continue;
end
if ~errB
metr{i} = dosomething(a);
end
end
有人知道发生了什么吗?在continue
语句之后,它似乎继续执行.我认为parfor循环中仅不支持break;
,并且continue
可以工作.
我很困惑...
Does anyone understand what is going on? It seems like it keeps executing after the continue
statement. I thought only break;
was not supported in parfor loops and that continue
worked.
I'm very confused...
p.s.错误:
为'metr'的工作器引发了UndefinedFunction错误.
这可能是因为包含"metr"的文件 无法在工作人员上访问.
An UndefinedFunction error was thrown on the workers for 'metr'.
This may be because the file containing 'metr' is not accessible on the workers.
编辑:好的,我发现谁出了错.看来,如果我从catch err
行中删除了err
变量,它突然可以正常工作!我仍然不知道为什么将错误分配给变量会使循环变得疯狂.
Edit: Okay I found who's at fault. It seems that if I remove the err
variable from the catch err
line it suddenly works correctly! I still have no clue why assigning the error to a variable makes the loop go crazy.
推荐答案
这个比较老的问题,但以防万一,我认为原始代码中的逻辑不正确.我将避免使用continue
语句,因为parfor的主体是在每个工作程序节点中独立执行的.对于工人来说,在这种情况下没有循环的概念,因此就目前而言,没有任何东西可以继续下去了.假设代码没有崩溃,parfor将继续执行迭代中的下一个元素.我对原始代码的看法是
Rather old question but just in case, I don't think the logic in the original code is correct. I would avoid using the continue
statement since the body of the parfor is executed in every worker node independently. For the worker there is no concept of a loop in this case and therefore as it stands there is nothing to continue to. parfor will continue to the next element in the iteration assuming the code hasn't crashed. My take on the original code would be
% Initialise empty cell array
metr = cell(1,length(paths));
% Iterate over the elements in parallel
parfor i = 1:length(paths)
try
% Try to execute the below
a = read(path{i});
errB = 0;
catch ME
% Suppress the exception - parfor loop now continues
errB = 1;
end
% Execute dosomething()
if ~errB
metr{i} = dosomething(a);
end
end
一个更好的选择可能是
% Initialise empty cell array
metr = cell(1,length(paths));
% Iterate over the elements in parallel
parfor i = 1:length(paths)
try
% Try to execute the below
metr{i} = dosomething(read(path{i}));
catch ME
% Suppress the exception - parfor loop now continues
% NOTE: metr{i} is empty for this i.
end
end
不确定是否有理由将metr
作为列,但是由于Matlab按列的主要顺序进行操作,所以改为metr = cell(length(paths),1)
感觉更自然了.
Not sure if there is a reason for having metr
as columns but since Matlab operates in column-major order it feels a bit more natural to do metr = cell(length(paths),1)
instead.
这篇关于继续在parfor循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!