Mathworks MATLAB

MATLAB (matrix laboratory) is a multi-paradigm numerical computing environment and fourth-generation programming language. Developed by MathWorks, MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages, including C, C++, Java, and Fortran. (taken from, more details there)

Work In Progress


We currently offer the following MATLAB versions:


You can query the versions yourself with module av |& grep -i matlab.


Load the most recent version available with:

$ module load math/MATLAB

You can also load a particular version with module load math/MATLAB/<version>, e.g. module load math/MATLAB/2017a

See the vendor documentation:

The currently available toolboxes are

Toolbox Version
Curve Fitting Toolbox3.5.8
Econometrics Toolbox5.1
Fuzzy Logic Toolbox2.4
Image Acquisition Toolbox5.5
Image Processing Toolbox10.3
MATLAB Compiler7.0
MATLAB Compiler SDK6.6
Mapping Toolbox4.7
Optimization Toolbox8.2
Parallel Computing Toolbox6.13
Partial Differential Equation Toolbox3.1
Signal Processing Toolbox8.1
Statistics and Machine Learning Toolbox11.4
Symbolic Math Toolbox8.2
Wavelet Toolbox5.1

Using Matlab

Using Matlab is possible several ways, which we would like to briefly introduce here.


where mostly taken from the vendor documentation

Starting MATLAB from a Linux system prompt using matlab gives you several start-up options. We only present an overview of the most commonly used option. A complete list can be found in the MATLAB documentation.

Option Result
-nojvm Start MATLAB without the JVM software. Features that require Java are not supported.
-nodisplay Start the JVM software without starting MATLAB desktop. Does not display X commands. Overrides the DISPLAY environment variable.
-nosplash Do not display the splash screen during startup.
-singleCompThread Limit MATLAB to a single computational thread. For numerical computations only. (Default)
-multiCompThread Do not limit MATLAB to a single computational thread.
-sd folder Set the MATLAB folder, specified as string, e.g. -sd "~/matlab" sets the folder to matlab.
-r statement Execute the statement. Use this option for interactive workflows. Do not use this option with the -batch option.
-batch statement Execute MATLAB script, statement, or function non-interactively. Important features are: -nodesktop, -nosplash, logs to stdout & stderr.
The use of -batch statement is to be preferred to -r statement.

Request resources according to your needs, e.g. with:

[joe_user@login]$ srun -n<ntasks> -p<partition> -A<account> -t<time> --mem<amount> --pty --preserve-env $SHELL  

Load the desired Matlab module:

module load math/MATLAB

Now you can run Matlab. Add -nojvm flag to start Matlab without the Java virtual machine, -nosplash prevents Matlab from displaying the Matlab logo.

matlab -nodisplay -nosplash

Check the version info and the available toolboxes for that version:

>> ver


MATLAB Version: (R2018b)
Operating System: Linux 3.10.0-957.5.1.el7.x86_64 #1 SMP Fri Feb 1 14:54:57 UTC 2019 x86_64
Java Version: Java is not enabled
MATLAB                                                Version 9.5         (R2018b)
Curve Fitting Toolbox                                 Version 3.5.8       (R2018b)
Econometrics Toolbox                                  Version 5.1         (R2018b)

The use of the Matlab-Compiler is to be preferred!

Only a limited number of licences are available on the cluster. It is therefore preferable to use the Matlab-Compiler to compile your scripts beforehand. We explain more about the Matlab-Compiler as follows.

There are several options to compile your Matlab code to stand-alone executables/libraries. Being independent of licenses is one of the major advantages here, of course. But when running compiled code with the Matlab Runtime Envirenment (MRE) on MOGON you have to consider the threading of your code just as well as when you run Matlab itself.

mcc Command Arguments

Option Description Comment
-? Display help message Cannot be used in a deploytool app.
-m Generate a standalone application. Equivalent to -W main -T link:exe. Cannot be used in a deploytool app.
-n Automatically treat numeric inputs as MATLAB doubles. Cannot be used in a deploytool app.
-o outputfile Specify name of final output file. Adds appropriate extension. Cannot be used in a deploytoolapp.
-R option Specify run-time options for MATLAB Runtime. Valid only for standalone applications using MATLAB Compiler.
option = -nojvm, -nodisplay, '-logfile filename', -startmsg, and -completemsg filename
-v Verbose; display compilation steps.

MATLAB Help Center, mcc Command Arguments Listed Alphabetically

If your code doesn't need the full multithreading capability, which often is the case, you should compile your code with:

mcc -m my_mfile.m 

This causes the -singleCompThread option to be applied automatically, which limits MATLAB to a single computational thread. This is enabled by default on MOGON.
Using -singleCompThread also ensures that your standalone code will run on a single computational thread, which not only doesn't frustrate the core scheduler and the other users less, but also improves the performance of your code by spending less time scheduling all the threads on a core.

Hello MOGON Example

function hellomogon
    fprintf('===[ Hello MOGON! ]===\n')
module load math/MATLAB
mcc -m -R -nodisplay mcc_example.m -o hello_mogon
===[ Hello MOGON! ]===

Generally, Matlab detects the number of physical cores and opens the same amount of threads to make full use of the multithreading implemented in the built-in functions. By calling

mcc -m -R -multiCompThread my_mfile.m

you obtain multithread code. Often this might be wanted, but you have to make sure that you select the appropriate resources for this then - namely, the appropriate CPUs. Since Matlab wants to use everything on a host you'll have to set #SBATCH --cpus-per-task and an appropriate memory reservation.

We would like to explain with an typical Hello MOGON! example how you can achieve this.

The trivial MATLAB script has only one line:

fprintf('Hello MOGON!\n')

Notice: Save your file with the .m extension, but call it without the .m extension.

A serial Job requires only one core, the Slurm job script therefore would read as follows:

The Job script (serial_matlab_job.slurm) i

#SBATCH --partition=smp
#SBATCH --account=<YourHPC-Account>
#SBATCH --time=0-00:02:00
#SBATCH --mem-per-cpu=512 #0.5GB
#SBATCH --ntasks=1
#SBATCH --job-name=serial_matlab
#SBATCH --output=%x_%j.out
#SBATCH --error=%x_%j.err
module purge
module load math/MATLAB
matlab -singleCompThread -nodisplay -nosplash -batch hello_mogon

The start-up options (-singleCompThread, -nodisplay, -nosplash) used are explaind above. To run the script, you can now simple submit it to the batch system with the following command:

sbatch serial_matlab_job.slurm

After the job completes, you can view the output with:

cat matlab-example.out

which should give you

[ ... ]
Hello MOGON!

as Matlab will not be able to fully utilize the node, but considers every FPU equal to a CPU.

The following example is a job that uses Matlab's Parallel Computing Toolbox. You can benefit from the built-in multithreading provided by Matlab's BLAS implementation, if you code makes use of this Toolbox or if you have intense computations. The following is an example of MathWorks and can be found here.

slurm_tasks = str2double(getenv('SLURM_CPUS_PER_TASK'));
fprintf('Number of available Slurm CPUs is: %g\n', slurm_cpus);
myCluster = parpool('local',slurm_tasks);
fprintf('The Number of MATLAB Workers is: %g\n', myCluster.NumWorkers);
n = 200;
A = 500;
a = zeros(n);
parfor i = 1:n
    a(i) = max(abs(eig(rand(A))));
fin = toc;
fprintf('With %g CPUs per Task the calculation took %g seconds.\n', slurm_cpus, fin);    

The Slurm job script is kept simple, with one process that has four cores for multithreading:

#SBATCH --account=<YourHPC-Account>
#SBATCH --job-name=parallel_matlab_job
#SBATCH --output=%x_%j.out
#SBATCH --error=%x_%j.err
#SBATCH --partition=smp
#SBATCH --time=0-00:05:00
#SBATCH --mem-per-cpu=4096 #4GB
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=4
module purge
module load math/MATLAB/2018b
matlab -nodisplay -nosplash -batch parfor_matrix

To run the script, simply submit it to the scheduler with the following command:

sbatch parallel_matlab_job.slurm

After the job has finished, view the output with the following command:

cat parallel_matlab*.out

which should give you the following output:

[ ... ]
With 4 CPUs per Task the calculation took 17.0605 seconds.

For optimal performance, the value of –cpus-per-task must be adjusted. Use the smallest value that gives you a significant performance gain. The matrix multiplication example from above has the following runtimes on MOGON II with different –cpus-per-task:

CPUs per Task Runtime (s)
2 29.22
4 17.06
6 11.43
8 8.40


The Parallel Computing Toolbox requires JVM, please do no specify -nojvm if you want to use it!

Testing computationally intensive operations
For operations where the number of floating-point computations performed per element read from or written to memory is high, the memory speed is much less important. In this case the number and speed of the floating-point units is the limiting factor. These operations are said to have high “computational density”.

A good test of computational performance is a matrix-matrix multiply. For multiplying two $N \times N$ matrices, the total number of floating-point calculations is $$ FLOPS(N) = 2N^3 - N^2 $$

Two input matrices are read and one resulting matrix is written, for a total of $3N^2$ elements read or written. This gives a computational density of $(2N - 1)/3$ FLOP/element. Contrast this with plus as used above, which has a computational density of $1/2$ FLOP/element.

MATLAB Help Center, Measuring GPU Performance

gpu = gpuDevice();
fprintf('Using an %s GPU\n', gpu.Name)
sizeOfDouble = 8;
sizes = power(2, 12:2:26);
N = sqrt(sizes);
mmTimesHost = inf(size(sizes));
mmTimesGPU = inf(size(sizes));
for ii=1:numel(sizes)
    % First do it on the host
    A = rand( N(ii), N(ii) );
    B = rand( N(ii), N(ii) );
    mmTimesHost(ii) = timeit(@() A*B);
    % Now on the GPU
    A = gpuArray(A);
    B = gpuArray(B);
    mmTimesGPU(ii) = gputimeit(@() A*B);
#SBATCH --account=<YourHPC-Account>
#SBATCH --job-name=matlab_gpu_job
#SBATCH --output=%x_%j.out
#SBATCH --error=%x_%j.err
#SBATCH --partition=m2_gpu
#SBATCH --gres=gpu:1
#SBATCH --time=0-00:35:00
#SBATCH --mem-per-cpu=4096 #4GB
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=4
module purge
module load math/MATLAB/2018b
matlab -nodisplay -nosplash -batch gpu_perf
cat gpu_job_*.out
[ ... ]
Achieved peak calculation rates of 72.9 GFLOPS on Host and 390.7 GFLOPS on GPU


Using the following shell script you can compile a m-file into a stand-alone c application. It's a variation of the script for using local scratch on a node.
# Store working directory to be safe
# We define a bash function to do the cleaning when the signal is caught
    cp /jobdir/${LSB_JOBID}/*$ ${SAVEDPWD}/
    cp /jobdir/${LSB_JOBID}/$FILENAME ${SAVEDPWD}/
    exit 0
# Register the cleanup function when SIGUSR2 is sent, ten minutes before the job gets killed
trap 'cleanup' SIGUSR2
# Copy input file
cp ${SAVEDPWD}/$FILENAME.m /jobdir/${LSB_JOBID}/
# Go to jobdir and start the program
cd /jobdir/${LSB_JOBID}
mcc -m $FILENAME.m
# Call the cleanup function when everything went fine
  • start/development/scripting_languages/matlab.txt
  • Last modified: 2021/04/16 18:22
  • by jrutte02