Skip to content

Matlab

Matlab example 1: Serial command line use

Matlab is installed on DelftBlue as a software module and, unless it is absolutely crucial to display graphics, should be run from the command line. Let's take a look at a simple example.

Which toolboxes are included?
>> ver
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.11.0.1769968 (R2021b)
MATLAB License Number: 329139
Operating System: Linux 4.18.0-372.26.1.el8_6.x86_64 #1 SMP Sat Aug 27 02:44:20 EDT 2022 x86_64
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
MATLAB                                                Version 9.11        (R2021b)
Simulink                                              Version 10.4        (R2021b)
5G Toolbox                                            Version 2.3         (R2021b)
AUTOSAR Blockset                                      Version 2.5         (R2021b)
Aerospace Blockset                                    Version 5.1         (R2021b)
Aerospace Toolbox                                     Version 4.1         (R2021b)
Antenna Toolbox                                       Version 5.1         (R2021b)
Audio Toolbox                                         Version 3.1         (R2021b)
Automated Driving Toolbox                             Version 3.4         (R2021b)
Bioinformatics Toolbox                                Version 4.15.2      (R2021b)
Communications Toolbox                                Version 7.6         (R2021b)
Computer Vision Toolbox                               Version 10.1        (R2021b)
Control System Toolbox                                Version 10.11       (R2021b)
Curve Fitting Toolbox                                 Version 3.6         (R2021b)
DDS Blockset                                          Version 1.1         (R2021b)
DSP System Toolbox                                    Version 9.13        (R2021b)
Database Toolbox                                      Version 10.2        (R2021b)
Datafeed Toolbox                                      Version 6.1         (R2021b)
Deep Learning HDL Toolbox                             Version 1.2         (R2021b)
Deep Learning Toolbox                                 Version 14.3        (R2021b)
Econometrics Toolbox                                  Version 5.7         (R2021b)
Embedded Coder                                        Version 7.7         (R2021b)
Filter Design HDL Coder                               Version 3.1.10      (R2021b)
Financial Instruments Toolbox                         Version 3.3         (R2021b)
Financial Toolbox                                     Version 6.2         (R2021b)
Fixed-Point Designer                                  Version 7.3         (R2021b)
Fuzzy Logic Toolbox                                   Version 2.8.2       (R2021b)
GPU Coder                                             Version 2.2         (R2021b)
Global Optimization Toolbox                           Version 4.6         (R2021b)
HDL Coder                                             Version 3.19        (R2021b)
HDL Verifier                                          Version 6.4         (R2021b)
Image Acquisition Toolbox                             Version 6.5         (R2021b)
Image Processing Toolbox                              Version 11.4        (R2021b)
Instrument Control Toolbox                            Version 4.5         (R2021b)
LTE Toolbox                                           Version 3.6         (R2021b)
Lidar Toolbox                                         Version 2.0         (R2021b)
MATLAB Coder                                          Version 5.3         (R2021b)
MATLAB Compiler                                       Version 8.3         (R2021b)
MATLAB Compiler SDK                                   Version 6.11        (R2021b)
MATLAB Report Generator                               Version 5.11        (R2021b)
Mapping Toolbox                                       Version 5.2         (R2021b)
Mixed-Signal Blockset                                 Version 2.1         (R2021b)
Model Predictive Control Toolbox                      Version 7.2         (R2021b)
Motor Control Blockset                                Version 1.3         (R2021b)
Navigation Toolbox                                    Version 2.1         (R2021b)
Optimization Toolbox                                  Version 9.2         (R2021b)
Parallel Computing Toolbox                            Version 7.5         (R2021b)
Partial Differential Equation Toolbox                 Version 3.7         (R2021b)
Phased Array System Toolbox                           Version 4.6         (R2021b)
Powertrain Blockset                                   Version 1.10        (R2021b)
Predictive Maintenance Toolbox                        Version 2.4         (R2021b)
RF Blockset                                           Version 8.2         (R2021b)
RF PCB Toolbox                                        Version 1.0         (R2021b)
RF Toolbox                                            Version 4.2         (R2021b)
ROS Toolbox                                           Version 1.4         (R2021b)
Radar Toolbox                                         Version 1.1         (R2021b)
Reinforcement Learning Toolbox                        Version 2.1         (R2021b)
Risk Management Toolbox                               Version 1.10        (R2021b)
Robotics System Toolbox                               Version 3.4         (R2021b)
Robust Control Toolbox                                Version 6.11        (R2021b)
Satellite Communications Toolbox                      Version 1.1         (R2021b)
Sensor Fusion and Tracking Toolbox                    Version 2.2         (R2021b)
SerDes Toolbox                                        Version 2.2         (R2021b)
Signal Integrity Toolbox                              Version 1.0         (R2021b)
Signal Processing Toolbox                             Version 8.7         (R2021b)
SimBiology                                            Version 6.2         (R2021b)
SimEvents                                             Version 5.11        (R2021b)
Simscape                                              Version 5.2         (R2021b)
Simscape Driveline                                    Version 3.4         (R2021b)
Simscape Electrical                                   Version 7.6         (R2021b)
Simscape Fluids                                       Version 3.3         (R2021b)
Simscape Multibody                                    Version 7.4         (R2021b)
Simulink 3D Animation                                 Version 9.3         (R2021b)
Simulink Check                                        Version 5.2         (R2021b)
Simulink Code Inspector                               Version 4.0         (R2021b)
Simulink Coder                                        Version 9.6         (R2021b)
Simulink Compiler                                     Version 1.3         (R2021b)
Simulink Control Design                               Version 6.0         (R2021b)
Simulink Coverage                                     Version 5.3         (R2021b)
Simulink Design Optimization                          Version 3.10        (R2021b)
Simulink Design Verifier                              Version 4.6         (R2021b)
Simulink PLC Coder                                    Version 3.5         (R2021b)
Simulink Report Generator                             Version 5.11        (R2021b)
Simulink Requirements                                 Version 1.8         (R2021b)
Simulink Test                                         Version 3.5         (R2021b)
SoC Blockset                                          Version 1.5         (R2021b)
Stateflow                                             Version 10.5        (R2021b)
Statistics and Machine Learning Toolbox               Version 12.2        (R2021b)
Symbolic Math Toolbox                                 Version 9.0         (R2021b)
System Composer                                       Version 2.1         (R2021b)
System Identification Toolbox                         Version 9.15        (R2021b)
Text Analytics Toolbox                                Version 1.8         (R2021b)
UAV Toolbox                                           Version 1.2         (R2021b)
Vehicle Dynamics Blockset                             Version 1.7         (R2021b)
Vehicle Network Toolbox                               Version 5.1         (R2021b)
Vision HDL Toolbox                                    Version 2.4         (R2021b)
WLAN Toolbox                                          Version 3.3         (R2021b)
Wavelet Toolbox                                       Version 6.0         (R2021b)
Wireless HDL Toolbox                                  Version 2.3         (R2021b)

1. Load necessary modules:

module load matlab

This already lets you use Matlab directly in text mode on the login node:

[<NetID>@login03 ~]$ matlab
MATLAB is selecting SOFTWARE OPENGL rendering.

                                  < M A T L A B (R) >
                        Copyright 1984-2021 The MathWorks, Inc.
                        R2021b (9.11.0.1769968) 64-bit (glnxa64)
                                   September 17, 2021


To get started, type doc.
For product information, visit www.mathworks.com.

>>

However, since we are working on a supercomputer, our goal is to make Matlab read and write data from/to files, all by itself. Let's learn how to do this!

2. Create an example script:

Here, we will plot a sine function to a file:

x = -2*pi:0.01:2*pi;
y = sin(x);
h = figure('Visible','off');
plot(x,y);
print(h,'-dpng','mySineWave.png');
close(h);

3. Check if this works (interactively, on a login node):

matlab -batch "run('${HOME}/tests/matlab/sine.m'); exit;"

This should generate the file called mySineWave.png:

mySineWave

4. Now, let's do the same properly, by submitting the job to the queue:

First, create a slurm script called matlab.sh:

#!/bin/sh
#
#SBATCH --job-name="matlab_demo"
#SBATCH --partition=compute
#SBATCH --time=00:10:00
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
#SBATCH --mem-per-cpu=1G
#SBATCH --account=research-<faculty>-<department>

module load matlab

srun matlab -batch "run('/scratch/$USER/tests/matlab/sine.m'); exit;"

Second, submit your job:

sbatch matlab.sh

This will submit the job to the queue, and will request 1 CPU per job. As a result, this will generate the slurm-XXX.out file and the mySineWave.png file.

Matlab example 2: Parallel execution on a single node

The following example (parEigenLocal.m) will use 24 (locally available) CPUs in parallel:

function [elapsedTime] =  test_for()
nworker = 24
myCluster = parcluster('local');
parpool(myCluster, nworker)
N = 1000;
A = zeros(N,1);
tic;
parfor i = 1 : N
    E = eig(rand(100))+i;
    A(i) = E(1);
end
elapsedTime = toc;
end

In order to submit this job, we use the following submission script. Please note, that we have to specify that the job is running on a single node with --nodes=1 and we have to request one extra CPU for the matlab wrapper script, e.g. if we want 24 "workers", we have to specify 25 CPUs with --ntasks=25 and --cpus-per-task=1; this means, that the maximum number of CPUs you can request is 48 and the maximum number of workers you can request is 47.

#!/bin/bash

#SBATCH --job-name="pforTest"
#SBATCH --time=00:15:00
#SBATCH --nodes=1
#SBATCH --ntasks=25
#SBATCH --cpus-per-task=1
#SBATCH --mem-per-cpu=1GB
#SBATCH --partition=compute
#SBATCH --account=research-<faculty>-<department>

module load matlab
matlab -r parEigenLocal

Slurm output file will look as follows:

                            < M A T L A B (R) >
                  Copyright 1984-2021 The MathWorks, Inc.
                  R2021b (9.11.0.1769968) 64-bit (glnxa64)
                             September 17, 2021


To get started, type doc.
For product information, visit www.mathworks.com.


nworker =

    24

Starting parallel pool (parpool) using the 'local' profile ...
Connected to the parallel pool (number of workers: 24).

ans =

 ProcessPool with properties:

            Connected: true
           NumWorkers: 24
              Cluster: local
        AttachedFiles: {}
    AutoAddClientPath: true
          IdleTimeout: 30 minutes (30 minutes remaining)
          SpmdEnabled: true


ans =

    0.9932

Matlab example 3: Parallel execution on multiple nodes

In order to run Matlab on more than one node, we have to rely on Matlab Parallel Server. What Parallel Server does, is submitting the job to the queue on your behalf, provided that the "Slurm profile" is configured properly. Then you do not need a slurm script (it will be generated automatically), but only need to specify the correct "Slurm profile" in the Matlab script itself, e.g. here for 2 nodes, using 2 CPUs per node:

function [elapsedTime] =  test_for()
nworker = 4
myCluster = parcluster('SlurmProfile1');
myCluster.ResourceTemplate = '--job-name=matlab-parallel --time=00:15:00 --account=research-<faculty>-<department> --nodes=2 --ntasks-per-node=2 --cpus-per-task=1 --mem-per-cpu=1GB';
parpool(myCluster, nworker)
N = 1000;
A = zeros(N,1);
tic;
parfor i = 1 : N
    E = eig(rand(100))+i;
    A(i) = E(1);
end
elapsedTime = toc;
end

Of course, before executing such a script, you have to setup the Slurm profile parameters correctly. In the example above, we pass these parameters with the myCluster.ResourceTemplate command:

myCluster.ResourceTemplate = '--job-name=matlab-parallel --time=00:15:00 --account=research-<faculty>-<department> --nodes=2 --ntasks-per-node=2 --cpus-per-task=1 --mem-per-cpu=1GB';

We execute the above script without GUI:

matlab -nodesktop -nosplash -r "parEigen,quit"

This should invoke Matlab and spawn a new job to the queue:

matlab-parallel-01

The result then is:

nworker =

     4

Starting parallel pool (parpool) using the 'SlurmProfile1' profile ...
Connected to the parallel pool (number of workers: 4).

ans =

 ClusterPool with properties:

            Connected: true
           NumWorkers: 4
              Cluster: SlurmProfile1
        AttachedFiles: {}
    AutoAddClientPath: true
          IdleTimeout: 30 minutes (30 minutes remaining)
          SpmdEnabled: true
 EnvironmentVariables: {}


ans =

    1.4681
What to do if I don't have SlurmProfile1?
  1. Start Matlab's GUI on a visualization node (see below).

  2. Create and configure a new Slurm profile:

    1. Go to Parallel -> Parallel Preferences:

      matlab-01

    2. Create a Slurm profile:

      matlab-03

    3. Configure it with appropriate parameters (the important thing there is "Additional Slurm properties", where you should enter all your usual #SBATCH flags):

      matlab-04

      matlab-05

    You can also verify your configuration:

    matlab-06

    When everything is configured correctly, you should see the following:

    matlab-07

    When you close Matlab, these configuration files should be saved in your /home/$USER/.matlab folder and you should be able to submit jobs via script, as I described earlier.

Matlab example 4: Interactive use via GUI on a visual node

1. Open remote connection to the visual node

Detailed discussion about visualization nodes can be found here.

a. Connect to DelftBlue and start a "desktop" job:

$ module load 2022r2
$ module load desktop
$ vnc_desktop 2h -- --cpus-per-task=8 --mem=16G

As a result, you should get a message along these lines:

Reserving a GPU node (partition "visual")

SLURM job ID is 912918
Waiting for job to start running on a node
We got assigned node visual01, waiting for output...
Waiting until VNC server is ready for connection...

VNC server visual01:1 ready for use!

Check which "port number" the system allocated to you, here "visual01:1", i.e. port "1".

b. Open a tunnel to the port above

Open a separate terminal window and open a tunnel to the corresponding port:

ssh -L 5901:visual01:5901 <netid>@login.delftblue.tudelft.nl

c. Connect your VNC client to the local port corresponding to the open tunnel

vncviewer localhost:1

or graphically:

VNC client window

You should be now prompted with your VNC password (not your NetID password!):

VNC password

Finally, we have our remote desktop up and running:

VNC open

2. Load necessary modules and run Matlab GUI

Inside of the remote desktop, open a terminal and type the following:

module load 2022r2
module load visual
module load virtualgl
module load matlab
vglrun matlab -nosoftwareopengl

You should see the following:

VNC ansys running

You are good to go now.