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!