Skip to content

MATLAB

MATLAB is a programming and numeric computing platform used by millions of engineers and scientists to analyze data, develop algorithms, and create models.

HPC Examples

Check out the HPC Examples Gitlab repo which includes all scripts below.

Choosing a MATLAB version

Run the command below to see the available MATLAB versions.

module spider matlab

------------------------------------
  matlab:
------------------------------------
     Versions:
        matlab/2017b
        matlab/2018b
        matlab/2019a
        matlab/2021b
In the submit script you must choose a specific version.

MATLAB GUI via Command Line

First, you will need to connect to the KU Community Cluster or Hawk cluster via X11 or X2Go.

After connecting above you will connected to the submit nodes. We don't suggest launching MATLAB on the submit nodes as any process will get killed automatically after 60 minutes of CPU time. You want to submit an interactive job. This will connect you to a compute node.

Once on the compute node, you can then launch MATLAB with the following command:

$ module load matlab/2021b
$ matlab

Note

It can take a minute or more for the GUI to appear and for initialization to complete

Running on a Compute Node via sbatch

Submitting a job via sbatch will launch MATLAB scripts with no display. This will allow scripts to be ran without a GUI.

In order to do this we need a submission script and a MATLAB script.

Serial MATLAB Job

A serial MATLAB job is one that requires only a single CPU-core. Here is an example of a trivial, one-line serial MATLAB script (hello_world.m):

hello_world.m
fprintf('Hello world.\n')

The submit script below can be used for serial jobs:

serial_submit.sh
#!/bin/bash
#SBATCH --job-name=matlab       # Job name
#SBATCH --partition=sixhour     # Partition Name (Required)
#SBATCH --ntasks=1              # Total number of tasks
#SBATCH --cpus-per-task=1       # Number of cores per task
#SBATCH --mem=2gb               # Job memory request
#SBATCH --time=0-00:05:00       # Time limit hrs:min:sec

module load matlab/2021b
matlab -singleCompThread -nodisplay -nosplash -r hello_world

No Display

By invoking MATLAB with -singleCompThread -nodisplay -nosplash, the GUI is suppressed as is the creation of multiple threads.

To run the MATLAB script, submit the job via sbatch

$ sbatch serial_submit.sh

After the job completes, you can view the output in the *.out file

Parallel MATLAB Job

Most of the time, running MATLAB in single-threaded mode (as described above) will meet your needs. However, if your code makes use of the Parallel Computing Toolbox (e.g., parfor), then you can run in multi-threaded mode.

Multi-node jobs are not possible

Multi-node jobs are not possible with the version of MATLAB that we have so your Slurm script should always use #SBATCH --nodes=1

Here is an example that uses multiple cores:

for_loop.m
poolobj = parpool;
fprintf('Number of workers: %g\n', poolobj.NumWorkers);

tic
n = 200;
A = 500;
a = zeros(n);
parfor i = 1:n
    a(i) = max(abs(eig(rand(A))));
end
toc

Multi-Core Job

Make sure your job can take advantage of all the cores you request. Requesting more cores than your job can use is a waste of resources.

The submit script can be used for parallel MATLAB jobs:

parallel_submit.sh
#!/bin/bash
#SBATCH --job-name=parfor       # Job name
#SBATCH --partition=sixhour     # Partition Name (Required)
#SBATCH --ntasks=1              # Total number of tasks
#SBATCH --cpus-per-task=4       # Number of cores per task
#SBATCH --mem-per-cpu=2gb       # Job memory request
#SBATCH --time=0-00:10:00       # Time limit hrs:min:sec

module load matlab/2021b

matlab -nodisplay -nosplash -r for_loop

Submit the parallel_submit.sh via sbatch:

$ sbatch parallel_submit.sh

You must change the option --cpus-per-task for how many cores you wish for your job to run on. Run the command crctool to see available nodes with core count in your owner group partition.

Parallel Performance

To make sure you code is actually using all allocated cores, check out your job's core usage at Measuring Memory and CPU Usage

Overriding the 12 core limit

By default MATLAB will restrict you to 12 worker threads. You can override this when making the parallel pool with the following line, for example, with 24 threads:

poolobj = parpool('local', 24);