Parallel Computing Toolbox (parfor)¶
The MATLAB Parallel Computing Toolbox (PCT) extends MATLAB with high-level parallel-processing features such as parallel for loops, parallel regions, message passing, distributed arrays, and parallel numerical methods.
This example illustrates the fine-grained parallelism of a parallel for loop, or parfor, in a pool job.
The examples below show a method for submitting a small parallel MATLAB program with a parfor loop as a job to a queue. The MATLAB program prints the name of the run host and shows the values of the variables numlabs and labindex for each iteration of the parfor loop.
This method uses the job submission command to submit a MATLAB client, which calls the MATLAB batch() function with a user-defined cluster profile.
MATLAB parfor Script¶
Prepare a MATLAB pool program in a MATLAB script with an appropriate filename. In this example, the file is named myscript.m.
The execution of a pool job starts with a worker executing the statements of the first serial region up to the parfor block, where it pauses. A set of workers, called the pool, executes the parfor block. When they finish, the first worker resumes by executing the second serial region.
The code displays the names of the compute nodes running the batch session and the worker pool.
MATLAB Batch Script¶
Prepare a MATLAB script that calls the MATLAB batch() function. This creates a four-lab pool on which to run the MATLAB code in myscript.m.
In this example, the file is named mylclbatch.m.
SLURM Job Submission File¶
Prepare a job submission file with an appropriate filename. In this example, the file is named myjob.sub.
Submit the Job¶
Submit the job as a single compute node with one processor core.
One processor core runs myjob.sub and mylclbatch.m.
Once this job starts, a second job submission is made by MATLAB through the configured SLURM cluster profile.
Example Output¶
The output may look similar to this:
Scaling Up¶
To scale this method for a real application:
- Increase the wall time in the SLURM submission command to accommodate a longer-running job.
- Increase the wall time of
myslurmprofileby using the MATLAB Cluster Profile Manager. - In the Cluster Profile Manager, use the
Parallelmenu to enter a new wall time in theSubmitArgumentsproperty.