|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights |
| 3 | + * reserved. SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include "../../sample_filter.cuh" // public filter types |
| 8 | +#include "../sample_filter_data.cuh" |
| 9 | +#include "jit_lto_kernels/cagra_filter_payload.cuh" |
| 10 | + |
| 11 | +#include <raft/core/error.hpp> |
| 12 | + |
| 13 | +#include <cuda_runtime_api.h> |
| 14 | + |
| 15 | +#include <cstddef> |
| 16 | +#include <cstdint> |
| 17 | +#include <cstring> |
| 18 | +#include <list> |
| 19 | +#include <mutex> |
| 20 | +#include <type_traits> |
| 21 | +#include <unordered_map> |
| 22 | + |
| 23 | +namespace cuvs::neighbors::cagra::detail { |
| 24 | + |
| 25 | +template <typename PayloadT> |
| 26 | +std::uint64_t cagra_payload_hash(PayloadT const& payload) |
| 27 | +{ |
| 28 | + static_assert(std::is_trivially_copyable_v<PayloadT>); |
| 29 | + constexpr std::uint64_t kOffset = 1469598103934665603ull; |
| 30 | + constexpr std::uint64_t kPrime = 1099511628211ull; |
| 31 | + auto const* bytes = reinterpret_cast<unsigned char const*>(&payload); |
| 32 | + std::uint64_t hash = kOffset; |
| 33 | + for (std::size_t i = 0; i < sizeof(PayloadT); ++i) { |
| 34 | + hash ^= bytes[i]; |
| 35 | + hash *= kPrime; |
| 36 | + } |
| 37 | + return hash; |
| 38 | +} |
| 39 | + |
| 40 | +template <typename PayloadT> |
| 41 | +struct cagra_device_payload_owner { |
| 42 | + struct state { |
| 43 | + PayloadT host_payload{}; |
| 44 | + PayloadT* device_payload{nullptr}; |
| 45 | + cudaStream_t stream{}; |
| 46 | + cudaEvent_t ready_event{}; |
| 47 | + int device{-1}; |
| 48 | + std::mutex mutex; |
| 49 | + |
| 50 | + explicit state(PayloadT payload) : host_payload(payload) {} |
| 51 | + |
| 52 | + ~state() noexcept |
| 53 | + { |
| 54 | + if (device_payload != nullptr) { |
| 55 | + RAFT_CUDA_TRY_NO_THROW(cudaFreeAsync(device_payload, stream)); |
| 56 | + } |
| 57 | + if (ready_event != nullptr) { RAFT_CUDA_TRY_NO_THROW(cudaEventDestroy(ready_event)); } |
| 58 | + } |
| 59 | + |
| 60 | + PayloadT* dev_ptr(cudaStream_t cuda_stream) |
| 61 | + { |
| 62 | + std::lock_guard<std::mutex> lock(mutex); |
| 63 | + if (device_payload == nullptr) { |
| 64 | + RAFT_CUDA_TRY(cudaGetDevice(&device)); |
| 65 | + RAFT_CUDA_TRY(cudaMallocAsync( |
| 66 | + reinterpret_cast<void**>(&device_payload), sizeof(PayloadT), cuda_stream)); |
| 67 | + RAFT_CUDA_TRY(cudaMemcpyAsync( |
| 68 | + device_payload, &host_payload, sizeof(PayloadT), cudaMemcpyHostToDevice, cuda_stream)); |
| 69 | + RAFT_CUDA_TRY(cudaEventCreateWithFlags(&ready_event, cudaEventDisableTiming)); |
| 70 | + RAFT_CUDA_TRY(cudaEventRecord(ready_event, cuda_stream)); |
| 71 | + stream = cuda_stream; |
| 72 | + } else { |
| 73 | + RAFT_CUDA_TRY(cudaStreamWaitEvent(cuda_stream, ready_event, 0)); |
| 74 | + } |
| 75 | + return device_payload; |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + // PayloadT is copied to device by value. Pointer fields inside PayloadT are shallow-copied and |
| 80 | + // must already point to device-addressable memory that remains valid while the cached payload is |
| 81 | + // usable. |
| 82 | + struct cache_key { |
| 83 | + std::uint64_t payload_hash{}; |
| 84 | + int device{}; |
| 85 | + |
| 86 | + bool operator==(cache_key const& other) const |
| 87 | + { |
| 88 | + return payload_hash == other.payload_hash && device == other.device; |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + struct cache_key_hash { |
| 93 | + std::size_t operator()(cache_key const& key) const |
| 94 | + { |
| 95 | + auto seed = static_cast<std::size_t>(key.payload_hash); |
| 96 | + seed ^= static_cast<std::size_t>(key.device) + 0x9e3779b9 + (seed << 6) + (seed >> 2); |
| 97 | + return seed; |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + cagra_device_payload_owner() = default; |
| 102 | + |
| 103 | + void* dev_ptr(PayloadT payload, cudaStream_t stream) const |
| 104 | + { |
| 105 | + int device{}; |
| 106 | + RAFT_CUDA_TRY(cudaGetDevice(&device)); |
| 107 | + |
| 108 | + // Keep cached payload copies for process lifetime to avoid per-search allocation/copy churn. |
| 109 | + // Cross-stream reuse is ordered by each state's ready_event before kernels consume the pointer. |
| 110 | + const auto key = cache_key{cagra_payload_hash(payload), device}; |
| 111 | + state* selected_state{}; |
| 112 | + { |
| 113 | + std::lock_guard<std::mutex> lock(cache_mutex_); |
| 114 | + auto& entries = cache_[key]; |
| 115 | + for (auto& cached : entries) { |
| 116 | + if (std::memcmp(&cached.host_payload, &payload, sizeof(PayloadT)) == 0) { |
| 117 | + selected_state = &cached; |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | + if (selected_state == nullptr) { |
| 122 | + entries.emplace_back(payload); |
| 123 | + selected_state = &entries.back(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return selected_state->dev_ptr(stream); |
| 128 | + } |
| 129 | + |
| 130 | + private: |
| 131 | + mutable std::mutex cache_mutex_; |
| 132 | + mutable std::unordered_map<cache_key, std::list<state>, cache_key_hash> cache_; |
| 133 | +}; |
| 134 | + |
| 135 | +template <typename T> |
| 136 | +struct is_bitset_filter : std::false_type {}; |
| 137 | + |
| 138 | +template <typename bitset_t, typename index_t> |
| 139 | +struct is_bitset_filter<::cuvs::neighbors::filtering::bitset_filter<bitset_t, index_t>> |
| 140 | + : std::true_type {}; |
| 141 | + |
| 142 | +template <typename T> |
| 143 | +struct is_udf_filter : std::false_type {}; |
| 144 | + |
| 145 | +template <> |
| 146 | +struct is_udf_filter<::cuvs::neighbors::filtering::udf_filter> : std::true_type {}; |
| 147 | + |
| 148 | +template <typename SourceIndexT, typename FilterT> |
| 149 | +::cuvs::neighbors::detail::bitset_filter_data_t<SourceIndexT> make_cagra_bitset_filter_storage( |
| 150 | + const FilterT& filter) |
| 151 | +{ |
| 152 | + const auto bitset_view = filter.view(); |
| 153 | + return ::cuvs::neighbors::detail::bitset_filter_data_t<SourceIndexT>{ |
| 154 | + const_cast<std::uint32_t*>(bitset_view.data()), |
| 155 | + static_cast<SourceIndexT>(bitset_view.size()), |
| 156 | + static_cast<SourceIndexT>(bitset_view.get_original_nbits())}; |
| 157 | +} |
| 158 | + |
| 159 | +template <typename PayloadT> |
| 160 | +void* get_cagra_device_payload(PayloadT payload, cudaStream_t stream) |
| 161 | +{ |
| 162 | + static cagra_device_payload_owner<PayloadT> owner; |
| 163 | + return owner.dev_ptr(payload, stream); |
| 164 | +} |
| 165 | + |
| 166 | +template <typename SourceIndexT, typename FilterT> |
| 167 | +void* make_cagra_bitset_filter_payload(const FilterT& filter, cudaStream_t stream) |
| 168 | +{ |
| 169 | + return get_cagra_device_payload(make_cagra_bitset_filter_storage<SourceIndexT>(filter), stream); |
| 170 | +} |
| 171 | + |
| 172 | +template <typename SourceIndexT, typename FilterT> |
| 173 | +void fill_cagra_sample_filter(cagra_sample_filter<SourceIndexT>& out, |
| 174 | + const FilterT& filter, |
| 175 | + cudaStream_t stream) |
| 176 | +{ |
| 177 | + using DecayedFilter = std::decay_t<FilterT>; |
| 178 | + if constexpr (is_bitset_filter<DecayedFilter>::value) { |
| 179 | + out.filter_data = make_cagra_bitset_filter_payload<SourceIndexT>(filter, stream); |
| 180 | + } else if constexpr (is_udf_filter<DecayedFilter>::value) { |
| 181 | + out.filter_data = filter.filter_data; |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +template <typename SourceIndexT, typename FilterT> |
| 186 | +std::uint64_t cagra_filter_payload_hash(const FilterT& filter) |
| 187 | +{ |
| 188 | + using DecayedFilter = std::decay_t<FilterT>; |
| 189 | + if constexpr (is_bitset_filter<DecayedFilter>::value) { |
| 190 | + return cagra_payload_hash(make_cagra_bitset_filter_storage<SourceIndexT>(filter)); |
| 191 | + } else if constexpr (requires { filter.filter; }) { |
| 192 | + return cagra_filter_payload_hash<SourceIndexT>(filter.filter); |
| 193 | + } else { |
| 194 | + return 0; |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +template <typename FilterT> |
| 199 | +void* cagra_filter_data_ptr(const FilterT& filter) |
| 200 | +{ |
| 201 | + using DecayedFilter = std::decay_t<FilterT>; |
| 202 | + if constexpr (is_udf_filter<DecayedFilter>::value) { |
| 203 | + return filter.filter_data; |
| 204 | + } else if constexpr (requires { filter.filter; }) { |
| 205 | + return cagra_filter_data_ptr(filter.filter); |
| 206 | + } else { |
| 207 | + return nullptr; |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +template <typename SampleFilterT> |
| 212 | +std::uint32_t cagra_filter_query_id_offset(const SampleFilterT& sample_filter) |
| 213 | +{ |
| 214 | + if constexpr (requires { |
| 215 | + sample_filter.filter; |
| 216 | + sample_filter.offset; |
| 217 | + }) { |
| 218 | + return sample_filter.offset; |
| 219 | + } else { |
| 220 | + return 0; |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +/// Host: fill @ref cagra_sample_filter from a CAGRA filter object. |
| 225 | +template <typename SourceIndexT, typename SampleFilterT> |
| 226 | +cagra_sample_filter<SourceIndexT> extract_cagra_sample_filter(const SampleFilterT& sample_filter, |
| 227 | + cudaStream_t stream) |
| 228 | +{ |
| 229 | + cagra_sample_filter<SourceIndexT> out; |
| 230 | + if constexpr (requires { |
| 231 | + sample_filter.filter; |
| 232 | + sample_filter.offset; |
| 233 | + }) { |
| 234 | + out.query_id_offset = sample_filter.offset; |
| 235 | + fill_cagra_sample_filter(out, sample_filter.filter, stream); |
| 236 | + } else { |
| 237 | + fill_cagra_sample_filter(out, sample_filter, stream); |
| 238 | + } |
| 239 | + return out; |
| 240 | +} |
| 241 | + |
| 242 | +/// Host: find UDF compile/link metadata only. Query offsets stay in the runtime payload produced |
| 243 | +/// by @ref extract_cagra_sample_filter and are applied before calling the linked sample_filter. |
| 244 | +template <typename SampleFilterT> |
| 245 | +const ::cuvs::neighbors::filtering::udf_filter* get_cagra_udf_filter( |
| 246 | + const SampleFilterT& sample_filter) |
| 247 | +{ |
| 248 | + using DecayedFilter = std::decay_t<SampleFilterT>; |
| 249 | + if constexpr (is_udf_filter<DecayedFilter>::value) { |
| 250 | + return &sample_filter; |
| 251 | + } else if constexpr (requires { sample_filter.filter; }) { |
| 252 | + return get_cagra_udf_filter(sample_filter.filter); |
| 253 | + } else { |
| 254 | + return nullptr; |
| 255 | + } |
| 256 | +} |
| 257 | + |
| 258 | +} // namespace cuvs::neighbors::cagra::detail |
0 commit comments