Skip to main content

numthreads

Set the number of threads used by OpenBLAS, MKL, OMP, NumExpr, and Accelerate

PyPI Build Status CodeCov GitHub Repo stars Documentation

numthreads is a really tiny and simple Python package designed to set the number of threads for various computing libraries including OpenBLAS, Intel's Math Kernel Library (MKL), OpenMP, NumExpr, and Accelerate. The number of threads can be set via the command line or in Python code. The performance of many numerical algorithms varies significantly based on the number of threads employed. While increasing the number of threads can often accelerate these algorithms, it's not always the case. In some instances, using more threads may actually impede computational efficiency. Therefore, it's important to be able to easily set the number of threads used by these libraries.

  • Simple and straightforward command-line interface.
  • Sets thread count for OpenBLAS, MKL, OpenMP, NumExpr, and Accelerate.
  • Context manager support for temporary thread setting in Python code.
  • Cross-platform compatibility (Linux, macOS, Windows).
  • Tiny (≤7KB) and no dependencies.

:books: Table of Contents

:package: Installation

To install numthreads, run the following command:

pip install numthreads

:rocket: Quick Start

Get started with numthreads in a few seconds.

Using as a Python Module

You can also use numthreads as a Python module:

from numthreads import set_num_threads

set_num_threads(4)

This will set the number of threads using the following environment variables:

  • OpenBLAS (via OPENBLAS_NUM_THREADS)
  • MKL (via MKL_NUM_THREADS)
  • OpenMP (via OMP_NUM_THREADS)
  • NumExpr (via NUMEXPR_NUM_THREADS)
  • Accelerate (via VECLIB_MAXIMUM_THREADS)

or use it as a context manager:

from numthreads import num_threads

with num_threads(4):
    # Your code here will run with the specified number of threads
    pass

[!WARNING] Since environment variables are global and typically need to be set before importing any libraries, it's recommended to set the number of threads at the beginning of your Python script.

To set OMP (OpenMP) threads at any time (OpenMP docs), you can use omp_set_num_threads or the omp_num_threads context manager:

from numthreads import omp_set_num_threads

omp_set_num_threads(4)

or

from numthreads import omp_num_threads

with omp_num_threads(4):
    # Your code here will run with the specified number of threads
    pass

Command Line Interface

After installing numthreads, you can easily set the number of threads used by supported libraries via the command line. For example, to print the command to set the number of threads to 4, run:

numthreads 4

Which will print the following:

export OPENBLAS_NUM_THREADS='4' ; export MKL_NUM_THREADS='4' ; export OMP_NUM_THREADS='4' ; export NUMEXPR_NUM_THREADS='4' ; export VECLIB_MAXIMUM_THREADS='4'

Unix-like Systems (Linux, macOS, WSL)

To apply the settings in your shell:

eval $(numthreads <number_of_threads>)

Windows (PowerShell)

In PowerShell, use:

Invoke-Expression $(numthreads <number_of_threads>)

Get the number of threads

To get the number of threads currently set, run:

eval $(numthreads 12)  # first set something
numthreads get

Which will print the following:

OPENBLAS_NUM_THREADS: 12
MKL_NUM_THREADS: 12
OMP_NUM_THREADS: 12
NUMEXPR_NUM_THREADS: 12
VECLIB_MAXIMUM_THREADS: 12

Check the numthreads -h message for more information:

usage: numthreads [-h] n

Set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate.
Usage: Run `numthreads <number>` to print the export commands. On Unix-like
systems (Linux, macOS, WSL), use `eval $(numthreads <number>)` in your shell
to apply these settings. On Windows, in PowerShell, use `Invoke-Expression
$(numthreads <number>)`.

positional arguments:
  n           Number of threads to set or use 'get' to display current
              settings.

options:
  -h, --help  show this help message and exit

:electric_plug: Using as a pytest Plugin

numthreads can be used as a pytest plugin to automatically set the number of threads for your tests.

To use it, make sure numthreads is installed. Then, run pytest with the --numthreads option followed by the desired number of threads:

pytest --numthreads 1

This will set the number of threads for OpenBLAS, MKL, OMP, NumExpr, and Accelerate before running your tests.

:question: Getting Help

For more information, or to report issues, please visit numthreads GitHub repository.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

numthreads-0.5.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

numthreads-0.5.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file numthreads-0.5.0.tar.gz.

File metadata

  • Download URL: numthreads-0.5.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for numthreads-0.5.0.tar.gz
Algorithm Hash digest
SHA256 3b534ce014ac05d2bca396c92c6aed35a013d81b5341b7a7756673ca2ac83dcc
MD5 a24129fc4ac1fe329e6a69e3e415fc26
BLAKE2b-256 0a36ddaa914cb43c870d58e1a223124cd793800fb75c71bc5148892f5e353831

See more details on using hashes here.

File details

Details for the file numthreads-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: numthreads-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for numthreads-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e56e83cbccef103901e678aa014d64b203cdb1b3a3be7cdedb2516ef62ec8fa1
MD5 190d1ed1a35013c65826c42754ec2bf9
BLAKE2b-256 04278739697a1d77f972feee90b844786b893217a133941477570d161de2750f

See more details on using hashes here.

Release history Release notifications | RSS feed

0.6.1

2 files

This release

0.5.0 This release

2 files

0.4.0

2 files

0.3.0

2 files

0.2.0

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page