How to check from a Matlab script if there is a Parallel computing toolbox installed?

Hello,
We are doing collective Matlab code development, with remote developpers. Some have license for Paralle computing toolbox and some haven't. In order to keep one base code for all, we would like to check in the code if such a license is installed and activated or not, so the code can bifurcate to the relevant code.
Is there any way to do this?
In general, can we check from the code that a specific toolbox is installed and activated?
Many thanks in advance,
Best regards,
Walid Sami

Answers (2)

If I understood your problem correctly then try this. First the function checks if toolbox is installed and then returns number of workers available
function [num_workers, isexist_ParallelToolBox] = getWorkersAvailable()
% First checks if Parallel Computing Toolbox exists and then
% Returns num of workers available for executing parallel computing
%
available_toolboxes = ver;
isexist_ParallelToolBox = false;
num_workers = 0; % default is set to 0 = single core processing
index =1;
while index < size(available_toolboxes,2)
if (convertCharsToStrings(available_toolboxes(index).Name) == "Parallel Computing Toolbox")
isexist_ParallelToolBox = true;
break;
end
index = index + 1;
end
% if Paralle Computing toolbox available then get num_workers/CPU cores
% available for batch processing
if isexist_ParallelToolBox
myCluster = parcluster('local');
num_workers = myCluster.NumWorkers;
end
end
A dynamic check is useful, but for testing and comparison an additional manual switch has more power:
[EDITED] TTP -> PPT
function reply = hasParallelToolbox(S)
% Get availability of the Parallel Processing Toolbox:
% R = hasParallelToolbox()
% Disable the flag for an existing PPT manually:
% hasParallelToolbox(false)
% Re-enable the actual status:
% hasParallelToolbox(true) % Considers a missing toolbox also
% or
% clear('hasParallelToolbox')
persisent hasPPT usePPT
if isempty(hasPPT)
try
% One of these 2 tests is sufficient already:
hasPPT = ~isempty(getCurrentTask()) && (matlabpool('size') > 0);
catch ME
if ~strcmp(ME.identifier, 'MATLAB:UndefinedFunction')
rethrow(ME);
end
hasPPT = false;
end
usePPT = hasPPT;
end
if nargin > 0
usePPT = (any(S)) && hasPPT;
end
reply = usePPT;
end
Maybe inserting this will help also to identify a pool with 1entity only (I'm not sure if something like this is possible):
p = gcp('nocreate'); % If no pool, do not create new one.
if isempty(p)
poolsize = 0;
else
poolsize = p.NumWorkers
end
reply = usePPT && (poolsize > 1);

3 Comments

Thank you Jan for your prompt response,
I tried your code above (corrected syntax hasPPT --> hasTTP) inside my script before a "parfor" loop that requires the parallel tool box but the answer was false (no toolbox detected). I tried it also inside the parfor loop to see if it detects anything, but still the answer is false.
I am not sure this can be done after all. Anyway, many thanks for your help,
Regards,
Walid
Maybe I made a typo. I do not have the PPT, such that I my tests are not exhaustive. What are the replies for:
getCurrentTask()
matlabpool('size')
Hi Jan,
Thanks again,
I made few more attempts, but still I don't get useful result.
It seems to me the first if in your code is always skipped, therefore I get reply=0 (false) all the time.
I inserted "Task=getCurrentTask" alone in my main script, the result is:
Task = []
I think I will give up on that one,
Many thanks again,
Regards,
Walid

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Asked:

on 7 Jan 2019

Answered:

on 10 May 2019

Community Treasure Hunt

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

Start Hunting!