function [tf, sz, id] = isPoolRunningOnEquivalentCluster(aPool, cluster)
% isPoolRunningOnThisCluster TRUE if an interactive pool for this
% session is open on a cluster equivalent to the supplied cluster.
% NB: the test is for EQUIVALENCE (via hEquivalentWorkers).
% Returns:
% tf - TRUE if pool is running on an equivalent cluster.
% sz - size of pool (valid only when TF == TRUE)
% id - job ID of pool (valid only when TF == TRUE)

% Copyright 2013-2019 The MathWorks, Inc.

tf = false;
sz = -1;
id = -1;
if isempty(aPool) || isa(aPool, 'parallel.ThreadPool')
    return
end
client = aPool.hGetClient();
sz = aPool.NumWorkers;
pjob   = client.ParallelJob;
id     = pjob.ID;
mpclus = pjob.Parent;
tf     = isa( cluster, 'parallel.Cluster' ) && ... % Only implemented for API2
    isa( mpclus, 'parallel.Cluster' ) && ...
    hEquivalentWorkers( cluster, mpclus );
end
