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

조회 수: 45 (최근 30일)
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
MathWorks Support Team 2023년 2월 7일
편집: MathWorks Support Team 2023년 2월 7일
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
Walter Roberson
Walter Roberson 2023년 2월 7일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Seongsu Jeong
Seongsu Jeong 2018년 2월 6일
>> 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
>>

카테고리

Help CenterFile Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by