Search code examples
bashunixhpcsungridengine

Accessing variables set in SGE job script header


Lets say I have a job script where I am requesting 4 cores and setting a memory limit in the header:

#! /bin/bash                                                                                                                                                               
#$ -pe mpi 4                                                                                                                                                              
#$ -l h_vmem=128G                                                                                                                                                         

echo "echo using 4 cores and 128 memory per core"

Is there anyway to access those values, something like $SGE_PE_MPI so that I could use the number of cores later in the script without having to hard code it multiple places?


Solution

  • The program could read the values from itself. Example code is:

    #! /bin/bash
    #$ -pe mpi 4
    #$ -l h_vmem=128G
    
    readonly sge_pe_mpi=$(sed -n 's/^#\$ -pe mpi \(.*\)/\1/p' -- "$0")
    readonly sge_l_h_vmem=$(sed -n 's/^#\$ -l h_vmem=\(.*\)/\1/p' -- "$0")
    
    printf 'using %s cores and %s memory per core\n' \
        "$sge_pe_mpi" "$sge_l_h_vmem"
    

    Example output:

    using 4 cores and 128G memory per core
    

    Note that the code above is just to illustrate the idea. It is very delicate. Any change to the format of the special comments (e.g. extra spaces) would break it, as would repeated comments.