Running MATLAB on Grex

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. MATLAB is available only for UManitoba users.

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 uofm/matlab

Then the matlab executable will be in the PATH.

Available Toolboxes#


To see a list of the MATLAB toolboxes available with MATLAB license on Grex, you can use the following command:

module load uofm/matlab
matlab -nodisplay -nojvm -batch "ver"

Running Matlab#


It is possible to run MATLAB GUI interactively, for best performance in an X2Go session, OOD session and a terminal. There is no Applications menu shortcut for MATLAB, because it is only in the PATH after the module is loaded from the command line. After loading the module, the command matlab will be in the PATH.

For running a MATLAB script in text mode, or a batch script, the following options can be used:

matlab -nodisplay -nojvm -nodesktop -nosplash -r your_matlab_script.m

However, 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 most cases, it might make sense to use compiled, standalone MATLAB code runners (MCRs) instead (please refer to the next section).

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 a 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 has to 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 uofm/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 in order 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.

Script example for running MATLAB via MCR
run-matlab-mcr.sh
#!/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=compute

# 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 particular 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.