MATLAB m-file submission to the SLURM queue

To launch Matlab job on ONE CPU of ONE compute node you can use the following slurm script:

#!/bin/bash
#SBATCH --time=8:00:00
#SBATCH --nodes=1
#SBATCH --partition=all
#SBATCH --ntasks-per-node=1
#SBATCH --cpus-per-task=8
#SBATCH --job-name="matlab_test"
#SBATCH --output=matlab_test.out
# we get current folder from which script is submitted:
WORKDIR=${SLURM_SUBMIT_DIR}
source ~/.bashrc


# Users can change below, e.g. the name of the m-file you want to run
mymfile=test.m
cd ${WORKDIR}
mfile=${WORKDIR}/$mymfile
srun matlab -nojvm -nodisplay -nosplash -nodesktop < $mfile

Save the above script in your working folder say as “matlab.slurm” and submit it as normal, say

sbatch matlab.slurm

If you specify more than one “task per node” e.g. “#SBATCH –ntasks-per-node=4” directive, you will get 4 matlab processes starting on the same node, each running the same “test.m”. See below ( in “Parallelisation Within One or Several Nodes”) how you can use this if you need to split one big problem into several smaller ones and run them in parallel.

Parallelisation Within One Node

Within one node you can use Matlab’s parallel processing toolbox capabilities, e.g. when doing for-loops, you can use “parfor” command in your m-files, preceded with

matlabpool 8
...
...
parfor i=1:0.001:1000
....
...
end
...
matlabpool close

which will start 8 Matlab “workers” on a target node (do not try to use more than 1 node with this approach, for that you need Matlab Cluster suite which we do not have a license for and the script will fail). By the end of the code you have to shut down all Matlab workers with “matlabpool close” (see above script).

Parallelisation Within One or Several Nodes

Another approach can be used if you want to start independent Matlab threads on different CPU’s and nodes (i.e. one compute node and more). In this case you can use either “srun” command of SLURM or MPI to launch several Matlab processes on every node you reserve:

#!/bin/bash

#SBATCH --time=12:00:00
#SBATCH --nodes=20
#SBATCH --exclusive
##for jobs longer than 24 hours use queue (partition) "long" otherwise use "all" for testing use "debug" queue:
#SBATCH --partition=all
 
#SBATCH --ntasks-per-node=8
#SBATCH --job-name="MPP_test"
#SBATCH --output=output.log


WORKDIR=${SLURM_SUBMIT_DIR}
cd ${WORKDIR}


#change the name of the m-file you want to run
mymfile=mymfile.m

cd ${WORKDIR}

srun matlab -nojvm -nodisplay -nosplash -nodesktop < $mfile ## OR: (alternative to the above command line "srun...") ## You can use mpirun to launch several processes on target nodes: (comment out the above "srun" command with "#" ## and uncomment following "#" lines, but not "##" lines) # MPIRUN=`which mpirun` # srun /bin/hostname > hostfile.txt
# NPROCS=$(($SLURM_JOB_NUM_NODES * $SLURM_NTASKS_PER_NODE))
## this is the actual commad line which will be launched:
#${MPIRUN} -np $NPROCS -hostfile ${WORKDIR}/hostfile.txt matlab -r "run $mymfile"

For each matlab instance (i.e. running on each CPU on every node) to know its identity, you can read the following environment variable in your Matlab m-file and assign it to e.g. “mythreadnr” variable:

mythreadnr=getenv('SLURM_PROCID');

OR (if you use mpirun to launch multiple instances of the matlab) you can use:

mythreadnr=getenv('OMPI_COMM_WORLD_RANK');

In this way you can parallelise your Matlab job for each launched Matlab instance to do a separate portion of your calculation (e.g. portion of a mesh or whatever) or saving results to a different filename etc..

E.g. in your m-file:

%% this is example portion of Matlab m-file which is launched as one of the several 
% instances with "srun matlab -nojvm -nodisplay -nosplash -nodesktop < mymfile.m"

mythreadnr=getenv('SLURM_PROCID');

%% we will read some e.g. "input values" for this particular "thread" of matlab job:
filename=['input', mythreadnr, '.txt'];
inputdata=load(filename,'-ascii');

....

%% by the end of our calculation we can save result in the separate file for this thread

outputfile=['result',threadnr,'.txt'];
save(outputfile, 'variablename1','variablename2','-ascii');

Note that just by requesting >1 tasks in SLURM will not result in your MATLAB script running on more than 1 CPUs or compute nodes.


Alex Pedcenko

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

css.php