Is there anything like parfor for while loops
18 次查看(过去 30 天)
显示 更早的评论
Dear all,
is there anything like parfor for while loops? Or can a create anything similar with a workaround?
Best Jate18
0 个评论
回答(2 个)
Edric Ellis
2016-3-23
As Walter says, there is no simple version of a parallel while loop in MATLAB. parfeval was designed with this sort of case in mind, and this example is roughly the sort of thing you'd need to do. In general, the pattern would be:
% Initiate work in parallel
numOutputs = 1; N = 100;
for idx = 1:N
f(idx) = parfeval(@rand, numOutputs);
end
% Collect results as they arrive
for idx = 1:N
[fIdx, result] = fetchNext(f);
if result > 0.95
% We're done, and can break out of the loop now
disp(result)
break;
end
end
0 个评论
Walter Roberson
2016-3-23
编辑:Walter Roberson
2016-3-23
No, and there cannot be. parfor() executes the interactions in an undefined order (though it typically does the last iteration first) and may allocate any number of consecutive iterations to a worker that it likes, and will assign new tasks to workers as workers finish, the order of which can vary even if they do exactly the same work, due to random processes about the order that interrupts happen to get queued. The iteration at which any particular condition was satisfied could come at any time.
while() on the other hand needs to stop the very first time sequentially that a condition is true, and must not do further iterations.
Perhaps you are looking for something like spmd and having the various workers labSend to each other when the realize that the condition has been met. You would need to coordinate work between the nodes.
Possibly you could make use of parfeval(), submitting runs with parameters and having something else peek at the results and canceling the futures as soon as the condition was detected.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!