10 #include <cuda_profiler_api.h>
11 #include "../../IndexFlat.h"
12 #include "../../IndexIVFPQ.h"
13 #include "../GpuIndexIVFPQ.h"
14 #include "../StandardGpuResources.h"
15 #include "../test/TestUtils.h"
16 #include "../utils/DeviceUtils.h"
17 #include "../utils/Timer.h"
18 #include <gflags/gflags.h>
22 DEFINE_int32(batches, 10,
"number of batches of vectors to add");
23 DEFINE_int32(batch_size, 10000,
"number of vectors in each batch");
24 DEFINE_int32(dim, 256,
"dimension of vectors");
25 DEFINE_int32(centroids, 4096,
"num coarse centroids to use");
26 DEFINE_int32(bytes_per_vec, 32,
"bytes per encoded vector");
27 DEFINE_int32(bits_per_code, 8,
"bits per PQ code");
28 DEFINE_int32(index, 2,
"0 = no indices on GPU; 1 = 32 bit, 2 = 64 bit on GPU");
29 DEFINE_bool(time_gpu,
true,
"time add to GPU");
30 DEFINE_bool(time_cpu,
false,
"time add to CPU");
31 DEFINE_bool(per_batch_time,
false,
"print per-batch times");
32 DEFINE_bool(reserve_memory,
false,
"whether or not to pre-reserve memory");
34 int main(
int argc,
char** argv) {
35 gflags::ParseCommandLineFlags(&argc, &argv,
true);
40 int numCentroids = FLAGS_centroids;
41 int bytesPerVec = FLAGS_bytes_per_vec;
42 int bitsPerCode = FLAGS_bits_per_code;
47 int numTrain = 4 * numCentroids;
48 std::vector<float> trainVecs = faiss::gpu::randVecs(numTrain, dim);
52 bytesPerVec, bitsPerCode);
54 cpuIndex.train(numTrain, trainVecs.data());
62 &res, dim, numCentroids, bytesPerVec, bitsPerCode,
63 faiss::METRIC_L2, config);
66 gpuIndex.train(numTrain, trainVecs.data());
67 if (FLAGS_reserve_memory) {
68 size_t numVecs = (size_t) FLAGS_batches * (
size_t) FLAGS_batch_size;
69 gpuIndex.reserveMemory(numVecs);
73 cudaDeviceSynchronize();
74 CUDA_VERIFY(cudaProfilerStart());
76 float totalGpuTime = 0.0f;
77 float totalCpuTime = 0.0f;
79 for (
int i = 0; i < FLAGS_batches; ++i) {
80 if (!FLAGS_per_batch_time) {
82 printf(
"Adding batch %d\n", i + 1);
86 auto addVecs = faiss::gpu::randVecs(FLAGS_batch_size, dim);
90 gpuIndex.add(FLAGS_batch_size, addVecs.data());
91 CUDA_VERIFY(cudaDeviceSynchronize());
96 if (FLAGS_per_batch_time) {
97 printf(
"Batch %d | GPU time to add %d vecs: %.3f ms (%.5f ms per)\n",
98 i + 1, FLAGS_batch_size, time, time / (
float) FLAGS_batch_size);
102 if (FLAGS_time_cpu) {
104 cpuIndex.add(FLAGS_batch_size, addVecs.data());
107 totalCpuTime += time;
109 if (FLAGS_per_batch_time) {
110 printf(
"Batch %d | CPU time to add %d vecs: %.3f ms (%.5f ms per)\n",
111 i + 1, FLAGS_batch_size, time, time / (
float) FLAGS_batch_size);
116 CUDA_VERIFY(cudaProfilerStop());
118 int total = FLAGS_batch_size * FLAGS_batches;
120 if (FLAGS_time_gpu) {
121 printf(
"%d dim, %d centroids, %d x %d encoding\n"
122 "GPU time to add %d vectors (%d batches, %d per batch): "
123 "%.3f ms (%.3f us per)\n",
124 dim, numCentroids, bytesPerVec, bitsPerCode,
125 total, FLAGS_batches, FLAGS_batch_size,
126 totalGpuTime, totalGpuTime * 1000.0f / (
float) total);
129 if (FLAGS_time_cpu) {
130 printf(
"%d dim, %d centroids, %d x %d encoding\n"
131 "CPU time to add %d vectors (%d batches, %d per batch): "
132 "%.3f ms (%.3f us per)\n",
133 dim, numCentroids, bytesPerVec, bitsPerCode,
134 total, FLAGS_batches, FLAGS_batch_size,
135 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.