| 1 |
/* |
| 2 |
* Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 |
* |
| 4 |
* This source code is licensed under the MIT license found in the |
| 5 |
* LICENSE file in the root directory of this source tree. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <cassert> |
| 9 |
#include <cstdio> |
| 10 |
#include <cstdlib> |
| 11 |
#include <random> |
| 12 |
|
| 13 |
#include <faiss/IndexFlat.h> |
| 14 |
#include <faiss/gpu/GpuIndexFlat.h> |
| 15 |
#include <faiss/gpu/GpuIndexIVFFlat.h> |
| 16 |
#include <faiss/gpu/StandardGpuResources.h> |
| 17 |
|
| 18 |
int main() { |
| 19 |
int d = 64; // dimension |
| 20 |
int nb = 100000; // database size |
| 21 |
int nq = 10000; // nb of queries |
| 22 |
|
| 23 |
std::mt19937 rng; |
| 24 |
std::uniform_real_distribution<> distrib; |
| 25 |
|
| 26 |
float* xb = new float[d * nb]; |
| 27 |
float* xq = new float[d * nq]; |
| 28 |
|
| 29 |
for (int i = 0; i < nb; i++) { |
| 30 |
for (int j = 0; j < d; j++) { |
| 31 |
xb[d * i + j] = distrib(rng); |
| 32 |
} |
| 33 |
xb[d * i] += i / 1000.; |
| 34 |
} |
| 35 |
|
| 36 |
for (int i = 0; i < nq; i++) { |
| 37 |
for (int j = 0; j < d; j++) { |
| 38 |
xq[d * i + j] = distrib(rng); |
| 39 |
} |
| 40 |
xq[d * i] += i / 1000.; |
| 41 |
} |
| 42 |
|
| 43 |
faiss::gpu::StandardGpuResources res; |
| 44 |
|
| 45 |
// Using a flat index |
| 46 |
|
| 47 |
faiss::gpu::GpuIndexFlatL2 index_flat(&res, d); |
| 48 |
|
| 49 |
printf("is_trained = %s\n", index_flat.is_trained ? "true" : "false"); |
| 50 |
index_flat.add(nb, xb); // add vectors to the index |
| 51 |
printf("ntotal = %ld\n", index_flat.ntotal); |
| 52 |
|
| 53 |
int k = 4; |
| 54 |
|
| 55 |
{ // search xq |
| 56 |
long* I = new long[k * nq]; |
| 57 |
float* D = new float[k * nq]; |
| 58 |
|
| 59 |
index_flat.search(nq, xq, k, D, I); |
| 60 |
|
| 61 |
// print results |
| 62 |
printf("I (5 first results)=\n"); |
| 63 |
for (int i = 0; i < 5; i++) { |
| 64 |
for (int j = 0; j < k; j++) { |
| 65 |
printf("%5ld ", I[i * k + j]); |
| 66 |
} |
| 67 |
printf("\n"); |
| 68 |
} |
| 69 |
|
| 70 |
printf("I (5 last results)=\n"); |
| 71 |
for (int i = nq - 5; i < nq; i++) { |
| 72 |
for (int j = 0; j < k; j++) { |
| 73 |
printf("%5ld ", I[i * k + j]); |
| 74 |
} |
| 75 |
printf("\n"); |
| 76 |
} |
| 77 |
|
| 78 |
delete[] I; |
| 79 |
delete[] D; |
| 80 |
} |
| 81 |
|
| 82 |
// Using an IVF index |
| 83 |
|
| 84 |
int nlist = 100; |
| 85 |
faiss::gpu::GpuIndexIVFFlat index_ivf(&res, d, nlist, faiss::METRIC_L2); |
| 86 |
|
| 87 |
assert(!index_ivf.is_trained); |
| 88 |
index_ivf.train(nb, xb); |
| 89 |
assert(index_ivf.is_trained); |
| 90 |
index_ivf.add(nb, xb); // add vectors to the index |
| 91 |
|
| 92 |
printf("is_trained = %s\n", index_ivf.is_trained ? "true" : "false"); |
| 93 |
printf("ntotal = %ld\n", index_ivf.ntotal); |
| 94 |
|
| 95 |
{ // search xq |
| 96 |
long* I = new long[k * nq]; |
| 97 |
float* D = new float[k * nq]; |
| 98 |
|
| 99 |
index_ivf.search(nq, xq, k, D, I); |
| 100 |
|
| 101 |
// print results |
| 102 |
printf("I (5 first results)=\n"); |
| 103 |
for (int i = 0; i < 5; i++) { |
| 104 |
for (int j = 0; j < k; j++) { |
| 105 |
printf("%5ld ", I[i * k + j]); |
| 106 |
} |
| 107 |
printf("\n"); |
| 108 |
} |
| 109 |
|
| 110 |
printf("I (5 last results)=\n"); |
| 111 |
for (int i = nq - 5; i < nq; i++) { |
| 112 |
for (int j = 0; j < k; j++) { |
| 113 |
printf("%5ld ", I[i * k + j]); |
| 114 |
} |
| 115 |
printf("\n"); |
| 116 |
} |
| 117 |
|
| 118 |
delete[] I; |
| 119 |
delete[] D; |
| 120 |
} |
| 121 |
|
| 122 |
delete[] xb; |
| 123 |
delete[] xq; |
| 124 |
|
| 125 |
return 0; |
| 126 |
} |