19 #include "../StandardGpuResources.h"
20 #include "../GpuIndexIVFPQ.h"
22 #include "../GpuAutoTune.h"
23 #include "../../index_io.h"
28 gettimeofday (&tv, NULL);
29 return tv.tv_sec + tv.tv_usec * 1e-6;
36 double t0 = elapsed();
42 size_t nb = 200 * 1000;
46 size_t nt = 100 * 1000;
54 int ncentroids = int (4 * sqrt (nb));
66 &resources, d, ncentroids, 4, 8, faiss::METRIC_L2, config);
69 printf (
"[%.3f s] Generating %ld vectors in %dD for training\n",
70 elapsed() - t0, nt, d);
72 std::vector <float> trainvecs (nt * d);
73 for (
size_t i = 0; i < nt * d; i++) {
74 trainvecs[i] = drand48();
77 printf (
"[%.3f s] Training the index\n",
81 index.train (nt, trainvecs.data());
85 const char *outfilename =
"/tmp/index_trained.faissindex";
86 printf (
"[%.3f s] storing the pre-trained index to %s\n",
87 elapsed() - t0, outfilename);
89 faiss::Index * cpu_index = faiss::gpu::index_gpu_to_cpu (&index);
91 write_index (cpu_index, outfilename);
97 std::vector<float> queries;
100 printf (
"[%.3f s] Building a dataset of %ld vectors to index\n",
103 std::vector <float> database (nb * d);
104 for (
size_t i = 0; i < nb * d; i++) {
105 database[i] = drand48();
108 printf (
"[%.3f s] Adding the vectors to the index\n",
111 index.add (nb, database.data());
113 printf (
"[%.3f s] done\n", elapsed() - t0);
120 queries.resize (nq * d);
121 for (
int i = i0; i < i1; i++) {
122 for (
int j = 0; j < d; j++) {
123 queries [(i - i0) * d + j] = database [i * d + j];
131 printf (
"[%.3f s] Searching the %d nearest neighbors "
132 "of %ld vectors in the index\n",
133 elapsed() - t0, k, nq);
135 std::vector<faiss::Index::idx_t> nns (k * nq);
136 std::vector<float> dis (k * nq);
138 index.search (nq, queries.data(), k, dis.data(), nns.data());
140 printf (
"[%.3f s] Query results (vector ids, then distances):\n",
143 for (
int i = 0; i < nq; i++) {
144 printf (
"query %2d: ", i);
145 for (
int j = 0; j < k; j++) {
146 printf (
"%7ld ", nns[j + i * k]);
149 for (
int j = 0; j < k; j++) {
150 printf (
"%7g ", dis[j + i * k]);
155 printf (
"note that the nearest neighbor is not at "
156 "distance 0 due to quantization errors\n");
int device
GPU device on which the index is resident.