Running matlab code on HPC with SLURM

Running MATLAB scripts on HPC

Today, the question came up how to run MATLAB code on HPC featuring a SLURM scheduler. The syntax for running matlab on the command line is indeed a bit counterintuitive, at least if you are (like me) used to running python or R scripts.

Example SLURM script

The following snippet is an example for how to submit a matlab script for execution on an HPC Server with the SLURM scheduler:

#! /bin/bash
# SBATCH -p <PARTITION>
# SBATCH -t <RUN TIME>
# SBATCH -N <NUMBER OF NODES>
# SBATCH -o job-%J.out
# SBATCH --mail-type=ALL
# SBATCH --mail-user=$USER

module load matlab
matlab -nodesktop -nodisplay -nojvm -nosplash -r "<matlab_script_basename>"

IMPORTANT:

  1. You must be in the directory containing the matlab script when you submit the batch job or configure the working directory or add a line cd <directory_containing_matlab_script> in the batch job script.

  2. The matlab script must have the extensios “.m”

  3. Replace <matlab_script_basename> with the filename of your matlab script but omit the extension. So if your matlab script is named “magic_script.m”, you have to write “magic_script” in the batch job.