Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
1-Flat.cpp
1 /**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the CC-by-NC license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 #include <cstdio>
10 #include <cstdlib>
11
12 #include <faiss/IndexFlat.h>
13
14
15 int main() {
16 int d = 64; // dimension
17 int nb = 100000; // database size
18 int nq = 10000; // nb of queries
19
20 float *xb = new float[d * nb];
21 float *xq = new float[d * nq];
22
23 for(int i = 0; i < nb; i++) {
24 for(int j = 0; j < d; j++)
25 xb[d * i + j] = drand48();
26 xb[d * i] += i / 1000.;
27 }
28
29 for(int i = 0; i < nq; i++) {
30 for(int j = 0; j < d; j++)
31 xq[d * i + j] = drand48();
32 xq[d * i] += i / 1000.;
33 }
34
35 faiss::IndexFlatL2 index(d); // call constructor
36 printf("is_trained = %s\n", index.is_trained ? "true" : "false");
37 index.add(nb, xb); // add vectors to the index
38 printf("ntotal = %ld\n", index.ntotal);
39
40 int k = 4;
41
42 { // sanity check: search 5 first vectors of xb
43 long *I = new long[k * 5];
44 float *D = new float[k * 5];
45
46 index.search(5, xb, k, D, I);
47
48 // print results
49 printf("I=\n");
50 for(int i = 0; i < 5; i++) {
51 for(int j = 0; j < k; j++)
52 printf("%5ld ", I[i * k + j]);
53 printf("\n");
54 }
55
56 printf("D=\n");
57 for(int i = 0; i < 5; i++) {
58 for(int j = 0; j < k; j++)
59 printf("%7g ", D[i * k + j]);
60 printf("\n");
61 }
62
63 delete [] I;
64 delete [] D;
65 }
66
67
68 { // search xq
69 long *I = new long[k * nq];
70 float *D = new float[k * nq];
71
72 index.search(nq, xq, k, D, I);
73
74 // print results
75 printf("I (5 first results)=\n");
76 for(int i = 0; i < 5; i++) {
77 for(int j = 0; j < k; j++)
78 printf("%5ld ", I[i * k + j]);
79 printf("\n");
80 }
81
82 printf("I (5 last results)=\n");
83 for(int i = nq - 5; i < nq; i++) {
84 for(int j = 0; j < k; j++)
85 printf("%5ld ", I[i * k + j]);
86 printf("\n");
87 }
88
89 delete [] I;
90 delete [] D;
91 }
92
93
94
95 delete [] xb;
96 delete [] xq;
97
98 return 0;
99 }
faiss::IndexFlatL2
Definition: IndexFlat.h:79