| 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 |
// -*- c -*- |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <time.h> |
| 13 |
|
| 14 |
#include "../AutoTune_c.h" |
| 15 |
#include "../Index_c.h" |
| 16 |
#include "../error_c.h" |
| 17 |
#include "../index_factory_c.h" |
| 18 |
#include "DeviceUtils_c.h" |
| 19 |
#include "GpuAutoTune_c.h" |
| 20 |
#include "StandardGpuResources_c.h" |
| 21 |
|
| 22 |
#define FAISS_TRY(C) \ |
| 23 |
{ \ |
| 24 |
if (C) { \ |
| 25 |
fprintf(stderr, "%s", faiss_get_last_error()); \ |
| 26 |
exit(-1); \ |
| 27 |
} \ |
| 28 |
} |
| 29 |
|
| 30 |
double drand() { |
| 31 |
return (double)rand() / (double)RAND_MAX; |
| 32 |
} |
| 33 |
|
| 34 |
int main() { |
| 35 |
time_t seed = time(NULL); |
| 36 |
srand(seed); |
| 37 |
|
| 38 |
int gpus = -1; |
| 39 |
FAISS_TRY(faiss_get_num_gpus(&gpus)); |
| 40 |
printf("%d GPU devices are available\n", gpus); |
| 41 |
|
| 42 |
printf("Generating some data...\n"); |
| 43 |
int d = 128; // dimension |
| 44 |
int nb = 100000; // database size |
| 45 |
int nq = 10000; // nb of queries |
| 46 |
float* xb = malloc(d * nb * sizeof(float)); |
| 47 |
float* xq = malloc(d * nq * sizeof(float)); |
| 48 |
|
| 49 |
for (int i = 0; i < nb; i++) { |
| 50 |
for (int j = 0; j < d; j++) |
| 51 |
xb[d * i + j] = drand(); |
| 52 |
xb[d * i] += i / 1000.; |
| 53 |
} |
| 54 |
for (int i = 0; i < nq; i++) { |
| 55 |
for (int j = 0; j < d; j++) |
| 56 |
xq[d * i + j] = drand(); |
| 57 |
xq[d * i] += i / 1000.; |
| 58 |
} |
| 59 |
|
| 60 |
printf("Loading standard GPU resources...\n"); |
| 61 |
FaissStandardGpuResources* gpu_res = NULL; |
| 62 |
FAISS_TRY(faiss_StandardGpuResources_new(&gpu_res)); |
| 63 |
|
| 64 |
printf("Building an index...\n"); |
| 65 |
FaissIndex* cpu_index = NULL; |
| 66 |
FAISS_TRY(faiss_index_factory( |
| 67 |
&cpu_index, d, "Flat", METRIC_L2)); // use factory to create index |
| 68 |
|
| 69 |
printf("Moving index to the GPU...\n"); |
| 70 |
FaissGpuIndex* index = NULL; |
| 71 |
FaissGpuClonerOptions* options = NULL; |
| 72 |
FAISS_TRY(faiss_GpuClonerOptions_new(&options)); |
| 73 |
FAISS_TRY(faiss_index_cpu_to_gpu_with_options( |
| 74 |
gpu_res, 0, cpu_index, options, &index)); |
| 75 |
|
| 76 |
printf("is_trained = %s\n", |
| 77 |
faiss_Index_is_trained(index) ? "true" : "false"); |
| 78 |
FAISS_TRY(faiss_Index_add(index, nb, xb)); // add vectors to the index |
| 79 |
printf("ntotal = %ld\n", faiss_Index_ntotal(index)); |
| 80 |
|
| 81 |
printf("Searching...\n"); |
| 82 |
int k = 5; |
| 83 |
|
| 84 |
{ // sanity check: search 5 first vectors of xb |
| 85 |
idx_t* I = malloc(k * 5 * sizeof(idx_t)); |
| 86 |
float* D = malloc(k * 5 * sizeof(float)); |
| 87 |
FAISS_TRY(faiss_Index_search(index, 5, xb, k, D, I)); |
| 88 |
printf("I=\n"); |
| 89 |
for (int i = 0; i < 5; i++) { |
| 90 |
for (int j = 0; j < k; j++) |
| 91 |
printf("%5ld (d=%2.3f) ", I[i * k + j], D[i * k + j]); |
| 92 |
printf("\n"); |
| 93 |
} |
| 94 |
free(I); |
| 95 |
free(D); |
| 96 |
} |
| 97 |
{ // search xq |
| 98 |
idx_t* I = malloc(k * nq * sizeof(idx_t)); |
| 99 |
float* D = malloc(k * nq * sizeof(float)); |
| 100 |
FAISS_TRY(faiss_Index_search(index, 5, xb, k, D, I)); |
| 101 |
printf("I=\n"); |
| 102 |
for (int i = 0; i < 5; i++) { |
| 103 |
for (int j = 0; j < k; j++) |
| 104 |
printf("%5ld (d=%2.3f) ", I[i * k + j], D[i * k + j]); |
| 105 |
printf("\n"); |
| 106 |
} |
| 107 |
free(I); |
| 108 |
free(D); |
| 109 |
} |
| 110 |
|
| 111 |
printf("Freeing index...\n"); |
| 112 |
faiss_Index_free(index); |
| 113 |
printf("Freeing GPU resources...\n"); |
| 114 |
faiss_GpuResources_free(gpu_res); |
| 115 |
faiss_GpuClonerOptions_free(options); |
| 116 |
printf("Done.\n"); |
| 117 |
|
| 118 |
return 0; |
| 119 |
} |