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
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
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
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 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
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 staging rocm-6.1.2
clang staging rocm-6.2.4
clang staging rocm-6.3.3
clang staging rocm-6.4.0
clang trunk rocm-6.1.2
clang trunk rocm-6.2.4
clang trunk rocm-6.3.3
clang trunk rocm-6.4.0
trunk sm_100a CUDA-12.8.1
Options
Source code
/* * Copyright (c) 2021-2022, 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 <thrust/device_vector.h> #include <thrust/iterator/counting_iterator.h> #include <thrust/iterator/transform_iterator.h> #include <thrust/logical.h> #include <thrust/transform.h> // User-defined key type #if !defined(CUCO_HAS_INDEPENDENT_THREADS) struct custom_key_type { int32_t a; int32_t b; __host__ __device__ custom_key_type() {} __host__ __device__ custom_key_type(int32_t x) : a{x}, b{x} {} }; #else // Key type larger than 8B only supported for sm_70 and up struct custom_key_type { int32_t a; int32_t b; int32_t c; __host__ __device__ custom_key_type() {} __host__ __device__ custom_key_type(int32_t x) : a{x}, b{x}, c{x} {} // Device equality operator is mandatory due to libcudacxx bug: // https://github.com/NVIDIA/libcudacxx/issues/223 __device__ bool operator==(custom_key_type const& other) const { return a == other.a and b == other.b and c == other.c; } }; #endif // User-defined value type struct custom_value_type { int32_t f; int32_t s; __host__ __device__ custom_value_type() {} __host__ __device__ custom_value_type(int32_t x) : f{x}, s{x} {} }; // User-defined device hash callable struct custom_hash { template <typename key_type> __device__ uint32_t operator()(key_type k) { return k.a; }; }; // User-defined device key equal callable struct custom_key_equals { template <typename key_type> __device__ bool operator()(key_type const& lhs, key_type const& rhs) { return lhs.a == rhs.a; } }; int main(void) { constexpr std::size_t num_pairs = 80'000; // Set emtpy sentinels auto const empty_key_sentinel = custom_key_type{-1}; auto const empty_value_sentinel = custom_value_type{-1}; // Create an iterator of input key/value pairs auto pairs_begin = thrust::make_transform_iterator( thrust::make_counting_iterator<int32_t>(0), [] __device__(auto i) { return cuco::make_pair(custom_key_type{i}, custom_value_type{i}); }); // Construct a map with 100,000 slots using the given empty key/value sentinels. Note the // capacity is chosen knowing we will insert 80,000 keys, for an load factor of 80%. cuco::static_map<custom_key_type, custom_value_type> map{ 100'000, cuco::empty_key{empty_key_sentinel}, cuco::empty_value{empty_value_sentinel}}; // Inserts 80,000 pairs into the map by using the custom hasher and custom equality callable map.insert(pairs_begin, pairs_begin + num_pairs, custom_hash{}, custom_key_equals{}); // Reproduce inserted keys auto insert_keys = thrust::make_transform_iterator(thrust::make_counting_iterator<int32_t>(0), [] __device__(auto i) { return custom_key_type{i}; }); thrust::device_vector<bool> contained(num_pairs); // Determine if all the inserted keys can be found by using the same hasher and equality // function as `insert`. If a key `insert_keys[i]` doesn't exist, `contained[i] == false`. map.contains( insert_keys, insert_keys + num_pairs, contained.begin(), custom_hash{}, custom_key_equals{}); // This will fail due to inconsistent hash and key equal. // map.contains(insert_keys, insert_keys + num_pairs, contained.begin()); // All inserted keys are contained assert( thrust::all_of(contained.begin(), contained.end(), [] __device__(auto const& b) { return b; })); 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