Skip to main content

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

Project description

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).
  • 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, 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
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>)

:question: Getting Help

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

Project details


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.4.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

numthreads-0.4.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: numthreads-0.4.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for numthreads-0.4.0.tar.gz
Algorithm Hash digest
SHA256 60863221aa2bfefe8220f4d636899c00d2e54d7c482ca67db54fa29cd0246a7b
MD5 03f1bedf26315c93942182dd116010bb
BLAKE2b-256 597b4827158700d490a5374974c3a28e326b6904558e3e8133e42511230b2091

See more details on using hashes here.

File details

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

File metadata

  • Download URL: numthreads-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for numthreads-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2e83fb457a3c1dddc9b1bb5c599b4b9fa79a53f1f4fc37c4e08ba1da6ba8fd63
MD5 4a987f707047134967f761bdf7c95f82
BLAKE2b-256 18135dae65a15ff8f6ed2de5bc86bf8696163b8edbd6951611e81e0a82858a48

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page