11 #include "IVFUtils.cuh"
12 #include "../utils/DeviceUtils.h"
13 #include "../utils/Limits.cuh"
14 #include "../utils/Select.cuh"
15 #include "../utils/StaticUtils.h"
16 #include "../utils/Tensor.cuh"
23 namespace faiss {
namespace gpu {
27 inline __device__
int binarySearchForBucket(
int* prefixSumOffsets,
33 while (end - start > 0) {
34 int mid = start + (end - start) / 2;
36 int midVal = prefixSumOffsets[mid];
47 assert(start != size);
52 template <
int ThreadsPerBlock,
57 pass2SelectLists(Tensor<float, 2, true> heapDistances,
58 Tensor<int, 2, true> heapIndices,
60 Tensor<int, 2, true> prefixSumOffsets,
61 Tensor<int, 2, true> topQueryToCentroid,
64 Tensor<float, 2, true> outDistances,
65 Tensor<long, 2, true> outIndices) {
66 constexpr
int kNumWarps = ThreadsPerBlock / kWarpSize;
68 __shared__
float smemK[kNumWarps * NumWarpQ];
69 __shared__
int smemV[kNumWarps * NumWarpQ];
71 constexpr
auto kInit = Dir ? kFloatMin : kFloatMax;
72 BlockSelect<float, int, Dir, Comparator<float>,
73 NumWarpQ, NumThreadQ, ThreadsPerBlock>
74 heap(kInit, -1, smemK, smemV, k);
76 auto queryId = blockIdx.x;
77 int num = heapDistances.getSize(1);
78 int limit = utils::roundDown(num, kWarpSize);
81 auto heapDistanceStart = heapDistances[queryId];
85 for (; i < limit; i += blockDim.x) {
86 heap.add(heapDistanceStart[i], i);
91 heap.addThreadQ(heapDistanceStart[i], i);
97 for (
int i = threadIdx.x; i < k; i += blockDim.x) {
98 outDistances[queryId][i] = smemK[i];
115 int offset = heapIndices[queryId][v];
120 int probe = binarySearchForBucket(prefixSumOffsets[queryId].data(),
121 prefixSumOffsets.getSize(1),
126 int listId = topQueryToCentroid[queryId][probe];
130 int listStart = *(prefixSumOffsets[queryId][probe].data() - 1);
131 int listOffset = offset - listStart;
134 if (opt == INDICES_32_BIT) {
135 index = (long) ((
int*) listIndices[listId])[listOffset];
136 }
else if (opt == INDICES_64_BIT) {
137 index = ((
long*) listIndices[listId])[listOffset];
139 index = ((long) listId << 32 | (
long) listOffset);
143 outIndices[queryId][i] = index;
148 runPass2SelectLists(Tensor<float, 2, true>& heapDistances,
149 Tensor<int, 2, true>& heapIndices,
150 thrust::device_vector<void*>& listIndices,
151 IndicesOptions indicesOptions,
152 Tensor<int, 2, true>& prefixSumOffsets,
153 Tensor<int, 2, true>& topQueryToCentroid,
156 Tensor<float, 2, true>& outDistances,
157 Tensor<long, 2, true>& outIndices,
158 cudaStream_t stream) {
159 constexpr
auto kThreadsPerBlock = 128;
161 auto grid = dim3(topQueryToCentroid.getSize(0));
162 auto block = dim3(kThreadsPerBlock);
164 #define RUN_PASS(NUM_WARP_Q, NUM_THREAD_Q, DIR) \
166 pass2SelectLists<kThreadsPerBlock, \
167 NUM_WARP_Q, NUM_THREAD_Q, DIR> \
168 <<<grid, block, 0, stream>>>(heapDistances, \
170 listIndices.data().get(), \
172 topQueryToCentroid, \
181 #define RUN_PASS_DIR(DIR) \
184 RUN_PASS(1, 1, DIR); \
185 } else if (k <= 32) { \
186 RUN_PASS(32, 2, DIR); \
187 } else if (k <= 64) { \
188 RUN_PASS(64, 3, DIR); \
189 } else if (k <= 128) { \
190 RUN_PASS(128, 3, DIR); \
191 } else if (k <= 256) { \
192 RUN_PASS(256, 4, DIR); \
193 } else if (k <= 512) { \
194 RUN_PASS(512, 8, DIR); \
195 } else if (k <= 1024) { \
196 RUN_PASS(1024, 8, DIR); \
207 FAISS_ASSERT_FMT(
false,
"unimplemented k value (%d)", k);