function [pool, wasCreated] = getOrCreatePool(purpose)
%parallel.internal.pool.getOrCreatePool Get or create a parallel.pool
%   [POOL, WASCREATED] = parallel.internal.pool.getOrCreatePool(PURPOSE)
%   returns in POOL a scalar parallel.Pool object, or an empty parallel.Pool
%   object. POOL is empty only if no pool is currently open, and the user's
%   preference disallows automatic pool creation. In particular, a non-empty
%   POOL does not imply that it is fit for PURPOSE.
%
%   PURPOSE is one of: 'spmd', 'parfor', 'parfeval', 'tall', or 'mapreduce'
%   depending on the underlying parallel language construct to be
%   used. WASCREATED is true if POOL was created during the call to
%   getOrCreatePool, and false otherwise.

% Copyright 2017-2019 The MathWorks, Inc.

import parallel.internal.pool.PoolArrayManager
import parallel.internal.pool.PoolPurpose

validateattributes(purpose, "parallel.internal.pool.PoolPurpose", {'scalar'});

if purpose == PoolPurpose.Spmd
    parpoolArgs = {'SpmdEnabled', true};
else
    parpoolArgs = {};
end

[pool, wasCreated] = PoolArrayManager.getOrAutoCreateWithCleanup(parpoolArgs{:});
end
