Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CompareFlat.cu
1 /**
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD+Patents license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 
10 #include "../../IndexFlat.h"
11 #include "../../utils.h"
12 #include "../GpuIndexFlat.h"
13 #include "IndexWrapper.h"
14 #include "../test/TestUtils.h"
15 #include "../utils/DeviceTensor.cuh"
16 #include "../utils/DeviceUtils.h"
17 #include "../utils/HostTensor.cuh"
18 #include "../utils/Timer.h"
19 #include <gflags/gflags.h>
20 #include <map>
21 #include <memory>
22 #include <vector>
23 
24 #include <cuda_profiler_api.h>
25 
26 DEFINE_int32(k, 3, "final number of closest results returned");
27 DEFINE_int32(num, 128, "# of vecs");
28 DEFINE_int32(dim, 128, "# of dimensions");
29 DEFINE_int32(num_queries, 3, "number of query vectors");
30 DEFINE_bool(diff, true, "show exact distance + index output discrepancies");
31 DEFINE_bool(use_float16, false, "use encodings in float16");
32 DEFINE_bool(use_float16_math, false, "perform math in float16");
33 DEFINE_bool(transposed, false, "store vectors transposed");
34 DEFINE_int64(seed, -1, "specify random seed");
35 DEFINE_int32(num_gpus, 1, "number of gpus to use");
36 DEFINE_int64(pinned_mem, 0, "pinned memory allocation to use");
37 DEFINE_bool(cpu, true, "run the CPU code for timing and comparison");
38 DEFINE_bool(use_unified_mem, false, "use Pascal unified memory for the index");
39 
40 using namespace faiss::gpu;
41 
42 int main(int argc, char** argv) {
43  gflags::ParseCommandLineFlags(&argc, &argv, true);
44 
45  cudaProfilerStop();
46 
47  auto seed = FLAGS_seed != -1L ? FLAGS_seed : time(nullptr);
48  printf("using seed %ld\n", seed);
49 
50  auto numQueries = FLAGS_num_queries;
51 
52  auto index = std::unique_ptr<faiss::IndexFlatL2>(
53  new faiss::IndexFlatL2(FLAGS_dim));
54 
55  HostTensor<float, 2, true> vecs({FLAGS_num, FLAGS_dim});
56  faiss::float_rand(vecs.data(), vecs.numElements(), seed);
57 
58  index->add(FLAGS_num, vecs.data());
59 
60  printf("Database: dim %d num vecs %d\n", FLAGS_dim, FLAGS_num);
61  printf("L2 lookup: %d queries, total k %d\n",
62  numQueries, FLAGS_k);
63  printf("float16 encoding %s\n", FLAGS_use_float16 ? "enabled" : "disabled");
64  printf("transposed storage %s\n", FLAGS_transposed ? "enabled" : "disabled");
65 
66  // Convert to GPU index
67  printf("Copying index to %d GPU(s)...\n", FLAGS_num_gpus);
68 
69  auto initFn = [&index](faiss::gpu::GpuResources* res, int dev) ->
70  std::unique_ptr<faiss::gpu::GpuIndexFlatL2> {
71  ((faiss::gpu::StandardGpuResources*) res)->setPinnedMemory(
72  FLAGS_pinned_mem);
73 
74  GpuIndexFlatConfig config;
75  config.device = dev;
76  config.useFloat16 = FLAGS_use_float16;
77  config.useFloat16Accumulator = FLAGS_use_float16_math;
78  config.storeTransposed = FLAGS_transposed;
79  config.memorySpace = FLAGS_use_unified_mem ?
80  MemorySpace::Unified : MemorySpace::Device;
81 
82  auto p = std::unique_ptr<faiss::gpu::GpuIndexFlatL2>(
83  new faiss::gpu::GpuIndexFlatL2(res, index.get(), config));
84  return p;
85  };
86 
87  IndexWrapper<faiss::gpu::GpuIndexFlatL2> gpuIndex(FLAGS_num_gpus, initFn);
88  printf("copy done\n");
89 
90  // Build query vectors
91  HostTensor<float, 2, true> cpuQuery({numQueries, FLAGS_dim});
92  faiss::float_rand(cpuQuery.data(), cpuQuery.numElements(), seed);
93 
94  // Time faiss CPU
95  HostTensor<float, 2, true> cpuDistances({numQueries, FLAGS_k});
96  HostTensor<faiss::Index::idx_t, 2, true> cpuIndices({numQueries, FLAGS_k});
97 
98  if (FLAGS_cpu) {
99  float cpuTime = 0.0f;
100 
101  CpuTimer timer;
102  index->search(numQueries,
103  cpuQuery.data(),
104  FLAGS_k,
105  cpuDistances.data(),
106  cpuIndices.data());
107 
108  cpuTime = timer.elapsedMilliseconds();
109  printf("CPU time %.3f ms\n", cpuTime);
110  }
111 
112  HostTensor<float, 2, true> gpuDistances({numQueries, FLAGS_k});
113  HostTensor<faiss::Index::idx_t, 2, true> gpuIndices({numQueries, FLAGS_k});
114 
115  CUDA_VERIFY(cudaProfilerStart());
116  faiss::gpu::synchronizeAllDevices();
117 
118  float gpuTime = 0.0f;
119 
120  // Time GPU
121  {
122  CpuTimer timer;
123 
124  gpuIndex.getIndex()->search(cpuQuery.getSize(0),
125  cpuQuery.data(),
126  FLAGS_k,
127  gpuDistances.data(),
128  gpuIndices.data());
129 
130  // There is a device -> host copy above, so no need to time
131  // additional synchronization with the GPU
132  gpuTime = timer.elapsedMilliseconds();
133  }
134 
135  CUDA_VERIFY(cudaProfilerStop());
136  printf("GPU time %.3f ms\n", gpuTime);
137 
138  if (FLAGS_cpu) {
139  compareLists(cpuDistances.data(), cpuIndices.data(),
140  gpuDistances.data(), gpuIndices.data(),
141  numQueries, FLAGS_k,
142  "", true, FLAGS_diff, false);
143  }
144 
145  CUDA_VERIFY(cudaDeviceSynchronize());
146 
147  return 0;
148 }
float elapsedMilliseconds()
Returns elapsed time in milliseconds.
Definition: Timer.cpp:51
CPU wallclock elapsed timer.
Definition: Timer.h:41
bool useFloat16
Whether or not data is stored as float16.
Definition: GpuIndexFlat.h:34
int device
GPU device on which the index is resident.
Definition: GpuIndex.h:26
MemorySpace memorySpace
Definition: GpuIndex.h:31