Hamilton supports using the Matlab programming environment. A number of versions are made available through the module command. module avail matlab will show what is availble.
The full Matlab graphical interface can be used on the login nodes by loading a module, e.g. module load matlab/R2019a.1 and executing the matlab command. We recommend that you do this using X2GO (see the Login page for details) to improve performance and stability.
Non-interactive Matlab scripts can be run on the compute nodes through the batch queue; however, this will use the text-based interface for Matlab so that the output can be captured in a text output file.
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, file $HOME/matlab/f.m containing the following will define 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.
Example Matlab program, in file my_matlab_program.m:
% Perform a simple calculation: x = 2; y = 3; x + y
Example job script my_matlab_job.sh to run a Matlab program using a single CPU core:
#!/bin/bash # Request resources: #SBATCH -n 1 # 1 CPU core #SBATCH --mem=1G # 1 GB RAM #SBATCH --time=6:0:0 # 6 hours (hours:minutes:seconds) # Run on the queue for serial ("sequential") work # (job will share node with other jobs) #SBATCH -p seq7.q # Make Matlab available: module load matlab/R2019a.1 # 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"
Submit it to the queue with the command: sbatch my_matlab_job.sh
The above example only uses one CPU core, but Matlab has a number of ways to making use of more cores in order to improve the performance of numerical work:
- Running many jobs
Hamilton can support running many different Matlab jobs at the same time.
Many Matlab routines, such as fft() are able to take advantage of more than one CPU core on a single machine.
- Distributed Computing
This is where Matlab creates multiple copies of itself, allowing a program to distribute work across them by using the parfor function
Matlab supports the use of GPU cards where available, although the code needs to be specially written to take advantage of such hardware.
The use of the Matlab profiler may be necessary in order to understand which of these techniques is best suitable for your code.
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. If it report a shared library error and does not run, you may need to execute the following command first:
Many, but not all, Matlab routines automatically take advantage of multiple CPU cores in a computer. As Hamilton's compute nodes typically have 16 or 24 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 --nodes=1 # 1 entire node #SBATCH --time=6:0:0 # 6 hours (hours:minutes:seconds) # Run on Hamilton7's queue for parallel work # (job will have an entire 24 core, 64G RAM compute node) #SBATCH -p par7.q # Make Matlab available: module load matlab/R2019a.1 # 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.
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 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 are spread across multiple nodes, allowing the 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:
- Execute the following Matlab command to start the workers: parpool('local', str2num(getenv('SLURM_CPUS_ON_NODE')))
- Use the parfor Matlab 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.