MATLAB Sample Scripts
On the CQ University HPC systems, a PBS (HPC Schedule) resource “MATLAB” has been implemented, which checks to see if a MATLAB license is available. If the MATLAB resource is available, as well as the other resources (CPU, Memory, etc), it will start the job, otherwise it will queue the job/s until the required resources become available. The purpose of using this resource allows to submit as many MATLAB jobs to the HPC system, without having to worry if a License is available or not.
A sample MATLAB PBS Script can be found on the HPC system at /apps/samples/PBS/matlab.pbs
which can be copied your home directory for use. The details for this script can be found below:
Example Matlab PBS Submission Script (/apps/samples/PBS/matlab.pbs)
###### Select resources #####
#PBS -N <jobname>
#PBS -l ncpus=<requested cpu's>
#PBS -l mem=<requested memory>
#PBS -l MATLAB=1
##### Queue #####
#PBS -q workq
##### Mail Options #####
#PBS -m abe
#PBS -M <your email address>
########## Output File ##########
#PBS -o \$PBS_O_WORKDIR/<output-filename>
########## Error File ##########
#PBS -e \$PBS_O_WORKDIR/<error-filename>
##### Change to current working directory #####
cd \$PBS_O_WORKDIR
##### Execute Program #####
matlab -nodisplay -nodesktop -nosplash < <matlab-program.m>
Additionally, a very basic “hello world” matlab program which passes a program id number and execute 10 programs concurrently.
These programs can be copied from /apps/samples/matlab to your home directory to try out and use as a foundation for your programs.
These scripts and programs are as follows:
Matlab Sample Program (/apps/samples/matlab/hello.m)
function hello(i)
message = ['Hello world from program ' num2str(i) ]
disp(message);
quit
and
Sample Matlab multiple Submission Script (/apps/samples/matlab/matlab-test.sh)
#!/bin/bash
for ((i=1; i<=10; i+=1))
do
jobname=Matlab_$i
cat << EOF | qsub
###### Select resources #####
#PBS -N $jobname
#PBS -l ncpus=1
#PBS -l mem=1g
#PBS -l MATLAB=1
##### Queue #####
#PBS -q workq
########## Output File ##########
#PBS -o \$PBS_O_WORKDIR/$jobname.txt
########## Error File ##########
#PBS -e \$PBS_O_WORKDIR/$jobname.err
##### Change to current working directory #####
cd \$PBS_O_WORKDIR
##### Execute Program #####
. /etc/profile.d/modules.sh
module unload matlab
module load matlab-r2017b
matlab -nodisplay -nodesktop -nosplash -r 'hello($i)'
EOF
done
You will noticed that the script above uses different “job names”, different error and output file names for each program. Additionally, we removed the email notifications, otherwise we would get 10 emails every time the jobs start and finish.
It is hoped that these sample scripts and programs will assist you to develop better MATLAB programs and assist to increase your research output, by being able to submit many jobs in parallel.