Skip to content
Projects

slurm_script_generator.runs

Run directories submitted as job arrays.

A parameter scan or a campaign is a set of directories, one per run, that all ask for the same resources and run the same commands. :class:RunSet submits such a set as one SLURM job array, records in each directory which task of which job it was given to, and reports where every run stands: finished, running, pending, failed, or not submitted yet. Submitting again only sends what is neither finished nor in the queue, so a failed or timed-out run goes back with the same call that started the set.

Attributes

NameDescription
ACTIVE_TASK_STATESNo description.
MARKERNo description.

Classes

NameDescription
RunOne run directory of a :class:RunSet and where it stands.
RunSetRun directories with one resource request, submitted as a job array.

Functions

NameDescription
array_specWrite task IDs as an --array specification.

ACTIVE_TASK_STATESattributemodule attribute#

ACTIVE_TASK_STATES = {
  'CONFIGURING',
  'COMPLETING',
  'PENDING',
  'REQUEUED',
  'REQUEUE_FED',
  'REQUEUE_HOLD',
  'RESIZING',
  'RESV_DEL_HOLD',
  'RUNNING',
  'SIGNALING',
  'STAGE_OUT',
  'STOPPED',
  'SUSPENDED'
}

MARKERattributemodule attribute#

MARKER = 'slurm_run.json'

Runclassdataclass#

class Run(directory: Path, task: int, state: str = 'new', job_id: Optional[int] = None, job_task: Optional[int] = None)

One run directory of a :class:RunSet and where it stands.

Attributes

directory : pathlib.Path The run directory. task : int Its position in the set, counted from 1: the array task ID it is submitted as. state : str 'finished', 'new' (never submitted), a SLURM state in capitals ('PENDING', 'RUNNING', 'FAILED', 'TIMEOUT', …), 'ended' (it has left the queue and accounting does not say how), or 'unknown' (submitted, and neither sacct nor squeue is available to ask). job_id : int, optional The array job it was last submitted with. job_task : int, optional The task ID it had in that job.

directoryattributeinstance attribute#

directory: Path

job_idattributeclass attributeinstance attribute#

job_id: Optional[int] = None

job_taskattributeclass attributeinstance attribute#

job_task: Optional[int] = None

stateattributeclass attributeinstance attribute#

state: str = 'new'

taskattributeinstance attribute#

task: int

is_activeproperty#

is_active: bool

True if the run is pending or running in the queue.

is_finishedproperty#

is_finished: bool

True if the run is done.

needs_submitproperty#

needs_submit: bool

True if the run is neither finished nor in the queue.

A run in the state 'unknown' does not count: it may be in the queue, and submitting it again could run it twice.

RunSetclass#

class RunSet(directories: Iterable[Union[str, Path]], script: SlurmScript, name: str = 'runs', root: Optional[Union[str, Path]] = None, is_finished: Optional[Callable[[Path], bool]] = None, log_to_run_directory: bool = True)

Run directories with one resource request, submitted as a job array.

Parameters

directories : iterable of str or pathlib.Path The run directories. Their order fixes the array task ID of each (counted from 1), so keep it the same between calls. script : SlurmScript The resource request, the modules and the commands of one run. The commands are executed inside the run directory. It must not set --array. name : str Name of the set: the job name unless the script has one, and the stem of the array script and of the output files of SLURM. root : str or pathlib.Path, optional Where the array script and the output files of SLURM go. Defaults to the parent of the first run directory. is_finished : callable, optional is_finished(directory) -> bool: whether a run is done, judged from what it wrote. Without it a run is finished when SLURM reports its task as COMPLETED, which needs accounting. log_to_run_directory : bool Send the output of the commands to slurm-<job>_<task>.out in the run directory (the default) instead of the output file of SLURM under root.

Examples

script = SlurmScript( … nodes=1, ntasks_per_node=32, time=“12:00:00”, … custom_commands=[“srun ./solver -i input.nml > solver.out”], … ) runs = RunSet(sorted(Path(“scan”).glob(“case_*”)), script, name=“scan”) runs.submit(throttle=8) # doctest: +SKIP 4242 print(runs) # doctest: +SKIP

directoriesattributeinstance attribute#

directories = [Path(d).resolve() for d in directories]

is_finishedattributeinstance attribute#

is_finished = is_finished

log_to_run_directoryattributeinstance attribute#

log_to_run_directory = log_to_run_directory

nameattributeinstance attribute#

name = name

rootattributeinstance attribute#

root = Path(root).resolve() if root else self.directories[0].parent

scriptattributeinstance attribute#

script = script

array_scriptmethod#

def array_script(tasks: Optional[Iterable[int]] = None, throttle: Optional[int] = None) -> SlurmScript

Return the script that runs the given tasks as a job array.

It is the script of the set with --array added, a job name and an output file unless the script has them, and in front of the commands the list of all run directories and a cd into the one of the task. The list is part of the script, which SLURM copies when the job is submitted, so a task that starts later still finds its directory even if the set has changed in the meantime.

Parameters

tasks : iterable of int, optional Task IDs to run. Defaults to all. throttle : int, optional Maximum number of tasks running at once.

Returns

SlurmScript The array script.

cancelmethod#

def cancel(verbose: bool = False) -> List[str]

Cancel the runs of the set that are pending or running.

Parameters

verbose : bool Print the scancel command.

Returns

list of str The tasks that were cancelled, as <job>_<task>.

Raises

RuntimeError If scancel fails.

countsmethod#

def counts() -> Dict[str, int]

Return how many runs are in each state.

Returns

dict of str -> int For example {'finished': 12, 'RUNNING': 8, 'PENDING': 10}.

statusmethod#

def status() -> List[Run]

Return every run of the set with its current state.

One sacct and one squeue call are made for all the jobs the runs were submitted with (see :func:~slurm_script_generator.squeue.task_states).

Returns

list of Run In the order of the directories.

submitmethod#

def submit(throttle: Optional[int] = None, retry: bool = True, force: bool = False, verbose: bool = False) -> Optional[int]

Submit, as one job array, the runs that need it.

These are the runs that were never submitted and, with retry, those that left the queue without finishing (failed, timed out, cancelled). Runs that are finished, pending or running are left alone, so calling this twice submits nothing the second time. The array script is written to <root>/<name>.sh, and each submitted run directory gets a file slurm_run.json with the job and task ID.

Parameters

throttle : int, optional Maximum number of tasks running at once. retry : bool Also submit runs that ended without finishing. Defaults to True. force : bool Submit every run that is not pending or running, finished ones and those in an unknown state included. verbose : bool Print what sbatch answers.

Returns

int or None The ID of the array job, or None if there was nothing to submit.

Raises

ValueError If the script holds a combination of pragmas sbatch rejects. RuntimeError If sbatch fails.

array_specfunction#

def array_spec(tasks: Iterable[int], throttle: Optional[int] = None) -> str

Write task IDs as an --array specification.

Consecutive IDs become ranges, and throttle limits how many tasks run at the same time.

Parameters

tasks : iterable of int The task IDs. throttle : int, optional Maximum number of tasks running at once (the %N suffix).

Returns

str For example '1-3,7,9-10%4'.

Raises

ValueError If there are no tasks, or the throttle is not positive.

Examples

array_spec([1, 2, 3, 7, 9, 10], throttle=4) ‘1-3,7,9-10%4’