How can I check if "matlabpool" is running when using Parallel Computing Toolbox?

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

Accepted Answer

MathWorks Support Team
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:
if isempty(p)
% There is no parallel pool
poolsize = 0;
% There is a parallel pool of <p.NumWorkers> workers
poolsize = p.NumWorkers
Function "parpool" is documented here:
Function "gcp" is documented here:
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;

Sign in to comment.

More Answers (1)

Seongsu Jeong
Seongsu Jeong on 6 Feb 2018
>> isempty(gcp('nocreate'))
ans =
>> parpool(2)
Starting parallel pool (parpool) using the 'local' profile ... connected to 2 workers.
ans =
Pool with properties:
Connected: true
NumWorkers: 2
Cluster: local
AttachedFiles: {}
IdleTimeout: 30 minutes (30 minutes remaining)
SpmdEnabled: true
>> isempty(gcp('nocreate'))
ans =
>> delete(gcp('nocreate'))
Parallel pool using the 'local' profile is shutting down.
>> isempty(gcp('nocreate'))
ans =


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!

Translated by