MATLAB: How to easily check if MATLABPOOL is running when using Parallel Computing Toolbox

executeexecutingmatlabpoolParallel Computing Toolboxrun

It is common to put a command to start a MATLABPOOL in scripts that run parallel code. This will error the second time it runs since the MATLABPOOL will already be running (unless you kill the MATLABPOOL at the end of the script, which would be inconvenient during code development and testing because of startup time).

Best Answer

  • This enhancement has been incorporated in MATLAB 7.7 (R2008b). You can type the following to check the size of MATLABPOOL:
    s = matlabpool('size')
    The above command will return '0' if MATLABPOOL is closed.
    For previous product releases, you can use the following function as a workaround:
    function x = pool_size
    %POOL_SIZE - temporary hack to 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