Matlab in batch or interactive mode.

Two methods are available:

  1. Using the Matlab engine to run your scripts.
    • Advantage: No Compilation needed.
    • Disadvantage: You will claim an amount of licenses with respect to the number of nodes running your script.
  2. Compile your script to a standalone application (Matlab Runtime).
    • Advantage: No license required when running (still needs one license during compilation)
    • Disadvantage: some parts are not supported (this you could check at mathworks).

Example Files

In this example we will be using a batch file named matlab-example.sbatch where we will be allocating 1 cores and running the file matlab_example.m in MATLAB. A quick snippet of code to run in MATLAB:

matlab_example.m
[X,Y] = meshgrid(-2:.2:2);
Z = X .* exp(-X.^2 - Y.^2);
surf(X,Y,Z);
print('example-plot','-dpng');
exit;

The sbatch file that tells the cluster what you want to do:

matlab-example.sbatch
#!/bin/bash
#SBATCH -J matlab_example               # job name, don't use spaces
#SBATCH -c 1                            # number of cores, 1
#SBATCH --mail-type=END,FAIL            # email status changes
 
# load env for matlab
module load mathworks/matlab_r2017a
 
# run your command
matlab -nodesktop -nosplash -nodisplay < matlab_example.m

Batch File Breakdown

The lines starting with #SBATCH are defining what resources we will be asking the cluster to use and some settings.

Note : The most common options are listed on the slurm_sbatch page.

The module load mathworks/matlab_r2017a line tells the system to load MATLAB specific environmental settings into your session. To view the available modules run the command module avail.

The last line is what we are actually going to run. MATLAB will be running with three options, -nodesktop -nosplash -nodisplay that are needed to suppress the GUI, while < _matlab_example.m_ pipes the contents of your .m file into MATLAB.

Running Your Job

Once you have these files on the cluster, using the sbatch command will submit it.

sbatch matlab-example.sbatch

Slurm will submit your job and returns the <jobid>. The standard output from the command will be saved in the same directory with a file name slurm-<jobid>.out and in this case the matlab code will create a plot and save it as example-plot.png.

Example Files

In this example we will be using a batch file named _mcr-example.sbatch_ where we will be allocating 1 cores and running the compiled _matlab_example.m_ script using the MATLAB Runtime. A quick snippet of code to run in MATLAB:

matlab_example.m
[X,Y] = meshgrid(-2:.2:2);
Z = X .* exp(-X.^2 - Y.^2);
surf(X,Y,Z);
print('example-plot','-dpng');
exit;

The batch file that tells the cluster what you want to do:

mcr-example.sbatch
#!/bin/bash
#SBATCH -J mcr_example             # job name, don't use spaces
#SBATCH -c 1                       # number of cores, 1
#SBATCH --mail-type=END,FAIL       # email status changes
 
# load env for mcr
module load mathworks/mcr_r2017a
 
# run your command (mcc),
./matlab_example
# run your command (deploytool),
#./matlab_example/for_testing/matlab_example

Batch File Breakdown

The lines starting with #SBATCH are defining what resources we will be asking the cluster to use and some settings.

Note : The most common options are listed on the slurm_sbatch page.

The module load mathworks/mcr_r2017a line tells the system to load MATLAB Runtime specific environmental settings into your session ($MCRROOT points to the MCR root folder). To view the available modules run the command module avail.

Keep in mind that mcc places the compiled files in the same folder as your matlab script, deploytool creates a folder structure, you should add the relative subfolder of the shell script. If you like to use the generated scripts (eg the *.sh files) to start the application, you should change the command into:

./run_matlab-example.sh $MCRROOT

Building the code

The first step is to build the standalone application using one of the provided methods :

  • Use the commandline compiler from command line or matlab shell :
mcc -o matlab_example -R -nodisplay -m matlab_example.m

This results in a standalone executable and a shell script in the folder of the compiled script. The “-R -nodisplay” parameters instruct the matlab compiler to suppress the GUI output in your code.

  • Use the “deploytool” to compile your project (from matlab shell)
deploytool
  1. Select Application Compiler
  2. Select the main matlab script matlab_example.m, fill in all the required fields and press Package

This will result in three additional folders

  • module_example/for_redistribution
  • module_example/for_redistribution_files_only
  • module_example/for_testing

Running Your Job

Once you have these files on the cluster, using the sbatch command will submit it.

sbatch mcr-example.sbatch

Slurm will submit your job and returns the <jobid>. The standard output from the command will be saved in the same directory with a file name slurm-<jobid>.out and in this case the matlab code will create a plot and save it as example-plot.png.

In interactive mode the user will interact with the user interface of the application. It is possible to run the software on the headnode directly, but due to low performance of these it is better to request a compute node to run it.

This will use the sinteractive wrapper and will depend on the availability of the requested resources !

Initial setup

Load the slurm utils software module (sinteractive)

module load slurm/utils

Request resources using sinteractive (optionally modify the default resources, default is : 2 cpu's for 1 hour.).

sinteractive --cpus-per-task=4 --time=1-0:0:0

Once the resources are claimed you will get a similar response as the following:

srun: job 164362 queued and waiting for resources
srun: job 164362 has been allocated resources

The result is that you will get a prompt on one of the compute nodes.

Start matlab

module load mathworks/matlab_r2022a
matlab
exit

Don't forget to exit the requested resources when you finish matlab (use the exit command) !