Am I running in parallel? (best way to check)

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 commentaires

Ryan G
Ryan G le 8 Jan 2013
I don't understand the question. If you are running code in a parfor loop, it will be distributed to the workers available.
Daniel Shub
Daniel Shub le 8 Jan 2013
Why do you need this? What is your definition of "best"?
Jan
Jan le 8 Jan 2013
@Ryan: It might be useful when a subfunction is called from inside a PARFOR loop.
@Daniel: I assume that "best" means: robust, minimal overhead, runs with and without parallel toolbox.
@Jan: you're on mark -- thanks for both clarifications.
Parallel workers are headless (w/ no display), so the print function only supports Ghostscript drivers, which ignores the resolution option. This all happens silently. I'm checking is_in_parallel() to defer making and saving figures until the parfor is done, at which point I start a sequential for-loop.
Why isn't it as simple as issuing the figure save commands after the
parfor...end
block? PARFOR won't return control to your mfile until it is finished.
@Matt: the user's preferences are as follows:
  1. If the Parallel Toolbox is unavailable, make & save figures as soon as possible (in the first loop) -- no need for a second loop;
  2. If the Parallel Toolbox is available, don't make crappy figures -- do only the calculations in the first parallel loop, then have a second sequential loop just to overcome parfor's limitations.
Other possibilities that I considered for checking were:
~isempty(ver('distcomp'))
and:
(matlabpool('size') > 0)
but neither seemed better than:
~isempty(getCurrentTask())
Tom Holden
Tom Holden le 8 Avr 2022
getCurrentTask throws an error within a thread based parallel pool. So perhaps answer should be true if an error is thrown!

Connectez-vous pour commenter.

 Réponse acceptée

Jan
Jan le 9 Jan 2013

0 votes

Let me summarize the current discussion:
No. There seems to be no better solution.

1 commentaire

parallel = gcp('nocreate');
if isempty(parallel)
S.parallel = 'off';
else
S.parallel = ['on (' num2str(parallel.NumWorkers) ' workers, Cluster: ' parallel.Cluster.Profile ')'];
end

Connectez-vous pour commenter.

Plus de réponses (3)

埃博拉酱
埃博拉酱 le 3 Avr 2023
Modifié(e) : 埃博拉酱 le 3 Avr 2023
parallel.internal.pool.isPoolThreadWorker||~isempty(getCurrentJob))
This code can robustly return true if and only if run on a parallel worker either as threads or processes.
Tom Holden
Tom Holden le 8 Avr 2022

1 vote

~usejava( 'desktop' ) seems to work even on thread based parallel pools.
Daniel Shub
Daniel Shub le 9 Jan 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 commentaires

Jan
Jan le 9 Jan 2013
I see benefits for isparallel as well as for isheadless as well as for using a flag to trigger a specific behavior. I recommend not to implement a FOR and a PARFOR version of the code, because maintaining and debugging two versions of the same code tends to result in two different functions, which will diverge faster as expected.
Therefore I would prefer: printFlag = isheadless; or printFlag = isparallel depending of what describes the job more accurate. This has the advantage, that you can test the single-thread mode on a multi-core machine easily.
@Jan: again, right on mark.
I'd rather not hard-code the printFlag value -- that's the very raison d'etre for isparallel(). I'd take isheadless(), were it provided by MATLAB.
Thanks for the comments everybody. I just wanted to check whether I was missing something.
The final code looks something like this:
parallel = false(N,1);
parfor i=1:N
result{i} = myFuncThatComputes();
parallel(i) = isparallel();
if parallel(i), continue; end
myFuncThatPrints(result{i});
end
if any(parallel)
for i=1:N
myFuncThatPrints(result{i});
end
end
My point was not to hard code printFlag, rather it was to move it outside the parfor loop. I would suggest:
printFlag = ~isparallel();
parfor i=1:N
result{i} = myFuncThatComputes();
if ~printFlag
myFuncThatPrints(result{i});
end
end
if printFlag
for i=1:N
myFuncThatPrints(result{i});
end
end
I think this will be more efficient since you only have to call isparallel once. The key difference, however, is that within this framework it is not really about checking isparallel, but rather if you want to print. At this point, the only reason you may not want to print might be because you are running in parallel. Later, however, you might have a headless workstation. In that case then you could do something like
printFlag = isparallel | isheadless
Later you might want to not print if it is parallel, headless, or someplace that doesn't have a printer.
Felipe
Felipe le 9 Jan 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.
Jan
Jan le 9 Jan 2013
getCurrentTask() works outside a PARFOR loop and replies [].

Connectez-vous pour commenter.

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by