20 #include "../StandardGpuResources.h"
21 #include "../GpuIndexIVFPQ.h"
23 #include "../GpuAutoTune.h"
24 #include "../../index_io.h"
29 gettimeofday (&tv, NULL);
30 return tv.tv_sec + tv.tv_usec * 1e-6;
37 double t0 = elapsed();
43 size_t nb = 200 * 1000;
47 size_t nt = 100 * 1000;
55 int ncentroids = int (4 * sqrt (nb));
64 &resources, dev_no, d,
65 ncentroids, 4, 8,
true,
66 faiss::gpu::INDICES_64_BIT,
72 printf (
"[%.3f s] Generating %ld vectors in %dD for training\n",
73 elapsed() - t0, nt, d);
75 std::vector <float> trainvecs (nt * d);
76 for (
size_t i = 0; i < nt * d; i++) {
77 trainvecs[i] = drand48();
80 printf (
"[%.3f s] Training the index\n",
84 index.train (nt, trainvecs.data());
88 const char *outfilename =
"/tmp/index_trained.faissindex";
89 printf (
"[%.3f s] storing the pre-trained index to %s\n",
90 elapsed() - t0, outfilename);
92 faiss::Index * cpu_index = faiss::gpu::index_gpu_to_cpu (&index);
94 write_index (cpu_index, outfilename);
100 std::vector<float> queries;
103 printf (
"[%.3f s] Building a dataset of %ld vectors to index\n",
106 std::vector <float> database (nb * d);
107 for (
size_t i = 0; i < nb * d; i++) {
108 database[i] = drand48();
111 printf (
"[%.3f s] Adding the vectors to the index\n",
114 index.add (nb, database.data());
116 printf (
"[%.3f s] done\n", elapsed() - t0);
123 queries.resize (nq * d);
124 for (
int i = i0; i < i1; i++) {
125 for (
int j = 0; j < d; j++) {
126 queries [(i - i0) * d + j] = database [i * d + j];
134 printf (
"[%.3f s] Searching the %d nearest neighbors "
135 "of %ld vectors in the index\n",
136 elapsed() - t0, k, nq);
138 std::vector<faiss::Index::idx_t> nns (k * nq);
139 std::vector<float> dis (k * nq);
141 index.search (nq, queries.data(), k, dis.data(), nns.data());
143 printf (
"[%.3f s] Query results (vector ids, then distances):\n",
146 for (
int i = 0; i < nq; i++) {
147 printf (
"query %2d: ", i);
148 for (
int j = 0; j < k; j++) {
149 printf (
"%7ld ", nns[j + i * k]);
152 for (
int j = 0; j < k; j++) {
153 printf (
"%7g ", dis[j + i * k]);
158 printf (
"note that the nearest neighbor is not at "
159 "distance 0 due to quantization errors\n");