Matlab
The Matlab programming environment is available on Hamilton as a software module that must be loaded in order to make the Matlab commands available. To see which versions are available and load one of them, type e.g:
module avail matlabmodule load matlab/R2025a
MATLAB can be used on Hamilton in two ways.
- The full MATLAB graphical interface can be run on a compute node via the Hamilton portal. For lighter work, it can also be used on the login nodes by loading a version of the matlab module, as above, and executing the
matlabcommand. If you wish to work this way, we recommend that you connect to Hamilton using X2GO (see the Login page for details) to improve performance and stability. - Matlab's command-line interface is used for most intensive Matlab work on Hamilton. It is typically used to submit work to the compute nodes as non-interactive batch jobs, and an example job script is given below. As batch jobs are not attached to any display device, any graphical output from them must be saved to files.
The command-line interface can also be used for light work on the login nodes. If you wish to use this, please constrain it by starting it with:matlab --singleCompThread
Installing Matlab code and extra toolboxes
If you would like to save a Matlab function in a file, create a matlab folder in your home directory and place .m files in it.
For example, a file $HOME/matlab/f.m could contain the following text that defines a function, f, which can be used from the Matlab prompt:
function y = f(x)y = x + 2
Alternatively, if you have downloaded a community-provided Matlab Toolbox, you can make it available to your Matlab session by adding the location of its directory to the MATLABPATH environment variable.
Running Matlab jobs
An example Matlab program, in file my_matlab_program.m, might read:
% Perform a simple calculation:x = 2;y = 3;x + y
The following example job script, my_matlab_job.sh, runs my_matlab_program.m in a batch job using a single CPU core:
#!/bin/bash# Request resources:#SBATCH -c 1 # 1 CPU core#SBATCH --mem=1G # 1 GB RAM#SBATCH --time=1:0:0 # 1 hour (days-hours:minutes:seconds)# Run in the 'shared' queue# (job will share node with other jobs)#SBATCH -p shared# Make Matlab available:module load matlab/R2021# Start Matlab:# (-nodisplay disables the graphics interface)# (-singleCompThread stops matlab from using more than one core)matlab -nodisplay -singleCompThread -r "run my_matlab_program.m"
To submit this job to the queue, type the command:
sbatch my_matlab_job.sh
Once your jobs are running, the Hamilton portal can be helpful to monitor performance.
Making Matlab faster
Matlab has a number of ways to parallelise tasks and increase performance. The Matlab profiler can help identify understand which of these techniques is best for your code.
Making Matlab faster tabs content
Running many jobs
Hamilton can support running many different Matlab jobs at the same time. Slurm array jobs may help with managing the submission of multiple, similar jobs.
If you need to run many hundreds of Matlab jobs at the same time, you may need to compile your program into a standalone executable. The executable does not run any more quickly, but it does minimise the amount of contact with the Matlab license server. To generate an executable using a single CPU core:
mcc -R -singleCompThread -m my_matlab_program.m
An executable called my_matlab_program will be created. You may need to execute the following command before running it:
export LD_LIBRARY_PATH=$MATLAB_HOME/sys/os/glnxa64:$MATLAB_HOME/bin/glnxa64:$MATLAB_HOME/runtime/glnxa64:$LD_LIBRARY_PATH
Multithreading
Many, but not all, Matlab routines automatically take advantage of multiple CPU cores in a computer. As Hamilton8's compute nodes each have 128 cores, this allows a significant speed up compared to running on many laptops or desktops.
Example job script, allowing the use of an entire compute node:
#!/bin/bash# Request resources:#SBATCH -c 128 # number of CPU cores. 128 is a whole compute node.#SBATCH -t 01:00:00 # 1 hour (hours:minutes:seconds)# Run in the default queue#SBATCH -p shared# Make Matlab available:module load matlab/R2025a# Start Matlab:# (-nodisplay disables the graphics interface)matlab -nodisplay -r "run my_matlab_program.m"
Experimentation may be required - comparing how quickly single CPU core and multithreaded job scripts run, to see if your Matlab program can take advantage of multiple cores.
Distributed computing
If multithreading alone does not provide the speed-up you require, Matlab is also able to create multiple copies of itself, called workers, which run code within a Matlab parfor loop. Note that functions inside the parfor loop may also multithread. Workers come in two flavours, which are licensed differently:
- Distributed Computing Toolbox (formerly Parallel Toolbox) - the workers are all on the same node as the main Matlab program
- Distributed Compute Engine (formerly Distributed Computing Server) - the workers can be spread across multiple nodes, allowing parfor to use more CPU cores and memory than available in a single node
To use Distributed Computing Toolbox, use the same job script as in the Multithreading section, but:
- In the job script, add the following lines after loading the Matlab module:
unset OMP_PLACESunset OMP_PROC_BIND
- In your Matlab script. adapt the following Matlab commands to make sure that Matlab's temporary files for the parpool are saved in a unique location for each job and start the correct number of workers:
% Set up location for temporary files
pooltmpdir = getenv('TMPDIR');pcluster = parcluster('local');pcluster.JobStorageLocation = pooltmpdir;% Start the workersparpool(pcluster, str2num(getenv('SLURM_CPUS_PER_TASK')))
After these lines, use the parfor command to distribute work across the workers.
If you wish to try Distributed Compute Engine, which may permit running much larger computations over several of Hamilton's compute nodes, please contact us first.
GPU
Some Matlab functions support the use of GPUs and are likely to perform faster on them. These functions will automatically make use of a GPU if they are supplied with at least one argument in the form of a gpuArray. For advice on specific types of data and how to check which functions are GPU-enabled, see the Mathworks advice on Running Matlab functions on a GPU.
Many functions in the Deep Learning toolbox will use GPUs automatically and will perform better on them. Data do not need to be stored in gpuArray format for this toolbox. For more information see the Mathworks advice on Deep Learning functions on GPUs.
Within your code, the function gpuDeviceTable will list the GPUs available, while gpuDevice lets you inspect the properties of a GPU and select/deselect which to use. With older Matlab modules and newer GPUs, gpuDevice may give a warning such as "GPU device is not supported because it has a higher compute capability (9.0) than is natively supported by the CUDA libraries." This is resolved by working with a newer Matlab module.
It is also possible to run Matlab code in parallel over more than one GPU, distributing between them using a parpool and parallel functions such as parfor. The function gpuDeviceCount can be used to set the pool size. For more information, see the Mathworks advice on using multiple GPUs.
For generic information about using MATLAB on GPUs, see the Mathworks web pages on GPU computing.
For more information about GPUs on Hamilton8, see the Systems, Queueing system and Example GPU jobs pages.