Load and Run MATLAB

Different recommended procedures for each HPC center:
  • UPPMAX, NSC, and HPC2N: use module system to load at command line

  • LUNARC: recommended to use Desktop On-Demand menu, but interactive and non-interactive command lines available

  • PDC: recommended to load at command line; can run interactively on a compute node with X-forwarding and salloc. ThinLinc access is restricted to 30 users for the whole Dardel cluster.

Most HPC centres in Sweden use the same or a similar module system for their software. The difference lies in which modules are installed and their versions/naming. The general examples below will be similar for all HPC centres in Sweden, with some variation in naming and available versions.

Objectives

  • Be able to load MATLAB

  • Be able to run MATLAB scripts and start the MATLAB graphical user interface (GUI)

Warning

  • Note that the module systems at UPPMAX, HPC2N, LUNARC, NSC, and PDC are slightly different.

  • While all modules at UPPMAX and NSC not directly related to bio-informatics are shown by ml avail, modules at the other centers may be hidden until one has loaded a prerequisite like the compiler GCC.

  • Thus, you need to use module spider to see all modules at HPC2N, LUNARC, and PDC, and ml avail for those available to load given your currently loaded prerequisites.

  • There is no system MATLAB that comes preloaded like Python, but ml load matlab with no release date will load the latest release, which is periodically updated. For reproducibility reasons, you should be sure to load the same release throughout a given project.

  • New sessions on Dardel (PDC) start with 13 modules loaded, but only one of them is sticky (i.e. will remain loaded after a ml purge command). We highly recommended that you save the preloaded modules as a collection so that you can quickly restore the default modules if you accidentally use ml purge instead of ml unload <module>.

Check for MATLAB versions

Type-Along

Checking for MATLAB versions

Check all available MATLAB versions with:

$ module avail matlab

Note

In this course we will mainly use MATLAB R2023b.

Load a MATLAB module

For reproducibility, we recommend ALWAYS loading a specific module instead of using the default version!

For this course, we recommend using MATLAB R2023x at UPPMAX (R2023b), NSC (2023b), and HPC2N (2023a.Update4), or R2024b at LUNARC (2024b). At PDC, we recommend r2024b for users affiliated with KTH or who have their own MathWorks accounts, or r2023b otherwise.

Type-Along

Loading a Matlab module at the command line, here R2023b

Go back and check which MATLAB modules were available. To load version 2023b, do:

$ module load matlab/R2023b

Note: all lowercase except the R. For short, you can also use:

$ ml matlab/R2023b

Start MATLAB at the Command Line

Most of the time, you will run either MATLAB live scripts (.mlx) or basic script or function files (.m). Live scripts can only be opened and worked on in the GUI, while basic function or script files can also be run from a batch script and/or at the command line. It is important to note that at the command line, function definition is typically not supported unless the function is short and anonymous; user-defined functions must generally be written up and saved to separate .m files.

The GUI is often the recommended interface where it is offered. The GUI provides ways to set up SLURM jobs through the Parallel Computing Toolbox, which will be discussed later. The resources required to run the GUI and those required to run a job submitted to SLURM are separate, so do not worry if the maximum allocation time for a MATLAB GUI session is much less than the limit for a SLURM job.

When starting MATLAB from the command line, the -singleCompThread flag is often required to prevent MATLAB from spawning as many processes as it thinks it needs up to the full capacity of a node. At most HPC centers, terminal instances launch MATLAB (either the GUI or command line) on a login node by default, so hogging a node can disrupt jobs and access for other users. Disrupting other users violates the NAISS user agreement, and HPC center staff reserve the right to kill disruptive tasks without warning.

Some HPC centers detect if you’ve started on a login node and set maxCompThreads to 1 automatically, but when in doubt, use -singleCompThread to be safe. Setting -singleCompThread does not prevent MATLAB from sending parallelized and/or multi-threaded jobs to SLURM or the MATLAB Distributed Computing Server (MDCS).

Type-Along

Starting MATLAB at the command line, here R2023b

Once you’ve loaded your preferred version of MATLAB, type:

$ matlab -nodisplay

to start MATLAB in the terminal. The maximum number of computational threads will be set to 1 automatically if you are on a log-in node.

Starting the MATLAB GUI

Running the MATLAB GUI requires that users be logged into a ThinLinc session. See https://uppmax.github.io/R-matlab-julia-HPC/common/login.html#log-in

For HPC2N, once logged into the remote desktop, the procedure for starting the MATLAB GUI is the same as what was shown above to start it at the command line, except that the -nodisplay flag is omitted (as are -nodesktop -nosplash if applicable). You should still include -singleCompThread!

It is possible to start the MATLAB GUI on a compute node on Kebnekaise (HPC2N). The procedure uses salloc and srun to achieve what other facilities often do with the interactive command (edit time, resources, project ID, and MATLAB version as needed):

$ salloc -N 1 -t 00:30:00 -A hpc2n20YY-XXX
$ module load MATLAB/2023b
$ srun matlab

Exercises

Try them yourself!

Load MATLAB in the terminal or GUI and do a few simple commands at the command line. For example,

$ ml matlab/2023b
$ matlab -singleCompThread -nodisplay
                            < M A T L A B (R) >
               Copyright 1984-2023 The MathWorks, Inc.
          R2023b Update 7 (23.2.0.2515942) 64-bit (glnxa64)
                           January 30, 2024
To get started, type doc.
For product information, visit www.mathworks.com.
>> a = 5;
>> b = eye(2);
>> c = a+b
c =
    6     5
    5     6

Copy the example function below to a file called add2.m in your working directory or the MATLAB directory that the configuration step created for you in your Documents folder. Then run it at the MATLAB command line.

function result = add2(x,y)
result = x+y
disp("The sum of "+x+" and "+y+" is "+result)
end

Exit the MATLAB command line with quit or exit (this can take a few seconds).

>> exit

Keypoints

  • You can start MATLAB either in a GUI or, with the -nodisplay flag, run it in the terminal.

  • If you start either interface from the terminal, you must first load the correct module(s).

  • For interactive work, you will usually have to reserve time on a compute node.

  • If you take the risk of starting on a login node, use the -singleCompThread flag in the starting command to avoid hogging the node, unless you are sure that your cluster sets that constraint on the login node automatically.