GPU-Accelerated Cuckoo Filter
Loading...
Searching...
No Matches
helpers.cuh
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <cstdio>
6
7namespace cuckoogpu::detail {
8
14constexpr bool powerOfTwo(size_t n) {
15 return n != 0 && (n & (n - 1)) == 0;
16}
17
25
31constexpr size_t nextPowerOfTwo(size_t n) {
32 if (powerOfTwo(n))
33 return n;
34
35 n--;
36 n |= n >> 1;
37 n |= n >> 2;
38 n |= n >> 4;
39 n |= n >> 8;
40 n |= n >> 16;
41 n |= n >> 32;
42 n++;
43
44 return n;
45}
46
60template <typename TagType, typename WordType>
61__host__ __device__ __forceinline__ constexpr WordType getZeroMask(WordType v) {
62 static_assert(sizeof(WordType) == 4 || sizeof(WordType) == 8, "WordType must be 32 or 64 bits");
63
64 if constexpr (sizeof(WordType) == 8) {
65 if constexpr (sizeof(TagType) == 1) {
66 return (v - 0x0101010101010101ULL) & ~v & 0x8080808080808080ULL;
67 } else if constexpr (sizeof(TagType) == 2) {
68 return (v - 0x0001000100010001ULL) & ~v & 0x8000800080008000ULL;
69 } else if constexpr (sizeof(TagType) == 4) {
70 return (v - 0x0000000100000001ULL) & ~v & 0x8000000080000000ULL;
71 } else {
72 return 0;
73 }
74 } else {
75 if constexpr (sizeof(TagType) == 1) {
76 return (v - 0x01010101U) & ~v & 0x80808080U;
77 } else if constexpr (sizeof(TagType) == 2) {
78 return (v - 0x00010001U) & ~v & 0x80008000U;
79 } else if constexpr (sizeof(TagType) == 4) {
80 return (v - 0x00000001U) & ~v & 0x80000000U;
81 } else {
82 return 0;
83 }
84 }
85}
86
95template <typename TagType, typename WordType>
96__host__ __device__ __forceinline__ constexpr bool hasZero(WordType v) {
98}
99
108template <typename TagType, typename WordType>
110 static_assert(sizeof(WordType) == 4 || sizeof(WordType) == 8, "WordType must be 32 or 64 bits");
111
112 if constexpr (sizeof(WordType) == 8) {
113 if constexpr (sizeof(TagType) == 1) {
114 return static_cast<uint64_t>(tag) * 0x0101010101010101ULL;
115 } else if constexpr (sizeof(TagType) == 2) {
116 return static_cast<uint64_t>(tag) * 0x0001000100010001ULL;
117 } else if constexpr (sizeof(TagType) == 4) {
118 return static_cast<uint64_t>(tag) * 0x0000000100000001ULL;
119 } else {
120 return tag;
121 }
122 } else {
123 if constexpr (sizeof(TagType) == 1) {
124 return static_cast<uint32_t>(tag) * 0x01010101U;
125 } else if constexpr (sizeof(TagType) == 2) {
126 return static_cast<uint32_t>(tag) * 0x00010001U;
127 } else if constexpr (sizeof(TagType) == 4) {
128 return static_cast<uint32_t>(tag);
129 } else {
130 return tag;
131 }
132 }
133}
134
135#if __CUDA_ARCH__ >= 1000
136
151template <typename T>
153 static_assert(sizeof(T) == 4 || sizeof(T) == 8, "T must be uint32_t or uint64_t");
154
155 if constexpr (sizeof(T) == 8) {
156 asm volatile("ld.global.nc.v4.u64 {%0, %1, %2, %3}, [%4];"
157 : "=l"(out[0]), "=l"(out[1]), "=l"(out[2]), "=l"(out[3])
158 : "l"(ptr));
159 } else {
160 asm volatile("ld.global.nc.v8.u32 {%0, %1, %2, %3, %4, %5, %6, %7}, [%8];"
161 : "=r"(out[0]),
162 "=r"(out[1]),
163 "=r"(out[2]),
164 "=r"(out[3]),
165 "=r"(out[4]),
166 "=r"(out[5]),
167 "=r"(out[6]),
168 "=r"(out[7])
169 : "l"(ptr));
170 }
171}
172
173#endif
174
178#define SDIV(x, y) (((x) + (y) - 1) / (y))
179
184#define CUCKOO_CUDA_CALL(err) \
185 do { \
186 cudaError_t err_ = (err); \
187 if (err_ == cudaSuccess) [[likely]] { \
188 break; \
189 } \
190 printf("%s:%d %s\n", __FILE__, __LINE__, cudaGetErrorString(err_)); \
191 exit(err_); \
192 } while (0)
193
203template <typename Kernel>
218
219} // namespace cuckoogpu::detail
constexpr size_t maxOccupancyGridSize(int32_t blockSize, Kernel kernel, size_t dynamicSMemSize)
Calculates the maximum occupancy grid size for a kernel.
Definition helpers.cuh:204
constexpr size_t nextPowerOfTwo(size_t n)
Calculates the next power of two greater than or equal to n.
Definition helpers.cuh:31
__host__ __device__ __forceinline__ constexpr WordType getZeroMask(WordType v)
Returns a bitmask indicating which slots in a packed word are zero.
Definition helpers.cuh:61
__host__ __device__ __forceinline__ constexpr bool hasZero(WordType v)
Checks if a packed word contains a zero slot.
Definition helpers.cuh:96
__host__ __device__ __forceinline__ uint32_t globalThreadId()
Calculates the global thread ID in a 1D grid.
Definition helpers.cuh:22
__host__ __device__ __forceinline__ constexpr WordType replicateTag(TagType tag)
Replicates a tag value across all slots in a word.
Definition helpers.cuh:109
constexpr bool powerOfTwo(size_t n)
Checks if a number is a power of two.
Definition helpers.cuh:14