PAOFLOW.utils.parallel_resources#

Core budgeting for joblib/loky parallelism inside MPI launches.

PAOFLOW is normally run as mpirun -np N python main.py, so every MPI rank executes the same script. A few modules (models/sk_fitting.py, models/band_unfold.py, spectrum/sparse_bands.py, pyskeaf/runner.py) additionally spawn joblib/loky worker processes. Without coordination each rank would size its pool for the whole node, oversubscribing the cores N-fold.

This module assigns every rank a core budget — its share of the node — and enforces it at two levels:

  1. configure_joblib_for_mpi() (called when PAOFLOW is imported) exports LOKY_MAX_CPU_COUNT; configure_native_threads() does the same for the Rust kernels through PAOFLOW_RS_THREADS. loky’s cpu_count() honours it, so n_jobs=-1 resolves to the budget for any joblib call, including user scripts, and loky caps BLAS/OpenMP threads per worker at budget // n_jobs.

  2. resolve_n_jobs() clips explicit worker counts requested at PAOFLOW call sites, which joblib itself never caps.

Launch detection only reads launcher environment variables and never performs MPI collectives, so it is cheap, does not import mpi4py, and is safe to call from a single rank.

Functions#

mpi_rank()

Return this process' MPI rank from common launcher env vars.

mpi_launch_sizes()

Detect the MPI world size and the number of ranks on this node.

core_budget()

Number of cores this process may use for joblib workers and threads.

configure_joblib_for_mpi()

Export LOKY_MAX_CPU_COUNT for multi-rank MPI launches.

configure_native_threads()

Export PAOFLOW_RS_THREADS (the Rust kernels' pool size) from the core budget.

resolve_n_jobs(requested[, n_tasks, threads_per_job])

Convert a joblib n_jobs request into a worker count within budget.

Module Contents#

PAOFLOW.utils.parallel_resources.mpi_rank()[source]#

Return this process’ MPI rank from common launcher env vars.

Returns:

The rank reported by the launcher, or None outside an MPI launch.

Return type:

int or None

PAOFLOW.utils.parallel_resources.mpi_launch_sizes()[source]#

Detect the MPI world size and the number of ranks on this node.

Returns:

(world_size, local_size). Both are 1 outside an MPI launch. When the launcher reports a world size but no per-node size, the world size is used for both, which is conservative on multi-node runs.

Return type:

tuple of int

PAOFLOW.utils.parallel_resources.core_budget()[source]#

Number of cores this process may use for joblib workers and threads.

Returns:

LOKY_MAX_CPU_COUNT if set (bounded by the available CPUs), otherwise this rank’s share of the node under MPI, otherwise all available CPUs.

Return type:

int

PAOFLOW.utils.parallel_resources.configure_joblib_for_mpi()[source]#

Export LOKY_MAX_CPU_COUNT for multi-rank MPI launches.

Returns:

Sets os.environ['LOKY_MAX_CPU_COUNT'] to this rank’s core share when more than one MPI rank is detected and the variable is not already defined. Serial and single-rank runs are left untouched.

Return type:

None

PAOFLOW.utils.parallel_resources.configure_native_threads()[source]#

Export PAOFLOW_RS_THREADS (the Rust kernels’ pool size) from the core budget.

Returns:

Sets os.environ['PAOFLOW_RS_THREADS'] to core_budget() when the budget is constrained (an MPI launch with several ranks, or LOKY_MAX_CPU_COUNT set) and the variable is not already defined, so that every rank’s rayon pool stays within its share of the node. Unconstrained serial runs are left to rayon’s default (all cores).

Return type:

None

PAOFLOW.utils.parallel_resources.resolve_n_jobs(requested, n_tasks=None, threads_per_job=1)[source]#

Convert a joblib n_jobs request into a worker count within budget.

Parameters:
  • requested (int or None) – joblib-style request: positive for an explicit count, -1 for all cores, -k for all but k - 1 cores, None for serial.

  • n_tasks (int or None, optional) – Number of independent tasks; the result never exceeds it.

  • threads_per_job (int, optional) – Threads each worker starts itself (for example a ThreadPoolExecutor), so that workers * threads_per_job stays within core_budget().

Returns:

Worker count >= 1. Emits a single RuntimeWarning (primary rank only) the first time an explicit request is reduced.

Return type:

int

Raises:

ValueError – If requested == 0, which has no meaning in joblib.