Recommended approach to "resetting" the workers for each new parallel computation?

7 views (last 30 days)
Every time I run a script, I want the parallel computation to be ran smoothly, regardless of any previous tasks the workers were working on the last time they were running or interrupted.
My ugly approach is this:
myCluster = parcluster('local');
delete(myCluster.Jobs); % I believe this "cleans" any unfinished/interrupted jobs and has the workers drop them
delete(gcp('nocreate')); % Delete the current parpool, in case it exists
myPool = parpool(myCluster,"AttachedFiles","example_library_file"); % Create a new(?) pool
I think this is not the proper way to do things.
It works fine for my purpose, although I do get the warning of "Cannot cancel or destroy a job that was not created by this Local cluster" upon the first time I run it for a new MATLAB session.
I don't think I fully understand how the pool and the cluster interact with each other. I am still a bit in the dark even after reading the documentation.
Could you correct my code so that it properly "clears" the current pool of workers and their jobs each time I run it?

Answers (1)

Raymond Norris
Raymond Norris on 1 Jul 2022
You have the right idea. A couple of quick comments
  • If you have a parallel pool running, then deleting the jobs will also delete the pool (a pool is a type of job). Therefore
is all you need.
  • The "Cannot cancel or destroy a job that was not created by this Local cluster" is a known issue that I believe was resolved in R2022a.
The "cluster" (myCluster in your case) is a MATLAB object that contains properties of the cluster (e.g., number of workers, name of host, location of MATLAB, etc.) and methods that can operate on it (e.g, job launchers). There are two job launchers: parpool and batch. By and large, when using the local profile, you'll only call parpool (but you could in theory run batch jobs as well).
Therefore, there are two reasons you'd want to call parcluster.
  • To override the default cluster
If you simply call
MATLAB will startup a pool of workers with a default pool size using your default profile. If you call
MATLAB will startup a pool of eight workers using your default profile (if using the local profile, setting the size is the most common reason to call parpool explicitly). If you call
MATLAB will startup a pool of 10 workers using the myCluster profile. As listed in your example, there's more arguments you can pass to parpool as well.
  • [Optional] To set properties the job submitter will pickup to tweak your job submission.
Depending on the scheduler (and in particular 3rd party schedulers like Slurm and PBS), where MATLAB will submit your job, you can interject scheduler flags (e.g., walltime, queue name, etc.). Therefore, you might run something like
myCluster = parcluster('slurm');
myCluster.AdditionalSubmitArgs = '-p short'; % Doing this off the top of my head, might be a bit off
pool = myCluster(8);


Find more on Parallel Computing Fundamentals in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!