🔌 API documentation

numthreads: Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, Accelerate, and Numba.

numthreads.main()[source]

Command-line interface.

Return type:

None

numthreads.num_threads(n=1, *, overwrite=True)[source]

Context manager to set and then restore thread number settings.

Return type:

Generator[None, None, None]

numthreads.omp_get_num_threads()[source]

Returns the number of threads in the current OpenMP parallel region.

Return type:

int

numthreads.omp_num_threads(num_threads, *, overwrite=True)[source]

Context manager to set and then restore OpenMP thread number settings.

Parameters:
  • num_threads (int) – Number of threads to set in the OpenMP parallel regions.

  • overwrite (bool) – Whether to overwrite existing values.

Return type:

Generator[None, None, None]

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.

Parameters:
  • num_threads (int) – Number of threads to set.

  • overwrite (bool) – If true, overwrite the existing value.

Return type:

None

numthreads.print_current_thread_counts()[source]

Prints the current values of the thread control environment variables.

Return type:

None

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_threads function to change the number of threads after importing the library.

Parameters:
  • n (int) – Number of threads to set.

  • overwrite (bool) – If true, overwrite the existing value.

Return type:

None

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_threads function to change the number of threads after importing the library.

Parameters:
  • n (int) – Number of threads to set.

  • overwrite (bool) – If true, overwrite the existing value.

Return type:

None