21#include <cuda_runtime.h>
28namespace fss::cuckoo_hash {
32constexpr double Log2(
double x) {
33 if (x <= 0)
return -1e308;
44 double y = (m - 1.0) / (m + 1.0);
45 double y2 = y * y, sum = 0, term = y;
46 for (
int k = 0; k < 40; ++k) {
47 sum += term / (2 * k + 1);
50 return e + 2.0 * sum / 0.6931471805599453;
53constexpr double Ceil(
double x) {
54 auto i =
static_cast<long long>(x);
55 return (x >
static_cast<double>(i)) ?
static_cast<double>(i + 1) :
static_cast<double>(i);
81 double td =
static_cast<double>(t);
82 double e = (
static_cast<double>(lambda) + 130.0 + detail::Log2(td)) / 123.5;
83 return static_cast<int>(detail::Ceil(e * td));
94template <
typename Prp,
typename In,
int kappa = 3>
114 std::pair<int, int>
Locate(int4 sigma, In x,
int k, __uint128_t n,
int b_size) {
115 __uint128_t val =
static_cast<__uint128_t
>(x) + n * k;
116 __uint128_t domain = n * kappa;
117 __uint128_t y = prp.Permu(sigma, val, domain);
119 auto bs =
static_cast<__uint128_t
>(b_size);
120 int bucket =
static_cast<int>(y / bs);
121 int index =
static_cast<int>(y % bs);
122 return {bucket, index};
135template <
typename Prp,
typename In,
int kappa = 3>
154 int Run(std::span<const In> as,
int m, int4 sigma, __uint128_t n,
int b_size,
int ch_retry,
155 std::span<std::pair<int, int>> table) {
157 int t =
static_cast<int>(as.size());
160 for (
int i = 0; i < m; ++i) {
164 std::mt19937 rng(42);
166 for (
int omega = 0; omega < t; ++omega) {
168 int cur_k =
static_cast<int>(rng() % kappa);
172 auto [bucket, _index] = hasher.Locate(sigma, as[cur_idx], cur_k, n, b_size);
176 if (table[bucket].first == -1) {
178 table[bucket] = {cur_idx, cur_k};
183 int evicted_idx = table[bucket].first;
184 int evicted_k = table[bucket].second;
185 table[bucket] = {cur_idx, cur_k};
188 cur_idx = evicted_idx;
189 cur_k =
static_cast<int>(rng() % kappa);
192 if (evictions > ch_retry) {
Small-domain pseudorandom permutation (PRP) interface.
Definition prp.cuh:23
constexpr int ChBucket(int t, int lambda)
Compute the number of Cuckoo-hash buckets from Lemma 5 of the paper, simplified per Remark 1.
Definition cuckoo_hash.cuh:76
Pseudorandom permutation (PRP) interface.
Compact Cuckoo hashing (Algorithm 4 from the paper).
Definition cuckoo_hash.cuh:137
int Run(std::span< const In > as, int m, int4 sigma, __uint128_t n, int b_size, int ch_retry, std::span< std::pair< int, int > > table)
Run Cuckoo hashing to place elements into a table.
Definition cuckoo_hash.cuh:154
PRP-based hash for Cuckoo hashing.
Definition cuckoo_hash.cuh:96
std::pair< int, int > Locate(int4 sigma, In x, int k, __uint128_t n, int b_size)
Compute bucket index and within-bucket index for hash function k.
Definition cuckoo_hash.cuh:114