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:
configure_joblib_for_mpi()(called whenPAOFLOWis imported) exportsLOKY_MAX_CPU_COUNT;configure_native_threads()does the same for the Rust kernels throughPAOFLOW_RS_THREADS. loky’scpu_count()honours it, son_jobs=-1resolves to the budget for any joblib call, including user scripts, and loky caps BLAS/OpenMP threads per worker atbudget // n_jobs.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#
|
Return this process' MPI rank from common launcher env vars. |
Detect the MPI world size and the number of ranks on this node. |
|
Number of cores this process may use for joblib workers and threads. |
|
Export |
|
Export |
|
|
Convert a joblib |
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
Noneoutside 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.
- PAOFLOW.utils.parallel_resources.core_budget()[source]#
Number of cores this process may use for joblib workers and threads.
- Returns:
LOKY_MAX_CPU_COUNTif set (bounded by the available CPUs), otherwise this rank’s share of the node under MPI, otherwise all available CPUs.- Return type:
- PAOFLOW.utils.parallel_resources.configure_joblib_for_mpi()[source]#
Export
LOKY_MAX_CPU_COUNTfor 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']tocore_budget()when the budget is constrained (an MPI launch with several ranks, orLOKY_MAX_CPU_COUNTset) 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_jobsrequest into a worker count within budget.- Parameters:
requested (int or None) – joblib-style request: positive for an explicit count,
-1for all cores,-kfor all butk - 1cores,Nonefor 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 thatworkers * threads_per_jobstays withincore_budget().
- Returns:
Worker count
>= 1. Emits a singleRuntimeWarning(primary rank only) the first time an explicit request is reduced.- Return type:
- Raises:
ValueError – If
requested == 0, which has no meaning in joblib.