Skip to content

MATLAB

MATLAB

MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming environment. Furthermore, it provides a vast library of mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization, numerical integration and solving ordinary differential equations. It provides built-in graphics for visualizing data and tools for creating custom plots.

DIPC owns licenses for all the existing toolboxes for MATLAB through all the modules that have the -IKUR suffix.

Usage

The MATLAB environment at DIPC can be accessed and used on our systems after loading the relevant module using the Lmod system. This can be done by issuing the following command in your terminal:

$ module load MATLAB/R2020b

Available Versions and Modules

Different versions of MATLAB can be listed by using the module spider command as shown below:

$ module spider MATLAB

This will display a list of all available MATLAB versions that can be loaded using Lmod. To load a specific version, replace R2020b in the module load command with the desired version.

Software version

Here you can check the available versions for MATLAB in the different clusters

MATLAB/R2011b
MATLAB/R2014b
MATLAB/R2018a
MATLAB/R2018b
MATLAB/R2019b
MATLAB/R2020b
MATLAB/R2022a-IKUR
MATLAB/R2017b
MATLAB/R2020b-IKUR
MATLAB/R2020b
MATLAB/R2022a-IKUR
MATLAB/2024a-r4
MATLAB/2023a-r5
MATLAB/2024a-r4

Running MATLAB Interactively and Non-Interactively

MATLAB can be run in two modes: interactive and non-interactive. While the interactive mode is common on desktop computers, non-interactive mode is the preferred way of running MATLAB on supercomputing systems.

Non-Interactive MATLAB Sessions

A non-interactive MATLAB session can be initiated by redirecting the standard input and output when invoking MATLAB, or invoking MATLAB from a batch script, submitted to the queue via the sbatch command. Here is an example of a non-interactive session:

$ matlab < script.m > output.log

In this example, MATLAB will run script.m and direct the output to output.log.

Please make sure to include the exit command at the end of your MATLAB script (script.m in the example). This is to ensure MATLAB quits after finishing execution of the code.

Interactive MATLAB Sessions

For interactive sessions, you can simply type matlab in the terminal after loading the module. This will open the MATLAB interface where you can run commands interactively.

$ matlab

Running MATLAB in Parallel using the Parallel Computing Toolbox

MATLAB's Parallel Computing Toolbox allows users to take advantage of multi-core processors. It provides parallel for-loops, special array types, and parallelized numerical algorithms. This allows you to solve computationally and data-intensive problems using multicore processors, GPUs, and computer clusters.

Here's how to run MATLAB in parallel using the Parallel Computing Toolbox:

  1. Load MATLAB module as shown in the Usage section.
  2. To start a parallel pool of workers, use the parpool function:
parpool('local',N) % N is the number of workers

Using MATLAB Compiler for Standalone Executables

The MATLAB Compiler enables you to share MATLAB programs as standalone applications. These applications use the MATLAB runtime, allowing deployment to users who do not need MATLAB. This does not use a license from the license server, which helps in sharing licenses among all users.

Here's how to create a standalone application:

  1. Load MATLAB module as shown in the Usage section.
  2. Compile your MATLAB script (myfile.m in this example) using the MATLAB Compiler (mcc):
$ mcc -v -R -singleCompThread -m myfile.m

The -singleCompThread option ensures that MATLAB uses a single computational thread. This is generally recommended for compatibility reasons, but if you are certain your code can benefit from multithreading and will not cause issues, you can remove this option.

Usage Examples

Example 1: Non-interactive MATLAB Job

  • Create a MATLAB script, say my_script.m, that includes your MATLAB code. End your script with an exit statement to ensure MATLAB quits after execution.
% my_script.m
disp('Hello, MATLAB!')
exit
  • Create a SLURM batch script, say run_matlab.sbatch:
#!/bin/bash
#SBATCH --qos=regular
#SBATCH --job-name=MATLAB_JOB
#SBATCH --mem=10gb
#SBATCH --cpus-per-task=1
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=1
#SBATCH --output=%x-%j.out
#SBATCH --error=%x-%j.err

module load MATLAB/R2020b

matlab -nodisplay -nosplash -singleCompThread < my_script.m > output.log
  • Submit your job using sbatch:
$ sbatch run_matlab.sbatch

Example 2: Parallel MATLAB Job using Parallel Computing Toolbox

  • Create a MATLAB script, say my_parallel_script.m, that includes your MATLAB code. Use parallel constructs like parfor for parallel execution. End your script with an exit statement.
% my_parallel_script.m
parpool('local', 4)

parfor i = 1:10
    disp(i^2);
end

delete(gcp) % Delete current parallel pool
exit
  • Create a SLURM batch script similar to Example 1, but change my_script.m to my_parallel_script.m, do not limit the number of threads to 1, and set --cpus-per-task=4 (or the amount of cores you wish to use in your parallel loop):
#!/bin/bash
#SBATCH --partition=regular
#SBATCH --job-name=MATLAB_JOB
#SBATCH --mem=10gb 
#SBATCH --cpus-per-task=4
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=1
#SBATCH --output=%x-%j.out
#SBATCH --error=%x-%j.err

module load MATLAB/R2020b

matlab -nodisplay -nosplash < my_parallel_script.m > output.log
  • Submit your job using sbatch as shown in Example 1.