Skip to content

Submitting MATLAB Jobs With Slurm

This script is an example of launching individual MATLAB jobs on the Kennesaw State VHPC Cluster. It includes code to help you safely run Parallel Computing Toolbox (PCT) jobs from within MATLAB, although you will have to adjust your MATLAB code to take advantage of this feature. Failure to do this can cause unpredictable results for your jobs and the compute nodes.

Slurm Shell Script

In our example, we saved the following Slurm script as run_matlab.slurm (making sure to change netid@kennesaw.edu and account_name in the script to the correct values for your job):

run_matlab.slurm
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
#SBATCH --partition="defq"
#SBATCH --time=10:00:00
#SBATCH --mail-type="BEGIN,END,FAIL"
#SBATCH --mail-user="netid@kennesaw.edu" (1)
#SBATCH --account="account_name" (2)

JOBID=`echo $SLURM_JOBID | cut -f1 -d.`

module load MATLAB

# Create a temp workdir under scratch
mkdir -p ${HOME}/work/matlab
export SLURM_MATLAB_WORKDIR=$( mktemp -d -p ${HOME}/work/matlab workdir_XXXXXXXXXX )

matlab -nodisplay -nosplash -logfile ${FILE}.log -r "run ${FILE}"

# Delete temp workdir
rm -rf ${SLURM_MATLAB_WORKDIR}
  1. 🙋‍♂️ Make sure to change netid@kennesaw.edu on this line to your KSU email address or you won't receive emails when the job starts and stops.
  2. 🙋‍♂️ Make sure to change account_name on this line to the VHPC billing account your job should be charged to.

MATLAB Code Changes

If you're using the PCT, you need to set the JobStorageLocation to the contents of the environment variable SLURM_MATLAB_WORKDIR. It will also set the PoolSize of the created cluster to the total number of requested processors in your job to make maintaining your script easier.

pc = parcluster('local');
pc.JobStorageLocation = getenv('SLURM_MATLAB_WORKDIR');
slurmNP = str2num(getenv('SLURM_NPROCS'));
parpool(pc,slurmNP);

Usage

Assuming you saved the Slurm script from above as run_matlab.slurm, and the name of your Rmpi script is matlab_script.m, you can submit your MATLAB job with the following command:

[barney@vhpc ~]$ sbatch --export=ALL,FILE=${PWD}/matlab_script.m run_matlab.slurm