MATLAB
Table of Contents
- How to access the software
- Working with jobs from within the MATLAB Desktop
- Example batch scripts
- Additional notes
Load MATLAB directly:
module load MATLAB/2022b
Available MATLAB versions can be listed with module spider MATLAB
. Specifying a version will list the needed modules: module spider MATLAB/2022b
The NAG Toolbox for MATLAB is tightly integrated into appropriate MATLAB versions, so you do not need to load any additional modules.
Working with jobs from within the MATLAB Desktop
Getting Started with Parallel Computing using MATLAB on the RWTH HPC Cluster
You can find further information in the workshop slides of our training "Scaling Parallel Computing with MATLAB to HPC at RWTH Aachen University" (s. Material section).
Serial Job:
#!/usr/bin/zsh
### Job name
#SBATCH --job-name=MATLAB_SERIAL
### File / path which STDOUT will be written to, %J is the job ID
#SBATCH --output=MATLAB_SERIAL.%J
### Request the time you need for execution. The full format is D-HH:MM:SS
### You must at least specify minutes or days and hours and may add or
### leave out any other parameters
#SBATCH --time=80
### Change to the work directory, if not in submit directory
#cd "$HOME/workdirectory/where/the/m-file/is"
### Load the required modules
module load MATLAB/2022b
### start non-interactive batch job
matlab -singleCompThread -nodisplay -nodesktop -nosplash -logfile job.${SLURM_JOB_ID}.txt <<EOF
run /path/to/the/dot-m-file.m;
quit();
EOF
Multiple Parallel Jobs Running At Same Time:
Matlab uses a shared directory in $HOME
for temporary 'parpool' files, which prohibit multiple 'parpool' jobs from running at same time.
Thus change the location of this directory before starting yet another 'parpool'.
#!/usr/bin/zsh
### Job name
#SBATCH --job-name=MATLAB_MULTI
### File / path which STDOUT will be written to, the %J is the job id
#SBATCH --output=MATLAB_MULTI.%J
### Request the time you need for execution. The full format is D-HH:MM:SS
### You must at least specify minutes or days and hours and may add or
### leave out any other parameters
#SBATCH --time=80
### Request the number of compute slots you want to use
#SBATCH --nodes=1
#SBATCH --ntasks=12
### Change to the work directory, if not in submit directory
cd "$HOME/workdirectory/where/the/m-file/is"
### Load required modules
module load MATLAB/2022b
### open 'parpool' and run the 'dot-m-file.m' Matlab script in current directory
matlab -nodisplay -nodesktop -nosplash -logfile job.${SLURM_JOB_ID}.txt <<EOF
cl = parcluster('local')
tmpdirforpool = tempname
mkdir(tmpdirforpool)
cl.JobStorageLocation = tmpdirforpool
cl.NumWorkers = str2num(getenv('SLURM_NTASKS'))
parpool(cl,str2num(getenv('SLURM_NTASKS')))
cl; poolobj = gcp;
disp(['%-- parpool working with (' num2str(poolobj.NumWorkers) ') workers --%']);
disp(['%-- parpool working here (' poolobj.Cluster.JobStorageLocation ') --%']);
% now run the 'dot-m-file.m' script
dot-m-file
delete(gcp)
quit();
EOF
### note that the file 'dot-m-file.m' called without '.m' above
MATLAB and tmp
Many hosts only have small SSDs as a harddisk. So if you need more space, if big ( in the size of GBytes ) temporary files are produced, you may need to change the default "tmp" directory of MATLAB. One suitable space would be e.g. the personal
$HPCWORK
. Lustre is a fairly fast filesystem, optimized for large files.In the jobscript itself, one can then set another TMP for MATLAB, e.g. this way:
OLDTMP=$TMP TMP=$HPCWORK/.tmp/matlab_${SLURM_JOB_NAME}.${SLURM_JOB_ID} mkdir -p $TMP export TMP # ... # matlab part of the script # ... rm -rf $TMP TMP=$OLDTMP
MATLAB on Frontends
By default, MATLAB makes use of the multithreading capabilities of the computer on which it is running. In this case MATLAB tries to utilize all available CPUs on the current node. In shared environments like frontends this behavior may lead to major oversubscription of nodes. Frontend processes with excessive resource consumption may be ended (killed) by the administrators. To avoid overloading on frontends / interactive usage: always start MATLAB with the
-singleCompThread
flag