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 is using the same or similar module system for their software. The difference lies in which modules are installed and their versions/naming. The general examples below will be the same for all/most HPC centres in Sweden - the specific names for the modules will vary a little.

Objectives

  • Show how to load MATLAB

  • Show how 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 use MATLAB R202??.

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

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/2023b

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

$ ml matlab/2023b

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.

For most of the resources above, the -singleCompThread is essential to prevent MATLAB from spawning as many processes as it thinks it needs, which can result it in hogging 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).

Examples

Try them yourself!

  1. Load MATLAB in the terminal and do a few simple commands.

$ 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
  1. Run an example function, add2(a,b) (the function file is in the exercises folder).

>> sum2 = add2(5,8);
result =
    13
The sum of 5 and 8 is 13
>> sum2
sum2 =
    13
  1. 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.