Using Matlab

This section was written by Julien Besle, School of Psychology

Limitation to 1 node (32 threads)

The current Matlab installation is limited to running on a single node of the HPC, and therefore on a maximum of 32 cores/thread. To run Matlab on multiple nodes, Matlab Parallel Server would have to be purchased and installed. Please contact the HPC’s administrator to discuss this option (hpcsupport@plymouth.ac.uk).

Basic commands

Matlab should be loaded on the computing node using the following command:

module load matlab/2023b

Matlab scripts should be run without graphical interface. A Matlab script can be called in “batch” mode using the following command:

matlab -batch "path/to/script/script_name.m"

Unless the Matlab path is set on your user account (see below), all Matlab scripts and functions should be called with full/relative path, (or possibly be on your $PATH variable in Bash, although I haven’t tested this).

Example sbatch and Matlab scripts

This example Matlab script (testHPC.m) uses a parallel for loop (parfor) to write 999 text files in the current folder and waits 0.1 seconds after each file has been written. The number of threads (workers) used can be set to any number between 0 (no parallel computing) and 32. The relevant variables in the script are numThreads and numWorkers, which should normally be set to the same value. With 32 threads, the parfor loop should take approximately 999 x 0.1 / 32 = 3.1 seconds to execute. Without parallel computing (or with only 1 thread), it would take approximately 999 x 0.1 = 99.9 seconds

This example sbatch script (job_matlab_parfor_test) submits the above Matlab script as a job to the computing node (and deletes the created text files at the end).

Copy both files in a folder on your user account and run the sbatch script from within this folder as follows:

sbatch job_matlab_parfor_test

After the job has run, you can look at the output in text file job.[job_ID].out, where [job_ID] is the ID of the job on the computing node. Find more information on how to run jobs using slurm in this section

Configuring Matlab’s path on your user account

To call Matlab scripts and functions without having to specify their path, you can configure Matlab’s path. Matlab’s path cannot be saved for future Matlab sessions because this would require root privileges. Instead, you can configure Matlab to re-create the path at startup using a startup file. To set this up, you can run the following commands on the HPC’s head node:

  • create default Matlab user directory

mkdir /home/[username]/Documents/MATLAB
  • set user path in Matlab
    • start Matlab (without Graphical interface)

    matlab -nojvm -nosplash -nodesktop
    
    • in Matlab, run the following command:

    userpath('reset')
    

    This should set the userpath variable to ‘/home/[username]/Documents/MATLAB’

    • exit Matlab

    exit
    
  • Once this is done, create a startup file in your Matlab user directory:

touch /home/[username]/Documents/MATLAB/startup.m

This startup script will execute every time Matlab starts up (e.g. from the computing node) and can be used to set Matlab’s path. To add folders to Matlab’s path, add the following command(s) to startup.m using a text editor:

addpath('/path/to/folder'); % this will add a folder, but not its subfolders
addpath(genpath('/path/to/folder')); % this will recursively add a folder and all its subfolders