🔌 API documentation¶
numthreads: Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba.
- numthreads.num_threads(n=1, *, overwrite=True)[source]¶
Context manager to set and then restore thread number settings.
- numthreads.omp_get_num_threads()[source]¶
Returns the number of threads in the current OpenMP parallel region.
- Return type:
- numthreads.omp_num_threads(num_threads, *, overwrite=True)[source]¶
Context manager to set and then restore OpenMP thread number settings.
- Parameters:
- Return type:
Examples
>>> import numpy as np >>> from numthreads import omp_num_threads >>> with omp_num_threads(2): ... np.ones(10) array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
- numthreads.omp_set_num_threads(num_threads, *, overwrite=True)[source]¶
Sets the number of threads to be used by OpenMP parallel regions.
When using OMP (OpenMP), you can use this function to change the number of threads after importing the library. This overrides whatever value is set in the
"OMP_NUM_THREADS"environment variable.
- numthreads.print_current_thread_counts()[source]¶
Prints the current values of the thread control environment variables.
- Return type:
- numthreads.pytest_addoption(parser)[source]¶
Add the –numthreads option to the pytest command line.
- Return type:
None
- numthreads.pytest_configure(config)[source]¶
Set the number of threads based on the –numthreads option.
- Return type:
None
- numthreads.set(n=1, *, overwrite=True)¶
Set the number of threads via environment variables.
Sets the following environment variables:
"OPENBLAS_NUM_THREADS"for OpenBLAS (Open Basic Linear Algebra Subprograms)"MKL_NUM_THREADS"for MKL (Intel Math Kernel Library)"OMP_NUM_THREADS"for OMP (OpenMP)"NUMEXPR_NUM_THREADS"for NumExpr (NumPy expression evaluator)"VECLIB_MAXIMUM_THREADS"for Accelerate (macOS)
Note that setting these environment variables typically needs to be done before importing the relevant libraries. For example, if you want to set the number of threads for NumPy, you should set the environment variables before importing NumPy. When using OMP (OpenMP), you can use the
omp_set_num_threadsfunction to change the number of threads after importing the library.
- numthreads.set_num_threads(n=1, *, overwrite=True)[source]¶
Set the number of threads via environment variables.
Sets the following environment variables:
"OPENBLAS_NUM_THREADS"for OpenBLAS (Open Basic Linear Algebra Subprograms)"MKL_NUM_THREADS"for MKL (Intel Math Kernel Library)"OMP_NUM_THREADS"for OMP (OpenMP)"NUMEXPR_NUM_THREADS"for NumExpr (NumPy expression evaluator)"VECLIB_MAXIMUM_THREADS"for Accelerate (macOS)
Note that setting these environment variables typically needs to be done before importing the relevant libraries. For example, if you want to set the number of threads for NumPy, you should set the environment variables before importing NumPy. When using OMP (OpenMP), you can use the
omp_set_num_threadsfunction to change the number of threads after importing the library.