torchspin.parallel#
CPU thread budget shared by every level of parallelism in torchspin.
torchspin can run three nested levels of CPU parallelism:
an outer pool of whole simulations (esfit population methods,
torchspin.ml.simulate_batch());inner thread pools inside one simulation (the batched eigendecomposition and chunked accumulation in
torchspin._linalg);the native BLAS/OpenMP pools of torch, NumPy and SciPy.
If each level sizes itself to the whole machine, they multiply and oversubscribe the cores. A small fit on a 32-core node can then run several times slower than on 8. This module gives the levels one shared budget:
cpu_budget()is the number of cores this process may use. It honors the CPU affinity mask (taskset, cgroups, batch schedulers), thenSLURM_CPUS_PER_TASK, thenos.cpu_count().thread_budget()is a context manager that caps the inner pools for the enclosed code. It works per thread (acontextvars.ContextVar), so concurrent workers of an outer thread pool can each hold their own share. Withblas=Trueandthreadpoolctlinstalled (the[hpc]extra) it also caps the NumPy/SciPy BLAS pools. That cap is process-wide, so use it only from a single thread.native_thread_env()sets the environment variables that size the native pools of a child process. They are read at library load, so they must be in place before a spawned worker starts.
The budget only limits how many workers torchspin’s own pools start. It never changes results: each matrix or chunk is still processed by the same routine.
- torchspin.parallel.cpu_budget()[source]#
Number of CPU cores this process may use.
In order of precedence: the CPU affinity mask (Linux
sched_getaffinity, which reflectstasksetand scheduler cgroups),SLURM_CPUS_PER_TASK, andos.cpu_count(). A SLURM value larger than the affinity mask is ignored, because the mask is what the kernel enforces. (Implemented intorchspin._resources, which also sizesn_workers='auto'by memory.)
- torchspin.parallel.inner_threads()[source]#
Worker count available to torchspin’s inner pools in the current context.
This is the value set by the innermost enclosing
thread_budget(), ortorch.get_num_threads()when no budget is active. It never exceedstorch.get_num_threads(), so an explicittorch.set_num_threadsstill caps it.
- torchspin.parallel.thread_budget(n, blas=False)[source]#
Cap torchspin’s inner pools at
nthreads (and BLAS too, withblas=True).Example:
with torchspin.parallel.thread_budget(4): x, y = pepper(sys, exp, opt) # eigh pool and accumulation use <= 4 threads
Nested budgets can only shrink the allowance. The inner-pool setting is local to the calling thread’s context, so concurrent workers can hold different budgets.
blas=Truealso appliesthreadpoolctl.threadpool_limitsto the BLAS libraries loaded in the process (a no-op ifthreadpoolctlis not installed). That limit is process-wide and not thread-safe, so do not useblas=Trueinside concurrent workers. Give worker processes their caps withnative_thread_env()instead.
- torchspin.parallel.native_thread_env(n)[source]#
Temporarily set
NATIVE_THREAD_VARSton(for spawning workers).Child processes inherit the environment at spawn and size their native pools from it at import. Without these caps every spawned worker starts one thread per core. Workers also start with
OMP_WAIT_POLICY=PASSIVEunless the user set a value (seetorchspin._omp). The previous values are restored on exit.
- torchspin.parallel.NATIVE_THREAD_VARS = ('OMP_NUM_THREADS', 'OPENBLAS_NUM_THREADS', 'MKL_NUM_THREADS', 'NUMEXPR_NUM_THREADS', 'VECLIB_MAXIMUM_THREADS')#
Environment variables that size the native thread pools of torch, NumPy and SciPy (OpenMP, OpenBLAS, MKL, numexpr, Apple Accelerate).