Skip to content

Submitting MATLAB Jobs With Torque

This script is an example of launching individual MATLAB jobs on the Kennesaw State HPC Cluster. It includes code to help you safely run Parallel Computing Toolbox (PCT) jobs from within MATLAB, although you will have to adjust your MATLAB code to take advantage of this feature. Failure to do this can cause unpredictable results for your jobs and the compute nodes.

Torque Shell Script

In our example, we saved the following Torque script as run_matlab.pbs (making sure to change netid@kennesaw.edu in the script to your correct email address):

run_matlab.pbs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#!/bin/bash
#PBS -l nodes=1:ppn=1
#PBS -l walltime=10:00:00
#PBS -m abe
#PBS -M netid@kennesaw.edu (1)

JOBID=`echo $PBS_JOBID | cut -f1 -d.`

module load MATLAB

# Create a temp workdir under scratch
mkdir -p ${HOME}/scratch/matlab
export PBS_MATLAB_WORKDIR=$( mktemp -d -p ${HOME}/scratch/matlab workdir_XXXXXXXXXX )

matlab -nodisplay -nosplash -logfile ${FILE}.log -r "run ${FILE}"

# Delete temp workdir
rm -rf ${PBS_MATLAB_WORKDIR}
  1. 🙋‍♂️ Make sure to change netid@kennesaw.edu on this line to your KSU email address or you won't receive emails when the job starts and stops.

MATLAB Code Changes

If you're using the PCT, you need to set the JobStorageLocation to the contents of the environment variable PBS_MATLAB_WORKDIR. It will also set the PoolSize of the created cluster to the total number of requested processors in your job to make maintaining your script easier.

pc = parcluster('local');
pc.JobStorageLocation = getenv('PBS_MATLAB_WORKDIR');
pbsNP = str2num(getenv('PBS_NP'));
parpool(pc,pbsNP);

Usage

Assuming you saved the Torque script from above as run_matlab.pbs, and the name of your Rmpi script is matlab_script.m, you can submit your Rmpi job with the following command:

[barney@hpc ~]$ qsub run_matlab.pbs -vFILE=${PWD}/matlab_script.m