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 CC-by-NC 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 instead of float32");
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
38 using namespace faiss::gpu;
39
40 int main(int argc, char** argv) {
41 gflags::ParseCommandLineFlags(&argc, &argv, true);
42
43 cudaProfilerStop();
44
45 auto seed = FLAGS_seed != -1L ? FLAGS_seed : time(nullptr);
46 printf("using seed %ld\n", seed);
47
48 auto numQueries = FLAGS_num_queries;
49
50 auto index = std::unique_ptr<faiss::IndexFlatL2>(
51 new faiss::IndexFlatL2(FLAGS_dim));
52
53 HostTensor<float, 2, true> vecs({FLAGS_num, FLAGS_dim});
54 faiss::float_rand(vecs.data(), vecs.numElements(), seed);
55
56 index->add(FLAGS_num, vecs.data());
57
58 printf("Database: dim %d num vecs %d\n", FLAGS_dim, FLAGS_num);
59 printf("L2 lookup: %d queries, total k %d\n",
60 numQueries, FLAGS_k);
61 printf("float16 encoding %s\n", FLAGS_use_float16 ? "enabled" : "disabled");
62 printf("transposed storage %s\n", FLAGS_transposed ? "enabled" : "disabled");
63
64 // Convert to GPU index
65 printf("Copying index to %d GPU(s)...\n", FLAGS_num_gpus);
66
67 auto initFn = [&index](faiss::gpu::GpuResources* res, int dev) ->
68 std::unique_ptr<faiss::gpu::GpuIndexFlatL2> {
69 ((faiss::gpu::StandardGpuResources*) res)->setPinnedMemory(
70 FLAGS_pinned_mem);
71
72 GpuIndexFlatConfig config;
73 config.device = dev;
74 config.useFloat16 = FLAGS_use_float16;
75 config.storeTransposed = FLAGS_transposed;
76
77 auto p = std::unique_ptr<faiss::gpu::GpuIndexFlatL2>(
78 new faiss::gpu::GpuIndexFlatL2(res, index.get(), config));
79 return p;
80 };
81
82 IndexWrapper<faiss::gpu::GpuIndexFlatL2> gpuIndex(FLAGS_num_gpus, initFn);
83 printf("copy done\n");
84
85 // Build query vectors
86 HostTensor<float, 2, true> cpuQuery({numQueries, FLAGS_dim});
87 faiss::float_rand(cpuQuery.data(), cpuQuery.numElements(), seed);
88
89 // Time faiss CPU
90 HostTensor<float, 2, true> cpuDistances({numQueries, FLAGS_k});
91 HostTensor<faiss::Index::idx_t, 2, true> cpuIndices({numQueries, FLAGS_k});
92
93 float cpuTime = 0.0f;
94
95 {
96 CpuTimer timer;
97 index->search(numQueries,
98 cpuQuery.data(),
99 FLAGS_k,
100 cpuDistances.data(),
101 cpuIndices.data());
102
103 cpuTime = timer.elapsedMilliseconds();
104 }
105
106 printf("CPU time %.3f ms\n", cpuTime);
107
108 HostTensor<float, 2, true> gpuDistances({numQueries, FLAGS_k});
109 HostTensor<faiss::Index::idx_t, 2, true> gpuIndices({numQueries, FLAGS_k});
110
111 CUDA_VERIFY(cudaProfilerStart());
112 faiss::gpu::synchronizeAllDevices();
113
114 float gpuTime = 0.0f;
115
116 // Time GPU
117 {
118 CpuTimer timer;
119
120 gpuIndex.getIndex()->search(cpuQuery.getSize(0),
121 cpuQuery.data(),
122 FLAGS_k,
123 gpuDistances.data(),
124 gpuIndices.data());
125
126 // There is a device -> host copy above, so no need to time
127 // additional synchronization with the GPU
128 gpuTime = timer.elapsedMilliseconds();
129 }
130
131 CUDA_VERIFY(cudaProfilerStop());
132 printf("GPU time %.3f ms\n", gpuTime);
133
134 compareLists(cpuDistances.data(), cpuIndices.data(),
135 gpuDistances.data(), gpuIndices.data(),
136 numQueries, FLAGS_k,
137 "", true, FLAGS_diff, false);
138
139 CUDA_VERIFY(cudaDeviceSynchronize());
140 // printf("\ncudaMalloc usage %zd\n",
141 // resources.getMemoryManager().getHighWaterCudaMalloc());
142
143 return 0;
144 }
faiss::gpu::CpuTimer::elapsedMilliseconds
float elapsedMilliseconds()
Returns elapsed time in milliseconds.
Definition: Timer.cpp:52
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::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