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
回答(2 个)
Sumeet Rohilla
2019-5-10
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 search in the forum will show this answer: https://www.mathworks.com/matlabcentral/answers/58228-am-i-running-in-parallel-best-way-to-check
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 个评论
Walid Sami
2019-1-8
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')
Walid Sami
2019-1-8
类别
在 帮助中心 和 File Exchange 中查找有关 Get Started with MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!