MATLAB Answers

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

70 views (last 30 days)
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

  7 Comments

Show 4 older comments
Matt J
Matt J on 8 Jan 2013
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.
Felipe G. Nievinski
Felipe G. Nievinski on 8 Jan 2013
@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.
Felipe G. Nievinski
Felipe G. Nievinski on 8 Jan 2013
Other possibilities that I considered for checking were:
~isempty(ver('distcomp'))
and:
(matlabpool('size') > 0)
but neither seemed better than:
~isempty(getCurrentTask())

Sign in to comment.

Accepted Answer

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

  0 Comments

Sign in to comment.

More Answers (1)

Daniel Shub
Daniel Shub on 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 Comments

Show 2 older comments
Daniel Shub
Daniel Shub on 9 Jan 2013
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 on 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 on 9 Jan 2013
getCurrentTask() works outside a PARFOR loop and replies [].

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by