|
1 | 1 | /* |
2 | | - * Copyright (c) 2022-2025, NVIDIA CORPORATION. |
| 2 | + * Copyright (c) 2022-2026, NVIDIA CORPORATION. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
21 | 21 | #include <cuco/pair.cuh> |
22 | 22 | #include <cuco/utility/allocator.hpp> |
23 | 23 |
|
| 24 | +#include <cuda/std/bit> |
| 25 | + |
24 | 26 | #include <catch2/catch_template_test_macros.hpp> |
25 | 27 |
|
| 28 | +#include <cstdint> |
| 29 | + |
26 | 30 | TEMPLATE_TEST_CASE_SIG("utility storage tests", |
27 | 31 | "", |
28 | 32 | ((typename Key, typename Value), Key, Value), |
@@ -96,4 +100,107 @@ TEMPLATE_TEST_CASE_SIG("utility storage tests", |
96 | 100 | STATIC_REQUIRE(num_buckets == size / bucket_size); |
97 | 101 | STATIC_REQUIRE(capacity == gold_capacity); |
98 | 102 | } |
| 103 | + |
| 104 | + SECTION("Storage alignment constant is correct for pairs.") |
| 105 | + { |
| 106 | + using storage_ref_type = |
| 107 | + cuco::bucket_storage_ref<cuco::pair<Key, Value>, bucket_size, cuco::extent<std::size_t>>; |
| 108 | + using bucket_type = typename storage_ref_type::bucket_type; |
| 109 | + |
| 110 | + constexpr auto alignment = storage_ref_type::alignment; |
| 111 | + constexpr auto expected_align = |
| 112 | + cuda::std::min(cuda::std::bit_ceil(sizeof(bucket_type)), std::size_t{16}); |
| 113 | + |
| 114 | + STATIC_REQUIRE(alignment == expected_align); |
| 115 | + STATIC_REQUIRE(cuda::std::has_single_bit(alignment)); |
| 116 | + } |
| 117 | + |
| 118 | + SECTION("Storage alignment constant is correct for keys.") |
| 119 | + { |
| 120 | + using storage_ref_type = cuco::bucket_storage_ref<Key, bucket_size, cuco::extent<std::size_t>>; |
| 121 | + using bucket_type = typename storage_ref_type::bucket_type; |
| 122 | + |
| 123 | + constexpr auto alignment = storage_ref_type::alignment; |
| 124 | + constexpr auto expected_align = |
| 125 | + cuda::std::min(cuda::std::bit_ceil(sizeof(bucket_type)), std::size_t{16}); |
| 126 | + |
| 127 | + STATIC_REQUIRE(alignment == expected_align); |
| 128 | + STATIC_REQUIRE(cuda::std::has_single_bit(alignment)); |
| 129 | + } |
| 130 | + |
| 131 | + SECTION("Storage data pointer is aligned to bucket boundary for pairs.") |
| 132 | + { |
| 133 | + auto s = cuco::bucket_storage<cuco::pair<Key, Value>, |
| 134 | + bucket_size, |
| 135 | + cuco::extent<std::size_t>, |
| 136 | + allocator_type>( |
| 137 | + cuco::extent{size}, allocator, cuda::stream_ref{cudaStream_t{nullptr}}); |
| 138 | + |
| 139 | + auto const ptr = reinterpret_cast<std::uintptr_t>(s.data()); |
| 140 | + auto const alignment = decltype(s)::ref_type::alignment; |
| 141 | + |
| 142 | + REQUIRE((ptr % alignment) == 0); |
| 143 | + } |
| 144 | + |
| 145 | + SECTION("Storage data pointer is aligned to bucket boundary for keys.") |
| 146 | + { |
| 147 | + auto s = cuco::bucket_storage<Key, bucket_size, cuco::extent<std::size_t>, allocator_type>( |
| 148 | + cuco::extent{size}, allocator, cuda::stream_ref{cudaStream_t{nullptr}}); |
| 149 | + |
| 150 | + auto const ptr = reinterpret_cast<std::uintptr_t>(s.data()); |
| 151 | + auto const alignment = decltype(s)::ref_type::alignment; |
| 152 | + |
| 153 | + REQUIRE((ptr % alignment) == 0); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +TEMPLATE_TEST_CASE_SIG("bucket storage alignment with different bucket sizes", |
| 158 | + "", |
| 159 | + ((typename T, int BucketSize), T, BucketSize), |
| 160 | + (int32_t, 1), |
| 161 | + (int32_t, 2), |
| 162 | + (int32_t, 4), |
| 163 | + (int64_t, 1), |
| 164 | + (int64_t, 2), |
| 165 | + (cuco::pair<int32_t, int32_t>, 1), |
| 166 | + (cuco::pair<int32_t, int32_t>, 2), |
| 167 | + (cuco::pair<int64_t, int64_t>, 1)) |
| 168 | +{ |
| 169 | + constexpr std::size_t size{1'000}; |
| 170 | + |
| 171 | + using allocator_type = cuco::cuda_allocator<char>; |
| 172 | + using storage_type = |
| 173 | + cuco::bucket_storage<T, BucketSize, cuco::extent<std::size_t>, allocator_type>; |
| 174 | + using storage_ref_type = typename storage_type::ref_type; |
| 175 | + using bucket_type = typename storage_ref_type::bucket_type; |
| 176 | + |
| 177 | + auto allocator = allocator_type{}; |
| 178 | + |
| 179 | + SECTION("Alignment constant is power of 2 and capped at 16.") |
| 180 | + { |
| 181 | + constexpr auto alignment = storage_ref_type::alignment; |
| 182 | + |
| 183 | + STATIC_REQUIRE(cuda::std::has_single_bit(alignment)); |
| 184 | + STATIC_REQUIRE(alignment <= 16); |
| 185 | + STATIC_REQUIRE(alignment >= sizeof(T)); |
| 186 | + } |
| 187 | + |
| 188 | + SECTION("Alignment matches expected value.") |
| 189 | + { |
| 190 | + constexpr auto alignment = storage_ref_type::alignment; |
| 191 | + constexpr auto expected = |
| 192 | + cuda::std::min(cuda::std::bit_ceil(sizeof(bucket_type)), std::size_t{16}); |
| 193 | + |
| 194 | + STATIC_REQUIRE(alignment == expected); |
| 195 | + } |
| 196 | + |
| 197 | + SECTION("Data pointer is aligned to bucket boundary.") |
| 198 | + { |
| 199 | + auto s = storage_type(cuco::extent{size}, allocator, cuda::stream_ref{cudaStream_t{nullptr}}); |
| 200 | + |
| 201 | + auto const ptr = reinterpret_cast<std::uintptr_t>(s.data()); |
| 202 | + auto const alignment = storage_ref_type::alignment; |
| 203 | + |
| 204 | + REQUIRE((ptr % alignment) == 0); |
| 205 | + } |
99 | 206 | } |
0 commit comments