Approvals: 0/1
This is an old revision of the document!
Matlab 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
There are two ways to run this from the command line option 1:
/opt/sw/matlab-80/bin/matlab -nodisplay < test.m
option 2:
/opt/sw/matlab-80/bin/matlab -nodisplay -r "add(2,3.4)"
Batch Matlab job on VSC-2
Example: Serial Matlab Task
Create a new directory
[username@l01 ~]$ mkdir TestMatlab [username@l01 ~]$ cd TestMatlab
Create an example file called myplot.m
or download it from here and put it in this directory:
[username@l01 TestMatlab]$ cat myplot.m h = figure; t = 0:pi/20:2*pi; plot('v6',t,sin(t).*2) saveas(h,'myFigFile','jpg')
Then create a new file called matlab.sh
as your job script, with the following basic contents:
#$ -N matlab #$ -l h_rt=00:10:00 /opt/sw/matlab-80/bin/matlab < myplot.m
The contents of your directory should now be:
[username@l01 TestMatlab]$ ls matlab.sh myplot.m
Submit your job:
[username@l01 TestMatlab]$ qsub matlab.sh
When the job has finished you will see a “myFigFile.jpg” image file created.
Example: Local Matlabpool
For using all cores on a machine effetively, a so-called matlabpool can be started:
#$ -N matlab #$ -pe mpich 8 #$ -V #$ -cwd #$ -l h_rt=00:10:00 /opt/matlab-tu/bin/matlab < 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
Currently not available on VSC: Matlab Parallel Jobs using Matlab distributed computed environment (MDCE)
In November 2012 a test installation of the MDCE enviroment was available for one month. For running Matlab in parallel it has to connect to so called matlab workers. The job script below starts the MDCE processes, sets up a MJS environment and connects a number of worker to it. Start up of the workers takes some minutes.
Currenlty this software is not installed. From VSC Administration point of view most task can be handled with the already existing campus workstation licences. If you are in need of this feature please contact system administration.
The job script and a sample parallel matlab job can be downloaded here: matlab_mjs.tar.gz
Job script:
#$ -N matlab_test #$ -pe mpich 32 #$ -V #$ -cwd MATLAB_SCRIPT=calc_mjs.m MATLAB=/opt/sw/matlab-80/bin/matlab MDCE=/opt/sw/matlab-dcs_R2012b/toolbox/distcomp/bin/mdce DIST_DIR=/opt/sw/matlab-80/toolbox/distcomp/bin NSLOTS_PER_NODE_AVAILABLE=8 NSLOTS_PER_NODE_USED=2 NSLOTS_REDUCED=`echo "$NSLOTS / $NSLOTS_PER_NODE_AVAILABLE * $NSLOTS_PER_NODE_USED" | bc ` echo "starting run with $NSLOTS_REDUCED processes; $NSLOTS_PER_NODE_USED per node" uniq $TMPDIR/machines > $TMPDIR/machines_uniq JOBMANAGER_HOST=`head -n 1 $TMPDIR/machines_uniq` JOBMANAGER=matlab_`whoami` #Kill all old matlab processes for node in `cat $TMPDIR/machines_uniq` do ssh $node 'for i in `ps axu |grep matlab| tr -s " "|grep -v grep | cut -d " " -f 2`;do echo $i; kill -9 $i; done; rm -rf /tmp/matlab ;' done $MDCE start -clean ${DIST_DIR}/startjobmanager -name $JOBMANAGER -remotehost $JOBMANAGER_HOST -v ${DIST_DIR}/nodestatus -remotehost $JOBMANAGER_HOST #pdsh is too fast for the license server ... for worker in `cat $TMPDIR/machines_uniq` do echo starting workers on host $worker ssh $worker $MDCE start -clean for i in `seq 1 $NSLOTS_PER_NODE_USED` do ${DIST_DIR}/startworker -remotehost $worker -name ${worker}_worker$i -jobmanager $JOBMANAGER -jobmanagerhost $JOBMANAGER_HOST done done #What we actually want to do: $MATLAB -nodisplay < $MATLAB_SCRIPT #Clean up: ${DIST_DIR}/stopjobmanager -name $JOBMANAGER -remotehost $JOBMANAGER_HOST -v for worker in `cat $TMPDIR/machines_uniq` do echo stopping worker on $worker for i in `seq 1 $NSLOTS_PER_NODE_USED` do ${DIST_DIR}/stopworker -remotehost $worker -name ${worker}_worker$i done ssh $worker $MDCE stop -clean done echo done
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/v8.5_R2015a # load selected version [username@l32]$ module list # check loaded modules Currently Loaded Modulefiles: 1) Matlab/v8.5_R2015a
(See also the introduction to the module command.)
Now, Matlab can be called by
[username@l32]$ matlab
Example: Serial Matlab Task
Analogously to VSC-2, we use the matlab m-file myplot.m and create a file called matlab.sh
as your job script, with the following basic contents:
#!/bin/bash # #SBATCH -J test #SBATCH -N 1 #SBATCH --ntasks-per-node=1 #SBATCH --ntasks-per-core=1 #SBATCH --threads-per-core=1 #SBATCH --time=10 # run time unit=minutes export OMP_NUM_THREADS=1 time matlab < myplot.m