Using Parallel Matlab on ADA
There are a number of ways in which Matlab can use multiple processors. You can run Matlab using additional workers running either on the same node or other compute nodes. The toolboxes are:
- Parallel Computing Toolbox (Mathworks site) a number of workers can be opened up on the same computer (i.e. on a node with 24 slots, you can run 24 workers). This is the equivalent to multithreading
- Parallel Server (Mathworks site) workers can be opened across multiple machines in the cluster, through the Slurm queuing system.
Set up Parallel Matlab
Prior to using parallel matlab you have to set up your matalb environment to match slurm settings on ADA. This sets the default queue (compute-16-64), one core pre task, and a parpool of 12.
- start an interactive session
- module add matlab/2020a
- start matlab
- click on parallel
- click on create and manage clusters
- select import
- select /gpfs/software/ada/matlab/R2020a/SlurmProfile1.mlsettings
Parallel Computing Toolbox
The Parallel Computing Toolbox allows you to use matlabpool up to the maximum number of slots on a node (16, 24 or 28 depending on the node) on a single node.
The Parallel Computing Toolbox opens up a number of workers on the same node, so it is important you request the appropriate number of slots. For example, if you are going to open 8 workers, then you should request 9 slots (1 for the master Matlab session, and 8 for the workers). You also need to set the memory required and a time limit for the job to run:
- interactive session :
srun -n 9 -p interactive -J interactive --time=12:00:00 --mem=36G --pty bash
- batch job :
# Mail events (NONE, BEGIN, END, FAIL, ALL)#SBATCH --mail-type=END,FAIL
# Where to send mail#SBATCH --mail-user=<username>@uea.ac.uk
#SBATCH -p compute-16-64 # Select compute queue with 16 cores and 64Gb RAM
#SBATCH --ntasks=9 #Set number of slots required
#SBATCH --ntasks-per-node=16 # maximum slots available on the node
#SBATCH -t 36:00:00 # Set time limit to 36 hours
#SBATCH --job-name=matlab-test_job # Set job name
#SBATCH -o matlab-test-%j.out # Write job output to MatJob-(job_number).out
#SBATCH -e matlab-test-%j.err # Write job error to MatJob-(job_number).err
#set up environment
module add matlab/2020a
#run the application
matlab -nodisplay -nojvm -nodesktop -nosplash -r my_matlab_m_file
Example of Parpool in the Parallel Computing Toolbox
Example Matlab code for a function that estimates pi by simulating 1 billion dart throws and is a good example of how parallel Matlab can result in faster processing.
This example can be copied from /gpfs/software/ada/matlab/samplescripts/MonteCarloPI.m
The key line is the parfor loop. As a parfor loop it is set up to run in parallel, rather than running as a standard sequential for loop.
Matlab Parallel Server
The Matlab Parallel Server allows you to open 'workers' up on other nodes through the SLURM queueing system by using matlabpool.
Usage is basically the same as with the Parallel Computing Toolbox, though instead of using the default local configuration for matlabpool, this time a specific SLURM configuration is used which submits jobs to the queue. Because there is a master Matlab session and workers are managed by SLURM, unlike the Parallel Computing Toolbox, you do not need to request additional slots.
>> parpool ('SlurmProfile1',28)
Starting parallel pool (parpool) using the 'SlurmProfile1' profile ... connected to 24 workers.
To close the parallel pool enter the following command: delete(gcp('nocreate'))
There are many ways of using parallel resources within Matlab. Please refer to the Matlab Documentation.