Am I inside a parfor?

4 次查看(过去 30 天)
Joan Puig
Joan Puig 2011-2-24
Hi,
There are certain functions I don't want to execute in parallel, and I would like to throw an error if they are. Is there anyway to know if I am inside a parfor?
Thanks Joan

采纳的回答

Sarah Wait Zaranek
Sarah Wait Zaranek 2011-2-25
You can check to see if you are currently running on a worker (aka running in parallel) by using the following commands.
parfor ii = 1:2
job = getCurrentJob;
if isempty(job)
display('not running in parallel')
else
display('running in parallel')
end
end
If you are not running in parallel, the function should return an empty. If you are running in parallel, it will return a job object corresponding to the job you are inside.

更多回答(1 个)

Joan Puig
Joan Puig 2011-2-25
With your code I was able to write this function which works great:
function ip = inParallel()
job = getCurrentJob();
ip = ~isempty(job);
end
Thanks!

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by