MATLAB Guide
- 1 MATLAB licensing
- 2 Using MATLAB on the cluster
- 2.1 Loading MATLAB module
- 2.2 Setting the desired GCC version for MATLAB
- 2.3 Submitting .m files with slurm
- 2.3.1 Single Core job
- 2.4 Running a matlab job in serial
- 2.4.1 Multithread job
- 2.4.2 How to write a script for parpool
- 2.5 Interactive MATLAB use with SLURM
- 2.6 Extra Notes
- 2.7 Too many open files error
MATLAB licensing
The HPC cluster uses the university site license for MATLAB, which includes an unlimited number of seats and all toolboxes. More information is available here: http://software.uconn.edu/matlab/ .
Using MATLAB on the cluster
All jobs on the cluster must be submitted through the SLURM scheduler using sbatch
. Please read the SLURM Guide for more details. The preferred way to run MATLAB
jobs is by using a .m
file. Please note that if your job uses many cores or a lot of memory it is better to submit to reserve a whole compute node (24 cores) in exclusive mode
Loading MATLAB module
You can view a list of all available MATLAB versions with:
module avail matlab
To load the MATLAB module:
module load matlab/R2023b
Setting the desired GCC version for MATLAB
If you want to use a specific gcc module with matlab, be sure to load the gcc module after loading the matlab module:
module load matlab/R2023b
module load gcc/11.3.0
To verify the version that Matlab is using, issue the following command in Matlab:
for gcc
for g++
Submitting .m files with slurm
Single Core job
To submit a job that uses a single core to run a MatLab file TestCluster.m. Create a script called matlabSP.sh:
NOTE: any ";" or " ' " after "-r" should be pre-appended by "\".
Then submit the script:
Running a matlab job in serial
To submit a job that uses a SLURM job array to run a Matlab file called serialMatlabtest, create a script called MatlabSerialRun.sh
Example contents of the serialMatlabtest Matlab .m file:
The Matlab script will convert the SLURM job scheduler “SLURM_ARRAY_TASK_ID” global environmental variable to a string that Matlab will be able to understand.
Once converted to a separate variable, the Job Array task id can be displayed to keep track which Slurm Job Array ID is currently running within a SLURM job array. Matlab will run using one single Computational thread in the above example.
Multithread job
To submit a job that uses 10 computational threads on one node, create a submission script matlabMP.sh:
any ";" or " ' " after "-r" should be pre-appended by "\".
Then submit the script by:
How to write a script for parpool
To run a job with parfor
, please rewrite TestCluster as:
The old function that parpool replaced was called matlabpool.
Then, submit your script as:
Interactive MATLAB use with SLURM
To run an interactive Matlab session with GUI, you should "ssh -X" to the cluster from a Linux machine or macOS machine with X11 enabled. For more info, check out our guide to using X11 on the HPC here. Then, run the below commands.
To open an interactive Matlab window with 10 cores available, you will want to do the following:
Please DO NOT FORGET to EXIT from the nodes so that the other users can use it. Exit out of the MATLAB window and then type exit
into the terminal to end your srun session.
Note that any interruption to the network will unfortunately cause your job to crash irrecoverably.
Extra Notes
When opening a parallel pool, you may receive a warning. This warning is fine to ignore, as this warning is currently a bug in the older versions of Matlab and is fixed on newer versions of Matlab.
The parallel pools help set the number of workers from the Slurm allocation so that Matlab spawns them in the local Matlab profile as well.
To make sure Matlab runs only one instance per the job submission, the matlab -r matlabfilename\;exit\; command is used in the slurm script
The parallel pool on the cluster gives errors currently if parpool() or matlabpool is used, this can be fixed by running the Matlab 2017b module.
The pool can be called with: delete(gcp('nocreate')) "this removes previous parallel interactive sessions" p=gcp "this assigns the gcp or current parallel pool instead of using p=parpool()"
Too many open files error
If Matlab runs into an error mentioning that there are too many open files within the Linux Operating System, this is due to the built-in Java functionality in Matlab.
The built-in Java will keep files open due to Java’s design.
To bypass this issue, the -nojvm Matlab option would need to be specified in the Matlab command: