-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
121 lines (96 loc) · 3.82 KB
/
test.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <fstream>
#include "src/hnsw.hpp"
#include "util/util.hpp"
#include "util/vecs_io.hpp"
#include "util/ground_truth.hpp"
#include "util/parameter.hpp"
#include <thread>
#include <mutex>
using namespace std;
using namespace HNSWLab;
int main() {
{
printf("single thread\n");
printf("load ground truth\n");
int gnd_n_vec = 100;
int gnd_vec_dim = 10;
char *path = "./data/siftsmall/gnd.ivecs";
int *gnd = read_ivecs(gnd_n_vec, gnd_vec_dim, path);
printf("load query\n");
int query_n_vec = 100;
int query_vec_dim = 128;
path = "./data/siftsmall/query.bvecs";
int *query = read_bvecs(query_n_vec, query_vec_dim, path);
printf("load base\n");
int base_n_vec = 10000;
int base_vec_dim = 128;
path = "./data/siftsmall/base.bvecs";
int *base = read_bvecs(base_n_vec, base_vec_dim, path);
HNSW hnsw;
printf("inserting\n");
double single_insert_time;
TimeRecord insert_record;
for (int i = 0; i < base_n_vec; i++) {
hnsw.insert(base + base_vec_dim * i, i);
}
single_insert_time = insert_record.get_elapsed_time_micro() / base_n_vec * 1e-3;
printf("single insert time %.1f ms\n", single_insert_time);
printf("querying with single thread\n");
vector <vector<int>> test_gnd_l;
double single_query_time;
TimeRecord query_record;
for (int i = 0; i < gnd_n_vec; i++) {
vector<int> test_gnd = hnsw.query(query + i * query_vec_dim, gnd_vec_dim);
test_gnd_l.push_back(test_gnd);
}
single_query_time = query_record.get_elapsed_time_micro() / query_n_vec * 1e-3;
double recall = count_recall(gnd_n_vec, gnd_vec_dim, test_gnd_l, gnd);
printf("With single thread: average recall: %.3f, single query time %.1f ms\n\n", recall, single_query_time);
}
{
printf("multi-thread\n");
printf("load ground truth\n");
int gnd_n_vec = 100;
int gnd_vec_dim = 10;
char *path = "./data/siftsmall/gnd.ivecs";
int *gnd = read_ivecs(gnd_n_vec, gnd_vec_dim, path);
printf("load query\n");
int query_n_vec = 100;
int query_vec_dim = 128;
path = "./data/siftsmall/query.bvecs";
int *query = read_bvecs(query_n_vec, query_vec_dim, path);
printf("load base\n");
int base_n_vec = 10000;
int base_vec_dim = 128;
path = "./data/siftsmall/base.bvecs";
int *base = read_bvecs(base_n_vec, base_vec_dim, path);
HNSW hnsw;
printf("inserting\n");
double single_insert_time;
TimeRecord insert_record;
for (int i = 0; i < base_n_vec; i++) {
hnsw.insert(base + base_vec_dim * i, i);
}
single_insert_time = insert_record.get_elapsed_time_micro() / base_n_vec * 1e-3;
printf("single insert time %.1f ms\n", single_insert_time);
printf("querying with multi-thread\n");
vector <vector<int>> test_gnd_l(gnd_n_vec);
double single_query_time;
TimeRecord query_record;
vector<thread> threads;
for (int i = 0; i < gnd_n_vec; i++) {
threads.push_back(thread([&hnsw, &query, &test_gnd_l, i, query_vec_dim, gnd_vec_dim](){
vector<int> test_gnd = hnsw.query(query + i * query_vec_dim, gnd_vec_dim);
test_gnd_l[i] = test_gnd;
}));
}
for (auto &t : threads) {
t.join();
}
single_query_time = query_record.get_elapsed_time_micro() / query_n_vec * 1e-3;
double recall = count_recall(gnd_n_vec, gnd_vec_dim, test_gnd_l, gnd);
printf("With multi-thread: average recall: %.3f, single query time %.1f ms\n", recall, single_query_time);
}
return 0;
}