Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
IVFUtilsSelect2.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 "IVFUtils.cuh"
11 #include "../utils/DeviceUtils.h"
12 #include "../utils/Limits.cuh"
13 #include "../utils/Select.cuh"
14 #include "../utils/StaticUtils.h"
15 #include "../utils/Tensor.cuh"
16
17 //
18 // This kernel is split into a separate compilation unit to cut down
19 // on compile time
20 //
21
22 namespace faiss { namespace gpu {
23
24 // This is warp divergence central, but this is really a final step
25 // and happening a small number of times
26 inline __device__ int binarySearchForBucket(int* prefixSumOffsets,
27 int size,
28 int val) {
29 int start = 0;
30 int end = size;
31
32 while (end - start > 0) {
33 int mid = start + (end - start) / 2;
34
35 int midVal = prefixSumOffsets[mid];
36
37 // Find the first bucket that we are <=
38 if (midVal <= val) {
39 start = mid + 1;
40 } else {
41 end = mid;
42 }
43 }
44
45 // We must find the bucket that it is in
46 assert(start != size);
47
48 return start;
49 }
50
51 template <int ThreadsPerBlock,
52 int NumWarpQ,
53 int NumThreadQ,
54 bool Dir>
55 __global__ void
56 pass2SelectLists(Tensor<float, 2, true> heapDistances,
57 Tensor<int, 2, true> heapIndices,
58 void** listIndices,
59 Tensor<int, 2, true> prefixSumOffsets,
60 Tensor<int, 2, true> topQueryToCentroid,
61 int k,
62 IndicesOptions opt,
63 Tensor<float, 2, true> outDistances,
64 Tensor<long, 2, true> outIndices) {
65 constexpr int kNumWarps = ThreadsPerBlock / kWarpSize;
66
67 __shared__ float smemK[kNumWarps * NumWarpQ];
68 __shared__ int smemV[kNumWarps * NumWarpQ];
69
70 constexpr auto kInit = Dir ? kFloatMin : kFloatMax;
71 BlockSelect<float, int, Dir, Comparator<float>,
72 NumWarpQ, NumThreadQ, ThreadsPerBlock>
73 heap(kInit, -1, smemK, smemV, k);
74
75 auto queryId = blockIdx.x;
76 int num = heapDistances.getSize(1);
77 int limit = utils::roundDown(num, kWarpSize);
78
79 int i = threadIdx.x;
80 auto heapDistanceStart = heapDistances[queryId];
81
82 // BlockSelect add cannot be used in a warp divergent circumstance; we
83 // handle the remainder warp below
84 for (; i < limit; i += blockDim.x) {
85 heap.add(heapDistanceStart[i], i);
86 }
87
88 // Handle warp divergence separately
89 if (i < num) {
90 heap.addThreadQ(heapDistanceStart[i], i);
91 }
92
93 // Merge all final results
94 heap.reduce();
95
96 for (int i = threadIdx.x; i < k; i += blockDim.x) {
97 outDistances[queryId][i] = smemK[i];
98
99 // `v` is the index in `heapIndices`
100 // We need to translate this into an original user index. The
101 // reason why we don't maintain intermediate results in terms of
102 // user indices is to substantially reduce temporary memory
103 // requirements and global memory write traffic for the list
104 // scanning.
105 // This code is highly divergent, but it's probably ok, since this
106 // is the very last step and it is happening a small number of
107 // times (#queries x k).
108 int v = smemV[i];
109 long index = -1;
110
111 if (v != -1) {
112 // `offset` is the offset of the intermediate result, as
113 // calculated by the original scan.
114 int offset = heapIndices[queryId][v];
115
116 // In order to determine the actual user index, we need to first
117 // determine what list it was in.
118 // We do this by binary search in the prefix sum list.
119 int probe = binarySearchForBucket(prefixSumOffsets[queryId].data(),
120 prefixSumOffsets.getSize(1),
121 offset);
122
123 // This is then the probe for the query; we can find the actual
124 // list ID from this
125 int listId = topQueryToCentroid[queryId][probe];
126
127 // Now, we need to know the offset within the list
128 // We ensure that before the array (at offset -1), there is a 0 value
129 int listStart = *(prefixSumOffsets[queryId][probe].data() - 1);
130 int listOffset = offset - listStart;
131
132 // This gives us our final index
133 if (opt == INDICES_32_BIT) {
134 index = (long) ((int*) listIndices[listId])[listOffset];
135 } else if (opt == INDICES_64_BIT) {
136 index = ((long*) listIndices[listId])[listOffset];
137 } else {
138 index = ((long) listId << 32 | (long) listOffset);
139 }
140 }
141
142 outIndices[queryId][i] = index;
143 }
144 }
145
146 void
147 runPass2SelectLists(Tensor<float, 2, true>& heapDistances,
148 Tensor<int, 2, true>& heapIndices,
149 thrust::device_vector<void*>& listIndices,
150 IndicesOptions indicesOptions,
151 Tensor<int, 2, true>& prefixSumOffsets,
152 Tensor<int, 2, true>& topQueryToCentroid,
153 int k,
154 bool chooseLargest,
155 Tensor<float, 2, true>& outDistances,
156 Tensor<long, 2, true>& outIndices,
157 cudaStream_t stream) {
158 constexpr auto kThreadsPerBlock = 128;
159
160 auto grid = dim3(topQueryToCentroid.getSize(0));
161 auto block = dim3(kThreadsPerBlock);
162
163 #define RUN_PASS(NUM_WARP_Q, NUM_THREAD_Q, DIR) \
164 do { \
165 pass2SelectLists<kThreadsPerBlock, \
166 NUM_WARP_Q, NUM_THREAD_Q, DIR> \
167 <<<grid, block, 0, stream>>>(heapDistances, \
168 heapIndices, \
169 listIndices.data().get(), \
170 prefixSumOffsets, \
171 topQueryToCentroid, \
172 k, \
173 indicesOptions, \
174 outDistances, \
175 outIndices); \
176 CUDA_TEST_ERROR(); \
177 return; /* success */ \
178 } while (0)
179
180 #define RUN_PASS_DIR(DIR) \
181 do { \
182 if (k == 1) { \
183 RUN_PASS(1, 1, DIR); \
184 } else if (k <= 32) { \
185 RUN_PASS(32, 2, DIR); \
186 } else if (k <= 64) { \
187 RUN_PASS(64, 3, DIR); \
188 } else if (k <= 128) { \
189 RUN_PASS(128, 3, DIR); \
190 } else if (k <= 256) { \
191 RUN_PASS(256, 4, DIR); \
192 } else if (k <= 512) { \
193 RUN_PASS(512, 8, DIR); \
194 } else if (k <= 1024) { \
195 RUN_PASS(1024, 8, DIR); \
196 } \
197 } while (0)
198
199 if (chooseLargest) {
200 RUN_PASS_DIR(true);
201 } else {
202 RUN_PASS_DIR(false);
203 }
204
205 // unimplemented / too many resources
206 FAISS_ASSERT_FMT(false, "unimplemented k value (%d)", k);
207
208 #undef RUN_PASS_DIR
209 #undef RUN_PASS
210 }
211
212 } } // namespace