Effacer les filtres
Effacer les filtres

Can parfor be used if no Parallel Computing Toolbox licenses are available?

36 vues (au cours des 30 derniers jours)
I was reading a recent blog post on HPC with Matlab and it reminded me of some frustration with parfor that I had some years ago, and I was wondering if the situation has changed at all -- or whether my recollection is incorrect. I recall running into problems if all my employer's PCT licenses were checked out. I recall getting errors to the effect of All Parallel Computing Toolbox licenses checked out. I still wanted to run my code, sure it'd be a bit slower, but I could at least do something. I found myself writing all of my code to check for the PCT license and then use either for loops or parfor loops based on the existence of the license. For example:
num_models = 10;
hasPCT = license('test', "Distrib_Computing_Toolbox" );
if hasPCT == true
parfor ii = 1:num_models
doExpensiveCalculation( rand );
for ii = 1:num_models
doExpensiveCalculation( rand );
Starting parallel pool (parpool) using the 'Processes' profile ... Connected to the parallel pool (number of workers: 2). Analyzing and transferring files to the workers ...done.
But that obviously looks a bit... ugly. Whereas it would be great if I could just do:
parfor ii = 1:num_models
doExpensiveCalculation( rand ); %% Serial if PCT license not available
And it would run without error regardless of whether licenses were available. Does anyone know if this has changed at all, or if my recollection is correct? If not, is my current workaround the best practice?
%% Utility function
function doExpensiveCalculation( cost )
pause( cost )

Réponse acceptée

Raymond Norris
Raymond Norris le 19 Nov 2022
Hi @Gregory Vernon. If Parallel Computing Toolbx is not installed, then MATLAB will run parfor serially, in reverse order. Though your question is slightly different. It's not a case of whether PCT is installed (it is in your case) -- you're asking what if there are no available licenses. MATLAB should behave the same way (as if PCT wasn't installed).
One way to pseudo test this is to run your code, but don't start a parallel pool AND don't have MATLAB automatically start one either (in the Parallel Computing Toolbox preferences).

Plus de réponses (2)

Matt J
Matt J le 19 Nov 2022
Modifié(e) : Matt J le 19 Nov 2022
You don't ned to check whether the Parallel Computing Toolbox is installed, as long as you don't try to run parfor on multiple workers.

Edric Ellis
Edric Ellis le 21 Nov 2022
The implementation of parfor is intended to gracefully fall back to running in serial if a Parallel Computing Toolbox licence cannot be checked out at the time you run the loop. (I don't know when this changed - my recollection is that this was always the case, but I could be wrong). If you try this and find it isn't working, please contact MathWorks support.


En savoir plus sur Startup and Shutdown dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by