Fork me on GitHub

MATLAB

Scientific computing and visualisation.

Interactive usage

After connecting to Bessemer, start an interactive session with the srun --pty bash –i command.

The latest version of MATLAB (currently 2019a) is made available by running:

module load MATLAB/2019a

You can then run MATLAB by entering matlab &.

Serial (one CPU) batch usage

Here, we assume that you wish to run the program helloworld.m on the system:

function helloworld
    disp('Hello World!')
end

First, you need to write a batch submission file. We assume you’ll call this my_job.slurm:

#!/bin/bash
#SBATCH --mem=16000                # Request  16 GB of real memory

module load MATLAB/2019a

matlab -nodesktop -nosplash -r helloworld

Ensure that helloworld.m and my_job.slurm are both in your current working directory, then submit your job to the batch system:

sbatch my_job.slurm

Note that we are running the script helloworld.m but we drop the .m in the call to MATLAB. That is, we do -r helloworld rather than -r helloworld.m. The output will be written to the job text file when the job finishes.

Parallel MATLAB

Parallel MATLAB using multiple nodes is restricted to a maximum of 40 cores.

Here is an example using 4 cores on a single node. Create a Slurm submission script called parallel_example.slurm containing:

#!/bin/bash
#SBATCH --nodes=1
#SBATCH --mem=16000
#SBATCH --ntasks-per-node=4
#SBATCH --time=00:05:00
#SBATCH --job-name=matlab_par_test

module load MATLAB/2019a

matlab -nodisplay -nosplash -r "parallel_example($SLURM_NTASKS)"

And create a MATLAB script called parallel_example.m containing:

function exit_code = parallel_example(n_cores)
    tic
    pool = parpool(n_cores)

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

    time=toc;
    fprintf('Wall clock duration: %d\n', time);

    hdf5write('out.h5', '/maxeigen', max_eigenvals);

    exit_code = 0;
end

Then submit this as a batch job using:

sbatch parallel_example.slurm

The MATLAB script, parallel_example.m, creates 200 square (500 x 500) matrices comprised of random values, calculates the eigenvalues of each and records the maximum eigenvalue for each matrix in the array max_eigenvals.