-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestfile.cpp
75 lines (66 loc) · 1.96 KB
/
testfile.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
#include <iostream>
#include <vector>
#include "index.h"
candidate_pool searchBrute(int N, int D, const float* data, const float* query, unsigned topK) {
candidate_pool candidate_set;
for (int i = 0; i < N; ++i) {
float dist = compare(query, data + i * D, D);
candidate_set.emplace(dist, i);
}
while (candidate_set.size() > topK) {
candidate_set.pop();
}
return candidate_set;
}
int main() {
// 关闭同步
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
Timer time;
time.tick();
unsigned N, D, k;
std::cout << "start" << std::endl;
float* data = read_fbin<float>("/home/yuxiang/src/data-50K", N, D);
float* center = new float[D];
for (unsigned j = 0; j < D; j++)
center[j] = 0;
for (unsigned i = 0; i < N; i++) {
for (unsigned j = 0; j < D; j++) {
center[j] += data[i * D + j];
}
}
for (int i = 0; i < N; i++) {
normalize(data + i * D, D);
}
for (unsigned j = 0; j < D; j++) {
center[j] /= N;
}
// ... 建图 ...
Index nsg(D, N, data, center);
nsg.BuildKNN(N, 10, 10, 10, 8);
time.tuck("build knn done");
time.tick();
nsg.BuildNSG(10, 10, 50);
// 输出 "ok"
if (nsg.has_built)
std::cout << "ok" << std::endl;
time.tuck("build nsg done");
unsigned nq = 0, gtd = 2;
k = 10;
float* query = read_fbin<float>("/home/yuxiang/src/sample-data-800", nq, D);
for (int i = 0; i < nq; i++) {
normalize(query + i * D, D);
}
unsigned* gt = read_fbin<unsigned>("/home/yuxiang/src/gt-50K-sample-800", nq, gtd);
time.tick();
nsg.testSearch(nq, gtd, query, gt, k, 600, true);
time.tuck("random search done");
time.tick();
// nsg.testSearch(nq, gtd, query, gt, k, 500);
nsg.testSearch(nq, gtd, query, gt, k, 600, false);
time.tuck("normal search done");
delete[] center;
delete[] query;
delete[] data;
return 0;
}