|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of CUDA Experimental in CUDA C++ Core Libraries, |
| 4 | +// under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. |
| 8 | +// |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#include <cuda/atomic> |
| 12 | +#include <cuda/devices> |
| 13 | +#include <cuda/functional> |
| 14 | +#include <cuda/hierarchy> |
| 15 | +#include <cuda/launch> |
| 16 | +#include <cuda/std/algorithm> |
| 17 | +#include <cuda/std/type_traits> |
| 18 | +#include <cuda/stream> |
| 19 | + |
| 20 | +#include <cuda/experimental/coop.cuh> |
| 21 | +#include <cuda/experimental/group.cuh> |
| 22 | + |
| 23 | +#include <testing.cuh> |
| 24 | + |
| 25 | +#include <c2h/catch2_test_helper.h> |
| 26 | +#include <c2h/extended_types.h> |
| 27 | +#include <c2h/generators.h> |
| 28 | +#include <catch2/matchers/catch_matchers_floating_point.hpp> |
| 29 | + |
| 30 | +constexpr int nwarps_in_group = 3; |
| 31 | +constexpr int warp_size = 32; |
| 32 | + |
| 33 | +/*********************************************************************************************************************** |
| 34 | + * Thread Reduce Wrapper Kernels |
| 35 | + **********************************************************************************************************************/ |
| 36 | + |
| 37 | +struct ReduceKernel |
| 38 | +{ |
| 39 | + template <class Config, int NumItems, class T, class RedOp> |
| 40 | + __device__ void operator()( |
| 41 | + Config config, |
| 42 | + cuda::std::integral_constant<int, NumItems>, |
| 43 | + const T* __restrict__ d_in, |
| 44 | + T* __restrict__ d_out, |
| 45 | + RedOp red_op) |
| 46 | + { |
| 47 | + cudax::this_block block{config}; |
| 48 | + |
| 49 | + using Barriers = cuda::barrier<cuda::thread_scope_block>[1]; |
| 50 | + __shared__ cuda::std::aligned_storage_t<sizeof(Barriers), alignof(Barriers)> barriers_storage; |
| 51 | + auto& barriers = reinterpret_cast<Barriers&>(barriers_storage); |
| 52 | + |
| 53 | + cudax::group group{ |
| 54 | + cuda::warp, block, cudax::group_by<nwarps_in_group, false>{}, cudax::barrier_synchronizer{barriers}}; |
| 55 | + |
| 56 | + // All threads that are not part of the groups should exit early. |
| 57 | + if (!cuda::gpu_thread.is_part_of(group)) |
| 58 | + { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + T thread_data[NumItems]; |
| 63 | + for (int i = 0; i < NumItems; ++i) |
| 64 | + { |
| 65 | + thread_data[i] = d_in[cuda::gpu_thread.rank_as<int>(group) + i * cuda::gpu_thread.count_as<int>(group)]; |
| 66 | + } |
| 67 | + const auto result = cudax::coop::reduce(group, thread_data, red_op); |
| 68 | + |
| 69 | + REQUIRE(result.has_value() == cuda::gpu_thread.is_root_rank(group)); |
| 70 | + if (cuda::gpu_thread.is_root_rank(group)) |
| 71 | + { |
| 72 | + *d_out = result.value(); |
| 73 | + } |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +/*********************************************************************************************************************** |
| 78 | + * Type list definition |
| 79 | + **********************************************************************************************************************/ |
| 80 | + |
| 81 | +using integral_type_list = |
| 82 | + c2h::type_list<cuda::std::int8_t, cuda::std::int16_t, cuda::std::uint16_t, cuda::std::int32_t, cuda::std::int64_t>; |
| 83 | + |
| 84 | +using fp_type_list = c2h::type_list<float, double>; |
| 85 | + |
| 86 | +using operator_integral_list = |
| 87 | + c2h::type_list<cuda::std::plus<>, |
| 88 | + cuda::std::multiplies<>, |
| 89 | + cuda::std::bit_and<>, |
| 90 | + cuda::std::bit_or<>, |
| 91 | + cuda::std::bit_xor<>, |
| 92 | + cuda::minimum<>, |
| 93 | + cuda::maximum<>>; |
| 94 | + |
| 95 | +using operator_fp_list = c2h::type_list<cuda::std::plus<>, cuda::std::multiplies<>, cuda::minimum<>, cuda::maximum<>>; |
| 96 | + |
| 97 | +/*********************************************************************************************************************** |
| 98 | + * Verify results and kernel launch |
| 99 | + **********************************************************************************************************************/ |
| 100 | + |
| 101 | +template <class T> |
| 102 | +void verify_results(const T& expected_data, const T& test_results) |
| 103 | +{ |
| 104 | + if constexpr (cuda::std::is_floating_point_v<T>) |
| 105 | + { |
| 106 | + REQUIRE_THAT(expected_data, Catch::Matchers::WithinRel(test_results, T{0.05})); |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + REQUIRE(expected_data == test_results); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +template <class T, class RedOp> |
| 115 | +void run_thread_reduce_kernel( |
| 116 | + cuda::stream_ref stream, int num_items, const c2h::device_vector<T>& in, c2h::device_vector<T>& out, RedOp red_op) |
| 117 | +{ |
| 118 | + const auto config = cuda::make_config(cuda::grid_dims<1>(), cuda::block_dims<(nwarps_in_group + 2) * warp_size>()); |
| 119 | + const auto in_ptr = thrust::raw_pointer_cast(in.data()); |
| 120 | + const auto out_ptr = thrust::raw_pointer_cast(out.data()); |
| 121 | + const ReduceKernel kernel{}; |
| 122 | + |
| 123 | + switch (num_items) |
| 124 | + { |
| 125 | + case 1: |
| 126 | + cuda::launch(stream, config, kernel, cuda::std::integral_constant<int, 1>{}, in_ptr, out_ptr, red_op); |
| 127 | + break; |
| 128 | + case 2: |
| 129 | + cuda::launch(stream, config, kernel, cuda::std::integral_constant<int, 2>{}, in_ptr, out_ptr, red_op); |
| 130 | + break; |
| 131 | + case 3: |
| 132 | + cuda::launch(stream, config, kernel, cuda::std::integral_constant<int, 3>{}, in_ptr, out_ptr, red_op); |
| 133 | + break; |
| 134 | + case 4: |
| 135 | + cuda::launch(stream, config, kernel, cuda::std::integral_constant<int, 4>{}, in_ptr, out_ptr, red_op); |
| 136 | + break; |
| 137 | + default: |
| 138 | + FAIL("Unsupported number of items"); |
| 139 | + } |
| 140 | + stream.sync(); |
| 141 | +} |
| 142 | + |
| 143 | +constexpr int max_size = 4; |
| 144 | +constexpr int num_seeds = 10; |
| 145 | + |
| 146 | +/*********************************************************************************************************************** |
| 147 | + * Test cases |
| 148 | + **********************************************************************************************************************/ |
| 149 | + |
| 150 | +_CCCL_DIAG_SUPPRESS_MSVC(4244) // warning C4244: '=': conversion from 'int' to '_Tp', possible loss of data |
| 151 | + |
| 152 | +C2H_TEST("reduce/this_warp Integral Type Tests", "[reduce][this_warp]", integral_type_list, operator_integral_list) |
| 153 | +{ |
| 154 | + using value_t = c2h::get<0, TestType>; |
| 155 | + using op_t = c2h::get<1, TestType>; |
| 156 | + constexpr auto reduce_op = op_t{}; |
| 157 | + constexpr auto operator_identity = cuda::identity_element<op_t, value_t>(); |
| 158 | + CAPTURE(c2h::type_name<value_t>(), max_size, c2h::type_name<decltype(reduce_op)>()); |
| 159 | + c2h::device_vector<value_t> d_in(max_size * nwarps_in_group * warp_size); |
| 160 | + c2h::device_vector<value_t> d_out(1); |
| 161 | + c2h::gen(C2H_SEED(num_seeds), d_in, cuda::std::numeric_limits<value_t>::min()); |
| 162 | + c2h::host_vector<value_t> h_in = d_in; |
| 163 | + cuda::stream stream{cuda::devices[0]}; |
| 164 | + for (int num_items = 1; num_items <= max_size; ++num_items) |
| 165 | + { |
| 166 | + auto reference_result = cuda::std::accumulate( |
| 167 | + h_in.begin(), h_in.begin() + num_items * nwarps_in_group * warp_size, operator_identity, reduce_op); |
| 168 | + run_thread_reduce_kernel(stream, num_items, d_in, d_out, reduce_op); |
| 169 | + verify_results(reference_result, c2h::host_vector<value_t>(d_out)[0]); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +C2H_TEST("reduce/this_warp Floating-Point Type Tests", "[reduce][this_warp]", fp_type_list, operator_fp_list) |
| 174 | +{ |
| 175 | + using value_t = c2h::get<0, TestType>; |
| 176 | + using op_t = c2h::get<1, TestType>; |
| 177 | + constexpr auto reduce_op = op_t{}; |
| 178 | + const auto operator_identity = cuda::identity_element<op_t, value_t>(); |
| 179 | + CAPTURE(c2h::type_name<value_t>(), max_size, c2h::type_name<decltype(reduce_op)>()); |
| 180 | + c2h::device_vector<value_t> d_in(max_size * nwarps_in_group * warp_size); |
| 181 | + c2h::device_vector<value_t> d_out(1); |
| 182 | + c2h::gen(C2H_SEED(num_seeds), d_in, cuda::std::numeric_limits<value_t>::min()); |
| 183 | + c2h::host_vector<value_t> h_in = d_in; |
| 184 | + cuda::stream stream{cuda::devices[0]}; |
| 185 | + for (int num_items = 1; num_items <= max_size; ++num_items) |
| 186 | + { |
| 187 | + auto reference_result = cuda::std::accumulate( |
| 188 | + h_in.begin(), h_in.begin() + num_items * nwarps_in_group * warp_size, operator_identity, reduce_op); |
| 189 | + run_thread_reduce_kernel(stream, num_items, d_in, d_out, reduce_op); |
| 190 | + verify_results(reference_result, c2h::host_vector<value_t>(d_out)[0]); |
| 191 | + } |
| 192 | +} |
0 commit comments