11 #include <cuda_profiler_api.h>
12 #include "../../IndexFlat.h"
13 #include "../../IndexIVFPQ.h"
14 #include "../GpuIndexIVFPQ.h"
15 #include "../StandardGpuResources.h"
16 #include "../test/TestUtils.h"
17 #include "../utils/DeviceUtils.h"
18 #include "../utils/Timer.h"
19 #include <gflags/gflags.h>
23 DEFINE_int32(batches, 10,
"number of batches of vectors to add");
24 DEFINE_int32(batch_size, 10000,
"number of vectors in each batch");
25 DEFINE_int32(dim, 256,
"dimension of vectors");
26 DEFINE_int32(centroids, 4096,
"num coarse centroids to use");
27 DEFINE_int32(bytes_per_vec, 32,
"bytes per encoded vector");
28 DEFINE_int32(bits_per_code, 8,
"bits per PQ code");
29 DEFINE_int32(index, 2,
"0 = no indices on GPU; 1 = 32 bit, 2 = 64 bit on GPU");
30 DEFINE_bool(time_gpu,
true,
"time add to GPU");
31 DEFINE_bool(time_cpu,
false,
"time add to CPU");
32 DEFINE_bool(per_batch_time,
false,
"print per-batch times");
33 DEFINE_bool(reserve_memory,
false,
"whether or not to pre-reserve memory");
35 int main(
int argc,
char** argv) {
36 gflags::ParseCommandLineFlags(&argc, &argv,
true);
41 int numCentroids = FLAGS_centroids;
42 int bytesPerVec = FLAGS_bytes_per_vec;
43 int bitsPerCode = FLAGS_bits_per_code;
48 int numTrain = 4 * numCentroids;
49 std::vector<float> trainVecs = faiss::gpu::randVecs(numTrain, dim);
53 bytesPerVec, bitsPerCode);
55 cpuIndex.train(numTrain, trainVecs.data());
63 &res, dim, numCentroids, bytesPerVec, bitsPerCode,
64 faiss::METRIC_L2, config);
67 gpuIndex.train(numTrain, trainVecs.data());
68 if (FLAGS_reserve_memory) {
69 size_t numVecs = (size_t) FLAGS_batches * (
size_t) FLAGS_batch_size;
70 gpuIndex.reserveMemory(numVecs);
74 cudaDeviceSynchronize();
75 CUDA_VERIFY(cudaProfilerStart());
77 float totalGpuTime = 0.0f;
78 float totalCpuTime = 0.0f;
80 for (
int i = 0; i < FLAGS_batches; ++i) {
81 if (!FLAGS_per_batch_time) {
83 printf(
"Adding batch %d\n", i + 1);
87 auto addVecs = faiss::gpu::randVecs(FLAGS_batch_size, dim);
91 gpuIndex.add(FLAGS_batch_size, addVecs.data());
92 CUDA_VERIFY(cudaDeviceSynchronize());
97 if (FLAGS_per_batch_time) {
98 printf(
"Batch %d | GPU time to add %d vecs: %.3f ms (%.5f ms per)\n",
99 i + 1, FLAGS_batch_size, time, time / (
float) FLAGS_batch_size);
103 if (FLAGS_time_cpu) {
105 cpuIndex.add(FLAGS_batch_size, addVecs.data());
108 totalCpuTime += time;
110 if (FLAGS_per_batch_time) {
111 printf(
"Batch %d | CPU time to add %d vecs: %.3f ms (%.5f ms per)\n",
112 i + 1, FLAGS_batch_size, time, time / (
float) FLAGS_batch_size);
117 CUDA_VERIFY(cudaProfilerStop());
119 int total = FLAGS_batch_size * FLAGS_batches;
121 if (FLAGS_time_gpu) {
122 printf(
"%d dim, %d centroids, %d x %d encoding\n"
123 "GPU time to add %d vectors (%d batches, %d per batch): "
124 "%.3f ms (%.3f us per)\n",
125 dim, numCentroids, bytesPerVec, bitsPerCode,
126 total, FLAGS_batches, FLAGS_batch_size,
127 totalGpuTime, totalGpuTime * 1000.0f / (
float) total);
130 if (FLAGS_time_cpu) {
131 printf(
"%d dim, %d centroids, %d x %d encoding\n"
132 "CPU time to add %d vectors (%d batches, %d per batch): "
133 "%.3f ms (%.3f us per)\n",
134 dim, numCentroids, bytesPerVec, bitsPerCode,
135 total, FLAGS_batches, FLAGS_batch_size,
136 totalCpuTime, totalCpuTime * 1000.0f / (
float) total);
float elapsedMilliseconds()
Returns elapsed time in milliseconds.
CPU wallclock elapsed timer.
int device
GPU device on which the index is resident.
IndicesOptions indicesOptions
Index storage options for the GPU.