Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,18 @@ All notable changes to CableXSec are recorded in this file. The format follows
at 0.1 (2e-9 at 0.02). The README and quickstart outputs, which had drifted before this
change (peak 548 N m and 219.8 J/m printed against 541 N m and 212.0 J/m computed), are
regenerated.

- `run_sea_state_batch` with `workers > 1`: each worker process writes a numba cache of its own
instead of sharing the caller's. numba numbers a new on-disk cache entry from the index it
last read and saves the index before the data, so two workers compiling one kernel for
different signatures at the same time could leave the index pointing at the other's machine
code, which a later process would load. Worker caches are slot folders `slot-<i>` under
`cablexsec/numba-batch` in the user cache directory (`CABLEXSEC_BATCH_CACHE_DIR` overrides
it), kept for later batches. Each worker locks its slot itself for as long as it runs (so
the slot stays taken even if the calling process ends first), a batch beside another takes
the next free slots, and a worker without a free slot or a writable folder gets a temporary
cache. Results are unchanged, and runs go to whichever worker is free, as before.

- `SectionLaw.from_cable` reads the cable's own materials (its `materials:` block) when the law
is built and every time it is evaluated. Outside `simulate_bending_test` it read the library
instead, and a layer whose material the library lacks silently dropped out of the stiffness
Expand Down
7 changes: 6 additions & 1 deletion doc/global_local_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,12 @@ result.sections["sag"].long_term.annual_damage, result.sections["sag"].life_year
mismatch stops the batch unless `allow_property_mismatch=True` is
declared, and the comparison is written to the manifest either way.
- Workers: `workers` processes (at most `MAX_WORKERS = 8`), one numerical
thread each, spawned; results do not depend on the worker count.
thread each, spawned; results do not depend on the worker count. Each
worker keeps its compiled kernels in a cache folder of its own, `slot-<i>`
under `cablexsec/numba-batch` in the user cache directory (set
`CABLEXSEC_BATCH_CACHE_DIR` to move it), so later batches start warm; a
slot is held by one worker at a time, and deleting the folder only costs a
recompilation.
- Output folder (must not exist): `manifest.json` (SHA-256 of every CableDyn
file and deck, the cable definition and YAML file, options, property check,
reader checks per run, results, and `inputs_sha256` over all of them);
Expand Down
224 changes: 209 additions & 15 deletions src/cablexsec/analysis/sea_state_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@
Parallel execution
------------------
Runs are processed in worker processes (``workers``, at most
:data:`MAX_WORKERS`), each limited to one numerical thread. Results do not
depend on the worker count. The output directory, if given, must not exist:
results are written once, with a manifest of SHA-256 digests of every input
file, the cable definition and the options.
:data:`MAX_WORKERS`), each limited to one numerical thread. Each worker keeps
its compiled kernels in a numba cache of its own: a slot directory
``slot-<i>`` under ``cablexsec/numba-batch`` in the user cache directory, or
under ``CABLEXSEC_BATCH_CACHE_DIR`` when that is set, which later batches
reuse. Each worker locks its slot itself for as long as it runs, so no two
processes write one cache; a batch beside another takes the next free slots,
and a worker without one gets a temporary cache. Results do not depend on the
worker count. The output directory, if given, must not exist: results are
written once, with a manifest of SHA-256 digests of every input file, the
cable definition and the options.
"""

from __future__ import annotations
Expand All @@ -54,9 +60,12 @@
import pickle
import platform
import sys
import tempfile
import threading
import warnings
from collections.abc import Mapping, Sequence
from concurrent.futures import ProcessPoolExecutor
from collections.abc import Iterator, Mapping, Sequence
from concurrent.futures import FIRST_COMPLETED, Future, ProcessPoolExecutor, wait
from contextlib import contextmanager
from dataclasses import asdict, dataclass, field, fields
from pathlib import Path
from types import MappingProxyType
Expand Down Expand Up @@ -109,6 +118,11 @@
"NUMBA_NUM_THREADS",
"NUMEXPR_NUM_THREADS",
)
_NUMBA_CACHE_VARIABLE = "NUMBA_CACHE_DIR"
#: Overrides the folder of the persistent worker caches (_batch_cache_directory).
_BATCH_CACHE_VARIABLE = "CABLEXSEC_BATCH_CACHE_DIR"
#: Persistent worker cache slots; a worker beyond them gets a temporary cache.
_CACHE_SLOTS = 2 * MAX_WORKERS

__all__ = [
"MAX_WORKERS",
Expand Down Expand Up @@ -468,6 +482,193 @@ def _fretting_summary(result: Any) -> dict[str, dict[str, Any]]:
return out


def _batch_cache_directory() -> Path:
"""Folder of the persistent numba caches of the batch workers.

``CABLEXSEC_BATCH_CACHE_DIR`` when set; otherwise ``cablexsec/numba-batch``
in the user cache directory (``%LOCALAPPDATA%`` on Windows,
``~/Library/Caches`` on macOS, ``$XDG_CACHE_HOME`` or ``~/.cache``
elsewhere). Deleting it only costs the workers a recompilation.
"""
override = os.environ.get(_BATCH_CACHE_VARIABLE)
if override:
return Path(override)
if sys.platform == "win32":
local = os.environ.get("LOCALAPPDATA")
base = Path(local) if local else Path.home() / "AppData" / "Local"
elif sys.platform == "darwin":
base = Path.home() / "Library" / "Caches"
else:
xdg = os.environ.get("XDG_CACHE_HOME")
base = Path(xdg) if xdg else Path.home() / ".cache"
return base / "cablexsec" / "numba-batch"


def _lock(handle: Any) -> bool:
"""Take an exclusive, non-blocking lock on an open file; False if held."""
try:
if sys.platform == "win32":
import msvcrt # noqa: PLC0415

handle.seek(0)
msvcrt.locking(handle.fileno(), msvcrt.LK_NBLCK, 1)
else:
import fcntl # noqa: PLC0415

fcntl.flock(handle.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
except OSError:
return False
return True


def _unlock(handle: Any) -> None:
if sys.platform == "win32":
import msvcrt # noqa: PLC0415

handle.seek(0)
msvcrt.locking(handle.fileno(), msvcrt.LK_UNLCK, 1)
else:
import fcntl # noqa: PLC0415

fcntl.flock(handle.fileno(), fcntl.LOCK_UN)


def _slot_is_free(path: Path) -> bool:
"""True if no process holds the slot lock ``path`` (it is not kept)."""
try:
path.parent.mkdir(parents=True, exist_ok=True)
with open(path, "a+b") as handle:
if not _lock(handle):
return False
_unlock(handle)
except OSError:
return False
return True


#: The slot lock a worker process holds for its lifetime (see _claim_slot).
_SLOT_HANDLE: Any = None
#: Serialises the NUMBA_CACHE_DIR change around a worker start (one per process).
_START_LOCK = threading.Lock()


def _claim_slot(path: str) -> bool:
"""Worker side: lock the slot ``path`` for the rest of this process.

The worker, not the caller, holds the lock, so the slot stays taken while
the worker can still write its cache, even if the caller has ended; the
operating system releases it when the worker ends.
"""
global _SLOT_HANDLE
try:
handle = open(path, "a+b") # noqa: SIM115 - held for the process
except OSError:
return False
if not _lock(handle):
handle.close()
return False
_SLOT_HANDLE = handle
return True


def _start_worker(cache: Path) -> ProcessPoolExecutor:
"""A one-process spawn pool whose worker writes the numba cache ``cache``.

The worker is started while ``NUMBA_CACHE_DIR`` names ``cache`` and
inherits it before numba is imported; the caller's value is restored. The
whole change is serialised, so that workers started from several threads
at once each inherit their own directory.
"""
import multiprocessing # noqa: PLC0415

pool = ProcessPoolExecutor(
max_workers=1,
mp_context=multiprocessing.get_context("spawn"),
initializer=_limit_threads,
)
with _START_LOCK:
previous = os.environ.get(_NUMBA_CACHE_VARIABLE)
os.environ[_NUMBA_CACHE_VARIABLE] = str(cache)
try:
pool.submit(os.getpid).result() # the worker now runs with it
except BaseException:
pool.shutdown(cancel_futures=True)
raise
finally:
if previous is None:
os.environ.pop(_NUMBA_CACHE_VARIABLE, None)
else:
os.environ[_NUMBA_CACHE_VARIABLE] = previous
return pool


@contextmanager
def _workers(count: int) -> Iterator[list[ProcessPoolExecutor]]:
"""``count`` worker pools, each writing a numba cache no other process writes.

numba numbers a new on-disk cache entry from the index it last read and
saves the index before the data, so two processes that compile one kernel
for different signatures at the same time can both take the same data file
and leave the index pointing at the other's machine code. A worker takes
the next free slot of :func:`_batch_cache_directory` and locks it itself
before it runs anything (:func:`_claim_slot`); if another process won the
slot meanwhile, the worker is replaced and the next slot tried. The slots
are kept for later batches; a worker without a free slot gets a temporary
cache, removed after the batch.
"""
root = _batch_cache_directory()
pools: list[ProcessPoolExecutor] = []
scratch: tempfile.TemporaryDirectory[str] | None = None
try:
for slot in range(_CACHE_SLOTS):
if len(pools) == count:
break
lock = root / f"slot-{slot}.lock"
if not _slot_is_free(lock):
continue
pool = _start_worker(root / f"slot-{slot}")
pools.append(pool)
if not pool.submit(_claim_slot, str(lock)).result():
pools.pop().shutdown()
if len(pools) < count:
scratch = tempfile.TemporaryDirectory(
prefix="cablexsec-batch-numba-", ignore_cleanup_errors=True
)
for index in range(count - len(pools)):
pools.append(_start_worker(Path(scratch.name) / f"worker-{index}"))
yield pools
finally:
for pool in pools:
pool.shutdown(cancel_futures=True)
if scratch is not None:
scratch.cleanup()


def _evaluate_in_pools(
tasks: Sequence[_Task], pools: Sequence[ProcessPoolExecutor]
) -> list[dict[str, Any]]:
"""Outcomes of ``tasks`` in order; each pool takes the next task when free."""
outcomes: dict[int, dict[str, Any]] = {}
queue = iter(enumerate(tasks))
pending: dict[Future[dict[str, Any]], tuple[int, ProcessPoolExecutor]] = {}

def feed(pool: ProcessPoolExecutor) -> None:
item = next(queue, None)
if item is not None:
index, task = item
pending[pool.submit(_evaluate_in_worker, task)] = (index, pool)

for pool in pools:
feed(pool)
while pending:
done, _ = wait(pending, return_when=FIRST_COMPLETED)
for future in done:
index, pool = pending.pop(future)
outcomes[index] = future.result()
feed(pool)
return [outcomes[index] for index in range(len(tasks))]


def _evaluate_in_worker(task: _Task) -> dict[str, Any]:
""":func:`_evaluate` in a worker process, with its warnings recorded.

Expand Down Expand Up @@ -979,20 +1180,13 @@ def run_sea_state_batch(
if opts.workers == 1 or len(tasks) == 1:
outcomes = [_evaluate(task) for task in tasks]
else:
import multiprocessing # noqa: PLC0415

context = multiprocessing.get_context("spawn")
saved = {name: os.environ.get(name) for name in _THREAD_VARIABLES}
try:
# Spawned workers inherit one-thread settings before any import.
for name in _THREAD_VARIABLES:
os.environ[name] = "1"
with ProcessPoolExecutor(
max_workers=min(opts.workers, len(tasks)),
mp_context=context,
initializer=_limit_threads,
) as pool:
outcomes = list(pool.map(_evaluate_in_worker, tasks))
with _workers(min(opts.workers, len(tasks))) as pools:
outcomes = _evaluate_in_pools(tasks, pools)
finally:
for name, value in saved.items():
if value is None:
Expand Down
14 changes: 11 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@
_NUMBA_CACHE = Path(_ROOT) / f"cablexsec-numba-{_RUN}-{_WORKER}"
os.environ["NUMBA_CACHE_DIR"] = str(_NUMBA_CACHE)

# The sea-state batch workers keep persistent numba caches in the user cache
# directory; the tests give them a folder of this process, removed at the end.
_BATCH_CACHE: Path | None = None
if "CABLEXSEC_BATCH_CACHE_DIR" not in os.environ:
_BATCH_CACHE = Path(tempfile.gettempdir()) / f"cablexsec-batch-{os.getpid()}"
os.environ["CABLEXSEC_BATCH_CACHE_DIR"] = str(_BATCH_CACHE)

_SCRIPTS = Path(__file__).resolve().parents[1] / "validation" / "scripts"
if str(_SCRIPTS) not in sys.path:
sys.path.insert(0, str(_SCRIPTS))


def pytest_unconfigure(config: object) -> None:
"""Remove the worker's own numba cache at the end of the run."""
if _NUMBA_CACHE is not None:
shutil.rmtree(_NUMBA_CACHE, ignore_errors=True)
"""Remove the numba caches of this process at the end of the run."""
for folder in (_NUMBA_CACHE, _BATCH_CACHE):
if folder is not None:
shutil.rmtree(folder, ignore_errors=True)
Loading
Loading