There are a number of Matlab versions that are installed in the cluster:
You can change the version of Matlab by using “module load” command.
For example:
module load matlab/2020b
To compile Matlab scripts to standalone code you need to load the module mcc
along with the version of Matlab you want to work with.
For example, to compile with Matlab version 2021a use this command first:
module load matlab/2021a mcc
Then you can compile your script with:
mcc -mv matlab_script.m
When compilation is finished there will be several new files in the folder of the Matlab script.
The ones that are most important are:
It may be better to create a new directory for each compiled Matlab script.
This can be done by creating a new directory, placing the Matlab script in it and issuing the mcc command to compile the script.
Or create a new directory and send the files created in the compile process to the new directory by using the -d argument like so:
mkdir /sci/labs/pi-login/your-login/my-compiled-matlab-script
mcc -m -d /sci/labs/pi-login/your-login/my-compiled-matlab-script matlab_script.m
To use the compiled Matlab script in a job you need to create a script that will include at least the following:
For example create a file called sbatch_matlab_script.sh and enter the following lines:
#!/bin/bash
#SBATCH -n 1
#SBATCH --time=1-0
#SBATCH --mem=5G
module load matlab/2021a
./run_matlab_script.sh $MATLABROOT <arguments>
Then submit to Slurm as usual with:
sbatch sbatch_matlab_script.sh
When executing a compiled MATLAB code, MATLAB creates a hidden cache containing binaries, links, scripts, etc.
By default this cache will be located in your home directory.
Performance of the compiled MATLAB code can be poor if the cache directory is located on a shared file system and a large number of parallel processes attempt to access it simultaneously.
To address this issue the cache directory can be configured to a local folder on the compute node the Matlab code is running by setting the MCR_CACHE_ROOT environmental variable.
Use following command in your sbatch script or add it to the script generated by the Matlab compiler:
export MCR_CACHE_ROOT=/tmp/$SLURM_JOB_ID
To run a Matlab script use the following command (when working in interactive mode) or add it to the script you submit to Slurm:
matlab -batch “/path/to/matlab/script <args>"
Notice that you should use the Matlab script name without the ending m.
For older version of Matlab (before version 2019a) use the following command:
matlab -nodisplay -nodesktop -nojvm -r “/path/to/matlab/script <args>"
If your script does not have an exit command at the end, add it to your command:
matlab -nodisplay -nodesktop -nojvm -r “/path/to/matlab/script <args>;exit"
Matlab has two main ways to take advantage of multicore and multiprocessor computers - multithreading and parallelism.
Multithreading is turned on by default in Matlab. It uses internal multi-threading by default, running as many threads as there are available cores. Not all internal Matlab functions are multi-threaded. See here a list of Matlab functions that are multithreaded.
When running a Matlab code in cluster set the cpu-per-task flag to more than 1 so that each thread will run on a different core:
#SBATCH --cpu-per-task=10
Or
#SBATCH -c 10
We recommend examining the code and optimizing it for performance, following Mathworks' recommendations in this link.
In particular, preallocation and vectorization can have a large impact on performance.