# slurm_script_generator.runs

*module*

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.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L1-L1)

## slurm_script_generator.runs.ACTIVE_TASK_STATES

*attribute* · *module attribute*

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

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L29-L43)

## slurm_script_generator.runs.MARKER

*attribute* · *module attribute*

```python
MARKER = 'slurm_run.json'
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L25-L25)

## slurm_script_generator.runs.Run

*class* · *dataclass*

```python
class Run
```

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.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L92-L138)

### slurm_script_generator.runs.Run.directory

*attribute* · *instance attribute*

```python
directory: Path
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L115-L115)

### slurm_script_generator.runs.Run.job_id

*attribute* · *class attribute* · *instance attribute*

```python
job_id: Optional[int] = None
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L118-L118)

### slurm_script_generator.runs.Run.job_task

*attribute* · *class attribute* · *instance attribute*

```python
job_task: Optional[int] = None
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L119-L119)

### slurm_script_generator.runs.Run.state

*attribute* · *class attribute* · *instance attribute*

```python
state: str = 'new'
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L117-L117)

### slurm_script_generator.runs.Run.task

*attribute* · *instance attribute*

```python
task: int
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L116-L116)

### slurm_script_generator.runs.Run.is_active

*property*

```python
is_active: bool
```

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

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L127-L129)

### slurm_script_generator.runs.Run.is_finished

*property*

```python
is_finished: bool
```

True if the run is done.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L122-L124)

### slurm_script_generator.runs.Run.needs_submit

*property*

```python
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.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L132-L138)

## slurm_script_generator.runs.RunSet

*class*

```python
class RunSet
```

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

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L141-L433)

### slurm_script_generator.runs.RunSet.directories

*attribute* · *instance attribute*

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

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L189-L189)

### slurm_script_generator.runs.RunSet.is_finished

*attribute* · *instance attribute*

```python
is_finished = is_finished
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L199-L199)

### slurm_script_generator.runs.RunSet.log_to_run_directory

*attribute* · *instance attribute*

```python
log_to_run_directory = log_to_run_directory
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L200-L200)

### slurm_script_generator.runs.RunSet.name

*attribute* · *instance attribute*

```python
name = name
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L197-L197)

### slurm_script_generator.runs.RunSet.root

*attribute* · *instance attribute*

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

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L198-L198)

### slurm_script_generator.runs.RunSet.script

*attribute* · *instance attribute*

```python
script = script
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L196-L196)

### slurm_script_generator.runs.RunSet.array_script

*method*

```python
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.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L204-L254)

### slurm_script_generator.runs.RunSet.cancel

*method*

```python
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.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L383-L409)

### slurm_script_generator.runs.RunSet.counts

*method*

```python
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}``.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L308-L319)

### slurm_script_generator.runs.RunSet.status

*method*

```python
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.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L267-L306)

### slurm_script_generator.runs.RunSet.submit

*method*

```python
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.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L323-L381)

## slurm_script_generator.runs.array_spec

*function*

```python
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'

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/runs.py#L46-L89)
