There are a number of Matlab versions that are installed in the cluster: 2018a, 2018b, 2019b, 2020a, 2020b, 2021a
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:
a binary file also named <matlab_script>
,
and a script named <matlab_script>.sh
.
It may be useful to create a new directory for each compiled Matlab script.
One way to do this is to create a new directory, then place the Matlab script in it and then the mcc
command to compile the script.
Another way is to create a new directory and then to send the files created in the compile process to the new directory by using the -d argument: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, we could 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 compiled MATLAB code, MATLAB will create a hidden cache containing binaries, links, scripts, etc. By default this cache will be located in your home directory.
If the cache directory is located on a shared file system and a large number of parallel processes attempt to access it simultaneously, this will adversely affect the performance of the compiled MATLAB code.
To address this, configure the cache directory to a local folder on the same compute node that the Matlab code is running on, by setting the MCR_CACHE_ROOT
environmental variable. To do this, use the following command in your sbatch script: export MCR_CACHE_ROOT=/tmp/$SLURM_JOB_ID
Alternatively, you can add it to the script generated by the Matlab compiler.
To run a Matlab script in slurm interactive mode, run: matlab -batch “/<path to matlab>/script <args>"
If you are submitting a script to slurm, add it to the script.
Notice that you should use the Matlab script name without the ending m.
For MATLAB versions older than 2019a) use this instead: matlab -nodisplay -nodesktop -nojvm -r “/<path to matlab>/script <args>"
If your script does not have an exit command at the end, you can add it to your command like so: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. However, not all internal Matlab functions are multi-threaded; this is a list of functions that are.
When running in a cluster, you need to explicitly allocate more than one cpu to each task to ensure that each thread runs on a different core. This is done by setting the cpu-per-task variable to the number of cpus the tasks will need (more than 1), like so: #SBATCH --cpu-per-task=10
Or, like so: #SBATCH -c 10
We recommend examining the code and optimizing it for performance, following Mathworks' recommendations, particularly regarding, preallocation and vectorization.