File size: 7,388 Bytes
ee5b44c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from __future__ import annotations

import os
from collections.abc import Mapping
from dataclasses import dataclass


class SlurmEnvironmentError(ValueError):
    """Raised when SLURM topology variables are present but inconsistent."""


@dataclass(frozen=True)
class SlurmArrayContext:
    """A regular SLURM job-array allocation expressed as a zero-based rank."""

    task_id: int
    rank: int
    count: int
    minimum: int
    maximum: int
    step: int


@dataclass(frozen=True)
class DistributedContext:
    """Global process topology derived from SLURM or explicit overrides."""

    rank: int
    world_size: int
    task_rank: int
    task_count: int
    array: SlurmArrayContext | None
    source: str

    @property
    def distributed(self) -> bool:
        return self.world_size > 1


def _environment(environ: Mapping[str, str] | None) -> Mapping[str, str]:
    return os.environ if environ is None else environ


def _parse_int(
    environ: Mapping[str, str],
    name: str,
    *,
    minimum: int,
) -> int | None:
    raw = environ.get(name)
    if raw is None or not str(raw).strip():
        return None
    try:
        value = int(str(raw), 10)
    except ValueError as exc:
        raise SlurmEnvironmentError(f"{name} must be an integer, got {raw!r}") from exc
    if value < minimum:
        comparator = "non-negative" if minimum == 0 else f"at least {minimum}"
        raise SlurmEnvironmentError(f"{name} must be {comparator}, got {value}")
    return value


def slurm_cpu_count(environ: Mapping[str, str] | None = None) -> int | None:
    """Return the CPUs assigned to this SLURM task, when explicitly available."""

    env = _environment(environ)
    return _parse_int(env, "SLURM_CPUS_PER_TASK", minimum=1)


def allocated_cpu_count(
    environ: Mapping[str, str] | None = None,
    *,
    override: int | None = None,
) -> int:
    """Resolve the CPU budget visible to one local process.

    Explicit overrides take precedence over SLURM. Outside SLURM, process CPU
    affinity is preferred over the machine-wide CPU count so a cgroup or taskset
    allocation is not accidentally oversubscribed.
    """

    if override is not None:
        if isinstance(override, bool) or not isinstance(override, int) or override <= 0:
            raise ValueError("CPU-count override must be a positive integer")
        return override

    slurm_count = slurm_cpu_count(environ)
    if slurm_count is not None:
        return slurm_count

    get_affinity = getattr(os, "sched_getaffinity", None)
    if get_affinity is not None:
        try:
            affinity_count = len(get_affinity(0))
        except OSError:
            affinity_count = 0
        if affinity_count > 0:
            return affinity_count
    return max(1, os.cpu_count() or 1)


def slurm_array_context(
    environ: Mapping[str, str] | None = None,
) -> SlurmArrayContext | None:
    """Parse a regular SLURM array into a compact zero-based rank.

    SLURM does not expose an ordinal for arbitrary sparse array specifications.
    Such layouts are rejected instead of silently assigning overlapping work.
    Contiguous and regularly stepped arrays (for example ``0-19`` or
    ``2-20:2``) are supported.
    """

    env = _environment(environ)
    task_id = _parse_int(env, "SLURM_ARRAY_TASK_ID", minimum=0)
    if task_id is None:
        return None

    minimum = _parse_int(env, "SLURM_ARRAY_TASK_MIN", minimum=0)
    maximum = _parse_int(env, "SLURM_ARRAY_TASK_MAX", minimum=0)
    step = _parse_int(env, "SLURM_ARRAY_TASK_STEP", minimum=1)
    count = _parse_int(env, "SLURM_ARRAY_TASK_COUNT", minimum=1)
    if minimum is None or maximum is None or count is None:
        raise SlurmEnvironmentError(
            "SLURM array tasks require SLURM_ARRAY_TASK_MIN, "
            "SLURM_ARRAY_TASK_MAX, and SLURM_ARRAY_TASK_COUNT"
        )
    if step is None:
        step = 1
    if maximum < minimum:
        raise SlurmEnvironmentError(
            "SLURM_ARRAY_TASK_MAX must not be smaller than SLURM_ARRAY_TASK_MIN"
        )
    if task_id < minimum or task_id > maximum:
        raise SlurmEnvironmentError(
            "SLURM_ARRAY_TASK_ID lies outside the declared array range"
        )
    delta = task_id - minimum
    if delta % step != 0:
        raise SlurmEnvironmentError(
            "SLURM_ARRAY_TASK_ID is not aligned to SLURM_ARRAY_TASK_STEP"
        )

    expected_count = (maximum - minimum) // step + 1
    rank = delta // step
    if expected_count != count or rank >= count:
        raise SlurmEnvironmentError(
            "SLURM array metadata is not a regular range; use explicit rank/world overrides"
        )
    return SlurmArrayContext(
        task_id=task_id,
        rank=rank,
        count=count,
        minimum=minimum,
        maximum=maximum,
        step=step,
    )


def distributed_context(
    environ: Mapping[str, str] | None = None,
    *,
    rank: int | None = None,
    world_size: int | None = None,
) -> DistributedContext:
    """Resolve one global rank from SLURM tasks and an optional job array.

    When both mechanisms are present, process ranks are nested inside array
    ranks: ``global_rank = array_rank * SLURM_NTASKS + SLURM_PROCID``.
    Passing ``rank`` and ``world_size`` explicitly bypasses SLURM discovery.
    """

    if (rank is None) != (world_size is None):
        raise ValueError("rank and world_size overrides must be provided together")
    if rank is not None and world_size is not None:
        if (
            isinstance(rank, bool)
            or not isinstance(rank, int)
            or isinstance(world_size, bool)
            or not isinstance(world_size, int)
            or world_size <= 0
            or rank < 0
            or rank >= world_size
        ):
            raise ValueError("distributed overrides require 0 <= rank < world_size")
        return DistributedContext(
            rank=rank,
            world_size=world_size,
            task_rank=rank,
            task_count=world_size,
            array=None,
            source="explicit",
        )

    env = _environment(environ)
    task_rank = _parse_int(env, "SLURM_PROCID", minimum=0)
    task_count = _parse_int(env, "SLURM_NTASKS", minimum=1)
    if task_rank is not None and task_count is None:
        raise SlurmEnvironmentError("SLURM_PROCID is set without SLURM_NTASKS")
    if task_count is None:
        task_count = 1
    if task_rank is None:
        if task_count > 1:
            raise SlurmEnvironmentError(
                "SLURM_NTASKS is greater than one but SLURM_PROCID is unavailable; "
                "launch through srun or provide explicit rank/world overrides"
            )
        task_rank = 0
    if task_rank >= task_count:
        raise SlurmEnvironmentError("SLURM_PROCID must be smaller than SLURM_NTASKS")

    array = slurm_array_context(env)
    if array is None:
        return DistributedContext(
            rank=task_rank,
            world_size=task_count,
            task_rank=task_rank,
            task_count=task_count,
            array=None,
            source="slurm" if task_count > 1 else "single",
        )

    return DistributedContext(
        rank=array.rank * task_count + task_rank,
        world_size=array.count * task_count,
        task_rank=task_rank,
        task_count=task_count,
        array=array,
        source="slurm_array",
    )