Effacer les filtres
Effacer les filtres

Parfor general question and thought

2 vues (au cours des 30 derniers jours)
Robert le 30 Juil 2021
I want to have 4 parfor loops and assign a worker to each loop so they execute together . is that possible? Or am I missing the point? Or is it to have 1 par for loop that 4 workers do work on at once. Which one is it? Thanks

Réponses (1)

Raymond Norris
Raymond Norris le 30 Juil 2021
A parfor is run synchoneously. That is, the parfor is blocking until complete. By that logic, two parfor loops can only run sequentially, as such
parfor idx = 1:N
% Can't be reached until 1st parfor loop is complete
parfor jdx = 1:M
What gets posted occasionally is nested parfor loops.
parfor idx = 1:N
parfor jdx = 1:M
The flaw here is that workers do not communicate with each other in parfor loops. Therefore, worker 1 (in the idx parfor) can't send iterations (from the jidx parfor) to worker W.
To answer your question, you would create one parfor with N workers to run all your code.
With that said, there is another option to consider. If these 4 for-loops could run at the same time, refactor the code into their own subfunction and then look at using parfeval or batch. For instance, let's assume your code looks something like this overly simplified example
% 4 independent loops that can run at the same time
for idx = 1:N1
A(idx) = . . .
for idx = 1:N2
B(idx) = . . .
for idx = 1:N3
C(idx) = . . .
for idx = 1:N4
D(idx) = . . .
Refactor it as follows:
A = for_fcn1(n1);
B = for_fcn2(n2);
C = for_fcn3(n3);
D = for_fcn4(n4);
The refactored code could be of the form
function ret = for_fcn1(n)
for idx = 1:n
ret(idx) = . . .
Now use parfeval
fcns_to_call = {@for_fcn1, @for_fcn2, @for_fcn3, @for_fcn4};
num_fcn_outputs = {n1, n2, n3, n4};
cluster = parcluster;
pool = cluster.parpool(4);
for idx = 1:4
f(idx) = cluster.parfeval(fcns_to_call,num_fcn_outputs(idx), . . .);
for idx = 1:4
% Block until "future task" is complete
[. . .] = f.fetchNext;
% Continue with your serial code.
The code with ". . ." will require more input on your part. Look at the doc for parfeval and it should be more obvious what they are.


En savoir plus sur Parallel for-Loops (parfor) dans Help Center et File Exchange





Community Treasure Hunt

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

Start Hunting!

Translated by