Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control number of processes in GaussianMixture

There are no n_jobs parameter for GaussianMixture. Meanwhile, whenever I fit the model

from sklearn.mixture import GaussianMixture as GMM
gmm = GMM(n_components=4,
          init_params='random',
          covariance_type='full',
          tol=1e-2,
          max_iter=100,
          n_init=1)
gmm.fit(X, y)

it spans 16 processes and uses full CPU power of my 16 CPUs machine. I do not want for it to be doing that.

In comparison, Kmeans has n_jobs parameter that controls mutliprocessing when having multiple initializations (n_init > 1). Here multiprocessing comes out of the blue.

My question is where its coming from and how to control it?

like image 906
tarashypka Avatar asked Oct 27 '25 07:10

tarashypka


1 Answers

You are observing parallel-processing in terms of basic algebraic operations, speed up by BLAS/LAPACK.

Modifying this is not as simple as setting a n_jobs parameter and depends on your implementation in use!

Common candidates are ATLAS, OpenBLAS and Intel's MKL.

I recommend checking which one is used first, then act accordingly:

import numpy as np
np.__config__.show()

Sadly these things can get tricky. A valid environment for MKL for example can look like this (source):

export MKL_NUM_THREADS="2"
export MKL_DOMAIN_NUM_THREADS="MKL_BLAS=2"
export OMP_NUM_THREADS="1"
export MKL_DYNAMIC="FALSE"
export OMP_DYNAMIC="FALSE"

For ATLAS, it seems, you define this at compile-time.

And according to this answer, the same applies to OpenBLAS.

As OP tested, it seems you can get away with setting environment-variables for OpenMP, effecting in modification of behaviour even for the open-source candidates Atlas and OpenBLAS (where a compile-time limit is the alternative):

export OMP_NUM_THREADS="4";
like image 88
sascha Avatar answered Oct 28 '25 21:10

sascha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!