HPC/Submitting and Managing Jobs/Example Job Script

From CNM Wiki
Jump to: navigation, search
Processing flow of a typical job.


A Torque job script is usually a shell script that begins with PBS directives. Directives are comment lines to the shell but are interpreted by Torque, and take the form

#PBS qsub_options

The job script is read by the qsub job submission program, which interpretes the directives and accordingly places the job in the queue. Review the qsub man page to learn about the options accepted and also the environment variables provided to the job script later at execution.

Place directives only at the beginning of the job script. Torque ignores directives after the first executable statement in the script. Empty lines are allowed, but not recommended. Best practice is to have a single block of directives at the beginning of the file.
Advanced usage
  • The job script may be written in any scripting language, such as Perl or Python. The interpreter is specified in the first script line in Unix hash-bang syntax #!/usr/bin/perl, or using the qsub -S path_list option.
  • The default directive token #PBS can be changed or unset entirely with the qsub -C option; see qsub, sec. Extended Description.

Application-specific job scripts

For most scalar and MPI-based parallel jobs on Carbon the scripts in the next section will be appropriate. Some applications however require customizations, typically copying $TMPDIR to an app-specific variable, or calling MPI through app-specifc wrapper scripts. Such application-specific custom scripts are located at the root directory of the application under /opt/soft, and can typically be reached as:




where APPNAME is the module name all UPPERCASED and with "-" (minus) characters replaced by "_" (underscore).

To find variables of this form:

env | grep _HOME

Generic job scripts

Here are a few example jobs for the most common tasks. Note how the PBS directives are mostly independent from the type of job, except for the node specification.

OpenMPI, InfiniBand

This is the default user environment. The openmpi and icc, ifort, mkl modules are preloaded in the system's shell startup files. The InfiniBand fast interconnect is selected in the openmpi module by means of the environment variable $OMPI_MCA_btl.

#  Basics: Number of nodes, processors per node (ppn), and walltime (hhh:mm:ss)
#PBS -l nodes=5:ppn=8
#PBS -l walltime=0:10:00
#PBS -N job_name
#PBS -A account
#  File names for stdout and stderr.  If not set here, the defaults
#PBS -o job.out
#PBS -e job.err
#  Send mail at begin, end, abort, or never (b, e, a, n). Default is "a".
#PBS -m ea

# change into the directory where qsub will be executed

# start MPI job over default interconnect; count allocated cores on the fly.
mpirun -machinefile  $PBS_NODEFILE -np $PBS_NP \
Updated 2012-07: Beginning with Torque-3.0, $PBS_NP can be used instead of the former construct $(wc -l < $PBS_NODEFILE).
  • If your program reads from files or takes options and/or arguments, use and adjust one of the following forms:
mpirun -machinefile  $PBS_NODEFILE -np $PBS_NP \
       programname  < run.in
mpirun -machinefile  $PBS_NODEFILE -np $PBS_NP \
       programname  -options arguments < run.in
mpirun -machinefile  $PBS_NODEFILE -np $PBS_NP \
       programname < run.in > run.out 2> run.err
In the last form, anything after programname is optional. If you use specific redirections for stdout or stderr as shown (>, 2>), the job-global files job.out, job.err declared earlier will remain empty or only contain output from your shell startup files (which should really be silent), and the rest of your job script.

OpenMPI, Ethernet

To select ethernet transport, such as for embarrasingly parallel jobs, specify an -mca option:

mpirun -machinefile $PBS_NODEFILE -np $PBS_NP \
	-mca btl self,tcp \


Under Intel-MPI the job script will be:

#PBS ... (same as above)


mpiexec.hydra 	-machinefile  $PBS_NODEFILE -np $PBS_NP \

Advanced Job scripts

Multithreading using OpenMP

See HPC/Submitting and Managing Jobs/Advanced node selection#Multithreading using OpenMP.

GPU nodes

As of late 2012, Carbon has 28 nodes with 1 NVIDIA C2075 GPU each. To specifically request GPU nodes, add :gpus=1 to the nodes request. At the moment this is synonymous with but preferable to specifying …:gen3.

#PBS -l nodes=N:ppn=PPN:gpus=1
  • The gpus=… modifier refers to GPUs per node and currently must always be 1.
  • GPU support depends on the application, in particular if several MPI processes or OpenMP threads can share a GPU. Test and optimize the N and PPN parameters for your situation. Start with nodes=1:ppn=4.
  • Each GPU node has 12 regular CPU cores; if you submit jobs with :ppn < 12 and :gpus=1 the node may be shared with pure CPU jobs. It is to be tested if and how much interference this causes for either job. See Advanced node selection to reserve entire nodes while controlling ppn for MPI or OpenMP.

Job scripts in other scripting languages

The job scripts shown above are written in bash, a Unix command shell language. Actually, qsub accepts scripts in pretty much any scripting language. The only pieces that Torque reads are the Torque directives, up to the first script command, i.e., a line which is (a) not empty, (b) not a PBS directive, and (c) not a "#"-style comment. The script interpreter is chosen by the Linux kernel from the first line of the script.

As with any job, the script execution during the job begins in the home directory, so one of the first commands in the script should be the equivalent of cd $PBS_O_WORKDIR. If you wish to use test your script interactively, you must avoid accessing the PBS_O_WORKDIR variable. (This problem - and a solution - exists for bash-based job scripts as well but usually having to use mpirun and to name the machinefile makes the expected environment more obvious.)

Then, a job script in Python would typically begin like this:

#PBS -l nodes=N:ppn=PPN
#PBS -l walltime=hh:mm:ss
#PBS -N job_name
#PBS -A account
#PBS -o filename
#PBS -j oe
#PBS -m ea    

import os
if os.environ.has_key('PBS_O_WORKDIR'):

# body of script here …

Important: The job script only ever runs in serial, on one core. Any parallelization must be initiated by leveraging MPI or OpenMP explicitly (e.g. http://mpi4py.scipy.org) or implicitly by calling a child application that runs in parallel (e.g. VASP). Such a child script or application either needs to be aware of or be told to use $PBS_NODEFILE.

This is not to be confused with using a "regular" bash script and mpirun to start several custom python interpreters in parallel, which is how GPAW and ATK operate:

mpirun -machinefile  $PBS_NODEFILE -np $PBS_NP \
	gpaw-python script.py
mpiexec.hydra -machinefile  $PBS_NODEFILE -np $PBS_NP \
	atkpython script.py

The account parameter

The parameter for option -A account can take the following forms:

for most jobs, containing your 5-digit proposal number.
(the actual string "user", not your user name) for a limited personal startup allocation.
for discretionary access by CNM staff.

You can check your account balance in hours as follows:

mybalance -h
gbalance -u $USER -h
The relevant column is Available, accounting for amounts reserved by current jobs and credits.

Timed Allocations

See HPC/FAQ#My account balance suddenly dropped.

Advanced node selection

You can refine the node selection (normally done via the PBS resource -l nodes=…) to finely control your node and core allocations. You may need to do so for the following reasons:

  • select specific node hardware generations,
  • permit shared vs. exclusive node access,
  • vary PPN across the nodes of a job,
  • accommodate multithreading (OpenMP).

See HPC/Submitting Jobs/Advanced node selection for these topics.