未验证 提交 46b68e20 编写于 作者: C Cai Yudong 提交者: GitHub

fix codacy warnings (#3308)

* improve code quality
Signed-off-by: Nyudong.cai <yudong.cai@zilliz.com>

* fix code quality warnings
Signed-off-by: Nyudong.cai <yudong.cai@zilliz.com>

* fix clang-format
Signed-off-by: Nyudong.cai <yudong.cai@zilliz.com>

* use vector reference instead
Signed-off-by: Nyudong.cai <yudong.cai@zilliz.com>

* clean codacy warnings
Signed-off-by: Nyudong.cai <yudong.cai@zilliz.com>

* clean codacy warnings
Signed-off-by: Nyudong.cai <yudong.cai@zilliz.com>
上级 b045200c
......@@ -582,7 +582,6 @@ ExecutionEngineImpl::ProcessRangeQuery(const std::unordered_map<std::string, Dat
SegmentPtr segment_ptr;
segment_reader_->GetSegment(segment_ptr);
auto status = Status::OK();
auto range_query_json = range_query->json_obj;
JSON_NULL_CHECK(range_query_json);
auto range_it = range_query_json.begin();
......@@ -604,7 +603,6 @@ ExecutionEngineImpl::BuildIndex() {
auto segment_visitor = segment_reader_->GetSegmentVisitor();
auto& snapshot = segment_visitor->GetSnapshot();
auto collection = snapshot->GetCollection();
auto& segment = segment_visitor->GetSegment();
snapshot::OperationContext context;
......
......@@ -258,10 +258,10 @@ MemSegment::Serialize(uint64_t wal_lsn) {
return status;
}
status = operation_->CommitRowCount(segment_writer_ptr_->RowCount());
status = operation_->Push();
STATUS_CHECK(operation_->CommitRowCount(segment_writer_ptr_->RowCount()));
STATUS_CHECK(operation_->Push());
LOG_ENGINE_DEBUG_ << "New segment " << segment_->GetID() << " serialized, lsn = " << wal_lsn;
return status;
return Status::OK();
}
int64_t
......
......@@ -60,8 +60,8 @@ MockEngine::QueryNoLock(const MetaQueryContext& context, AttrsMapList& attrs) {
auto& candidate_raws = resources_[context.table_];
bool selected = true;
if (!context.filter_attrs_.empty()) {
bool selected = true;
for (auto& raw : candidate_raws) {
for (auto& filter_attr : context.filter_attrs_) {
auto iter = raw.find(filter_attr.first);
......
......@@ -860,7 +860,6 @@ Status
CreatePartitionOperation::DoExecute(StorePtr store) {
STATUS_CHECK(CheckStale());
auto collection = GetAdjustedSS()->GetCollection();
auto partition = context_.new_partition;
if (context_.new_partition) {
......@@ -946,53 +945,55 @@ CreateCollectionOperation::DoExecute(StorePtr store) {
auto& field_schema = field_kv.first;
auto& field_elements = field_kv.second;
FieldPtr field;
status = store->CreateResource<Field>(
Field(field_schema->GetName(), field_idx, field_schema->GetFtype(), field_schema->GetParams()), field);
STATUS_CHECK(store->CreateResource<Field>(
Field(field_schema->GetName(), field_idx, field_schema->GetFtype(), field_schema->GetParams()), field));
auto f_ctx_p = ResourceContextBuilder<Field>().SetOp(meta::oUpdate).CreatePtr();
AddStepWithLsn(*field, c_context_.lsn, f_ctx_p);
MappingT element_ids = {};
FieldElementPtr raw_element;
status = store->CreateResource<FieldElement>(FieldElement(collection->GetID(), field->GetID(), ELEMENT_RAW_DATA,
FieldElementType::FET_RAW, ELEMENT_RAW_DATA),
raw_element);
STATUS_CHECK(
store->CreateResource<FieldElement>(FieldElement(collection->GetID(), field->GetID(), ELEMENT_RAW_DATA,
FieldElementType::FET_RAW, ELEMENT_RAW_DATA),
raw_element));
auto fe_ctx_p = ResourceContextBuilder<FieldElement>().SetOp(meta::oUpdate).CreatePtr();
AddStepWithLsn(*raw_element, c_context_.lsn, fe_ctx_p);
element_ids.insert(raw_element->GetID());
for (auto& element_schema : field_elements) {
FieldElementPtr element;
status = store->CreateResource<FieldElement>(
STATUS_CHECK(store->CreateResource<FieldElement>(
FieldElement(collection->GetID(), field->GetID(), element_schema->GetName(), element_schema->GetFtype(),
element_schema->GetTypeName()),
element);
element));
auto t_fe_ctx_p = ResourceContextBuilder<FieldElement>().SetOp(meta::oUpdate).CreatePtr();
AddStepWithLsn(*element, c_context_.lsn, t_fe_ctx_p);
element_ids.insert(element->GetID());
}
FieldCommitPtr field_commit;
status = store->CreateResource<FieldCommit>(FieldCommit(collection->GetID(), field->GetID(), element_ids),
field_commit);
STATUS_CHECK(store->CreateResource<FieldCommit>(FieldCommit(collection->GetID(), field->GetID(), element_ids),
field_commit));
auto fc_ctx_p = ResourceContextBuilder<FieldCommit>().SetOp(meta::oUpdate).CreatePtr();
AddStepWithLsn(*field_commit, c_context_.lsn, fc_ctx_p);
field_commit_ids.insert(field_commit->GetID());
}
SchemaCommitPtr schema_commit;
status = store->CreateResource<SchemaCommit>(SchemaCommit(collection->GetID(), field_commit_ids), schema_commit);
STATUS_CHECK(
store->CreateResource<SchemaCommit>(SchemaCommit(collection->GetID(), field_commit_ids), schema_commit));
auto sc_ctx_p = ResourceContextBuilder<SchemaCommit>().SetOp(meta::oUpdate).CreatePtr();
AddStepWithLsn(*schema_commit, c_context_.lsn, sc_ctx_p);
PartitionPtr partition;
status = store->CreateResource<Partition>(Partition("_default", collection->GetID()), partition);
STATUS_CHECK(store->CreateResource<Partition>(Partition("_default", collection->GetID()), partition));
auto p_ctx_p = ResourceContextBuilder<Partition>().SetOp(meta::oUpdate).CreatePtr();
AddStepWithLsn(*partition, c_context_.lsn, p_ctx_p);
context_.new_partition = partition;
PartitionCommitPtr partition_commit;
status = store->CreateResource<PartitionCommit>(PartitionCommit(collection->GetID(), partition->GetID()),
partition_commit);
STATUS_CHECK(store->CreateResource<PartitionCommit>(PartitionCommit(collection->GetID(), partition->GetID()),
partition_commit));
auto pc_ctx_p = ResourceContextBuilder<PartitionCommit>().SetOp(meta::oUpdate).CreatePtr();
AddStepWithLsn(*partition_commit, c_context_.lsn, pc_ctx_p);
context_.new_partition_commit = partition_commit;
CollectionCommitPtr collection_commit;
status = store->CreateResource<CollectionCommit>(
CollectionCommit(collection->GetID(), schema_commit->GetID(), {partition_commit->GetID()}), collection_commit);
STATUS_CHECK(store->CreateResource<CollectionCommit>(
CollectionCommit(collection->GetID(), schema_commit->GetID(), {partition_commit->GetID()}), collection_commit));
auto cc_ctx_p = ResourceContextBuilder<CollectionCommit>().SetOp(meta::oUpdate).CreatePtr();
AddStepWithLsn(*collection_commit, c_context_.lsn, cc_ctx_p);
context_.new_collection_commit = collection_commit;
......
......@@ -27,7 +27,7 @@ LogOut(const char* pattern, ...) {
va_list vl;
va_start(vl, pattern);
vsnprintf(str_p.get(), len, pattern, vl);
vsnprintf(str_p.get(), len, pattern, vl); // NOLINT
va_end(vl);
return std::string(str_p.get());
......
......@@ -265,6 +265,5 @@ endif ()
target_link_libraries(test_structured_index_sort ${depend_libs} ${unittest_libs} ${basic_libs})
install(TARGETS test_structured_index_sort DESTINATION unittest)
#add_subdirectory(faiss_ori)
#add_subdirectory(faiss_benchmark)
#add_subdirectory(metric_alg_benchmark)
......@@ -56,32 +56,32 @@ elapsed() {
}
void
normalize(float* arr, size_t nq, size_t dim) {
for (size_t i = 0; i < nq; i++) {
normalize(float* arr, int32_t nq, int32_t dim) {
for (int32_t i = 0; i < nq; i++) {
double vecLen = 0.0, inv_vecLen = 0.0;
for (size_t j = 0; j < dim; j++) {
for (int32_t j = 0; j < dim; j++) {
double val = arr[i * dim + j];
vecLen += val * val;
}
inv_vecLen = 1.0 / std::sqrt(vecLen);
for (size_t j = 0; j < dim; j++) {
for (int32_t j = 0; j < dim; j++) {
arr[i * dim + j] = (float)(arr[i * dim + j] * inv_vecLen);
}
}
}
void*
hdf5_read(const std::string& file_name, const std::string& dataset_name, H5T_class_t dataset_class, size_t& d_out,
size_t& n_out) {
hdf5_read(const std::string& file_name, const std::string& dataset_name, H5T_class_t dataset_class, int32_t& d_out,
int32_t& n_out) {
hid_t file, dataset, datatype, dataspace, memspace;
H5T_class_t t_class; /* data type class */
hsize_t dimsm[3]; /* memory space dimensions */
hsize_t dims_out[2]; /* dataset dimensions */
hsize_t count[2]; /* size of the hyperslab in the file */
hsize_t offset[2]; /* hyperslab offset in the file */
hsize_t count_out[3]; /* size of the hyperslab in memory */
hsize_t offset_out[3]; /* hyperslab offset in memory */
void* data_out; /* output buffer */
H5T_class_t t_class; /* data type class */
hsize_t dimsm[3]; /* memory space dimensions */
hsize_t dims_out[2]; /* dataset dimensions */
hsize_t count[2]; /* size of the hyperslab in the file */
hsize_t offset[2]; /* hyperslab offset in the file */
hsize_t count_out[3]; /* size of the hyperslab in memory */
hsize_t offset_out[3]; /* hyperslab offset in memory */
void* data_out = nullptr; /* output buffer */
/* Open the file and the dataset. */
file = H5Fopen(file_name.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
......@@ -152,7 +152,7 @@ get_index_file_name(const std::string& ann_test_name, const std::string& index_k
}
bool
parse_ann_test_name(const std::string& ann_test_name, size_t& dim, faiss::MetricType& metric_type) {
parse_ann_test_name(const std::string& ann_test_name, int32_t& dim, faiss::MetricType& metric_type) {
size_t pos1, pos2;
if (ann_test_name.empty())
......@@ -179,14 +179,14 @@ parse_ann_test_name(const std::string& ann_test_name, size_t& dim, faiss::Metric
}
int32_t
GetResultHitCount(const faiss::Index::idx_t* ground_index, const faiss::Index::idx_t* index, size_t ground_k, size_t k,
size_t nq, int32_t index_add_loops) {
size_t min_k = std::min(ground_k, k);
GetResultHitCount(const faiss::Index::idx_t* ground_index, const faiss::Index::idx_t* index, int32_t ground_k,
int32_t k, int32_t nq, int32_t index_add_loops) {
int32_t min_k = std::min(ground_k, k);
int hit = 0;
for (int i = 0; i < nq; i++) {
for (int32_t i = 0; i < nq; i++) {
std::set<faiss::Index::idx_t> ground(ground_index + i * ground_k,
ground_index + i * ground_k + min_k / index_add_loops);
for (int j = 0; j < min_k; j++) {
for (int32_t j = 0; j < min_k; j++) {
faiss::Index::idx_t id = index[i * k + j];
if (ground.count(id) > 0) {
hit++;
......@@ -198,7 +198,7 @@ GetResultHitCount(const faiss::Index::idx_t* ground_index, const faiss::Index::i
#if DEBUG_VERBOSE
void
print_array(const char* header, bool is_integer, const void* arr, size_t nq, size_t k) {
print_array(const char* header, bool is_integer, const void* arr, int32_t nq, int32_t k) {
const int ROW = 10;
const int COL = 10;
assert(ROW <= nq);
......@@ -221,7 +221,7 @@ print_array(const char* header, bool is_integer, const void* arr, size_t nq, siz
void
load_base_data(faiss::Index*& index, const std::string& ann_test_name, const std::string& index_key,
faiss::gpu::StandardGpuResources& res, const faiss::MetricType metric_type, const size_t dim,
faiss::gpu::StandardGpuResources& res, const faiss::MetricType metric_type, const int32_t dim,
int32_t index_add_loops, QueryMode mode = MODE_CPU) {
double t0 = elapsed();
......@@ -236,7 +236,7 @@ load_base_data(faiss::Index*& index, const std::string& ann_test_name, const std
printf("[%.3f s] Reading index file: %s\n", elapsed() - t0, index_file_name.c_str());
cpu_index = faiss::read_index(index_file_name.c_str());
} catch (...) {
size_t nb, d;
int32_t nb, d;
printf("[%.3f s] Loading HDF5 file: %s\n", elapsed() - t0, ann_file_name.c_str());
float* xb = (float*)hdf5_read(ann_file_name, HDF5_DATASET_TRAIN, H5T_FLOAT, d, nb);
assert(d == dim || !"dataset does not have correct dimension");
......@@ -246,21 +246,21 @@ load_base_data(faiss::Index*& index, const std::string& ann_test_name, const std
normalize(xb, nb, d);
}
printf("[%.3f s] Creating CPU index \"%s\" d=%ld\n", elapsed() - t0, index_key.c_str(), d);
printf("[%.3f s] Creating CPU index \"%s\" d=%d\n", elapsed() - t0, index_key.c_str(), d);
cpu_index = faiss::index_factory(d, index_key.c_str(), metric_type);
printf("[%.3f s] Cloning CPU index to GPU\n", elapsed() - t0);
gpu_index = faiss::gpu::index_cpu_to_gpu(&res, GPU_DEVICE_IDX, cpu_index);
delete cpu_index;
printf("[%.3f s] Training on %ld vectors\n", elapsed() - t0, nb);
printf("[%.3f s] Training on %d vectors\n", elapsed() - t0, nb);
gpu_index->train(nb, xb);
// add index multiple times to get ~1G data set
for (int i = 0; i < index_add_loops; i++) {
printf("[%.3f s] No.%d Indexing database, size %ld*%ld\n", elapsed() - t0, i, nb, d);
printf("[%.3f s] No.%d Indexing database, size %d*%d\n", elapsed() - t0, i, nb, d);
std::vector<faiss::Index::idx_t> xids(nb);
for (int t = 0; t < nb; t++) {
for (int32_t t = 0; t < nb; t++) {
xids[t] = i * nb + t;
}
gpu_index->add_with_ids(nb, xb, xids.data());
......@@ -286,10 +286,10 @@ load_base_data(faiss::Index*& index, const std::string& ann_test_name, const std
}
void
load_query_data(faiss::Index::distance_t*& xq, size_t& nq, const std::string& ann_test_name,
const faiss::MetricType metric_type, const size_t dim) {
load_query_data(faiss::Index::distance_t*& xq, int32_t& nq, const std::string& ann_test_name,
const faiss::MetricType metric_type, const int32_t dim) {
double t0 = elapsed();
size_t d;
int32_t d;
const std::string ann_file_name = ann_test_name + HDF5_POSTFIX;
......@@ -303,16 +303,16 @@ load_query_data(faiss::Index::distance_t*& xq, size_t& nq, const std::string& an
}
void
load_ground_truth(faiss::Index::idx_t*& gt, size_t& k, const std::string& ann_test_name, const size_t nq) {
load_ground_truth(faiss::Index::idx_t*& gt, int32_t& k, const std::string& ann_test_name, const int32_t nq) {
const std::string ann_file_name = ann_test_name + HDF5_POSTFIX;
// load ground-truth and convert int to long
size_t nq2;
int32_t nq2;
int* gt_int = (int*)hdf5_read(ann_file_name, HDF5_DATASET_NEIGHBORS, H5T_INTEGER, k, nq2);
assert(nq2 == nq || !"incorrect nb of ground truth index");
gt = new faiss::Index::idx_t[k * nq];
for (int i = 0; i < k * nq; i++) {
for (int32_t i = 0; i < k * nq; i++) {
gt[i] = gt_int[i];
}
delete[] gt_int;
......@@ -335,19 +335,19 @@ load_ground_truth(faiss::Index::idx_t*& gt, size_t& k, const std::string& ann_te
void
test_with_nprobes(const std::string& ann_test_name, const std::string& index_key, faiss::Index* cpu_index,
faiss::gpu::StandardGpuResources& res, const QueryMode query_mode, const faiss::Index::distance_t* xq,
const faiss::Index::idx_t* gt, const std::vector<size_t> nprobes, const int32_t index_add_loops,
const faiss::Index::idx_t* gt, const std::vector<int32_t>& nprobes, const int32_t index_add_loops,
const int32_t search_loops) {
double t0 = elapsed();
const std::vector<size_t> NQ = {10, 100};
const std::vector<size_t> K = {10, 100, 1000};
const size_t GK = 100; // topk of ground truth
const std::vector<int32_t> NQ = {10, 100};
const std::vector<int32_t> K = {10, 100, 1000};
const int32_t GK = 100; // topk of ground truth
std::unordered_map<size_t, std::string> mode_str_map = {
std::unordered_map<int32_t, std::string> mode_str_map = {
{MODE_CPU, "MODE_CPU"}, {MODE_MIX, "MODE_MIX"}, {MODE_GPU, "MODE_GPU"}};
double copy_time = 0.0;
faiss::Index *gpu_index, *index;
faiss::Index *gpu_index = nullptr, *index = nullptr;
if (query_mode != MODE_CPU) {
faiss::gpu::GpuClonerOptions option;
option.allInGpu = true;
......@@ -402,6 +402,8 @@ test_with_nprobes(const std::string& ann_test_name, const std::string& index_key
delete cpu_index;
index = gpu_index;
break;
default:
break;
}
} else {
index = cpu_index;
......@@ -429,13 +431,13 @@ test_with_nprobes(const std::string& ann_test_name, const std::string& index_key
faiss::Index::idx_t* I = new faiss::Index::idx_t[NQ.back() * K.back()];
faiss::Index::distance_t* D = new faiss::Index::distance_t[NQ.back() * K.back()];
printf("\n%s | %s - %s | nprobe=%lu\n", ann_test_name.c_str(), index_key.c_str(),
printf("\n%s | %s - %s | nprobe=%d\n", ann_test_name.c_str(), index_key.c_str(),
mode_str_map[query_mode].c_str(), nprobe);
printf("======================================================================================\n");
for (size_t j = 0; j < K.size(); j++) {
size_t t_k = K[j];
int32_t t_k = K[j];
for (size_t i = 0; i < NQ.size(); i++) {
size_t t_nq = NQ[i];
int32_t t_nq = NQ[i];
faiss::indexIVF_stats.quantization_time = 0.0;
faiss::indexIVF_stats.search_time = 0.0;
......@@ -456,7 +458,7 @@ test_with_nprobes(const std::string& ann_test_name, const std::string& index_key
// k = 100 for ground truth
int32_t hit = GetResultHitCount(gt, I, GK, t_k, t_nq, index_add_loops);
printf("nq = %4ld, k = %4ld, elapse = %.4fs (quant = %.4fs, search = %.4fs), R@ = %.4f\n", t_nq, t_k,
printf("nq = %4d, k = %4d, elapse = %.4fs (quant = %.4fs, search = %.4fs), R@ = %.4f\n", t_nq, t_k,
(t_end - t_start) / search_loops, faiss::indexIVF_stats.quantization_time / 1000 / search_loops,
faiss::indexIVF_stats.search_time / 1000 / search_loops,
(hit / float(t_nq * std::min(GK, t_k) / index_add_loops)));
......@@ -473,14 +475,14 @@ test_with_nprobes(const std::string& ann_test_name, const std::string& index_key
void
test_ann_hdf5(const std::string& ann_test_name, const std::string& cluster_type, const std::string& index_type,
const QueryMode query_mode, int32_t index_add_loops, const std::vector<size_t>& nprobes,
const QueryMode query_mode, int32_t index_add_loops, const std::vector<int32_t>& nprobes,
int32_t search_loops) {
double t0 = elapsed();
faiss::gpu::StandardGpuResources res;
faiss::MetricType metric_type;
size_t dim;
int32_t dim;
if (query_mode == MODE_MIX && index_type != "SQ8Hybrid") {
assert(index_type == "SQ8Hybrid" || !"Only SQ8Hybrid support MODE_MIX");
......@@ -494,7 +496,7 @@ test_ann_hdf5(const std::string& ann_test_name, const std::string& cluster_type,
return;
}
size_t nq, k;
int32_t nq, k;
faiss::Index* index;
faiss::Index::distance_t* xq;
faiss::Index::idx_t* gt; // ground-truth index
......@@ -505,7 +507,7 @@ test_ann_hdf5(const std::string& ann_test_name, const std::string& cluster_type,
printf("[%.3f s] Loading queries\n", elapsed() - t0);
load_query_data(xq, nq, ann_test_name, metric_type, dim);
printf("[%.3f s] Loading ground truth for %ld queries\n", elapsed() - t0, nq);
printf("[%.3f s] Loading ground truth for %d queries\n", elapsed() - t0, nq);
load_ground_truth(gt, k, ann_test_name, nq);
test_with_nprobes(ann_test_name, index_key, index, res, query_mode, xq, gt, nprobes, index_add_loops, search_loops);
......@@ -530,7 +532,7 @@ test_ann_hdf5(const std::string& ann_test_name, const std::string& cluster_type,
*************************************************************************************/
TEST(FAISSTEST, BENCHMARK) {
std::vector<size_t> param_nprobes = {8, 128};
std::vector<int32_t> param_nprobes = {8, 128};
const int32_t SEARCH_LOOPS = 5;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
......
......@@ -64,32 +64,32 @@ elapsed() {
}
void
normalize(float* arr, size_t nq, size_t dim) {
for (size_t i = 0; i < nq; i++) {
normalize(float* arr, int32_t nq, int32_t dim) {
for (int32_t i = 0; i < nq; i++) {
double vecLen = 0.0, inv_vecLen = 0.0;
for (size_t j = 0; j < dim; j++) {
for (int32_t j = 0; j < dim; j++) {
double val = arr[i * dim + j];
vecLen += val * val;
}
inv_vecLen = 1.0 / std::sqrt(vecLen);
for (size_t j = 0; j < dim; j++) {
for (int32_t j = 0; j < dim; j++) {
arr[i * dim + j] = (float)(arr[i * dim + j] * inv_vecLen);
}
}
}
void*
hdf5_read(const std::string& file_name, const std::string& dataset_name, H5T_class_t dataset_class, size_t& d_out,
size_t& n_out) {
hdf5_read(const std::string& file_name, const std::string& dataset_name, H5T_class_t dataset_class, int32_t& d_out,
int32_t& n_out) {
hid_t file, dataset, datatype, dataspace, memspace;
H5T_class_t t_class; /* data type class */
hsize_t dimsm[3]; /* memory space dimensions */
hsize_t dims_out[2]; /* dataset dimensions */
hsize_t count[2]; /* size of the hyperslab in the file */
hsize_t offset[2]; /* hyperslab offset in the file */
hsize_t count_out[3]; /* size of the hyperslab in memory */
hsize_t offset_out[3]; /* hyperslab offset in memory */
void* data_out; /* output buffer */
H5T_class_t t_class; /* data type class */
hsize_t dimsm[3]; /* memory space dimensions */
hsize_t dims_out[2]; /* dataset dimensions */
hsize_t count[2]; /* size of the hyperslab in the file */
hsize_t offset[2]; /* hyperslab offset in the file */
hsize_t count_out[3]; /* size of the hyperslab in memory */
hsize_t offset_out[3]; /* hyperslab offset in memory */
void* data_out = nullptr; /* output buffer */
/* Open the file and the dataset. */
file = H5Fopen(file_name.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
......@@ -160,7 +160,7 @@ get_index_file_name(const std::string& ann_test_name, const std::string& index_k
}
bool
parse_ann_test_name(const std::string& ann_test_name, size_t& dim, faiss::MetricType& metric_type) {
parse_ann_test_name(const std::string& ann_test_name, int32_t& dim, faiss::MetricType& metric_type) {
size_t pos1, pos2;
if (ann_test_name.empty())
......@@ -187,14 +187,14 @@ parse_ann_test_name(const std::string& ann_test_name, size_t& dim, faiss::Metric
}
int32_t
GetResultHitCount(const faiss::Index::idx_t* ground_index, const faiss::Index::idx_t* index, size_t ground_k, size_t k,
size_t nq, int32_t index_add_loops) {
size_t min_k = std::min(ground_k, k);
GetResultHitCount(const faiss::Index::idx_t* ground_index, const faiss::Index::idx_t* index, int32_t ground_k,
int32_t k, int32_t nq, int32_t index_add_loops) {
int32_t min_k = std::min(ground_k, k);
int hit = 0;
for (int i = 0; i < nq; i++) {
for (int32_t i = 0; i < nq; i++) {
std::set<faiss::Index::idx_t> ground(ground_index + i * ground_k,
ground_index + i * ground_k + min_k / index_add_loops);
for (int j = 0; j < min_k; j++) {
for (int32_t j = 0; j < min_k; j++) {
faiss::Index::idx_t id = index[i * k + j];
if (ground.count(id) > 0) {
hit++;
......@@ -206,7 +206,7 @@ GetResultHitCount(const faiss::Index::idx_t* ground_index, const faiss::Index::i
#if DEBUG_VERBOSE
void
print_array(const char* header, bool is_integer, const void* arr, size_t nq, size_t k) {
print_array(const char* header, bool is_integer, const void* arr, int32_t nq, int32_t k) {
const int ROW = 10;
const int COL = 10;
assert(ROW <= nq);
......@@ -229,7 +229,7 @@ print_array(const char* header, bool is_integer, const void* arr, size_t nq, siz
void
load_base_data(faiss::Index*& index, const std::string& ann_test_name, const std::string& index_key,
faiss::gpu::StandardGpuResources& res, const faiss::MetricType metric_type, const size_t dim,
faiss::gpu::StandardGpuResources& res, const faiss::MetricType metric_type, const int32_t dim,
int32_t index_add_loops, QueryMode mode = MODE_CPU) {
double t0 = elapsed();
......@@ -244,7 +244,7 @@ load_base_data(faiss::Index*& index, const std::string& ann_test_name, const std
printf("[%.3f s] Reading index file: %s\n", elapsed() - t0, index_file_name.c_str());
cpu_index = faiss::read_index(index_file_name.c_str());
} catch (...) {
size_t nb, d;
int32_t nb, d;
printf("[%.3f s] Loading HDF5 file: %s\n", elapsed() - t0, ann_file_name.c_str());
float* xb = (float*)hdf5_read(ann_file_name, HDF5_DATASET_TRAIN, H5T_FLOAT, d, nb);
assert(d == dim || !"dataset does not have correct dimension");
......@@ -254,21 +254,21 @@ load_base_data(faiss::Index*& index, const std::string& ann_test_name, const std
normalize(xb, nb, d);
}
printf("[%.3f s] Creating CPU index \"%s\" d=%ld\n", elapsed() - t0, index_key.c_str(), d);
printf("[%.3f s] Creating CPU index \"%s\" d=%d\n", elapsed() - t0, index_key.c_str(), d);
cpu_index = faiss::index_factory(d, index_key.c_str(), metric_type);
printf("[%.3f s] Cloning CPU index to GPU\n", elapsed() - t0);
gpu_index = faiss::gpu::index_cpu_to_gpu(&res, GPU_DEVICE_IDX, cpu_index);
delete cpu_index;
printf("[%.3f s] Training on %ld vectors\n", elapsed() - t0, nb);
printf("[%.3f s] Training on %d vectors\n", elapsed() - t0, nb);
gpu_index->train(nb, xb);
// add index multiple times to get ~1G data set
for (int i = 0; i < index_add_loops; i++) {
printf("[%.3f s] No.%d Indexing database, size %ld*%ld\n", elapsed() - t0, i, nb, d);
printf("[%.3f s] No.%d Indexing database, size %d*%d\n", elapsed() - t0, i, nb, d);
std::vector<faiss::Index::idx_t> xids(nb);
for (int t = 0; t < nb; t++) {
for (int32_t t = 0; t < nb; t++) {
xids[t] = i * nb + t;
}
gpu_index->add_with_ids(nb, xb, xids.data());
......@@ -294,10 +294,10 @@ load_base_data(faiss::Index*& index, const std::string& ann_test_name, const std
}
void
load_query_data(faiss::Index::distance_t*& xq, size_t& nq, const std::string& ann_test_name,
const faiss::MetricType metric_type, const size_t dim) {
load_query_data(faiss::Index::distance_t*& xq, int32_t& nq, const std::string& ann_test_name,
const faiss::MetricType metric_type, const int32_t dim) {
double t0 = elapsed();
size_t d;
int32_t d;
const std::string ann_file_name = ann_test_name + HDF5_POSTFIX;
......@@ -311,16 +311,16 @@ load_query_data(faiss::Index::distance_t*& xq, size_t& nq, const std::string& an
}
void
load_ground_truth(faiss::Index::idx_t*& gt, size_t& k, const std::string& ann_test_name, const size_t nq) {
load_ground_truth(faiss::Index::idx_t*& gt, int32_t& k, const std::string& ann_test_name, const int32_t nq) {
const std::string ann_file_name = ann_test_name + HDF5_POSTFIX;
// load ground-truth and convert int to long
size_t nq2;
int32_t nq2;
int* gt_int = (int*)hdf5_read(ann_file_name, HDF5_DATASET_NEIGHBORS, H5T_INTEGER, k, nq2);
assert(nq2 == nq || !"incorrect nb of ground truth index");
gt = new faiss::Index::idx_t[k * nq];
for (int i = 0; i < k * nq; i++) {
for (int32_t i = 0; i < k * nq; i++) {
gt[i] = gt_int[i];
}
delete[] gt_int;
......@@ -341,7 +341,7 @@ load_ground_truth(faiss::Index::idx_t*& gt, size_t& k, const std::string& ann_te
}
faiss::ConcurrentBitsetPtr
CreateBitset(size_t size, int32_t percentage) {
CreateBitset(int32_t size, int32_t percentage) {
if (percentage < 0 || percentage > 100) {
assert(false);
}
......@@ -349,7 +349,7 @@ CreateBitset(size_t size, int32_t percentage) {
faiss::ConcurrentBitsetPtr bitset_ptr = std::make_shared<faiss::ConcurrentBitset>(size);
if (percentage != 0) {
int32_t step = 100 / percentage;
for (int64_t i = 0; i < size; i += step) {
for (int32_t i = 0; i < size; i += step) {
bitset_ptr->set(i);
}
}
......@@ -359,22 +359,22 @@ CreateBitset(size_t size, int32_t percentage) {
void
test_with_nprobes(const std::string& ann_test_name, const std::string& index_key, faiss::Index* cpu_index,
faiss::gpu::StandardGpuResources& res, const QueryMode query_mode, const faiss::Index::distance_t* xq,
const faiss::Index::idx_t* gt, const std::vector<size_t> nprobes, const int32_t index_add_loops,
const faiss::Index::idx_t* gt, const std::vector<int32_t>& nprobes, const int32_t index_add_loops,
const int32_t search_loops) {
double t0 = elapsed();
const std::vector<size_t> NQ = {100};
const std::vector<size_t> K = {100};
const size_t GK = 100; // topk of ground truth
const std::vector<int32_t> NQ = {100};
const std::vector<int32_t> K = {100};
const int32_t GK = 100; // topk of ground truth
std::unordered_map<size_t, std::string> mode_str_map = {
std::unordered_map<int32_t, std::string> mode_str_map = {
{MODE_CPU, "MODE_CPU"}, {MODE_MIX, "MODE_MIX"}, {MODE_GPU, "MODE_GPU"}};
double copy_time = 0.0;
faiss::Index *gpu_index, *index;
faiss::Index *gpu_index = nullptr, *index = nullptr;
if (query_mode != MODE_CPU) {
faiss::gpu::GpuClonerOptions option;
option.allInGpu = true;
double copy_time = 0.0;
faiss::IndexComposition index_composition;
index_composition.index = cpu_index;
......@@ -426,6 +426,8 @@ test_with_nprobes(const std::string& ann_test_name, const std::string& index_key
delete cpu_index;
index = gpu_index;
break;
default:
break;
}
} else {
index = cpu_index;
......@@ -461,11 +463,11 @@ test_with_nprobes(const std::string& ann_test_name, const std::string& index_key
faiss::Index::distance_t* D = new faiss::Index::distance_t[NQ.back() * K.back()];
for (size_t j = 0; j < K.size(); j++) {
size_t t_k = K[j];
int32_t t_k = K[j];
for (size_t i = 0; i < NQ.size(); i++) {
size_t t_nq = NQ[i];
int32_t t_nq = NQ[i];
printf("\n%s | %s - %s | nq = %4ld, k = %4ld, nprobe=%lu\n", ann_test_name.c_str(), index_key.c_str(),
printf("\n%s | %s - %s | nq = %4d, k = %4d, nprobe=%d\n", ann_test_name.c_str(), index_key.c_str(),
mode_str_map[query_mode].c_str(), t_nq, t_k, nprobe);
printf("================================================================================\n");
for (size_t s = 0; s < bitset_array.size(); s++) {
......@@ -508,14 +510,14 @@ test_with_nprobes(const std::string& ann_test_name, const std::string& index_key
void
test_ann_hdf5(const std::string& ann_test_name, const std::string& cluster_type, const std::string& index_type,
const QueryMode query_mode, int32_t index_add_loops, const std::vector<size_t>& nprobes,
const QueryMode query_mode, int32_t index_add_loops, const std::vector<int32_t>& nprobes,
int32_t search_loops) {
double t0 = elapsed();
faiss::gpu::StandardGpuResources res;
faiss::MetricType metric_type;
size_t dim;
int32_t dim;
if (query_mode == MODE_MIX && index_type != "SQ8Hybrid") {
assert(index_type == "SQ8Hybrid" || !"Only SQ8Hybrid support MODE_MIX");
......@@ -529,7 +531,7 @@ test_ann_hdf5(const std::string& ann_test_name, const std::string& cluster_type,
return;
}
size_t nq, k;
int32_t nq, k;
faiss::Index* index;
faiss::Index::distance_t* xq;
faiss::Index::idx_t* gt; // ground-truth index
......@@ -540,7 +542,7 @@ test_ann_hdf5(const std::string& ann_test_name, const std::string& cluster_type,
printf("[%.3f s] Loading queries\n", elapsed() - t0);
load_query_data(xq, nq, ann_test_name, metric_type, dim);
printf("[%.3f s] Loading ground truth for %ld queries\n", elapsed() - t0, nq);
printf("[%.3f s] Loading ground truth for %d queries\n", elapsed() - t0, nq);
load_ground_truth(gt, k, ann_test_name, nq);
test_with_nprobes(ann_test_name, index_key, index, res, query_mode, xq, gt, nprobes, index_add_loops, search_loops);
......@@ -565,7 +567,7 @@ test_ann_hdf5(const std::string& ann_test_name, const std::string& cluster_type,
*************************************************************************************/
TEST(FAISSTEST, BENCHMARK) {
std::vector<size_t> param_nprobes = {32, 128};
std::vector<int32_t> param_nprobes = {32, 128};
const int32_t SEARCH_LOOPS = 1;
const int32_t SIFT_INSERT_LOOPS = 1;
......
if (MILVUS_GPU_VERSION)
include_directories(${INDEX_SOURCE_DIR}/thirdparty)
include_directories(${INDEX_SOURCE_DIR}/include)
set(unittest_libs
gtest gmock gtest_main gmock_main)
set(depend_libs
faiss
)
if (FAISS_WITH_MKL)
set(depend_libs ${depend_libs}
"-Wl,--start-group \
${MKL_LIB_PATH}/libmkl_intel_ilp64.a \
${MKL_LIB_PATH}/libmkl_gnu_thread.a \
${MKL_LIB_PATH}/libmkl_core.a \
-Wl,--end-group -lgomp -lpthread -lm -ldl"
)
else ()
set(depend_libs ${depend_libs}
${OpenBLAS_LIBRARIES}
${LAPACK_LIBRARIES}
)
endif ()
set(basic_libs
gomp gfortran pthread
)
include_directories(${CUDA_INCLUDE_DIRS})
link_directories("${CUDA_TOOLKIT_ROOT_DIR}/lib64")
set(cuda_lib
cudart
cublas
)
set(basic_libs ${basic_libs}
${cuda_lib}
)
#<GPU-TEST>
if (NOT TARGET test_gpu)
add_executable(test_gpu gpuresource_test.cpp)
endif ()
target_link_libraries(test_gpu ${depend_libs} ${unittest_libs} ${basic_libs})
install(TARGETS test_gpu DESTINATION unittest)
endif ()
\ No newline at end of file
// 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 <gtest/gtest.h>
#include <faiss/AutoTune.h>
#include <faiss/Index.h>
#include <faiss/gpu/GpuAutoTune.h>
#include <faiss/gpu/GpuIndexIVFFlat.h>
#include <faiss/gpu/StandardGpuResources.h>
#include <faiss/index_io.h>
#include <chrono>
#include <iostream>
#include <thread>
class TestGpuRes {
public:
TestGpuRes() {
res_ = new faiss::gpu::StandardGpuResources;
}
~TestGpuRes() {
delete res_;
delete index_;
}
std::shared_ptr<faiss::Index>
Do() {
int d = 128; // dimension
int nb = 100000; // database size
int nq = 100; // nb of queries
int nlist = 1638;
float* xb = new float[d * nb];
float* xq = new float[d * nq];
for (int i = 0; i < nb; i++) {
for (int j = 0; j < d; j++) xb[d * i + j] = drand48();
xb[d * i] += i / 1000.;
}
for (int i = 0; i < nq; i++) {
for (int j = 0; j < d; j++) xq[d * i + j] = drand48();
xq[d * i] += i / 1000.;
}
index_ = new faiss::gpu::GpuIndexIVFFlat(res_, d, nlist, faiss::METRIC_L2);
index_->train(nb, xb);
index_->add(nb, xb);
std::shared_ptr<faiss::Index> host_index = nullptr;
host_index.reset(faiss::gpu::index_gpu_to_cpu(index_));
return host_index;
}
private:
faiss::gpu::GpuResources* res_ = nullptr;
faiss::Index* index_ = nullptr;
};
TEST(gpuresource, resource) {
TestGpuRes t;
t.Do();
}
TEST(test, resource_re) {
int d = 128; // dimension
int nb = 1000000; // database size
int nq = 100; // nb of queries
int nlist = 16384;
int k = 100;
float* xb = new float[d * nb];
float* xq = new float[d * nq];
for (int i = 0; i < nb; i++) {
for (int j = 0; j < d; j++) xb[d * i + j] = drand48();
xb[d * i] += i / 1000.;
}
for (int i = 0; i < nq; i++) {
for (int j = 0; j < d; j++) xq[d * i + j] = drand48();
xq[d * i] += i / 1000.;
}
auto elems = nq * k;
auto res_ids = (int64_t*)malloc(sizeof(int64_t) * elems);
auto res_dis = (float*)malloc(sizeof(float) * elems);
faiss::gpu::StandardGpuResources res;
auto cpu_index = faiss::index_factory(d, "IVF16384, Flat");
auto device_index = faiss::gpu::index_cpu_to_gpu(&res, 0, cpu_index);
device_index->train(nb, xb);
device_index->add(nb, xb);
auto new_index = faiss::gpu::index_gpu_to_cpu(device_index);
delete device_index;
std::cout << "start clone" << std::endl;
auto load = [&] {
std::cout << "start" << std::endl;
faiss::gpu::StandardGpuResources res;
// res.noTempMemory();
for (int l = 0; l < 100; ++l) {
auto x = faiss::gpu::index_cpu_to_gpu(&res, 1, new_index);
delete x;
}
std::cout << "load finish" << std::endl;
};
auto search = [&] {
faiss::gpu::StandardGpuResources res;
auto device_index = faiss::gpu::index_cpu_to_gpu(&res, 1, new_index);
std::cout << "search start" << std::endl;
for (int l = 0; l < 10000; ++l) {
device_index->search(nq, xq, 10, res_dis, res_ids);
}
std::cout << "search finish" << std::endl;
delete device_index;
delete cpu_index;
};
load();
search();
std::thread t1(search);
std::this_thread::sleep_for(std::chrono::seconds(1));
std::thread t2(load);
t1.join();
t2.join();
std::cout << "finish clone" << std::endl;
// std::this_thread::sleep_for(5s);
//
// auto device_index_2 = faiss::gpu::index_cpu_to_gpu(&res, 1, cpu_index);
// device_index->train(nb, xb);
// device_index->add(nb, xb);
// std::cout << "finish clone" << std::endl;
// std::this_thread::sleep_for(5s);
// std::this_thread::sleep_for(2s);
// std::cout << "start clone" << std::endl;
// auto new_index = faiss::clone_index(device_index);
// std::cout << "start search" << std::endl;
// new_index->search(nq, xq, k, res_dis, res_ids);
// std::cout << "start clone" << std::endl;
//{
// faiss::gpu::StandardGpuResources res;
// auto cpu_index = faiss::index_factory(d, "IVF1638, Flat");
// auto device_index = faiss::gpu::index_cpu_to_gpu(&res, 1, cpu_index);
// device_index->train(nb, xb);
// device_index->add(nb, xb);
// std::cout << "finish clone" << std::endl;
// delete device_index;
// delete cpu_index;
// std::cout << "finish clone" << std::endl;
//}
//
// std::cout << "finish clone" << std::endl;
}
......@@ -397,9 +397,9 @@ TEST(METRICTEST, BENCHMARK) {
GenerateData(DIM, NQ, xq.data());
std::vector<float> distance_faiss(NB * NQ);
std::vector<float> distance_nsg(NB * NQ);
// std::vector<float> distance_nsg(NB * NQ);
std::vector<float> distance_annoy(NB * NQ);
std::vector<float> distance_hnsw(NB * NQ);
// std::vector<float> distance_hnsw(NB * NQ);
std::cout << "==========" << std::endl;
TestMetricAlg(func_map, "FAISS::L2", LOOP, distance_faiss.data(), NB, xb.data(), NQ, xq.data(), DIM);
......
......@@ -118,7 +118,7 @@ TEST_P(KnowhereWrapperTest, BASE_TEST) {
fiu_enable("VecIndexImpl.BuildAll.throw_knowhere_exception", 1, nullptr, 0);
fiu_enable("BFIndex.BuildAll.throw_knowhere_exception", 1, nullptr, 0);
fiu_enable("IVFMixIndex.BuildAll.throw_knowhere_exception", 1, nullptr, 0);
auto s = index_->BuildAll(nb, xb.data(), ids.data(), conf);
index_->BuildAll(nb, xb.data(), ids.data(), conf);
fiu_disable("IVFMixIndex.BuildAll.throw_knowhere_exception");
fiu_disable("BFIndex.BuildAll.throw_knowhere_exception");
fiu_disable("VecIndexImpl.BuildAll.throw_knowhere_exception");
......@@ -126,25 +126,25 @@ TEST_P(KnowhereWrapperTest, BASE_TEST) {
fiu_enable("VecIndexImpl.BuildAll.throw_std_exception", 1, nullptr, 0);
fiu_enable("BFIndex.BuildAll.throw_std_exception", 1, nullptr, 0);
fiu_enable("IVFMixIndex.BuildAll.throw_std_exception", 1, nullptr, 0);
s = index_->BuildAll(nb, xb.data(), ids.data(), conf);
index_->BuildAll(nb, xb.data(), ids.data(), conf);
fiu_disable("IVFMixIndex.BuildAll.throw_std_exception");
fiu_disable("BFIndex.BuildAll.throw_std_exception");
fiu_disable("VecIndexImpl.BuildAll.throw_std_exception");
fiu_enable("VecIndexImpl.Add.throw_knowhere_exception", 1, nullptr, 0);
s = index_->Add(nb, xb.data(), ids.data());
index_->Add(nb, xb.data(), ids.data());
fiu_disable("VecIndexImpl.Add.throw_knowhere_exception");
fiu_enable("VecIndexImpl.Add.throw_std_exception", 1, nullptr, 0);
s = index_->Add(nb, xb.data(), ids.data());
index_->Add(nb, xb.data(), ids.data());
fiu_disable("VecIndexImpl.Add.throw_std_exception");
fiu_enable("VecIndexImpl.Search.throw_knowhere_exception", 1, nullptr, 0);
s = index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
fiu_disable("VecIndexImpl.Search.throw_knowhere_exception");
fiu_enable("VecIndexImpl.Search.throw_std_exception", 1, nullptr, 0);
s = index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
fiu_disable("VecIndexImpl.Search.throw_std_exception");
}
}
......
......@@ -41,13 +41,13 @@ SystemInfo::Init() {
// initialize CPU information
try {
struct tms time_sample;
char line[128];
last_cpu_ = times(&time_sample);
last_sys_cpu_ = time_sample.tms_stime;
last_user_cpu_ = time_sample.tms_utime;
num_processors_ = 0;
FILE* file = fopen("/proc/cpuinfo", "r");
if (file) {
char line[128];
while (fgets(line, 128, file) != nullptr) {
if (strncmp(line, "processor", 9) == 0) {
num_processors_++;
......@@ -202,7 +202,7 @@ SystemInfo::getTotalCpuTime(std::vector<int64_t>& work_time_array) {
return total_time_array;
}
sscanf(buffer, "cpu %16lu %16lu %16lu %16lu %16lu %16lu %16lu %16lu %16lu %16lu", &user, &nice, &system,
sscanf(buffer, "cpu %16ld %16ld %16ld %16ld %16ld %16ld %16ld %16ld %16ld %16ld", &user, &nice, &system,
&idle, &iowait, &irq, &softirq, &steal, &guest, &guestnice);
work_time_array.push_back(user + nice + system);
......
......@@ -47,15 +47,14 @@ FaissFlatPass::Run(const TaskPtr& task) {
ResourcePtr res_ptr;
if (!gpu_enable_) {
LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissFlatPass: gpu disable, specify cpu to search!", "search", 0);
LOG_SERVER_DEBUG_ << LogOut("FaissFlatPass: gpu disable, specify cpu to search!");
res_ptr = ResMgrInst::GetInstance()->GetResource("cpu");
} else if (search_task->nq() < (int64_t)threshold_) {
LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissFlatPass: nq < gpu_search_threshold, specify cpu to search!",
"search", 0);
LOG_SERVER_DEBUG_ << LogOut("FaissFlatPass: nq < gpu_search_threshold, specify cpu to search!");
res_ptr = ResMgrInst::GetInstance()->GetResource("cpu");
} else {
LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissFlatPass: nq >= gpu_search_threshold, specify gpu %d to search!",
"search", 0, search_gpus_[idx_]);
LOG_SERVER_DEBUG_ << LogOut("FaissFlatPass: nq >= gpu_search_threshold, specify gpu %d to search!",
search_gpus_[idx_]);
res_ptr = ResMgrInst::GetInstance()->GetResource(ResourceType::GPU, search_gpus_[idx_]);
idx_ = (idx_ + 1) % search_gpus_.size();
}
......
......@@ -48,15 +48,14 @@ FaissIVFFlatPass::Run(const TaskPtr& task) {
ResourcePtr res_ptr;
if (!gpu_enable_) {
LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissIVFFlatPass: gpu disable, specify cpu to search!", "search", 0);
LOG_SERVER_DEBUG_ << LogOut("FaissIVFFlatPass: gpu disable, specify cpu to search!");
res_ptr = ResMgrInst::GetInstance()->GetResource("cpu");
} else if (search_task->nq() < (uint64_t)threshold_) {
LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissIVFFlatPass: nq < gpu_search_threshold, specify cpu to search!",
"search", 0);
LOG_SERVER_DEBUG_ << LogOut("FaissIVFFlatPass: nq < gpu_search_threshold, specify cpu to search!");
res_ptr = ResMgrInst::GetInstance()->GetResource("cpu");
} else {
LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissIVFFlatPass: nq >= gpu_search_threshold, specify gpu %d to search!",
"search", 0, search_gpus_[idx_]);
LOG_SERVER_DEBUG_ << LogOut("FaissIVFFlatPass: nq >= gpu_search_threshold, specify gpu %d to search!",
search_gpus_[idx_]);
res_ptr = ResMgrInst::GetInstance()->GetResource(ResourceType::GPU, search_gpus_[idx_]);
idx_ = (idx_ + 1) % search_gpus_.size();
}
......
......@@ -50,15 +50,14 @@ FaissIVFPQPass::Run(const TaskPtr& task) {
ResourcePtr res_ptr;
if (!gpu_enable_) {
LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissIVFPQPass: gpu disable, specify cpu to search!", "search", 0);
LOG_SERVER_DEBUG_ << LogOut("FaissIVFPQPass: gpu disable, specify cpu to search!");
res_ptr = ResMgrInst::GetInstance()->GetResource("cpu");
} else if (search_task->nq() < (uint64_t)threshold_) {
LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissIVFPQPass: nq < gpu_search_threshold, specify cpu to search!",
"search", 0);
LOG_SERVER_DEBUG_ << LogOut("FaissIVFPQPass: nq < gpu_search_threshold, specify cpu to search!");
res_ptr = ResMgrInst::GetInstance()->GetResource("cpu");
} else {
LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissIVFPQPass: nq >= gpu_search_threshold, specify gpu %d to search!",
"search", 0, search_gpus_[idx_]);
LOG_SERVER_DEBUG_ << LogOut("FaissIVFPQPass: nq >= gpu_search_threshold, specify gpu %d to search!",
search_gpus_[idx_]);
res_ptr = ResMgrInst::GetInstance()->GetResource(ResourceType::GPU, search_gpus_[idx_]);
idx_ = (idx_ + 1) % search_gpus_.size();
}
......
......@@ -47,16 +47,15 @@ FaissIVFSQ8HPass::Run(const TaskPtr& task) {
ResourcePtr res_ptr;
if (!gpu_enable_) {
LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissIVFSQ8HPass: gpu disable, specify cpu to search!", "search", 0);
res_ptr = ResMgrInst::GetInstance()->GetResource("cpu");
LOG_SERVER_ERROR_ << LogOut("FaissIVFSQ8HPass: SQ8H index only works with gpu enabled!");
return false;
}
if (search_task->nq() < (uint64_t)threshold_) {
LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissIVFSQ8HPass: nq < gpu_search_threshold, specify cpu to search!",
"search", 0);
LOG_SERVER_DEBUG_ << LogOut("FaissIVFSQ8HPass: nq < gpu_search_threshold, specify cpu to search!");
res_ptr = ResMgrInst::GetInstance()->GetResource("cpu");
} else {
LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissIVFSQ8HPass: nq >= gpu_search_threshold, specify gpu %d to search!",
"search", 0, search_gpus_[idx_]);
LOG_SERVER_DEBUG_ << LogOut("FaissIVFSQ8HPass: nq >= gpu_search_threshold, specify gpu %d to search!",
search_gpus_[idx_]);
res_ptr = ResMgrInst::GetInstance()->GetResource(ResourceType::GPU, search_gpus_[idx_]);
idx_ = (idx_ + 1) % search_gpus_.size();
}
......
......@@ -48,15 +48,14 @@ FaissIVFSQ8Pass::Run(const TaskPtr& task) {
ResourcePtr res_ptr;
if (!gpu_enable_) {
LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissIVFSQ8Pass: gpu disable, specify cpu to search!", "search", 0);
LOG_SERVER_DEBUG_ << LogOut("FaissIVFSQ8Pass: gpu disable, specify cpu to search!");
res_ptr = ResMgrInst::GetInstance()->GetResource("cpu");
} else if (search_task->nq() < (uint64_t)threshold_) {
LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissIVFSQ8Pass: nq < gpu_search_threshold, specify cpu to search!",
"search", 0);
LOG_SERVER_DEBUG_ << LogOut("FaissIVFSQ8Pass: nq < gpu_search_threshold, specify cpu to search!");
res_ptr = ResMgrInst::GetInstance()->GetResource("cpu");
} else {
LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissIVFSQ8Pass: nq >= gpu_search_threshold, specify gpu %d to search!",
"search", 0, search_gpus_[idx_]);
LOG_SERVER_DEBUG_ << LogOut("FaissIVFSQ8Pass: nq >= gpu_search_threshold, specify gpu %d to search!",
search_gpus_[idx_]);
res_ptr = ResMgrInst::GetInstance()->GetResource(ResourceType::GPU, search_gpus_[idx_]);
idx_ = (idx_ + 1) % search_gpus_.size();
}
......
......@@ -44,7 +44,7 @@ CompactReq::OnExecute() {
TimeRecorderAuto rc(hdr);
bool exist = false;
auto status = DBWrapper::DB()->HasCollection(collection_name_, exist);
STATUS_CHECK(DBWrapper::DB()->HasCollection(collection_name_, exist));
if (!exist) {
return Status(SERVER_COLLECTION_NOT_EXIST, "Collection not exist: " + collection_name_);
}
......
......@@ -40,7 +40,7 @@ CountEntitiesReq::OnExecute() {
TimeRecorderAuto rc(hdr);
bool exist = false;
auto status = DBWrapper::DB()->HasCollection(collection_name_, exist);
STATUS_CHECK(DBWrapper::DB()->HasCollection(collection_name_, exist));
if (!exist) {
return Status(SERVER_COLLECTION_NOT_EXIST, "Collection not exist: " + collection_name_);
}
......
......@@ -54,7 +54,7 @@ CreatePartitionReq::OnExecute() {
}
bool exist = false;
status = DBWrapper::DB()->HasCollection(collection_name_, exist);
STATUS_CHECK(DBWrapper::DB()->HasCollection(collection_name_, exist));
if (!exist) {
return Status(SERVER_COLLECTION_NOT_EXIST, "Collection not exist: " + collection_name_);
}
......
......@@ -47,7 +47,7 @@ DeleteEntityByIDReq::OnExecute() {
TimeRecorderAuto rc("DeleteEntityByIDReq");
bool exist = false;
auto status = DBWrapper::DB()->HasCollection(collection_name_, exist);
STATUS_CHECK(DBWrapper::DB()->HasCollection(collection_name_, exist));
if (!exist) {
return Status(SERVER_COLLECTION_NOT_EXIST, "Collection not exist: " + collection_name_);
}
......
......@@ -50,19 +50,12 @@ DescribeIndexReq::OnExecute() {
TimeRecorderAuto rc(hdr);
// step 1: check arguments
auto status = ValidateCollectionName(collection_name_);
if (!status.ok()) {
return status;
}
status = ValidateFieldName(field_name_);
if (!status.ok()) {
return status;
}
STATUS_CHECK(ValidateCollectionName(collection_name_));
STATUS_CHECK(ValidateFieldName(field_name_));
// only process root collection, ignore partition collection
engine::CollectionIndex index;
status = DBWrapper::DB()->DescribeIndex(collection_name_, field_name_, index);
auto status = DBWrapper::DB()->DescribeIndex(collection_name_, field_name_, index);
if (!status.ok()) {
if (status.code() == DB_NOT_FOUND) {
return Status(SERVER_COLLECTION_NOT_EXIST, "Collection not exist: " + collection_name_);
......
......@@ -36,7 +36,7 @@ DropCollectionReq::OnExecute() {
STATUS_CHECK(ValidateCollectionName(collection_name_));
bool exist = false;
auto status = DBWrapper::DB()->HasCollection(collection_name_, exist);
STATUS_CHECK(DBWrapper::DB()->HasCollection(collection_name_, exist));
if (!exist) {
return Status(SERVER_COLLECTION_NOT_EXIST, "Collection not exist: " + collection_name_);
}
......
......@@ -45,13 +45,13 @@ DropIndexReq::OnExecute() {
TimeRecorderAuto rc(hdr);
bool exist = false;
auto status = DBWrapper::DB()->HasCollection(collection_name_, exist);
STATUS_CHECK(DBWrapper::DB()->HasCollection(collection_name_, exist));
if (!exist) {
return Status(SERVER_COLLECTION_NOT_EXIST, "Collection not exist: " + collection_name_);
}
// step 2: drop index
status = DBWrapper::DB()->DropIndex(collection_name_, field_name_);
auto status = DBWrapper::DB()->DropIndex(collection_name_, field_name_);
fiu_do_on("DropIndexReq.OnExecute.drop_index_fail", status = Status(milvus::SERVER_UNEXPECTED_ERROR, ""));
if (!status.ok()) {
return status;
......
......@@ -47,7 +47,7 @@ DropPartitionReq::OnExecute() {
/* check collection */
bool exist = false;
auto status = DBWrapper::DB()->HasCollection(collection_name_, exist);
STATUS_CHECK(DBWrapper::DB()->HasCollection(collection_name_, exist));
if (!exist) {
return Status(SERVER_COLLECTION_NOT_EXIST, "Collection not exist: " + collection_name_);
}
......
......@@ -45,7 +45,6 @@ FlushReq::OnExecute() {
}
TimeRecorderAuto rc(hdr);
Status status = Status::OK();
LOG_SERVER_DEBUG_ << hdr;
// flush all collections
......
......@@ -46,7 +46,7 @@ GetCollectionStatsReq::OnExecute() {
STATUS_CHECK(ValidateCollectionName(collection_name_));
bool exist = false;
auto status = DBWrapper::DB()->HasCollection(collection_name_, exist);
STATUS_CHECK(DBWrapper::DB()->HasCollection(collection_name_, exist));
if (!exist) {
return Status(SERVER_COLLECTION_NOT_EXIST, "Collection not exist: " + collection_name_);
}
......
......@@ -61,7 +61,7 @@ InsertReq::OnExecute() {
// step 1: check collection existence
bool exist = false;
auto status = DBWrapper::DB()->HasCollection(collection_name_, exist);
STATUS_CHECK(DBWrapper::DB()->HasCollection(collection_name_, exist));
if (!exist) {
return Status(SERVER_COLLECTION_NOT_EXIST, "Collection not exist: " + collection_name_);
}
......@@ -76,7 +76,7 @@ InsertReq::OnExecute() {
}
// step 3: check insert data limitation
status = ValidateInsertDataSize(data_chunk);
auto status = ValidateInsertDataSize(data_chunk);
if (!status.ok()) {
LOG_SERVER_ERROR_ << LogOut("[%s][%d] Invalid vector data: %s", "insert", 0, status.message().c_str());
return status;
......
......@@ -40,7 +40,7 @@ ListPartitionsReq::OnExecute() {
/* check collection existence */
bool exist = false;
auto status = DBWrapper::DB()->HasCollection(collection_name_, exist);
STATUS_CHECK(DBWrapper::DB()->HasCollection(collection_name_, exist));
if (!exist) {
return Status(SERVER_COLLECTION_NOT_EXIST, "Collection not exist: " + collection_name_);
}
......
......@@ -137,16 +137,16 @@ CopyRowRecords(const google::protobuf::RepeatedPtrField<::milvus::grpc::VectorRo
std::vector<float> float_array(float_data_size, 0.0f);
std::vector<uint8_t> binary_array(binary_data_size, 0);
int64_t float_offset = 0, binary_offset = 0;
int64_t offset = 0;
if (float_data_size > 0) {
for (auto& record : grpc_records) {
memcpy(&float_array[float_offset], record.float_data().data(), record.float_data_size() * sizeof(float));
float_offset += record.float_data_size();
memcpy(&float_array[offset], record.float_data().data(), record.float_data_size() * sizeof(float));
offset += record.float_data_size();
}
} else if (binary_data_size > 0) {
for (auto& record : grpc_records) {
memcpy(&binary_array[binary_offset], record.binary_data().data(), record.binary_data().size());
binary_offset += record.binary_data().size();
memcpy(&binary_array[offset], record.binary_data().data(), record.binary_data().size());
offset += record.binary_data().size();
}
}
......
......@@ -74,12 +74,12 @@ namespace {
void
RemoveDirectory(const std::string& path) {
DIR* dir = nullptr;
struct dirent* dmsg;
const int32_t buf_size = 256;
char file_name[buf_size];
std::string folder_name = path + "/%s";
if ((dir = opendir(path.c_str())) != nullptr) {
struct dirent* dmsg;
while ((dmsg = readdir(dir)) != nullptr) {
if (strcmp(dmsg->d_name, ".") != 0 && strcmp(dmsg->d_name, "..") != 0) {
snprintf(file_name, buf_size, folder_name.c_str(), dmsg->d_name);
......
......@@ -42,9 +42,9 @@ StringHelpFunctions::SplitStringByDelimeter(const std::string& str, const std::s
return;
}
size_t prev = 0, pos = 0;
size_t prev = 0;
while (true) {
pos = str.find_first_of(delimeter, prev);
size_t pos = str.find_first_of(delimeter, prev);
if (pos == std::string::npos) {
result.emplace_back(str.substr(prev));
break;
......
......@@ -970,6 +970,7 @@ TEST_F(DBTest, StatsTest) {
// validate collection stats
milvus::json json_stats;
status = db_->GetCollectionStats(collection_name, json_stats);
ASSERT_TRUE(status.ok());
std::string ss = json_stats.dump();
ASSERT_FALSE(ss.empty());
......
......@@ -132,8 +132,8 @@ TEST_F(SnapshotTest, ResourceHoldersTest) {
auto prev_cnt = collection->ref_count();
{
auto collection_2 = CollectionsHolder::GetInstance().GetResource(collection_id, false);
ASSERT_EQ(collection->GetID(), collection_id);
ASSERT_EQ(collection->ref_count(), prev_cnt);
ASSERT_EQ(collection_2->GetID(), collection_id);
ASSERT_EQ(collection_2->ref_count(), prev_cnt);
}
{
......@@ -229,11 +229,13 @@ TEST_F(SnapshotTest, CreateCollectionOperationTest) {
ASSERT_FALSE(status.ok());
status = Snapshots::GetInstance().GetSnapshot(latest_ss, collection_name);
ASSERT_TRUE(status.ok());
ASSERT_TRUE(latest_ss);
ASSERT_TRUE(latest_ss->GetName() == collection_name);
IDS_TYPE ids;
status = Snapshots::GetInstance().GetCollectionIds(ids);
ASSERT_TRUE(status.ok());
ASSERT_EQ(ids.size(), 6);
ASSERT_EQ(ids.back(), latest_ss->GetCollectionId());
......@@ -997,7 +999,7 @@ TEST_F(SnapshotTest, OperationTest2) {
ASSERT_TRUE(seg_file);
auto prev_segment_commit = ss->GetSegmentCommitBySegmentId(seg_file->GetSegmentId());
auto prev_segment_commit_mappings = prev_segment_commit->GetMappings();
// auto prev_segment_commit_mappings = prev_segment_commit->GetMappings();
ASSERT_FALSE(prev_segment_commit->ToString().empty());
auto new_size = RandomInt(1000, 20000);
......@@ -1928,11 +1930,11 @@ TEST_F(SnapshotTest, CompoundTest2) {
}
struct GCSchedule {
static constexpr const char* Name = "GCSchedule";
// static constexpr const char* Name = "GCSchedule";
};
struct FlushSchedule {
static constexpr const char* Name = "FlushSchedule";
// static constexpr const char* Name = "FlushSchedule";
};
using IEventHandler = milvus::engine::snapshot::IEventHandler;
......
......@@ -47,7 +47,7 @@ milvus::PartitionParam
BuildPartitionParam(int32_t index) {
std::string tag = std::to_string(index);
std::string partition_name = std::string(COLLECTION_NAME) + "_" + tag;
milvus::PartitionParam partition_param = {COLLECTION_NAME, tag};
milvus::PartitionParam partition_param = {partition_name, tag};
return partition_param;
}
......
......@@ -72,7 +72,6 @@ ClientTest::Connect() {
conn = milvus::Connection::Create();
milvus::Status stat = conn->Connect(param);
if (!stat.ok()) {
std::string msg = "Connect function call status: " + stat.message();
std::cout << "Connect function call status: " << stat.message() << std::endl;
}
return conn;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册