Load and Run MATLAB

Different recommended procedures for each HPC center:
  • UPPMAX 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

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, and LUNARC are slightly different.

  • While all modules at UPPMAX not directly related to bio-informatics are shown by ml avail, modules at HPC2N and LUNARC 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 and LUNARC, 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.

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), LUNARC (2023b), and HPC2N (2023a.Update4).

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. For short, you can also use:

$ ml matlab/R2023b

Start MATLAB and Run a Script

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 typically 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.

Type-Along

Starting MATLAB at the command line, here R2023b

Once you’ve loaded matlab/2023b, or your preferred version, type:

$ matlab

to start the GUI, or

$ matlab -nodisplay

to start MATLAB in the terminal.

The -singleCompThread is usually required to prevent MATLAB from spawning as many processes as it thinks it needs, which can cause the user to accidentally take over a full node. Most terminal instances launch MATLAB (either the GUI or command line) on a login node by default, so hogging a node can stall other users’ jobs, a violation of the NAISS user agreement. Setting -singleCompThread does not prevent MATLAB from sending parallelized and/or multi-threaded jobs to SLURM or the MATLAB Distributed Computing Server (MDCS).

Starting the MATLAB GUI

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

For HPC2N and UPPMAX users, 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 must still include -singleCompThread!

../_images/Rackham-Matlab.png

Both ways of starting MATLAB on Rackham.

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 (recommended) 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 and always include -singleCompThread to avoid hogging a login node.