Is there anything like parfor for while loops

19 Ansichten (letzte 30 Tage)
Jannes Quer
Jannes Quer am 22 Mär. 2016
Bearbeitet: Walter Roberson am 23 Mär. 2016
Dear all,
is there anything like parfor for while loops? Or can a create anything similar with a workaround?
Best Jate18

Antworten (2)

Edric Ellis
Edric Ellis am 23 Mär. 2016
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

Walter Roberson
Walter Roberson am 23 Mär. 2016
Bearbeitet: Walter Roberson am 23 Mär. 2016
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.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by