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

39 ビュー (過去 30 日間)
Felipe G. Nievinski
Felipe G. Nievinski 2013 年 1 月 8 日
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 件のコメント
Felipe G. Nievinski
Felipe G. Nievinski 2013 年 1 月 8 日
Other possibilities that I considered for checking were:
~isempty(ver('distcomp'))
and:
(matlabpool('size') > 0)
but neither seemed better than:
~isempty(getCurrentTask())

サインインしてコメントする。

採用された回答

Jan
Jan 2013 年 1 月 9 日
Let me summarize the current discussion:
No. There seems to be no better solution.

その他の回答 (1 件)

Daniel Shub
Daniel Shub 2013 年 1 月 9 日
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 件のコメント
Jan
Jan 2013 年 1 月 9 日
getCurrentTask() works outside a PARFOR loop and replies [].

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by