Configuring CPU threads

Qrunch defaults to the number of physical CPU cores for native parallel computation, including PySCF, PyFraME’s OpenMP embedding calculations, and Qrunch’s Rayon-based Rust code. If the physical-core count cannot be determined, Qrunch warns and uses one thread.

Set QRUNCH_NUM_THREADS to a positive integer to select one thread count for all of these libraries. This works identically with the installed Qrunch wheel:

QRUNCH_NUM_THREADS=4 python calculation.py

Alternatively, set the variable at the very beginning of your Python script:

import os

os.environ["QRUNCH_NUM_THREADS"] = "4"

import qrunch
# Import PySCF, PyFraME, NumPy, etc. only after importing Qrunch.

On import, Qrunch configures OMP_NUM_THREADS, RAYON_NUM_THREADS, MKL_NUM_THREADS, BLIS_NUM_THREADS, and OPENBLAS_NUM_THREADS before loading numerical libraries. QRUNCH_NUM_THREADS overrides all five variables. If it is unset, existing library-specific values are preserved and only unset variables receive the physical-core default. For example, you can set OMP_NUM_THREADS=2 RAYON_NUM_THREADS=4 to configure OpenMP and Rayon separately. Invalid QRUNCH_NUM_THREADS values raise ValueError during import.

Thread counts also depend on the capabilities of the installed native libraries. In particular, PySCF built without OpenMP support always reports one OpenMP thread, regardless of OMP_NUM_THREADS; calling pyscf.lib.num_threads(n) cannot enable threading in such a build either. To use multiple OpenMP threads, install a PySCF build with OpenMP support. BLAS threading is configured independently.

Important

Set the variables before importing Qrunch, and import Qrunch before other numerical libraries. Already initialized native thread pools are not reconfigured by changing environment variables. Restart the Python process or notebook kernel to apply a different startup configuration. PySCF also supports changing its OpenMP count at runtime with pyscf.lib.num_threads(number_of_threads), but that does not reconfigure Rayon or the other native thread pools.

For MPI, multiprocessing, containers, or batch jobs, set the thread count explicitly to the CPU allocation per process to avoid oversubscription; the machine’s physical-core count may exceed your allocation. Component-specific options that explicitly create their own worker pools remain independent of this setting.