User Tools

Site Tools


doku:matlab

Matlab Batch Jobs

Run Matlab Task from command line

Generate a matlab package called test.m,

add(2,3.4)

and a function add.m,

function z=add(x,y)
z=x+y

Run this from the command line:

/opt/sw/matlab-80/bin/matlab -nodisplay -r  "add(2,3.4)"

Batch Matlab job on VSC-3

In order to be able to use matlab, you have to load the program with the 'module load xyz' command

[username@l32]$ module avail  # select a matlab version
[username@l32]$ module load Matlab/v9.5_R2018b # load desired version
[username@l32]$ module list   # check loaded modules
Currently Loaded Modulefiles:
  1) Matlab/v9.5_R2018b    

(See also the introduction to the module command.)

Now, Matlab can be called by

[username@l32]$ matlab  

Example: Serial Matlab Task

We use the matlab m-file myplot.m and create a file called jobSerial.sh as your job script, with the following basic contents:

#!/bin/bash
#
#SBATCH -J test                     # job name
#SBATCH -N 1                        # number of nodes
#SBATCH --ntasks-per-node=1       
#SBATCH --ntasks-per-core=1
#SBATCH --threads-per-core=1
#SBATCH --time=10                   # run time unit=minutes
#SBATCH -L matlab@vsc

module purge
module load Matlab/v9.5_R2018b # load desired version

export OMP_NUM_THREADS=1

time matlab < myplot.m
[username@l32]$ sbatch jobSerial.sh   # submit your job
[username@l32]$ squeue -u username    # check state of your job

Example: Local Matlabpool

We use the m-file main.m and create the following shellscript jobPool.sh

jobPool.sh
#!/bin/bash
#
#SBATCH -J test                     # job name
#SBATCH -N 1                        # number of nodes
#SBATCH --ntasks-per-node=16        # here we use all 16 cores of the node
#SBATCH --ntasks-per-core=1
#SBATCH --threads-per-core=1
#SBATCH --time=100                   # run time unit=minutes
#SBATCH -L matlab@vsc

module purge
module load Matlab/v9.5_R2018b # load desired version

export OMP_NUM_THREADS=1

time matlab < main.m
main.m
matlabpool %open local 8	% local profile is default setting, at most 8 workers are allowed with the local scheduler

matlabpool size

n = 500; K = 50; L = 10; T = zeros(1,L);

% serial inner loop
for l = 1:L
tic
for k = 1:K
    eig(rand(n));
end
T(l) = toc;
end

disp(['serial: ',num2str(mean(T))])

% parallel inner loop
for l = 1:L
tic
parfor k = 1:K
    eig(rand(n));
end
T(l) = toc;
end

disp(['parallel: ',num2str(mean(T))])

matlabpool close
command line on the login node
[username@l32]$ sbatch jobPool.sh     # submit your job
[username@l32]$ squeue -u username    # check state of your job
LDAP: couldn't connect to LDAP server
doku/matlab.txt · Last modified: 2020/01/16 12:46 by ir