Getting Started with Parallel Computing using MATLAB on the HPC Cluster
Note: If you are not making use of parallel functions then you can ignore this document and just launch standard MATLAB jobs with the following in your shell script:
module load software/matlab-R2020b matlab -batch my_matlab_script
This document provides the steps to configure MATLAB to submit jobs to a cluster, retrieve results, and debug errors.
CONFIGURATION
After logging into the cluster use the module command to select the version of MATLAB you require:
module load software/matlab-R2019b or module load software/matlab-R2020b
Then configure MATLAB to run parallel jobs on your cluster by calling the shell script configCluster.sh This only needs to be called once per version of MATLAB.
$ module load software/matlab-R2019b $ configCluster.sh
Jobs will now default to the cluster rather than submit to the local machine.
CONFIGURING JOBS
Prior to submitting the job, we can specify various parameters to pass to our jobs, such as queue, e-mail, walltime, etc. Only WallTime is required.
>> % Get a handle to the cluster >> c = parcluster; [REQUIRED] >> % Specify the walltime (e.g. 5 hours) >> c.AdditionalProperties.WallTime = '05:00:00'; [OPTIONAL] >> % Specify an account to use for MATLAB jobs >> c.AdditionalProperties.AccountName = 'account-name'; >> % Specify e-mail address to receive notifications about your job >> c.AdditionalProperties.EmailAddress = 'user-id@uct.ac.za'; >> % Request 2 Fermi GPU cards per node >> c.AdditionalProperties.GpuCard = 'fermi'; >> c.AdditionalProperties.GpusPerNode = 2; >> % Specify memory to use for MATLAB jobs, per core (MB) >> c.AdditionalProperties.MemUsage = '4000'; >> % Specify a queue to use for MATLAB jobs >> c.AdditionalProperties.QueueName = 'queue-name';
Save changes after modifying AdditionalProperties for the above changes to persist between MATLAB sessions.
>> c.saveProfile
To see the values of the current configuration options, display AdditionalProperties.
>> % To view current properties >> c.AdditionalProperties
Unset a value when no longer needed.
>> % Turn off email notifications
>> c.AdditionalProperties.EmailAddress = '';
>> c.saveProfile
INTERACTIVE JOBS
To run an interactive pool job on the cluster, continue to use parpool as you’ve done before.
>> % Get a handle to the cluster >> c = parcluster; >> % Open a pool of 4 workers on the cluster >> p = c.parpool(4);
Rather than running local on the local machine, the pool can now run across multiple nodes on the cluster.
>> % Run a parfor over 1000 iterations
>> parfor idx = 1:1000
a(idx) = …
end
Once we’re done with the pool, delete it.
>> % Delete the pool
>> p.delete
EXAMPLES
Running a job on multiple cores on a single cluster node:
c = parcluster; c.AdditionalProperties.ProcsPerNode = 4 c.saveProfile p = c.parpool(4); spmd, A = rand(1000); b = rand(1000,1); x = A\b; end
Running a job across multiple nodes:
c = parcluster; c.AdditionalProperties.ProcsPerNode = 1 c.saveProfile p = c.parpool(4); spmd, A = distributed.rand(10000); b = distributed.rand(10000,1); x = A\b; end
TO LEARN MORE
To learn more about the MATLAB Parallel Computing Toolbox, check out these resources: