Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
Clojure
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Helion
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Yul (Solidity IR)
Zig
Javascript
GIMPLE
Ygen
sway
cuda source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
10.0.0 sm_75 CUDA-10.2
10.0.1 sm_75 CUDA-10.2
11.0.0 sm_75 CUDA-10.2
16.0.0 sm_90 CUDA-11.8
17.0.1(libc++) sm_90 CUDA-12.1
18.1.0(libc++) sm_90 CUDA-12.3.1
19.1.0 sm_90 CUDA-12.5.1
20.1.0 sm_90 CUDA-12.5.1
20.1.0 sm_90 CUDA-12.6.1
20.1.0 sm_90 CUDA-12.6.2
NVCC 10.0.130
NVCC 10.1.105
NVCC 10.1.168
NVCC 10.1.243
NVCC 10.2.89
NVCC 11.0.2
NVCC 11.0.3
NVCC 11.1.0
NVCC 11.1.1
NVCC 11.2.0
NVCC 11.2.1
NVCC 11.2.2
NVCC 11.3.0
NVCC 11.3.1
NVCC 11.4.0
NVCC 11.4.1
NVCC 11.4.2
NVCC 11.4.3
NVCC 11.4.4
NVCC 11.5.0
NVCC 11.5.1
NVCC 11.5.2
NVCC 11.6.0
NVCC 11.6.1
NVCC 11.6.2
NVCC 11.7.0
NVCC 11.7.1
NVCC 11.8.0
NVCC 12.0.0
NVCC 12.0.1
NVCC 12.1.0
NVCC 12.2.1
NVCC 12.3.1
NVCC 12.4.1
NVCC 12.5.1
NVCC 12.6.1
NVCC 12.6.2
NVCC 12.8.1
NVCC 12.9.0
NVCC 12.9.1
NVCC 13.0.0
NVCC 13.0.1
NVCC 13.0.2
NVCC 9.1.85
NVCC 9.2.88
NVRTC 11.0.2
NVRTC 11.0.3
NVRTC 11.1.0
NVRTC 11.1.1
NVRTC 11.2.0
NVRTC 11.2.1
NVRTC 11.2.2
NVRTC 11.3.0
NVRTC 11.3.1
NVRTC 11.4.0
NVRTC 11.4.1
NVRTC 11.5.0
NVRTC 11.5.1
NVRTC 11.5.2
NVRTC 11.6.0
NVRTC 11.6.1
NVRTC 11.6.2
NVRTC 11.7.0
NVRTC 11.7.1
NVRTC 11.8.0
NVRTC 12.0.0
NVRTC 12.0.1
NVRTC 12.1.0
NVRTC 12.2.1
NVRTC 12.3.1
NVRTC 12.4.1
NVRTC 12.5.1
NVRTC 12.6.1
NVRTC 12.6.2
NVRTC 12.8.1
NVRTC 12.9.0
NVRTC 12.9.1
NVRTC 13.0.0
NVRTC 13.0.1
NVRTC 13.0.2
clang 7.0.0 sm_70 CUDA-9.1
clang 8.0.0 sm_75 CUDA-10.0
clang 9.0.0 sm_75 CUDA-10.1
clang rocm-4.5.2
clang rocm-5.0.2
clang rocm-5.1.3
clang rocm-5.2.3
clang rocm-5.3.2
clang rocm-5.7.0
clang rocm-6.0.2
clang rocm-6.1.2
clang rocm-6.2.4
clang rocm-6.3.3
clang rocm-6.4.0
clang rocm-7.0.1
clang staging rocm-6.1.2
clang staging rocm-6.2.4
clang staging rocm-6.3.3
clang staging rocm-6.4.0
clang staging rocm-7.0.1
clang trunk rocm-6.1.2
clang trunk rocm-6.2.4
clang trunk rocm-6.3.3
clang trunk rocm-6.4.0
clang trunk rocm-7.0.1
trunk sm_100a CUDA-12.8.1
Options
Source code
/* * Copyright (c) 2025, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include <cuco/static_map.cuh> #include <cuda/functional> #include <cuda/std/tuple> #include <thrust/detail/raw_reference_cast.h> #include <thrust/device_vector.h> #include <thrust/host_vector.h> #include <thrust/iterator/counting_iterator.h> #include <thrust/iterator/transform_iterator.h> #include <iostream> /** * @file heterogeneous_lookup_example.cu * * @brief Demonstrates how to perform heterogeneous lookups with `cuco::static_map`. * * This example demonstrates heterogeneous lookup, which allows you to perform lookups with a key * type that is different from the container's key type, without having to first construct an * object of the container's key type. * * In many workflows the format of the keys used when inserting into a hash table differs from the * format that is available at query time. This example stores keys as `cuco::pair<int, int>` * representing `(sensor_id, channel)` but performs lookups directly using 3-element tuples * `(sensor_id, channel, timestamp)` without needing to construct intermediate `cuco::pair` objects. * * Heterogeneous lookup is enabled by custom hash and equality functors that can operate on * "compatible" key types. The functors only consider the first two elements, allowing both * the stored `cuco::pair` and query `tuple` types to interoperate transparently and efficiently. */ using stored_key = cuco::pair<int, int>; // Key type used for insertion: (sensor_id, channel) using probe_key = cuda::std::tuple<int, int, int>; // Key type used for querying: (sensor_id, channel, timestamp) using value_type = float; // Declare that value_type is bitwise comparable since float doesn't have unique object // representations CUCO_DECLARE_BITWISE_COMPARABLE(value_type); // Heterogeneous hasher that can hash both cuco::pair and tuple types without conversion. // The template allows it to accept any key type and extract the first two elements. struct heterogeneous_hasher { template <typename Key> __device__ std::size_t operator()(Key const& key) const { auto const& ref = thrust::raw_reference_cast(key); auto const major = cuda::std::get<0>(ref); // Works for both pair.first and get<0>(tuple) auto const minor = cuda::std::get<1>(ref); // Works for both pair.second and get<1>(tuple) return static_cast<std::size_t>(major * 131 + minor); } }; // Heterogeneous equality functor that can compare cuco::pair and tuple types without conversion. // The template allows it to accept any combination of key types and compare their first two // elements. struct heterogeneous_key_equal { template <typename LHS, typename RHS> __device__ bool operator()(LHS const& lhs, RHS const& rhs) const { auto const& left = thrust::raw_reference_cast(lhs); auto const& right = thrust::raw_reference_cast(rhs); return (cuda::std::get<0>(left) == cuda::std::get<0>(right)) and // Compare first elements (cuda::std::get<1>(left) == cuda::std::get<1>(right)); // Compare second elements } }; int main() { constexpr std::size_t num_entries = 4; auto constexpr empty_key = stored_key{-1, -1}; auto constexpr empty_value = value_type{-1.0f}; // Allocate a map with ~50% load factor. auto map = cuco::static_map{cuco::extent<std::size_t>{num_entries * 2}, cuco::empty_key{empty_key}, cuco::empty_value{empty_value}, heterogeneous_key_equal{}, cuco::linear_probing<1, heterogeneous_hasher>{heterogeneous_hasher{}}}; thrust::device_vector<stored_key> d_keys = { stored_key{101, 3}, stored_key{104, 8}, stored_key{215, 1}, stored_key{305, 0}, }; thrust::device_vector<value_type> d_values = {36.5f, 41.2f, 27.1f, 33.8f}; auto pairs_begin = thrust::make_transform_iterator( thrust::counting_iterator{0}, cuda::proclaim_return_type<cuco::pair<stored_key, value_type>>( [keys = d_keys.begin(), values = d_values.begin()] __device__(int i) { return cuco::pair<stored_key, value_type>{keys[i], values[i]}; })); map.insert(pairs_begin, pairs_begin + num_entries); // Query using 3-element tuples that include an additional timestamp field. // The heterogeneous hash and equality functors only consider the first two components // (sensor_id, channel) when comparing against the stored cuco::pair keys. thrust::device_vector<probe_key> d_queries{ probe_key{101, 3, 1210}, // present in the map probe_key{215, 1, 1345}, // present in the map probe_key{999, 4, 2000}, // missing entry }; thrust::device_vector<bool> d_contains(d_queries.size()); map.contains(d_queries.begin(), d_queries.end(), d_contains.begin()); thrust::device_vector<value_type> d_found(d_queries.size()); map.find(d_queries.begin(), d_queries.end(), d_found.begin()); // Copy results back to host for printing thrust::host_vector<probe_key> h_queries = d_queries; thrust::host_vector<bool> h_contains = d_contains; thrust::host_vector<value_type> h_found = d_found; for (std::size_t i = 0; i < h_queries.size(); ++i) { auto const& query = h_queries[i]; auto const present = h_contains[i]; std::cout << "Lookup tuple (sensor " << cuda::std::get<0>(query) << ", channel " << cuda::std::get<1>(query) << ", timestamp " << cuda::std::get<2>(query) << ") -> " << (present ? "found" : "missing"); if (present) { std::cout << ", stored value = " << h_found[i]; } std::cout << "\n"; } return 0; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree