Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unintended multithreading in Python (scikit-learn)

I'm using mixture submodule of sklearn module for Gaussian Mixture Model... When I run my code on a multicore system, it uses multiple cores even though I do not ask for it in the code. Is this a default behavior? And more important, how can I disable it?

Thanks

like image 374
ahmethungari Avatar asked Oct 08 '13 19:10

ahmethungari


People also ask

Is Scikit learn multithreaded?

Scikit-learn relies heavily on NumPy and SciPy, which internally call multi-threaded linear algebra routines implemented in libraries such as MKL, OpenBLAS or BLIS.

Does python have true multithreading?

Python doesn't support multi-threading because Python on the Cpython interpreter does not support true multi-core execution via multithreading. However, Python does have a threading library. The GIL does not prevent threading.

Does multithreading in python improve performance?

This is why Python multithreading can provide a large speed increase. The processor can switch between the threads whenever one of them is ready to do some work. Using the threading module in Python or any other interpreted language with a GIL can actually result in reduced performance.

Does multithreading in python use multiple cores?

Python does not support multithreading as the CPython interpreter does not support multi-core execution through multithreading. It will not allow you to use the extra CPU cores.


1 Answers

If you are using MKL then try

export MKL_NUM_THREADS=1

For Numpy with OpenBLAS:

export OPENBLAS_NUM_THREADS=1

For some versions of Numpy this variation has been suggested:

export NUMEXPR_NUM_THREADS=1

The environment variable has to be set before the script is run (setting inside the script itself does not have the desired effect). For setting threads at runtime see: Set max number of threads at runtime on numpy/openblas

See the following for identifying how your numpy is setup: How to check blas/lapack linkage in numpy/scipy?

like image 155
filitchp Avatar answered Sep 27 '22 20:09

filitchp