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

128 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 7 Feb 2023
Modificato: MathWorks Support Team il 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;
else
% There is a parallel pool of <p.NumWorkers> workers
poolsize = p.NumWorkers
end
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();
else
x = 0;
end
else
x = 0;
end
end
  1 Commento
Walter Roberson
Walter Roberson il 7 Feb 2023
What output do you see when you try these in R2022b ?
p = gcp('nocreate')
p = 0×0 Pool array with no properties.
isempty(p)
ans = logical
1
pool = parpool
Starting parallel pool (parpool) using the 'Processes' profile ... Connected to the parallel pool (number of workers: 2). pool = ProcessPool with properties: Connected: true NumWorkers: 2 Busy: false Cluster: Processes (Local Cluster) AttachedFiles: {} AutoAddClientPath: true FileStore: [1x1 parallel.FileStore] ValueStore: [1x1 parallel.ValueStore] IdleTimeout: 30 minutes (30 minutes remaining) SpmdEnabled: true
p = gcp('nocreate')
p = ProcessPool with properties: Connected: true NumWorkers: 2 Busy: false Cluster: Processes (Local Cluster) AttachedFiles: {} AutoAddClientPath: true FileStore: [1x1 parallel.FileStore] ValueStore: [1x1 parallel.ValueStore] IdleTimeout: 30 minutes (30 minutes remaining) SpmdEnabled: true
isempty(p)
ans = logical
0

Accedi per commentare.

Più risposte (1)

Seongsu Jeong
Seongsu Jeong il 6 Feb 2018
>> isempty(gcp('nocreate'))
ans =
logical
1
>> 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 =
logical
0
>> delete(gcp('nocreate'))
Parallel pool using the 'local' profile is shutting down.
>> isempty(gcp('nocreate'))
ans =
logical
1
>>

Categorie

Scopri di più su Parallel Computing Fundamentals in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by