Matlab
Installed versions
Resource | Version |
---|---|
Dardel/cpe23.12 | r2024a, r2024b |
Dardel/cpe23.03 | r2024a |
General information
General usage
MATLAB is a proprietary multi-paradigm programming language and numeric computing environment developed by MathWorks. MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages. In order to use Matlab you need to have a Matlab license. KTH has a site-wide license for Matlab which includes all Matlab toolboxes.
External links
How to use
Getting Started with serial and parallel MATLAB
Load the matlab module by
ml add PDC
ml matlab/r2024b
Running interactively
MATLAB can be run interactively on an allocated node. To book a single node for one hour, type
salloc -N 1 -t 1:00:00 -A pdc.staff -p main
# wait for a node to be reserved
salloc: Granted job allocation 591571
salloc: Waiting for resource configuration
salloc: Nodes nid001015 are ready for job
ssh -X nid001015
ml add PDC
ml matlab/r2024b
matlab
salloc -c 24 -t 1:00:00 -A pdc.staff -p shared
>> p=parpool(24)
Starting parallel pool (parpool) using the 'local' profile ... connected to 24 workers.
p =
Pool with properties:
Connected: true
NumWorkers: 24
Cluster: local
AttachedFiles: {}
IdleTimeout: 30 minute(s) (30 minutes remaining)
SpmdEnabled: true
>> parallel_example
t =
2.4711
ans =
2.4711
function t = parallel_example
t0 = tic;
parfor idx = 1:24
A(idx) = idx;
pause(2)
end
t = toc(t0)
Running parallel batch jobs
You can also submit parallel workflows to the SLURM queueing system. The following job script allocates 16 cores on Dardel and runs one MATLAB program.
#!/bin/bash
#SBATCH -A naissYYYY-X-XX
#SBATCH -J myjob
#SBATCH -p shared
#SBATCH -c 16
#SBATCH -t 10:00:00
# Load the MATLAB module
ml add PDC/23.12
ml matlab/r2024b
# Launch the MATLAB calculation
matlab -nosplash -nodesktop -nodisplay < your_matlab_program.m > your_matlab_program.out
run_matlab.sh
.
You can then submit the job with
sbatch run_matlab.sh
Running multiple serial batch jobs
If you have several serial MATLAB jobs that can be run simultaneously (for example, one MATLAB program with different input parameters), these can be run simultaneously on a booked node (in the main partition) or on booked cores (in the shared partition). A simple bash script for submitting multiple MATLAB jobs on 24 cores in the shared partition of Dardel node can look like
#!/bin/bash
#SBATCH -A naissYYYY-X-XX
#SBATCH -J myjob
#SBATCH -p shared
#SBATCH -c 24
#SBATCH -t 10:00:00
# Load the Matlab module
ml add PDC/23.12
ml matlab/r2024b
# Run matlab for 24 individual programs serial_program_1.m,
# serial_program_2.m ... and print output in files logfile_1, logfile_2, ...
# The input files must be in the directory where you submit this script.
# This is also where the output will be created.
for i in {1..24} ; do
matlab -nosplash -nodesktop -nodisplay < serial_program_${i}.m > logfile_$i &
done
# This wait command must be present since otherwise the job will terminate immediately
wait
run_matlab.sh
.
You can then submit the job with
sbatch run_matlab.sh