Introduction#
MATLAB is a general-purpose high-level programming package for numerical work such as linear algebra, signal processing and other calculations involving matrices or vectors of data. We have a campus license for MATLAB which is used on Grex and other local computing resources.
As with most of the Grex software, MATLAB is available as a module. The following command will load the latest version available on Grex:
module load matlab
Then the matlab executable will be in the PATH.
To see all the available versions, use:
module spider matlab
Available Toolboxes#
To see a list of the MATLAB toolboxes available with MATLAB license on Grex, for a given MATLAB version, use the following command:
module load matlab
matlab -nodisplay -nojvm -batch "ver"
Running Matlab#
It is possible to run MATLAB GUI interactively, for best performance in a remote OOD Desktop session. There is no menu shortcuts for Desktops for MATLAB, so a Terminal window has to be open and the matlab module loaded in it. After loading the module, the command matlab will be in the PATH.
OOD also provides two versions of Matlab-specifc Interactive Applications: MatlabDesktop and MatlabServer. These apps would start a Matlab user interface directly.
The interactive / GUI access is very useful for debugging your Matlab code and for using Matlab to visualize your data. However, for production calculations that may take long time, or significant resources, or a large number of short Matlab tasks, we recommend using Matlab in batch mode. For running a MATLAB script in text mode, and/or a batch script, the following options can be used:
module load matlab
# to run a script your_matlab_script.m in batch mode, use -batch with the name only, no extension:
matlab -nodisplay -nojvm -nodesktop -nosplash -batch your_matlab_script
Here is an example of a SLURM job script to submit a job that uses serial MATLAB in batch mode:
#!/bin/bash
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
#SBATCH --mem-per-cpu=4000M
#SBATCH --time=0-3:00:00
#SBATCH --job-name=Matlab-Serial
#SBATCH --partition=skylake
# Load Matlab module/
module load matlab
echo "Starting run at: `date`"
# This is an example to run a code "matlab-code.m".
# Use the file without the extension ".m"
matlab -nodisplay -nojvm -nodesktop -nosplash -singleCompThread -batch "matlab-code"
echo "Program finished with exit code $? at: `date`"
Here is an example of a SLURM job script to submit a job that uses parallel (threaded) MATLAB in batch mode:
#!/bin/bash
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=4
#SBATCH --mem-per-cpu=4000M
#SBATCH --time=0-3:00:00
#SBATCH --job-name=Matlab-Parallel
#SBATCH --partition=skylake
# Load Matlab module/
module load matlab
echo "Starting run at: `date`"
# This is an example to run a code "matlab-code.m".
# Use the file without the extension ".m"
# On this example, the script ois set to use 4 CPUs:
# Adjust --cpus-per-task and --mem-per-cpu as needed.
matlab -nodisplay -nojvm -nodesktop -nosplash -batch "matlab-code"
echo "Program finished with exit code $? at: `date`"
However, when running matlab executable directly, each instance, GUI or command line, will consume a license unit. By submitting sufficiently many MATLAB jobs concurrently, there is a possibility to exhaust the entire University’s license pool. Thus, in many cases, it makes sense to use compiled, standalone MATLAB code runners (MCRs) instead (please refer to the MCR section below).
Using different BLAS/LAPACK in Matlab#
Matlab relies heavily on linear algebra calculations. By default, Matlab would use Intel MKL for BLAS/LAPACK. Since version 2022a, it is possible to configure Matlab to use AMD AOCL libraries (BLIS and FLAME) instead, which may give some performance increase on AMD CPUs.
module load matlab
# lets check which linear algebra libraries are in use
matlab -nodisplay -nojvm -batch "version -blas, version -lapack"
# the answer should be something like "ans =Intel(R) oneAPI Math Kernel Library"
# lets change to use AOCL
export BLAS_VERSION=libblis-mt.so
export LAPACK_VERSION=libflame.so
# and check again
matlab -nodisplay -nojvm -batch "version -blas, version -lapack"
# "ans= AOCL-BLIS..., ans= AOCL-libFLAME... "
# When the above environments are set, Matlab would use them instead of MKL
More information can be found on this Matlab Knowledge Base Article .
Standalone Matlab runners: MCR#
MATLAB compiler, the mcc command can be used to compile a source code (.m file) into a standalone executable. There are couple of important considerations to keep in mind when creating an executable that can be run in the batch oriented, HPC environment. One is that there is no graphical display attached to your session and the other is that the number of threads used by the standalone application must be controlled.
For example, with code mycode.m a source directory src, with the compiled files being written to a directory called deploy, the following mcc command line (at the Linux shell prompt) could be used:
module load matlab
mkdir deploy
cd src
mcc -R -nodisplay -R -singleCompThread -m -v -w enable -d ../deploy mycode.m
Note the option -singleCompThread has been included to limit the executable to just one computational thread.
In the deploy directory, an executable mycode will be created along with a script run_mycode.sh. These two files should be copied to the target machine where the code is going to be run as a batch job.
Example of SLURM script: MCR#
After the standalone executable mycode and corresponding script run_mycode.sh have been transferred to a directory on the target system on which they will be run, a batch job script needs to be created in the same directory. Here is an example batch job script.
#!/bin/bash
#SBATCH --cpus-per-task=1
#SBATCH --mem-per-cpu=4000M
#SBATCH --time=0-3:00:00
#SBATCH --job-name=Matlab-mcr-job
#SBATCH --partition=skylake
# Choose the MCR directory according to the compiler version used.
# The one below for uofm/matlab/R2017A
MCR=/global/software/matlab/mcr/v93
# If running on Grex, uncomment the following line to set MCR_CACHE_ROOT:
module load mcr/mcr
echo "Running on host: `hostname`"
echo "Current working directory is `pwd`"
echo "Starting run at: `date`"
./run_mycode.sh $MCR > mycode_${SLURM_JOBID}.out
echo "Program finished with exit code $? at: `date`"
The job is then submitted as any ordinary SLURM job with the sbatch command. See the Running Jobs page for more information. If the above script is called run-matlab-mcr.sh, it could be submitted using:
sbatch run-matlab-mcr.sh
The specified --time and total memory (--mem-per-cpu) limits should be adjusted to appropriate values for your specific run. The option --partition is used to specify the partition to use for running the job. For more information, visit the page running jobs on Grex
An important part of the above script is the location of the MATLAB Compiler Runtime (MCR) directory. This directory contains files necessary for the standalone application to run. The version of the MCR files specified must match the version of MATLAB used to compile the code (check the link for matching module and MCR versions).
MATLAB on the Alliance’s clusters#
For using MATLAB on the Alliance’s clusters, please visit the corresponding MATLAB page . While there is a wide MATLAB license accessible for all users on cedar , beluga and narval , using MATLAB on graham requires access to an external license . UManitoba users could use MATLAB on graham without additional settings.