# Using MATLAB on Quest

Setup and reference instructions for running MATLAB on Quest.

When running MATLAB on Quest, you have the option to make explicit use of parallelization. However, even if you aren't explicitly parallelizing your code, you should be aware of MATLAB's use of multithreading. This is discussed first below. Then, there are instructions for setting up MATLAB to run code in parallel. How you use MATLAB's parallelization will depend on whether you are running on a single node, or on multiple nodes.

## Background

Compute nodes on Quest have at least 28 cores, but some architectures (collections of compute nodes) have more than 28 cores. See Quest Technical Specifications.

If you want to use more than 28 cores per node (in the example below 52 cores), you can request nodes in a specific partition of Quest with the Slurm option:

#SBATCH --constraint="quest10"

## Multithreading

MATLAB has built-in multithreading for some linear algebra and numerical functions. By default, MATLAB will try to use all of the cores on a machine to perform these computations. However, if a job you've submitted to Quest uses more cores than were requested, the job will be cancelled. To avoid this situation, you can start MATLAB with the singleCompThread option to restrict a MATLAB process to a single core:

matlab -singleCompThread

**This is the recommended way to run MATLAB on Quest.**

However, if you want MATLAB to be able to use multiple cores for these calculations, then you can omit the -singleCompThread option when starting MATLAB and request an entire node for your job. Use the options below in your submission script or sbatch command:

#SBATCH --nodes=1 #SBATCH --ntasks-per-node=<numberofcores> #SBATCH --constraint="<partitionName>"

**Example**

#SBATCH --nodes=1 #SBATCH --ntasks-per-node=52 #SBATCH --constraint="quest10"

A quest10 node has 52 cores so when you request 52 tasks per node, you effectively reserve all the node for yourself.

It is strongly recommended not to mix multithreading and explicit parallelization within MATLAB to avoid your process either running at reduced efficiency or being cancelled for exceeding allotted resources.

Note: it may be possible to use multithreading with less than a full node with the maxNumCompThreads option in your MATLAB code, but some libraries may not adhere to the limits set with this option, and it may not be respected by MATLAB in the future.

## Single Node Parallel MATLAB Jobs on Quest

If you want to use MATLAB's parallelization capabilities with a small number of cores, such that the job will fit on a single node (see limits above), then you do not need to create a parallel profile as shown below. You can use the default "local" profile.

In your MATLAB script (<matlabscript.m> in the example below), use

parpool('local', N)

where N is the number of cores you want to use.

Then create a Slurm submission script for your job. There are more details at Submitting a Job on Quest, but a sample script myjob.sh looks like:

mymatlabjob.sh

#!/bin/bash #SBATCH -A <allocationID> #SBATCH -p <partitionName> #SBATCH --nodes=1 #SBATCH --ntasks-per-node=<N> #SBATCH --mem-per-cpu=<memorypercore> #SBATCH -t <hh:mm:ss> ## job commands; <matlabscript> is your MATLAB .m file, specified ## without the .m extension module load matlab/r2018a matlab -nosplash -nodesktop -singleCompThread -r <matlabscript>

Where the necessary variables to be filled out are explained as below:

Flag | Description |
---|---|

<allocationID> | ID of your allocation |

<partitionName> | The name of the partition (i.e. queue) |

<hh:mm:ss> | Time required for the job to finish |

<N> | Number of cores per node |

<memorypercore> | Required memory per core |

Additionally, you can add the following lines to the #SBATCH block in the job submission script to give a name for your job and receive emails when your job's status changes (i.e. begin, end or fail):

#SBATCH -J <jobName> #SBATCH --mail-type=BEGIN,END,FAIL #SBATCH --mail-user=<emailaddress>

Once the submission script is completed, you can submit this script as a batch job using sbatch command:

sbatch myjob.sh

## Cross-node Parallel MATLAB Jobs on Quest

Setting up multi-node parallelization for MATLAB 2018 (and above) is different than the one for MATLAB 2015b, 2016a and 2017a on Quest. Please see below for further instructions.

Note that any version before 2015b (i.e. 2015a, 2014b, 2014a and 2013a) is not supported by Slurm scheduler for cross-node parallelization.