What is MATLAB?

MATLAB is a technical programming environment for high-performance numeric computation and visualization. MATLAB integrates numerical analysis, matrix computation, signal processing, and graphics in an easy-to-use environment where problems and solutions are expressed just as they are written mathematically–without traditional programming.

Availability

The University of Hong Kong has a MATLAB Campus-Wide License arrangement to provides unlimited use of  MATLAB and Simulink software to all faculty, staff and students. The software can be installed on the on-campus computing facilities, laboratories, classrooms and research centres, as well as faculty-owned and student-owned personal computers.

Staff and students can access HKU’s MATLAB Portal to download, install and activate the MATLAB license. MATLAB is installed at HPC cluster systems. Staff/students with valid HKU Portal UID can apply an HPC account to run MATLAB on the system.

Toolboxes available

Under the MATLAB Campus-Wide license, it covers the access to the most up-to-date suite of MATLAB and Simulink products by all staff and students. Below are some toolboxes that are commonly used by our researchers.

MATLAB Simulink
Bioinformatics Toolbox Control System Toolbox
Curve Fitting Toolbox DSP System Toolbox
Deep Learning Toolbox Econometrics Toolbox
Financial Toolbox Fixed-Point Designer
Fuzzy Logic Toolbox Image Processing Toolbox
MATLAB Compiler Optimization Toolbox
Parallel Computing Toolbox Partial Differential Equation Toolbox
Signal Processing Toolbox Simulink Control Design
Statistics and Machine Learning Toolbox Symbolic Math Toolbox
Wavelet Toolbox

Using MATLAB in HPC system

User should login any of the HPC Linux cluster machines first. Type the following command to set up the required environment variables for MATLAB:

System MATLAB version Module command
HPC2015 R2015a module load matlab/r2015a
R2015b module load matlab/r2015b
R2016b module load matlab/r2016b
R2017b module load matlab/r2017b
R2018b module load matlab/r2018b
HPC2021 R2021b module load matlab/r2021b
R2022b module load matlab/r2022b

If version number is not provided (i.e. module load matlab), the latest MATLAB version will be used.

Interactive MATLAB

  • To start an interactive, command line (non GUI) version of MATLAB
    Please enter matlab -nodisplay from console.
  • To start an interactive GUI version of MATLAB
    If you are able to use X-11 forwarding and have enabled it in your SSH client software preferences, you can run MATLAB using the GUI by typing the command “matlab“. This will be similar to the one used on WINDOWS. (Reference: How to run a GUI application on HPC cluster systems? )

Only use interactive MATLAB session at hpc2015-file/hpc2021-io1(X server of HPC2015/HPC2021 system). Since the login node of the HPC cluster system is a shared resource, running scripts that require significant computational resources will impact the usability of the cluster for others. Therefore you should not use interactive MATLAB sessions on the master node for any significant computation. If your MATLAB script requires significant time, CPU power, or memory, you should run your MATLAB program via the batch system.

Batch MATLAB Jobs

Batch job system is available in the HPC clusters only. In order to run MATLAB via the batch system, you will require a job submission script and a MATLAB M-file containing the script that will be run via the batch system. Sample programs and PBS/Slurm scripts can be obtained in /share1/matlab/sample/.

  1. Running serial MATLAB job
    Step 1: Prepare a “MATLAB ready” m-file. e.g. test.m

    disp 'Hello World'

    Step 2: Start MATLAB by entering command matlab -nodisplay

    Step 3: Compile the MATLAB m-file with mcc in MATLAB command prompt

    mcc -m test.m

    You will find an executable test in your current directory.

    Step 4: Prepare a job submission script. Sample PBS/SLURM script are as follows:
    For HPC2015 system – pbs-matlab.cmd

    #!/bin/sh
    #PBS –N test_matlab
    #PBS -q parallel
    #PBS -l walltime=00:10:00
    #PBS -l nodes=1:ppn=1
    #PBS -j oe
    
    module load matlab
    cd $PBS_O_WORKDIR
    ./test > ${PBS_JOBNAME}.${PBS_JOBID}
    # end of example file

    For HPC2021 system – slurm-matlab.cmd

    #!/bin/bash
    #SBATCH --job-name=t-matlab
    #SBATCH --time=30:00
    #SBATCH --cpus-per-task=32
    #SBATCH --nodes=1
    #SBATCH --ntasks=1
    #SBATCH --output=%x.out.%j 
    #SBATCH --error=%x.err.%j 
    
    module load matlab
    cd ${SLURM_SUBMIT_DIR}
    ./test > ${SLURM_JOB_NAME}.${SLURM_JOBID}
    matlab -batch test
    

    Step 5: Submit the job with PBS/SLURM script.

    qsub pbs-matlab.cmd
    sbatch slurm-matlab.cmd
  2. Running multiple MATLAB serial jobs
    Step 1: Prepare a “MATLAB ready” m-file. e.g. test.m
    Step 2: Start MATLAB console mode by entering command matlab -nodisplay
    Step 3: Compile the MATLAB m-file with mcc in MATLAB command prompt

    mcc -m test.m

    You will find an executable “test” in your current directory.

    Step 4: Modify the file “msf.sh”.
    For example, change ./test > $1.log
    $1 means the serial job number, please do not alter it.

    Step 5: Modify the file “msf.dat” by changing the number of serial jobs

    ./msf.sh
    1
    1 10 1

    i.e. change “10” to “5”, meaning that you only require to run 5 serial jobs, and do not alter other parameters and format

    Step 6: Submit the job through pbs-msf.cmd
    You can alter the parameters according to our own usage

  3. Running MATLAB parallel job

Configure a new cluster profile

    1. Launch MATLAB Graphical User Interface
      Use a web browser to access  https://hpc2021-io2.hku.hk/pun/sys/dashboard/batch_connect/sys/matlab/session_contexts/new and launch MATLAB
    2. Click “Parallel” -> “Create and Manage Clusters”
    3. Click “Add Cluster Profile” > “Slurm” to create a new cluster profile
    4. Edit the cluster profile according to your job’s need. Most of the setting can be left as default while “ADDITONAL SLURM PROPERTIES” may be customized, such as the partition name.
    5. Click “Done” to save the profile.

Configure a MATLAB script to use the multi-node cluster profile

  1. Modify a MATLAB script as below
    % open the desired Slurm cluster profile
    p = parcluster('hpc2021-2node');
    
    % get the number of workers from environment variable “N_WORKER”
    N_WORKER = str2num(getenv('N_WORKER'));
    
    % open the pool using workers spread across multiple nodes
    parpool(c, N_workers)
    
    
  2. Compile the MATLAB script as myprog
    $ mcc -m myprog.m

Configure a SLURM batch script

  1. Prepare a batch job file for launching MATLAB, only 1 CPU is needed because this job is simply a launcher that will submits a second SLURM job requesting multiple CPUs
    #!/bin/bash
    #SBATCH --job-name=myprog             # Job name
    #SBATCH --partition=intel             # Request a partition
    #SBATCH --qos=normal                  # Request a QoS
    #SBATCH --ntasks-per-node=1           # Request total number of tasks (MPI workers)
    #SBATCH --nodes=1                     # Request number of nodes
    #SBATCH --cpus-per-task=1             # Request number of cpus per task
    module load matlab
    N_WORKER=32
    matlab -batch myprog
    
  2. Run “showjob” to confirm the job is using CPU in multiple nodes.

Example codes are under /share1/matlab/sample/parallel-tool-box

Additional Information

Official documentation is available at  MathWorks website.
Online tutorials and Webinars by Mathworks: