I am trying to limit the number of CPUs' usage when I fit a model using sklearn RandomizedSearchCV
, but somehow I keep using all CPUs. Following an answer from Python scikit learn n_jobs I have seen that in scikit-learn, we can use n_jobs
to control the number of CPU-cores used.
n_jobs
is an integer, specifying the maximum number of concurrently running workers. If 1 is given, nojoblib
parallelism is used at all, which is useful for debugging. If set to -1, all CPUs are used.
Forn_jobs
below -1,(n_cpus + 1 + n_jobs)
are used. For example withn_jobs=-2
, all CPUs but one are used.
But when setting n_jobs
to -5 still all CPUs continue to run to 100%. I looked into joblib library to use Parallel
and delayed
. But still all my CPUs continue to be used. Here what I tried:
from sklearn.model_selection import RandomizedSearchCV
from joblib import Parallel,delayed
def rscv_l(model, param_grid, X_train, y_train):
rs_model = RandomizedSearchCV(model, param_grid, n_iter=10,
n_jobs=-5, verbose=2, cv=5,
scoring='r2')
rs_model.fit(X_train, y_train) # the cpu usage problem comes here
return rs_model
# Here my attempt to parallelize and set my function as iterable
results = Parallel( n_jobs = -5 )( delayed( rscv_l )( model,
param_grid,
X, y )
for X, y
in zip( [X_train],
[y_train] ) )
What is going wrong?
UPDATE: Looking at How do you stop numpy from multithreading?, I think I might have a multithreading problem. When I inspect numpy configuration I find:
blas_mkl_info:
libraries = ['mkl_rt', 'pthread']
library_dirs = ['user/lib']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
include_dirs = ['user/include']
blas_opt_info:
libraries = ['mkl_rt', 'pthread']
library_dirs = ['user/lib']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
include_dirs = ['user/include']
lapack_mkl_info:
libraries = ['mkl_rt', 'pthread']
library_dirs = ['user/lib']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
include_dirs = ['user/include']
lapack_opt_info:
libraries = ['mkl_rt', 'pthread']
library_dirs = ['user/lib']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
include_dirs = ['user/include']
but still the solutions proposed are not working for me:
import os
os.environ["OMP_NUM_THREADS"] = "4" # export OMP_NUM_THREADS=4
os.environ["OPENBLAS_NUM_THREADS"] = "4" # export OPENBLAS_NUM_THREADS=4
os.environ["MKL_NUM_THREADS"] = "6" # export MKL_NUM_THREADS=6
os.environ["VECLIB_MAXIMUM_THREADS"] = "4" # export VECLIB_MAXIMUM_THREADS=4
os.environ["NUMEXPR_NUM_THREADS"] = "6" # export NUMEXPR_NUM_THREADS=6
import numpy
from sklearn.model_selection import RandomizedSearchCV
THIS SOLVED MY PROBLEM: Thanks to @user3666197 answer, I decided to limit the number of cpus for the whole script and simply use n_jobs with a positive integer. This solved my CPU usage problem:
import os
n_jobs = 2 # The number of tasks to run in parallel
n_cpus = 2 # Number of CPUs assigned to this process
pid = os.getpid()
print("PID: %i" % pid)
# Control which CPUs are made available for this script
cpu_arg = ''.join([str(ci) + ',' for ci in list(range(n_cpus))])[:-1]
cmd = 'taskset -cp %s %i' % (cpu_arg, pid)
print("executing command '%s' ..." % cmd)
os.system(cmd)
# hyperparameter tunning
rs_model = RandomizedSearchCV(xgb, param_grid, n_iter=10,
n_jobs=n_jobs, verbose=2, cv= n_folds,
scoring='r2')
#model fitting
rs_model.fit(X_train,y_train)