Am I running in parallel? (best way to check)
29 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Felipe G. Nievinski
il 8 Gen 2013
Commentato: Carlos M. Velez S.
il 14 Feb 2025 alle 2:00
Is there a better way than this?
function answer = is_in_parallel ()
try
answer = ~isempty(getCurrentTask());
catch err
if ~strcmp(err.identifier, 'MATLAB:UndefinedFunction')
rethrow(err);
end
answer = false;
end
end
8 Commenti
Tom Holden
il 8 Apr 2022
getCurrentTask throws an error within a thread based parallel pool. So perhaps answer should be true if an error is thrown!
Risposta accettata
Jan
il 9 Gen 2013
Let me summarize the current discussion:
No. There seems to be no better solution.
1 Commento
Carlos M. Velez S.
il 14 Feb 2025 alle 2:00
parallel = gcp('nocreate');
if isempty(parallel)
S.parallel = 'off';
else
S.parallel = ['on (' num2str(parallel.NumWorkers) ' workers, Cluster: ' parallel.Cluster.Profile ')'];
end
Più risposte (3)
Tom Holden
il 8 Apr 2022
~usejava( 'desktop' ) seems to work even on thread based parallel pools.
0 Commenti
Daniel Shub
il 9 Gen 2013
MATLAB has a number of such functions that tell you about the state of the world (e.g., ispc, isdeployed, isjava). These functions seem to be necessary since there are thing you can/cannot do when they are true/false. I am not sure that something like isparallel is needed. It seems you are looking for something more like isheadless. That said, I am not sure this is the best approach. I am guessing here, but it sounds like your two uses cases are something like
parfor = 1:N
myFuncThatComputesAndPrints;
end
and
for = 1:N
myFuncThatComputesAndPrints;
end
and you want myFuncThatComputesAndPrints to know if it is running headless so that it will suppress the printing. I would suggest that you modify myFuncThatComputesAndPrints to take a flag to determine if it should print giving you
printFlag = true;
parfor = 1:N
myFuncThatComputesAndPrints(printFlag);
end
myFuncThatOnlyPrints;
and
printFlag = false;
for = 1:N
myFuncThatComputesAndPrints(printFlag);
end
This would allow you to expand the cases where you choose not to print.
5 Commenti
Felipe
il 9 Gen 2013
How would you implement isparallel that would work if called outside the parfor? I don't think any of ~isempty(ver('distcomp')), (matlabpool('size') > 0), or ~isempty(getCurrentTask()) would work.
Vedere anche
Categorie
Scopri di più su Startup and Shutdown in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!