How to Write a shell alias or function for quick login to a node managed by SLURM#

For tasks better run in a dedicated but interactive fashion, it might be advantageous to save the repeating pattern of reserving resources and starting a new associated shell in an alias or function, as explained below.

If you use bash as default shell you can place the following alias definition in your ~/.bashrc file and source this file in the ~/.bash_profile or in the ~/.profile file:

alias get-node='salloc --x11 --mem=10G -p interactive -A <prj-account>'

where <prj-account> needs to be replaces by a valid project account authorizing you to use Slurm.

If you use tcsh as default shell you can put the following line in your ~/.cshrc or ~/tcshrc file to define the alias get-node:

alias get-node  'salloc --x11 --mem=10G -p interactive -A <prj-account>'

Thereafter, the command get-node can be used to allocate computing resources and log into the allotted node in one step:

$ get-node
salloc: Pending job allocation 292893
salloc: job 292893 queued and waiting for resources
salloc: job 292893 has been allocated resources
salloc: Granted job allocation 292893
salloc: Waiting for resource configuration
salloc: Nodes l12345 are ready for job
userid@l12345:~>

An even more flexible solution is to define a shell function that accepts options to specify resources requirements likely to change (e.g. number of tasks, memory amount, partition, project account).

The example below defines a bash function get-node that can be added to your ~/.bashrc file:

get-node(){
    # set default values for number of tasks, partition, account, and time limit
    ntasks=1
    partition=interactive
    account=$SALLOC_ACCOUNT
    timelimit=08:00:00
    OPTIND=1
    # parse options with arguments
    while getopts :n:p:a: opt; do
        case ${opt} in
            n ) ntasks=$OPTARG
                ;;
            p ) partition=$OPTARG
                ;;
            a ) account=$OPTARG
                ;;
            t ) timelimit=$OPTARG
                ;;
           \? ) echo "Invalid option: $OPTARG" 1>&2
                return
                ;;
            : ) echo "Invalid option: $OPTARG requires an argument" 1>&2
                return
                ;;
        esac
    done
    # allocate ressources and log into the allotted node
    salloc --x11 -A ${account} -n ${ntasks} -p ${partition} -t ${timelimit}
}

and used to start interactive sessions according to different needs, for example:

# ask for 4 CPUs on one node from the interactive partition using account from SALLOC_ACCOUNT
$ get-node -n 4

# allocate ressources for 4 hours and use account xz0456 instead of the default account
$ get-node -a xz0456 -t 240

# use compute partition (for interactive debugging purposes)
$ get-node -p compute

Tcsh or csh do not allow for shell functions. This deficiency can be partly compensated for by defining an alias with positional parameters as shown in the example below:

alias get-node 'salloc --x11 -n \!:1 -p \!:2 -A \!:3'

However, for tcsh or csh, all parameters MUST be specified and the order of parameters (in the example above number of tasks, partition, account) DOES matter:

# ask for 4 CPUs on a node from the interactive partition using account xz0123
$ get-node 4 interactive xz0123

# use account xz0456 instead of default account
$ get-node 1 interactive xz0456

Another possibility (not shown here) is to write a Unix utility using a language of your choice (python, perl etc.), place it in your ~/bin directory and add this directory to your PATH.