Skip to content

Commit

Permalink
Limit faiss ivf index build thread num (#136)
Browse files Browse the repository at this point in the history
Signed-off-by: chasingegg <[email protected]>
  • Loading branch information
chasingegg authored Oct 10, 2023
1 parent 6d743e2 commit 7144b89
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 8 additions & 2 deletions include/knowhere/comp/thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,14 @@ class ThreadPool {
int omp_before;

public:
explicit ScopedOmpSetter(int num_threads = 1) : omp_before(omp_get_max_threads()) {
omp_set_num_threads(num_threads);
explicit ScopedOmpSetter(int num_threads = 0) {
if (global_build_thread_pool_size_ == 0) { // this should not happen in prod
omp_before = omp_get_max_threads();
} else {
omp_before = global_build_thread_pool_size_;
}

omp_set_num_threads(num_threads <= 0 ? omp_before : num_threads);
}
~ScopedOmpSetter() {
omp_set_num_threads(omp_before);
Expand Down
4 changes: 4 additions & 0 deletions src/index/ivf/ivf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ IvfIndexNode<T>::Train(const DataSet& dataset, const Config& cfg) {
std::unique_ptr<ThreadPool::ScopedOmpSetter> setter;
if (base_cfg.num_build_thread.has_value()) {
setter = std::make_unique<ThreadPool::ScopedOmpSetter>(base_cfg.num_build_thread.value());
} else {
setter = std::make_unique<ThreadPool::ScopedOmpSetter>();
}

bool is_cosine = IsMetricType(base_cfg.metric_type.value(), knowhere::metric::COSINE);
Expand Down Expand Up @@ -356,6 +358,8 @@ IvfIndexNode<T>::Add(const DataSet& dataset, const Config& cfg) {
std::unique_ptr<ThreadPool::ScopedOmpSetter> setter;
if (base_cfg.num_build_thread.has_value()) {
setter = std::make_unique<ThreadPool::ScopedOmpSetter>(base_cfg.num_build_thread.value());
} else {
setter = std::make_unique<ThreadPool::ScopedOmpSetter>();
}
try {
if constexpr (std::is_same<faiss::IndexBinaryIVF, T>::value) {
Expand Down

0 comments on commit 7144b89

Please sign in to comment.