forked from ZJULearning/nsg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_nsg_optimized_search.cpp
92 lines (79 loc) · 2.73 KB
/
test_nsg_optimized_search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// Created by 付聪 on 2017/6/21.
//
#include <efanna2e/index_nsg.h>
#include <efanna2e/util.h>
#include <chrono>
#include <string>
void load_data(char* filename, float*& data, unsigned& num,
unsigned& dim) { // load data with sift10K pattern
std::ifstream in(filename, std::ios::binary);
if (!in.is_open()) {
std::cout << "open file error" << std::endl;
exit(-1);
}
in.read((char*)&dim, 4);
// std::cout<<"data dimension: "<<dim<<std::endl;
in.seekg(0, std::ios::end);
std::ios::pos_type ss = in.tellg();
size_t fsize = (size_t)ss;
num = (unsigned)(fsize / (dim + 1) / 4);
data = new float[(size_t)num * (size_t)dim];
in.seekg(0, std::ios::beg);
for (size_t i = 0; i < num; i++) {
in.seekg(4, std::ios::cur);
in.read((char*)(data + i * dim), dim * 4);
}
in.close();
}
void save_result(const char* filename,
std::vector<std::vector<unsigned> >& results) {
std::ofstream out(filename, std::ios::binary | std::ios::out);
for (unsigned i = 0; i < results.size(); i++) {
unsigned GK = (unsigned)results[i].size();
out.write((char*)&GK, sizeof(unsigned));
out.write((char*)results[i].data(), GK * sizeof(unsigned));
}
out.close();
}
int main(int argc, char** argv) {
if (argc != 7) {
std::cout << argv[0]
<< " data_file query_file nsg_path search_L search_K result_path"
<< std::endl;
exit(-1);
}
float* data_load = NULL;
unsigned points_num, dim;
load_data(argv[1], data_load, points_num, dim);
float* query_load = NULL;
unsigned query_num, query_dim;
load_data(argv[2], query_load, query_num, query_dim);
assert(dim == query_dim);
unsigned L = (unsigned)atoi(argv[4]);
unsigned K = (unsigned)atoi(argv[5]);
if (L < K) {
std::cout << "search_L cannot be smaller than search_K!" << std::endl;
exit(-1);
}
// data_load = efanna2e::data_align(data_load, points_num, dim);//one must
// align the data before build query_load = efanna2e::data_align(query_load,
// query_num, query_dim);
efanna2e::IndexNSG index(dim, points_num, efanna2e::FAST_L2, nullptr);
index.Load(argv[3]);
index.OptimizeGraph(data_load);
efanna2e::Parameters paras;
paras.Set<unsigned>("L_search", L);
paras.Set<unsigned>("P_search", L);
std::vector<std::vector<unsigned> > res(query_num);
for (unsigned i = 0; i < query_num; i++) res[i].resize(K);
auto s = std::chrono::high_resolution_clock::now();
for (unsigned i = 0; i < query_num; i++) {
index.SearchWithOptGraph(query_load + i * dim, K, paras, res[i].data());
}
auto e = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = e - s;
std::cout << "search time: " << diff.count() << "\n";
save_result(argv[6], res);
return 0;
}