| 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 <cstdio> |
| 9 |
#include <cstdlib> |
| 10 |
#include <random> |
| 11 |
|
| 12 |
#include <faiss/IndexFlat.h> |
| 13 |
#include <faiss/gpu/GpuAutoTune.h> |
| 14 |
#include <faiss/gpu/GpuCloner.h> |
| 15 |
#include <faiss/gpu/GpuIndexFlat.h> |
| 16 |
#include <faiss/gpu/StandardGpuResources.h> |
| 17 |
#include <faiss/gpu/utils/DeviceUtils.h> |
| 18 |
|
| 19 |
int main() { |
| 20 |
int d = 64; // dimension |
| 21 |
int nb = 100000; // database size |
| 22 |
int nq = 10000; // nb of queries |
| 23 |
|
| 24 |
std::mt19937 rng; |
| 25 |
std::uniform_real_distribution<> distrib; |
| 26 |
|
| 27 |
float* xb = new float[d * nb]; |
| 28 |
float* xq = new float[d * nq]; |
| 29 |
|
| 30 |
for (int i = 0; i < nb; i++) { |
| 31 |
for (int j = 0; j < d; j++) { |
| 32 |
xb[d * i + j] = distrib(rng); |
| 33 |
} |
| 34 |
xb[d * i] += i / 1000.; |
| 35 |
} |
| 36 |
|
| 37 |
for (int i = 0; i < nq; i++) { |
| 38 |
for (int j = 0; j < d; j++) { |
| 39 |
xq[d * i + j] = distrib(rng); |
| 40 |
} |
| 41 |
xq[d * i] += i / 1000.; |
| 42 |
} |
| 43 |
|
| 44 |
int ngpus = faiss::gpu::getNumDevices(); |
| 45 |
|
| 46 |
printf("Number of GPUs: %d\n", ngpus); |
| 47 |
|
| 48 |
std::vector<faiss::gpu::GpuResourcesProvider*> res; |
| 49 |
std::vector<int> devs; |
| 50 |
for (int i = 0; i < ngpus; i++) { |
| 51 |
res.push_back(new faiss::gpu::StandardGpuResources); |
| 52 |
devs.push_back(i); |
| 53 |
} |
| 54 |
|
| 55 |
faiss::IndexFlatL2 cpu_index(d); |
| 56 |
|
| 57 |
faiss::Index* gpu_index = |
| 58 |
faiss::gpu::index_cpu_to_gpu_multiple(res, devs, &cpu_index); |
| 59 |
|
| 60 |
printf("is_trained = %s\n", gpu_index->is_trained ? "true" : "false"); |
| 61 |
gpu_index->add(nb, xb); // add vectors to the index |
| 62 |
printf("ntotal = %ld\n", gpu_index->ntotal); |
| 63 |
|
| 64 |
int k = 4; |
| 65 |
|
| 66 |
{ // search xq |
| 67 |
long* I = new long[k * nq]; |
| 68 |
float* D = new float[k * nq]; |
| 69 |
|
| 70 |
gpu_index->search(nq, xq, k, D, I); |
| 71 |
|
| 72 |
// print results |
| 73 |
printf("I (5 first results)=\n"); |
| 74 |
for (int i = 0; i < 5; i++) { |
| 75 |
for (int j = 0; j < k; j++) { |
| 76 |
printf("%5ld ", I[i * k + j]); |
| 77 |
} |
| 78 |
printf("\n"); |
| 79 |
} |
| 80 |
|
| 81 |
printf("I (5 last results)=\n"); |
| 82 |
for (int i = nq - 5; i < nq; i++) { |
| 83 |
for (int j = 0; j < k; j++) { |
| 84 |
printf("%5ld ", I[i * k + j]); |
| 85 |
} |
| 86 |
printf("\n"); |
| 87 |
} |
| 88 |
|
| 89 |
delete[] I; |
| 90 |
delete[] D; |
| 91 |
} |
| 92 |
|
| 93 |
delete gpu_index; |
| 94 |
|
| 95 |
for (int i = 0; i < ngpus; i++) { |
| 96 |
delete res[i]; |
| 97 |
} |
| 98 |
|
| 99 |
delete[] xb; |
| 100 |
delete[] xq; |
| 101 |
|
| 102 |
return 0; |
| 103 |
} |