SBatchJob
Provides a simple interface to formatting SLURM
files so that they can be submitted to sbatch.
The hope is that this can be subclassed codify
options for different HPC paritions and whatnot.
slurm_keys: list
default_opts: dict
sbatch_opt_template: str
sbatch_template: str
sbatch_enter_command: str
sbatch_exit_command: str
__init__(self, description=None, job_name=None, account=None, partition=None, mem=None, nodes=None, ntasks_per_node=None, chdir=None, output=None, steps=(), precall=None, environment=None, **opts):
LLM Docstring
Build a SLURM batch job from the common #SBATCH options plus any additional
ones, the job steps, and optional environment/precall hooks.
description:str | Nonea human-readable description echoed into the script
job_name:str | Nonethe SLURM job name
account:str | Nonethe SLURM account
partition:str | Nonethe SLURM partition
mem:Anythe memory request
nodes:Anythe node count
ntasks_per_node:Anytasks per node
chdir:str | Nonethe working directory
output:str | Nonethe output-file pattern
steps:tuple | strthe job steps (shell commands)
precall:Callable | Nonea callable run before writing the file
environment:dict | Noneenvironment variables to export
opts:Anyadditional
#SBATCHoptions
clean_opts(self, opts):
Makes sure opt names are clean. Does no validation of the values sent in.
opts:Any:returns:_
format_opt_block(self):
Formats block of options
:returns:_
format(self):
Formats an SBATCH file from the held options
call_steps:Any:returns:_
write(self, file, output_dir=None, mode='w+', **kwargs):
LLM Docstring
Write the formatted SLURM script to a file, optionally within a working
directory (into which any precall hook is run).
file:str | IOan open stream or a file path
output_dir:str | Nonedirectory to write into (split from
fileif omitted)mode:strthe file mode when a path is given
kwargs:Anyextra arguments for
open:returns:str | IOthe path (or stream) written
run(self, file=None, output_dir=None, sbatch_function='sbatch', delete=True, text=True, capture_output=True, *args, **kwargs):
LLM Docstring
Write the SLURM script and submit it with sbatch (via subprocess.run),
optionally deleting the script afterward.
file:str | Nonethe script file name (a temporary name is generated if omitted)
output_dir:str | Nonedirectory to write the script into
sbatch_function:strthe submission command
delete:boolremove the script file after submission
text:boolrun the subprocess in text mode
capture_output:boolcapture the subprocess output
args:Anyextra positional arguments passed to
sbatchkwargs:Anyextra flags passed to
sbatch:returns:subprocess.CompletedProcessthe completed-process result