MATLAB

As of May 17th, 2018 MATLAB requires a license checked out from SLURM to function. See the examples below for the proper syntax.

Millions of engineers and scientists worldwide use MATLABĀ® to analyze and design the systems and products transforming our world. MATLAB is in automobile active safety systems, interplanetary spacecraft, health monitoring devices, smart power grids, and LTE cellular networks. It is used for machine learning, signal processing, image processing, computer vision, communications, computational finance, control design, robotics, and much more.

Usage

For these examples we will be using the following script named power_table.m:

power_table.m:

%% POWER_TABLE prints a table of the squares and cubes of the integers.
%  Licensing:
%    This code is distributed under the GNU LGPL license.
%  Modified:
%    31 October 2007
%  Author:
%    John Burkardt

  fprintf ( 1, '\n' );
  fprintf ( 1, '           N   N-squared     N-cubed\n' );
  fprintf ( 1, '\n' );
  for i = 0 : 10
    fprintf ( 1, '  %10d  %10d  %10d\n', i, i^2, i^3 );
  end

  exit

After you create the script (or upload it) we need to create a SBATCH script. This file has three distinct parts:

  1. SLURM Configuration (always at the top of the script)
  2. Module commands to load the MATLAB software
  3. The command to run the MATLAB script

For this example the SBATCH script is called matlab_sbatch.sh:

matlab_sbatch.sh:

#!/bin/bash
#--------------------------------------------------------------------------------
#  SBATCH CONFIG
#--------------------------------------------------------------------------------
#SBATCH --job-name=matlab_first-test  # name for the job
#SBATCH --cpus-per-task=1             # number of cores
#SBATCH --mem=4G                      # total memory
#SBATCH --time 0-00:15                # time limit in the form days-hours:minutes
#SBATCH --mail-user=username@missouri.edu  # email address for notifications

#SBATCH --partition General           # max of 1 node and 2 hours; use `Lewis` for larger jobs
#SBATCH --licenses=matlab:1           # qty of licenses needed
#--------------------------------------------------------------------------------

echo "### Starting at: $(date) ###"

## Module Commands
module load matlab/matlab-R2018b
module list

## Run the matlab script
SCRIPT='power_table.m'
srun matlab -nodesktop -nosplash -nodisplay -r "run('${SCRIPT}');exit"

echo "### Ending at: $(date) ###"

The section at the top labeled 'SBATCH CONFIG' tells SLURM what resources you need for the job. The rest of the script is a standard bash script that sets up your modules and runs the script. Once you have this example working, you will update the line that says SCRIPT='power_table.m' with the name of your own MATLAB script.

To start the job we will use the sbatch command with the name of our SBATCH script like so:

sbatch matlab_sbatch.sh

Output:

[jgotberg@lewis4-r710-login-node223 p-matlab]$ sbatch matlab_sbatch.sh
Submitted batch job 5686984
[jgotberg@lewis4-r710-login-node223 p-matlab]$

The SLURM system will assign a worker node and complete your job as soon as resources are available. The output will be automatically saved to a file called slurm-5686984.out. Notice that the job was given a unique id after we submitted the job and that the output file has that same number included (in this case 5686984). We can check on the progress of our job with the sacct command. See the SLURM pages for more info. Once the job is completed you can view the output with the less command:

ls
less slurm-5686984.out

Output:

[jgotberg@lewis4-r710-login-node223 p-matlab]$ ls
matlab_sbatch.sh power_table.m  slurm-5686984.out
[jgotberg@lewis4-r710-login-node223 p-matlab]$ less slurm-5686984.out
### Starting at: Thu May 17 09:56:57 CDT 2018 ###
INFO: License(s) matlab:1
Currently Loaded Modulefiles:
  1) matlab/matlab-R2018b

                            < M A T L A B (R) >
                  Copyright 1984-2018 The MathWorks, Inc.
                   R2018b (9.4.0.813654) 64-bit (glnxa64)
                             February 23, 2018


To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.


           N   N-squared     N-cubed

           0           0           0
           1           1           1
           2           4           8
           3           9          27
           4          16          64
           5          25         125
           6          36         216
           7          49         343
           8          64         512
           9          81         729
          10         100        1000
### Ending at: Thu May 17 09:57:11 CDT 2018 ###

Scaling up from one core

MATLAB is a serial code unless you implicitly use the parallel toolbox. Assigning more than one core is a waste of resources unless you are using parfor and other parallel concepts in your code. See the documentation and/or attend a training session to learn more.

Interactive Usage with the GUI (Advanced)

Once you have your MATLAB code ready you shouldn't need to interactively run MATLAB except to debug or develop features. Using the cluster interactively has serious downsides and should not be used for everyday production research.

X11 Windows Forwarding must be enabled in your terminal for this to work. See the GUI How To page for more info.

srun -p Interactive --qos interactive --mem 4G --licenses=matlab:1 --pty /bin/bash
module load matlab/matlab-R2018b
matlab

Once you run those commands you will be presented with the MATLAB GUI.