Search code examples
mpipbsqsubtorque

Determine total CPU count after qsub within PBS script


For a PBS script called with qsub, I want to know how many total CPU's have actually been allocated in case the number defined in the PBS file is overwritten by inputs from the command line. For example with the following pbs script file:

jobscript.pbs:

#!/bin/bash
#PBS -N test_run
#PBS -l nodes=32
#PBS -l walltime=06:00:00
#PBS -j oe
#PBS -q normal
#PBS -o output.txt

cd $PBS_O_WORKDIR

module load gcc-openmpi-1.2.7
time mpiexec visct

This script could be run with just 16 CPU's (instead of 32) using the following command line:

$ qsub -l nodes=2:ppn=8 jobscript.pbs

So I would like a robust method for determining how many CPU's are actually available from within the script.


Solution

  • I was able to answer my own question with the following solution using the $PBS_NODEFILE environment variable which contains the path to a file listing information about the available nodes:

    jobscript.pbs:

    #!/bin/bash
    #PBS -N test_run
    #PBS -l nodes=32
    #PBS -l walltime=06:00:00
    #PBS -j oe
    #PBS -q normal
    #PBS -o output.txt
    
    # This finds out the number of nodes we have
    NP=$(wc -l $PBS_NODEFILE | awk '{print $1}')
    echo "Total CPU count = $NP"
    

    Thanks to "Source" after much online searching.