question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set number of threads after numpy import

See original GitHub issue

This is not a bug report but just an enhancement proposal.

I think it would be useful and important to be able to easily set the number of threads used by Numpy after Numpy import.

From the perspective of library developers, it is often useful to be able to control the number of threads used by Numpy, see for example https://github.com/biopython/biopython/pull/1401.

It is not difficult to do in a simple script when we are sure that Numpy or Scipy have not been imported previously with something like:

import os
os.environ["OMP_NUM_THREADS"] = "1"
import numpy as np

However, in a library there is a good chance that the user has already imported Numpy in its main script, with something like

import numpy as np

import fluidimage

In this case, I don’t see how to set the number of threads used by Numpy from the fluidimage code.

Thus, it would be very convenient to have a function np.set_num_threads.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:48 (26 by maintainers)

github_iconTop GitHub Comments

13reactions
mattipcommented, Aug 18, 2019

Closing, since the threadpoolctl package handles this.

4reactions
tomMoralcommented, Nov 9, 2018

@Amir-Arsalan The environment variable are the most reliable and cross platform way to control the number of threads for given implementations and if you want to set it for a given interpreter. You can use:

OMP_NUM_THREADS: openmp,
OPENBLAS_NUM_THREADS: openblas,
MKL_NUM_THREADS: mkl,
VECLIB_MAXIMUM_THREADS: accelerate,
NUMEXPR_NUM_THREADS: numexpr

It is possible to set all these variables to control the number of threads, without knowing which library is loaded in advance.

The issue is when you want dynamic control over the number of threads used. Indeed, once the thread pool is started, changing the environment variable will have no effect. Then, you need to access C-library functionality exposed to set the number of threads used, if they are provided by the underlying implementation. @seberg codes works on linux system, where find_library is able to locate and load the correct library file (.so) to call the dynamic thread-pool rescaling functions. The code in a PR for loky try to do it in a cross platform way, by looping through all DLL and setting the number of threads when it is possible. But both implementations have shortcomings, notably they will fail for some platforms (debian?) and implementations (Accelerate has no way to set the number of threads dynamically).

I am currently working on improving the loky code to make it the most reliable possible but it probably needs feedback. I am open to any suggestions for improvements or better support and comments if you see big drawbacks in the proposed way of changing the number of threads used.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Limit number of threads in numpy - python - Stack Overflow
So I know it is using blas, but I can't figure out how to make it use 1 thread for matrix multiplication.
Read more >
Global State — NumPy v1.25.dev0 Manual
One way to control the number of threads is the package threadpoolctl ... When set to madvise NumPy will typically use hugepages for...
Read more >
Better way to set number of threads used by NumPy
My solution below involves loading the libraries at runtime and calling the corresponding C functions from Python.
Read more >
Parallel Processing — scqubits Documentation - Read the Docs
Limiting the number of threads will only be effective if environment variables are set before the first import of Numpy. Several environment variables...
Read more >
Automatic mulit-threading with python numpy - Rocket Science
The random number generation is a serial operation, but the dot product is parallelized by default. import os #must set these before loading ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found