-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvs_faiss.cpp
57 lines (52 loc) · 1.45 KB
/
vs_faiss.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
#include <cstdio>
#include <random>
#include <faiss/IndexHNSW.h>
using idx_t = faiss::idx_t;
// https://github.com/facebookresearch/faiss/blob/main/tutorial/cpp/6-HNSW.cpp
void run_faiss() {
int d = 64; // dimension
int nb = 100000; // database size
int nq = 10000; // nb of queries
std::mt19937 rng;
std::uniform_real_distribution<> distrib;
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] = distrib(rng);
xb[d * i] += i / 1000.;
}
for (int i = 0; i < nq; i++) {
for (int j = 0; j < d; j++)
xq[d * i + j] = distrib(rng);
xq[d * i] += i / 1000.;
}
int k = 4;
faiss::IndexHNSWFlat index(d, 32);
index.add(nb, xb);
{ // search xq
idx_t* I = new idx_t[k * nq];
float* D = new float[k * nq];
index.search(nq, xq, k, D, I);
printf("I=\n");
for (int i = nq - 5; i < nq; i++) {
for (int j = 0; j < k; j++)
printf("%5lld ", I[i * k + j]);
printf("\n");
}
printf("D=\n");
for (int i = nq - 5; i < nq; i++) {
for (int j = 0; j < k; j++)
printf("%5f ", D[i * k + j]);
printf("\n");
}
delete[] I;
delete[] D;
}
delete[] xb;
delete[] xq;
}
int main(int argc, char **argv) {
run_faiss();
return 0;
}