How can I check if "matlabpool" is running when using Parallel Computing Toolbox?
164 views (last 30 days)
Show older comments
MathWorks Support Team on 27 Jun 2009
Edited: Riccardo Micci on 8 Feb 2023
I am writing MATLAB scripts using the Parallel Computing Toolbox Release R2015b. Many of my scripts contain a call to "matlabpool" to start a parallel computing pool. However, sometimes the pool is already initiated and I will get an error due to a duplicate call.
How can I check if a parallel pool is already running, so I can avoid trying to start it twice?
MathWorks Support Team on 7 Feb 2023
Edited: MathWorks Support Team on 7 Feb 2023
Please note that the function "matlabpool" was deprecated in release R2016a. This answer contains three different methods to check the existence of a parallel pool and get its size depending on the MATLAB release being used.
For R2016a and later releases
Start a parallel pool with the command "parpool". You can then get the current parallel pool with the command "gcp" and the argument 'nocreate' to avoid creating a pool of none exists.
p = gcp('nocreate')
You can then check if the object 'p' exists, and how many workers it has assigned:
% There is no parallel pool
poolsize = 0;
% There is a parallel pool of <p.NumWorkers> workers
poolsize = p.NumWorkers
Function "parpool" is documented here: https://www.mathworks.com/help/parallel-computing/parpool.html
Function "gcp" is documented here: https://www.mathworks.com/help/parallel-computing/gcp.html
For releases between R2008b (7.7) and R2015b
You can type the following to check the size of "matlabpool":
poolsize = matlabpool('size')
The above command will return '0' if "matlabpool" is closed.
For releases earlier than R2008b:
The option 'size' was introduced in R2008b. For earlier releases, use the following function as a workaround:
function x = pool_size
%POOL_SIZE - Return size of current MATLABPOOL
session = com.mathworks.toolbox.distcomp.pmode.SessionFactory.getCurrentSession;
if ~isempty( session ) && session.isSessionRunning() && session.isPoolManagerSession()
client = distcomp.getInteractiveObject();
if strcmp( client.CurrentInteractiveType, 'matlabpool' )
x = session.getLabs().getNumLabs();
x = 0;
x = 0;
Riccardo Micci on 8 Feb 2023
More Answers (1)
Seongsu Jeong on 6 Feb 2018
Starting parallel pool (parpool) using the 'local' profile ... connected to 2 workers.
Pool with properties:
IdleTimeout: 30 minutes (30 minutes remaining)
Parallel pool using the 'local' profile is shutting down.
Find more on Parallel Computing Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!