MATLAB: How to define ‘local’ matlabpool size at runtime for Parallel Computing Toolbox use in compiled MATLAB code

compilerlocal configurationmatlabpoolparallel computing

Is it possible for a user of a standalone compiled MATLAB code, which uses the Parallel Computing Toolbox, to define the matlabpool size at run time?
My code queries the local configuration to get the number of workers:
schd = findResource('scheduler', 'configuration', 'local');
numWorkers = schd.ClusterSize;
I then allow the user to revise that number down via a GUI contol (so they don't max out their system if they have other stuff running). When processing begins I call:
matlabpool('local', numWorkers)
... % do stuff with parfor
matlabpool close
It works fine on my machine, however, when I compile this code on my machine (where the standard 'local' configuration has 8 workers) and then run it on a machine with only 2 cores, the number of workers gets set to 8, even though only 2 are available. Clearly the Compiler has compiled up and included my 'local' config.
If I don't know in advance how many cores a user will have, how can I access THEIR 'local' matlabpool configuration at runtime, instead of mine?
I'm using R2011a.
Many thanks, Nick

Best Answer

Hi Nick,
When you compile a PCT application, all of your configurations get bundled into the compiled application, so if your local configuration has a hard-coded ClusterSize value of 8, then when the compiled application runs, it will always use a ClusterSize of 8, regardless of the number of actual cores on the machine.
You can supply a custom configuration to use for the application using the mcruserdata flag. e.g, if your application is myExe.exe, and your exported configuration is myConfig.mat, then you would use this command:
myExe.exe -mcruserdata ParallelConfigurationFile:myConfig.mat
See the documentation for more details.
In order to take advantage of this, your code need to be configuration agnostic and always use the default configuration. Your code would then look like this:
matlabpool % Open a matlabpool using the default configuration
... %do stuff
matlabpool close
In general, it is better for your compiled code to use the default configuration rather than a hard-coded one because it allows users of the compiled application to run it on different clusters without you having to modify the actual code.
Elwin