Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
Add ut when single thread
Browse files Browse the repository at this point in the history
Signed-off-by: lixinguo <[email protected]>
  • Loading branch information
lixinguo committed Sep 30, 2022
1 parent bd86149 commit d10a209
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 3 deletions.
1 change: 1 addition & 0 deletions unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ endif ()

set( UTIL_SRCS
${KNOWHERE_SOURCE_DIR}/unittest/utils.cpp
${KNOWHERE_SOURCE_DIR}/unittest/ThreadChecker.cpp
)

set( ALL_INDEXING_SRCS
Expand Down
4 changes: 2 additions & 2 deletions unittest/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ constexpr int64_t K = 10;
constexpr int64_t PINMEM = 1024 * 1024 * 200;
constexpr int64_t TEMPMEM = 1024 * 1024 * 300;
constexpr int64_t RESNUM = 2;
constexpr int64_t BUILD_INDEX_OMP_NUM = 3;
constexpr int64_t QUERY_OMP_NUM = 4;
constexpr int64_t BUILD_INDEX_OMP_NUM = 17;
constexpr int64_t QUERY_OMP_NUM = 13;

class ParamGenerator {
public:
Expand Down
50 changes: 50 additions & 0 deletions unittest/ThreadChecker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under the License.

#include "unittest/ThreadChecker.h"

namespace knowhere::threadchecker {

#define MAX_THREAD_NUM 100000
#define MIN_THREAD_NUM 0
#define THREAD_LENGTH 7 // the length of max_thread_num +1

int32_t
GetBuildOmpThread(const Config& conf) {
return CheckKeyInConfig(conf, meta::BUILD_INDEX_OMP_NUM) ? GetMetaBuildIndexOmpNum(conf) : omp_get_max_threads();
}

int32_t
GetQueryOmpThread(const Config& conf) {
return CheckKeyInConfig(conf, meta::QUERY_OMP_NUM) ? GetMetaQueryOmpNum(conf) : omp_get_max_threads();
}

int32_t
GetThreadNum(int pid) {
std::string cmd = "ps -p " + std::to_string(pid) + " -Tf | wc -l";
FILE* file = popen(cmd.c_str(), "r");
char fBuff[THREAD_LENGTH];
if (fgets(fBuff, sizeof(fBuff), file) == nullptr) {
KNOWHERE_THROW_MSG("could not open the file to get thread number");
}
pclose(file);
const std::size_t len = strlen(fBuff);
if (len > 0 && fBuff[len - 1] == '\n') {
fBuff[len - 1] = '\0';
}
int32_t ans = atoi(fBuff);
if (ans < MIN_THREAD_NUM || ans > MAX_THREAD_NUM) {
KNOWHERE_THROW_MSG("thread number is out of control");
}
return ans;
}

} // namespace knowhere::threadchecker
32 changes: 32 additions & 0 deletions unittest/ThreadChecker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under the License.

#pragma once

#include <omp.h>
#include <stdio.h>
#include <unistd.h>

#include "common/Log.h"
#include "knowhere/index/vector_index/helpers/IndexParameter.h"

namespace knowhere::threadchecker {

int32_t
GetBuildOmpThread(const Config& conf);

int32_t
GetQueryOmpThread(const Config& conf);

int32_t
GetThreadNum(int pid);

} // namespace knowhere::threadchecker
12 changes: 12 additions & 0 deletions unittest/test_annoy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "knowhere/index/vector_index/adapter/VectorAdapter.h"
#include "knowhere/index/vector_index/helpers/IndexParameter.h"
#include "unittest/Helper.h"
#include "unittest/ThreadChecker.h"
#include "unittest/utils.h"

using ::testing::Combine;
Expand Down Expand Up @@ -146,3 +147,14 @@ TEST_P(AnnoyTest, annoy_slice) {
auto result = index_->Query(query_dataset, conf_, nullptr);
AssertAnns(result, nq, knowhere::GetMetaTopk(conf_));
}

TEST_P(AnnoyTest, annoy_omp) {
int pid = getpid();
int32_t num_threads_before_build = knowhere::threadchecker::GetThreadNum(pid);
index_->BuildAll(base_dataset, conf_);
int32_t num_threads_after_build = knowhere::threadchecker::GetThreadNum(pid);
EXPECT_GE(knowhere::threadchecker::GetBuildOmpThread(conf_), num_threads_after_build - num_threads_before_build + 1);
auto result = index_->Query(query_dataset, conf_, nullptr);
int32_t num_threads_after_query = knowhere::threadchecker::GetThreadNum(pid);
EXPECT_GE(knowhere::threadchecker::GetQueryOmpThread(conf_), num_threads_after_query - num_threads_after_build + 1);
}
12 changes: 12 additions & 0 deletions unittest/test_binaryidmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "knowhere/index/vector_index/adapter/VectorAdapter.h"
#include "unittest/Helper.h"
#include "unittest/range_utils.h"
#include "unittest/ThreadChecker.h"
#include "unittest/utils.h"

using ::testing::Combine;
Expand Down Expand Up @@ -268,3 +269,14 @@ TEST_P(BinaryIDMAPTest, binaryidmap_range_search_substructure) {
auto qd = knowhere::GenDataset(nq, dim, xq_bin.data());
ASSERT_ANY_THROW(index_->QueryByRange(qd, conf_, nullptr));
}

TEST_P(BinaryIDMAPTest, binaryidmap_omp) {
int pid = getpid();
int32_t num_threads_before_build = knowhere::threadchecker::GetThreadNum(pid);
index_->BuildAll(base_dataset, conf_);
int32_t num_threads_after_build = knowhere::threadchecker::GetThreadNum(pid);
EXPECT_GE(knowhere::threadchecker::GetBuildOmpThread(conf_), num_threads_after_build - num_threads_before_build + 1);
auto result = index_->Query(query_dataset, conf_, nullptr);
int32_t num_threads_after_query = knowhere::threadchecker::GetThreadNum(pid);
EXPECT_GE(knowhere::threadchecker::GetQueryOmpThread(conf_), num_threads_after_query - num_threads_after_build + 1);
}
12 changes: 12 additions & 0 deletions unittest/test_binaryivf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "knowhere/index/vector_index/adapter/VectorAdapter.h"
#include "unittest/Helper.h"
#include "unittest/range_utils.h"
#include "unittest/ThreadChecker.h"
#include "unittest/utils.h"

using ::testing::Combine;
Expand Down Expand Up @@ -228,3 +229,14 @@ TEST_P(BinaryIVFTest, binaryivf_range_search_substructure) {
knowhere::SetMetaMetricType(conf_, knowhere::metric::SUBSTRUCTURE);
ASSERT_ANY_THROW(index_->Train(base_dataset, conf_));
}

TEST_P(BinaryIVFTest, binaryivf_omp) {
int pid = getpid();
int32_t num_threads_before_build = knowhere::threadchecker::GetThreadNum(pid);
index_->BuildAll(base_dataset, conf_);
int32_t num_threads_after_build = knowhere::threadchecker::GetThreadNum(pid);
EXPECT_GE(knowhere::threadchecker::GetBuildOmpThread(conf_), num_threads_after_build - num_threads_before_build + 1);
auto result = index_->Query(query_dataset, conf_, nullptr);
int32_t num_threads_after_query = knowhere::threadchecker::GetThreadNum(pid);
EXPECT_GE(knowhere::threadchecker::GetQueryOmpThread(conf_), num_threads_after_query - num_threads_after_build + 1);
}
12 changes: 12 additions & 0 deletions unittest/test_hnsw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "knowhere/index/vector_index/helpers/IndexParameter.h"
#include "unittest/Helper.h"
#include "unittest/range_utils.h"
#include "unittest/ThreadChecker.h"
#include "unittest/utils.h"

using ::testing::Combine;
Expand Down Expand Up @@ -315,3 +316,14 @@ TEST_P(HNSWTest, HNSW_range_trace_visit) {

CheckFederResult(result, nb);
}

TEST_P(HNSWTest, HNSW_omp) {
int pid = getpid();
int32_t num_threads_before_build = knowhere::threadchecker::GetThreadNum(pid);
index_->BuildAll(base_dataset, conf_);
int32_t num_threads_after_build = knowhere::threadchecker::GetThreadNum(pid);
EXPECT_GE(knowhere::threadchecker::GetBuildOmpThread(conf_), num_threads_after_build - num_threads_before_build + 1);
auto result = index_->Query(query_dataset, conf_, nullptr);
int32_t num_threads_after_query = knowhere::threadchecker::GetThreadNum(pid);
EXPECT_GE(knowhere::threadchecker::GetQueryOmpThread(conf_), num_threads_after_query - num_threads_after_build + 1);
}
12 changes: 12 additions & 0 deletions unittest/test_idmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "knowhere/index/vector_index/helpers/FaissGpuResourceMgr.h"
#endif
#include "unittest/range_utils.h"
#include "unittest/ThreadChecker.h"
#include "unittest/utils.h"
#include "unittest/Helper.h"

Expand Down Expand Up @@ -258,6 +259,17 @@ TEST_P(IDMAPTest, idmap_range_search_ip) {
knowhere::KnowhereConfig::SetBlasThreshold(old_blas_threshold);
}

TEST_P(IDMAPTest, idmap_omp) {
int pid = getpid();
int32_t num_threads_before_build = knowhere::threadchecker::GetThreadNum(pid);
index_->BuildAll(base_dataset, conf_);
int32_t num_threads_after_build = knowhere::threadchecker::GetThreadNum(pid);
EXPECT_GE(knowhere::threadchecker::GetBuildOmpThread(conf_), num_threads_after_build - num_threads_before_build + 1);
auto result = index_->Query(query_dataset, conf_, nullptr);
int32_t num_threads_after_query = knowhere::threadchecker::GetThreadNum(pid);
EXPECT_GE(knowhere::threadchecker::GetQueryOmpThread(conf_), num_threads_after_query - num_threads_after_build + 1);
}

#ifdef KNOWHERE_GPU_VERSION
TEST_P(IDMAPTest, idmap_copy) {
ASSERT_TRUE(!xb.empty());
Expand Down
12 changes: 12 additions & 0 deletions unittest/test_ivf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include "unittest/Helper.h"
#include "unittest/range_utils.h"
#include "unittest/ThreadChecker.h"
#include "unittest/utils.h"

using ::testing::Combine;
Expand Down Expand Up @@ -216,6 +217,17 @@ TEST_P(IVFTest, ivf_range_search_ip) {
}
}

TEST_P(IVFTest, ivf_omp) {
int pid = getpid();
int32_t num_threads_before_build = knowhere::threadchecker::GetThreadNum(pid);
index_->BuildAll(base_dataset, conf_);
int32_t num_threads_after_build = knowhere::threadchecker::GetThreadNum(pid);
EXPECT_GE(knowhere::threadchecker::GetBuildOmpThread(conf_), num_threads_after_build - num_threads_before_build + 1);
auto result = index_->Query(query_dataset, conf_, nullptr);
int32_t num_threads_after_query = knowhere::threadchecker::GetThreadNum(pid);
EXPECT_GE(knowhere::threadchecker::GetQueryOmpThread(conf_), num_threads_after_query - num_threads_after_build + 1);
}

// TODO(linxj): deprecated
#ifdef KNOWHERE_GPU_VERSION
TEST_P(IVFTest, clone_test) {
Expand Down
15 changes: 14 additions & 1 deletion unittest/test_ivf_nm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "knowhere/utils/distances_simd.h"
#include "unittest/Helper.h"
#include "unittest/range_utils.h"
#include "unittest/ThreadChecker.h"
#include "unittest/utils.h"

using ::testing::Combine;
Expand Down Expand Up @@ -258,4 +259,16 @@ TEST_P(IVFNMTest, ivfnm_get_meta) {
knowhere::Config j2 = nlohmann::json::parse(json_id_set);
ASSERT_NO_THROW(nlohmann::from_json(j2, id_set));
std::cout << "id_set num = " << id_set.size() << std::endl;
}
}

TEST_P(IVFNMTest, ivfnm_omp) {
int pid = getpid();
int32_t num_threads_before_build = knowhere::threadchecker::GetThreadNum(pid);
index_->BuildAll(base_dataset, conf_);
int32_t num_threads_after_build = knowhere::threadchecker::GetThreadNum(pid);
EXPECT_GE(knowhere::threadchecker::GetBuildOmpThread(conf_), num_threads_after_build - num_threads_before_build + 1);
LoadRawData(index_, base_dataset, conf_);
auto result = index_->Query(query_dataset, conf_, nullptr);
int32_t num_threads_after_query = knowhere::threadchecker::GetThreadNum(pid);
EXPECT_GE(knowhere::threadchecker::GetQueryOmpThread(conf_), num_threads_after_query - num_threads_after_build + 1);
}

0 comments on commit d10a209

Please sign in to comment.