Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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