Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache TBB local executors #2203

Merged
merged 6 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 9 additions & 8 deletions catboost/python-package/catboost/_catboost.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ from util.generic.array_ref cimport TArrayRef, TConstArrayRef
from util.generic.hash cimport THashMap
from util.generic.hash_set cimport THashSet
from util.generic.maybe cimport TMaybe
from util.generic.ptr cimport THolder, TIntrusivePtr, MakeHolder
from util.generic.ptr cimport TAtomicSharedPtr, THolder, TIntrusivePtr, MakeHolder
from util.generic.string cimport TString, TStringBuf
from util.generic.vector cimport TVector
from util.system.types cimport ui8, ui16, ui32, ui64, i32, i64
Expand Down Expand Up @@ -411,13 +411,13 @@ cdef extern from "catboost/private/libs/options/load_options.h" namespace "NCatb

cdef class Py_ObjectsOrderBuilderVisitor:
cdef TDataProviderBuilderOptions options
cdef THolder[TTbbLocalExecutor] local_executor
cdef TAtomicSharedPtr[TTbbLocalExecutor] local_executor
cdef THolder[IDataProviderBuilder] data_provider_builder
cdef IRawObjectsOrderDataVisitor* builder_visitor
cdef const TFeaturesLayout* features_layout

def __cinit__(self, int thread_count):
self.local_executor = MakeHolder[TTbbLocalExecutor](thread_count)
self.local_executor = GetCachedLocalExecutor(thread_count)
CreateDataProviderBuilderAndVisitor(
self.options,
<ILocalExecutor*>self.local_executor.Get(),
Expand All @@ -440,13 +440,13 @@ cdef class Py_ObjectsOrderBuilderVisitor:

cdef class Py_FeaturesOrderBuilderVisitor:
cdef TDataProviderBuilderOptions options
cdef THolder[TTbbLocalExecutor] local_executor
cdef TAtomicSharedPtr[TTbbLocalExecutor] local_executor
cdef THolder[IDataProviderBuilder] data_provider_builder
cdef IRawFeaturesOrderDataVisitor* builder_visitor
cdef const TFeaturesLayout* features_layout

def __cinit__(self, int thread_count):
self.local_executor = MakeHolder[TTbbLocalExecutor](thread_count)
self.local_executor = GetCachedLocalExecutor(thread_count)
CreateDataProviderBuilderAndVisitor(
self.options,
<ILocalExecutor*>self.local_executor.Get(),
Expand Down Expand Up @@ -980,6 +980,7 @@ cdef extern from "catboost/python-package/catboost/helpers.h":
int threadCount,
ui64 cpuUsedRamLimit
) except +ProcessException
cdef TAtomicSharedPtr[TTbbLocalExecutor] GetCachedLocalExecutor(int threadsCount)


cdef extern from "catboost/python-package/catboost/helpers.h":
Expand Down Expand Up @@ -4238,7 +4239,7 @@ cdef class _PoolBase:
feature matrix : np.ndarray of shape (object_count, feature_count)
"""
cdef int thread_count = UpdateThreadCount(-1)
cdef THolder[TTbbLocalExecutor] local_executor = MakeHolder[TTbbLocalExecutor](thread_count)
cdef TAtomicSharedPtr[TTbbLocalExecutor] local_executor = GetCachedLocalExecutor(thread_count)
cdef TFeaturesLayout* features_layout =self.__pool.Get()[0].MetaInfo.FeaturesLayout.Get()
cdef TRawObjectsDataProvider* raw_objects_data_provider = dynamic_cast_to_TRawObjectsDataProvider(
self.__pool.Get()[0].ObjectsData.Get()
Expand Down Expand Up @@ -5620,7 +5621,7 @@ cdef class _StagedPredictIterator:
cdef TVector[TVector[double]] __approx
cdef TVector[TVector[double]] __pred
cdef TFullModel* __model
cdef THolder[TTbbLocalExecutor] __executor
cdef TAtomicSharedPtr[TTbbLocalExecutor] __executor
cdef TModelCalcerOnPool* __modelCalcerOnPool
cdef EPredictionType predictionType
cdef int ntree_start, ntree_end, eval_period, thread_count
Expand All @@ -5633,7 +5634,7 @@ cdef class _StagedPredictIterator:
self.eval_period = eval_period
self.thread_count = UpdateThreadCount(thread_count)
self.verbose = verbose
self.__executor = MakeHolder[TTbbLocalExecutor](thread_count)
self.__executor = GetCachedLocalExecutor(thread_count)

cdef _initialize_model_calcer(self, TFullModel* model, _PoolBase pool):
self.__model = model
Expand Down
26 changes: 26 additions & 0 deletions catboost/python-package/catboost/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#include <catboost/private/libs/options/split_params.h>
#include <catboost/private/libs/target/data_providers.h>

#include <util/system/guard.h>
#include <util/system/info.h>
#include <util/system/mutex.h>


extern "C" PyObject* PyCatboostExceptionType;

Expand Down Expand Up @@ -295,3 +299,25 @@ void TrainEvalSplit(
*evalDataProvider = getSubset(postShuffleTestIndices);
}
}

TAtomicSharedPtr<NPar::TTbbLocalExecutor<false>> GetCachedLocalExecutor(int threadsCount) {
static TMutex lock;
static TAtomicSharedPtr<NPar::TTbbLocalExecutor<false>> cachedExecutor;

CB_ENSURE(threadsCount == -1 || 0 < threadsCount, "threadsCount should be positive or -1");

if (threadsCount == -1) {
threadsCount = NSystemInfo::CachedNumberOfCpus();
}

with_lock (lock) {
if (cachedExecutor && cachedExecutor->GetThreadCount() + 1 == threadsCount) {
return cachedExecutor;
}

cachedExecutor.Reset();
cachedExecutor = MakeAtomicShared<NPar::TTbbLocalExecutor<false>>(threadsCount);

return cachedExecutor;
}
}
3 changes: 3 additions & 0 deletions catboost/python-package/catboost/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
#include <catboost/libs/train_lib/options_helper.h>

#include <library/cpp/json/json_value.h>
#include <library/cpp/threading/local_executor/tbb_local_executor.h>

#include <util/generic/array_ref.h>
#include <util/generic/fwd.h>
#include <util/generic/noncopyable.h>
#include <util/generic/ptr.h>
#include <util/generic/xrange.h>

#include <type_traits>
Expand Down Expand Up @@ -277,3 +279,4 @@ void TrainEvalSplit(
);


TAtomicSharedPtr<NPar::TTbbLocalExecutor<false>> GetCachedLocalExecutor(int threadsCount);