Skip to content
Projects

slurm_script_generator.salloc

Turn a batch script into an interactive allocation.

slurm-alloc job.sh reads the #SBATCH pragmas out of a script and starts salloc with the same resource request, so the job can be debugged interactively on the nodes it would have run on. With --run the body of the script is executed inside the allocation instead of dropping into a shell.

Attributes

NameDescription
SALLOC_OPTIONSNo description.

Functions

NameDescription
build_commandBuild the salloc command for a batch script.
mainEntry point for the slurm-alloc command-line tool.
pragma_to_salloc_argRender a pragma as a single salloc command-line argument.
salloc_argsSplit a script's pragmas into salloc arguments and dropped options.
script_bodyExtract the commands of a batch script, dropping shebang and pragmas.

SALLOC_OPTIONSattributemodule attribute#

SALLOC_OPTIONS = frozenset(
  {
    '--account',
    '--acctg-freq',
    '--bb',
    '--bbf',
    '--begin',
    '--bell',
    '--chdir',
    '--cluster-constraint',
    '--clusters',
    '--comment',
    '--consolidate-segments',
    '--constraint',
    '--container',
    '--container-id',
    '--container-type',
    '--contiguous',
    '--core-spec',
    '--cores-per-socket',
    '--cpu-freq',
    '--cpus-per-gpu',
    '--cpus-per-task',
    '--deadline',
    '--delay-boot',
    '--dependency',
    '--distribution',
    '--exclude',
    '--exclusive',
    '--extra',
    '--extra-node-info',
    '--gpu-bind',
    '--gpu-freq',
    '--gpus',
    '--gpus-per-node',
    '--gpus-per-socket',
    '--gpus-per-task',
    '--gres',
    '--gres-flags',
    '--hint',
    '--hold',
    '--immediate',
    '--job-name',
    '--kill-command',
    '--licenses',
    '--mail-type',
    '--mail-user',
    '--mcs-label',
    '--mem',
    '--mem-bind',
    '--mem-per-cpu',
    '--mem-per-gpu',
    '--mem-update',
    '--mincpus',
    '--network',
    '--nice',
    '--no-bell',
    '--no-kill',
    '--no-shell',
    '--nodefile',
    '--nodelist',
    '--nodes',
    '--ntasks',
    '--ntasks-per-core',
    '--ntasks-per-gpu',
    '--ntasks-per-node',
    '--ntasks-per-socket',
    '--oom-kill-step',
    '--overcommit',
    '--oversubscribe',
    '--partition',
    '--prefer',
    '--priority',
    '--profile',
    '--qos',
    '--quiet',
    '--reboot',
    '--reservation',
    '--resources',
    '--resv-ports',
    '--segment',
    '--signal',
    '--sockets-per-node',
    '--spread-job',
    '--spread-segments',
    '--stepmgr',
    '--switches',
    '--thread-spec',
    '--threads-per-core',
    '--time',
    '--time-min',
    '--tmp',
    '--tres-bind',
    '--tres-per-task',
    '--use-min-nodes',
    '--verbose',
    '--wait-all-nodes',
    '--wckey',
    '--x11'
  }
)

build_commandfunction#

def build_command(path: str, run: bool = False, extra_args: List[str] | None = None) -> Tuple[List[str], str | None]

Build the salloc command for a batch script.

Parameters

NameTypeDefaultDescription
pathstrrequiredPath to the batch script.
runboolFalseWhether to execute the script’s commands in the allocation instead of starting an interactive shell.
extra_argsList[str] | NoneNoneAdditional arguments to append to the salloc invocation.

Returns

Tuple[List[str], str | None]
A tuple of (command as an argument list, the body to run or None).

mainfunction#

def main(argv: List[str] | None = None) -> int

Entry point for the slurm-alloc command-line tool.

Parameters

NameTypeDefaultDescription
argvList[str] | NoneNoneArguments to parse, defaulting to the process arguments.

Returns

int
The exit code of salloc.

pragma_to_salloc_argfunction#

def pragma_to_salloc_arg(pragma: Pragma) -> str

Render a pragma as a single salloc command-line argument.

Parameters

NameTypeDescription
pragmaPragmaThe pragma to render.

Returns

str
Either "--flag" for a valueless switch or "--flag=value".

salloc_argsfunction#

def salloc_args(script: SlurmScript) -> Tuple[List[str], List[str]]

Split a script’s pragmas into salloc arguments and dropped options.

Parameters

NameTypeDescription
scriptSlurmScriptThe parsed batch script.

Returns

Tuple[List[str], List[str]]
A tuple of (arguments to pass to salloc, flags salloc does not accept).

script_bodyfunction#

def script_body(text: str) -> str

Extract the commands of a batch script, dropping shebang and pragmas.

The body is taken verbatim rather than rebuilt from the parsed script, so that comments, quoting and here-documents survive untouched.

Parameters

NameTypeDescription
textstrThe full text of the batch script.

Returns

str
The remaining lines, with leading and trailing blank lines removed.