CPPuddle
Loading...
Searching...
No Matches
kokkos_aggregation_util.hpp
Go to the documentation of this file.
1// LANL Copyright Notice:
2//
3//© 2026. Triad National Security, LLC. All rights reserved.
4//
5// This program was produced under U.S. Government contract 89233218CNA000001
6// for Los Alamos National Laboratory (LANL), which is operated by Triad
7// National Security, LLC for the U.S. Department of Energy/National Nuclear
8// Security Administration. All rights in the program are reserved by Triad
9// National Security, LLC, and the U.S. Department of Energy/National Nuclear
10// Security Administration. The Government is granted for itself and others
11// acting on its behalf a nonexclusive, paid-up, irrevocable worldwide license
12// in this material to reproduce, prepare. derivative works, distribute copies
13// to the public, perform publicly and display publicly, and to permit others
14// to do so.
15//
16//
17// Distributed under the Boost Software License, Version 1.0. (See accompanying
18// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
19
24
25// I originally developed and tested these utilities within Octotiger. See:
26// STEllAR-GROUP/octotiger/pull/469 and STEllAR-GROUP/octotiger/pull/487
27// However, I think they are better fit for CPPuddle as they can be used
28// independent of Octotiger with the work aggregation
29#ifndef KOKKOS_AGGREGATION_UTIL_HPP
30#define KOKKOS_AGGREGATION_UTIL_HPP
31#include <hpx/futures/future.hpp>
32//#define KOKKOS_OPT_RANGE_AGGRESSIVE_VECTORIZATION
33#include <hpx/kokkos/executors.hpp>
34#include <Kokkos_Core.hpp>
35#include <hpx/kokkos.hpp>
36
39#ifdef __NVCC__
40#include <cuda/std/tuple>
41#if defined(HPX_CUDA_VERSION) && (HPX_CUDA_VERSION < 1202)
42// cuda::std::tuple structured bindings are broken in CUDA < 1202
43// See https://github.com/NVIDIA/libcudacxx/issues/316
44// According to https://github.com/NVIDIA/libcudacxx/pull/317 the fix for this
45// is to move tuple element and tuple size into the std namespace
46// which the following snippet does. This is only necessary for old CUDA versions
47// the newer ones contain a fix for this issue
48namespace std {
49 template<size_t _Ip, class... _Tp>
50 struct tuple_element<_Ip, _CUDA_VSTD::tuple<_Tp...>>
51 : _CUDA_VSTD::tuple_element<_Ip, _CUDA_VSTD::tuple<_Tp...>> {};
52 template <class... _Tp>
53 struct tuple_size<_CUDA_VSTD::tuple<_Tp...>>
54 : _CUDA_VSTD::tuple_size<_CUDA_VSTD::tuple<_Tp...>> {};
55}
56#endif
57#endif
58
59#if defined(__CUDACC__)
60#define CPPUDDLE_HOST_DEVICE_METHOD __host__ __device__
61#elif (defined(__clang__) && defined(__HIP__)) // for HIP compilation
62#define CPPUDDLE_HOST_DEVICE_METHOD __host__ __device__
63#else
64#define CPPUDDLE_HOST_DEVICE_METHOD
65#endif
66
67namespace cppuddle {
68namespace kernel_aggregation {
69
71template <typename Agg_view_t>
72CPPUDDLE_HOST_DEVICE_METHOD typename Agg_view_t::view_type
73get_slice_subview(const size_t slice_id, const size_t max_slices,
74 const Agg_view_t &agg_view) {
75 const size_t slice_size = agg_view.size() / max_slices;
76 return Kokkos::subview(
77 agg_view, std::make_pair<size_t, size_t>(slice_id * slice_size,
78 (slice_id + 1) * slice_size));
79}
80
83template <typename Integer,
84 std::enable_if_t<std::is_integral<Integer>::value, bool> = true,
85 typename Agg_view_t, typename... Args>
87map_views_to_slice(const Integer slice_id, const Integer max_slices,
88 const Agg_view_t &current_arg, const Args &...rest) {
89 static_assert(Kokkos::is_view<typename Agg_view_t::view_type>::value,
90 "Argument not an aggregated view");
91#if defined(HPX_COMPUTE_DEVICE_CODE) && defined(__NVCC__)
92 if constexpr (sizeof...(Args) > 0) {
93 return cuda::std::tuple_cat(
94 cuda::std::make_tuple(
95 get_slice_subview(slice_id, max_slices, current_arg)),
96 map_views_to_slice(slice_id, max_slices, rest...));
97 } else {
98 return cuda::std::make_tuple(
99 get_slice_subview(slice_id, max_slices, current_arg));
100 }
101#else
102 if constexpr (sizeof...(Args) > 0) {
103 return std::tuple_cat(
104 std::make_tuple(get_slice_subview(slice_id, max_slices, current_arg)),
105 map_views_to_slice(slice_id, max_slices, rest...));
106 } else {
107 return std::make_tuple(
108 get_slice_subview(slice_id, max_slices, current_arg));
109 }
110#endif
111}
112
115template <
116 typename Agg_executor_t, typename Agg_view_t,
117 std::enable_if_t<Kokkos::is_view<typename Agg_view_t::view_type>::value,
118 bool> = true,
119 typename... Args>
121map_views_to_slice(const Agg_executor_t &agg_exec,
122 const Agg_view_t &current_arg, const Args &...rest) ->
123 std::tuple<typename Agg_view_t::view_type,
124 typename Args::view_type...> {
125 const size_t slice_id = agg_exec.id;
126 const size_t max_slices = agg_exec.max_slices;
127 static_assert(Kokkos::is_view<typename Agg_view_t::view_type>::value,
128 "Argument not an aggregated view");
129 if constexpr (sizeof...(Args) > 0) {
130 return std::tuple_cat(
131 std::make_tuple(get_slice_subview(slice_id, max_slices, current_arg)),
132 map_views_to_slice(agg_exec, rest...));
133 } else {
134 return std::make_tuple(
135 get_slice_subview(slice_id, max_slices, current_arg));
136 }
137}
138
140template <typename Agg_executor_t, typename TargetView_t, typename SourceView_t>
141void aggregated_deep_copy(Agg_executor_t &agg_exec, TargetView_t &target,
142 SourceView_t &source) {
143 if (agg_exec.sync_aggregation_slices()) {
144 Kokkos::deep_copy(agg_exec.get_underlying_executor().instance(), target,
145 source);
146 }
147}
148
150template <typename Agg_executor_t, typename TargetView_t, typename SourceView_t>
151void aggregated_deep_copy(Agg_executor_t &agg_exec, TargetView_t &target,
152 SourceView_t &source, int elements_per_slice) {
153 if (agg_exec.sync_aggregation_slices()) {
154 const size_t number_slices = agg_exec.number_slices;
155 auto target_slices = Kokkos::subview(
156 target,
157 std::make_pair<size_t, size_t>(0, number_slices * elements_per_slice));
158 auto source_slices = Kokkos::subview(
159 source,
160 std::make_pair<size_t, size_t>(0, number_slices * elements_per_slice));
161 Kokkos::deep_copy(agg_exec.get_underlying_executor().instance(),
162 target_slices, source_slices);
163 }
164}
165
167template <typename executor_t, typename TargetView_t, typename SourceView_t>
168hpx::shared_future<void> aggregrated_deep_copy_async(
169 typename Aggregated_Executor<executor_t>::Executor_Slice &agg_exec,
170 TargetView_t &target, SourceView_t &source) {
171 const size_t gpu_id = agg_exec.parent.gpu_id;
172 auto launch_copy_lambda =
173 [gpu_id](TargetView_t &target, SourceView_t &source,
174 executor_t &exec) -> hpx::shared_future<void> {
177 return hpx::kokkos::deep_copy_async(exec.instance(), target, source);
178 };
179 return agg_exec.wrap_async(launch_copy_lambda, target, source,
180 agg_exec.get_underlying_executor());
181}
182
184template <typename executor_t, typename TargetView_t, typename SourceView_t>
185hpx::shared_future<void> aggregrated_deep_copy_async(
186 typename Aggregated_Executor<executor_t>::Executor_Slice &agg_exec,
187 TargetView_t &target, SourceView_t &source, int elements_per_slice) {
188 const size_t number_slices = agg_exec.number_slices;
189 const size_t gpu_id = agg_exec.parent.gpu_id;
190 auto launch_copy_lambda = [gpu_id, elements_per_slice, number_slices](
191 TargetView_t &target, SourceView_t &source,
192 executor_t &exec) -> hpx::shared_future<void> {
194 executor_t,
196 gpu_id);
197 auto target_slices = Kokkos::subview(
198 target,
199 std::make_pair<size_t, size_t>(0, number_slices * elements_per_slice));
200 auto source_slices = Kokkos::subview(
201 source,
202 std::make_pair<size_t, size_t>(0, number_slices * elements_per_slice));
203 return hpx::kokkos::deep_copy_async(exec.instance(), target_slices,
204 source_slices);
205 };
206 return agg_exec.wrap_async(launch_copy_lambda, target, source,
207 agg_exec.get_underlying_executor());
208}
209
210} // namespace kernel_aggregation
211} // namespace cppuddle
212
213#endif
static void select_device(size_t gpu_id)
Definition executor_pools_management.hpp:189
#define CPPUDDLE_HOST_DEVICE_METHOD
Definition kokkos_aggregation_util.hpp:64
detail::round_robin_pool_impl< Interface > round_robin_pool_impl
Round robin pool strategy implementation.
Definition executor_pools_interface.hpp:23
hpx::shared_future< void > aggregrated_deep_copy_async(typename Aggregated_Executor< executor_t >::Executor_Slice &agg_exec, TargetView_t &target, SourceView_t &source)
Convenience function to launch an aggregated kernel and get a future back.
Definition kokkos_aggregation_util.hpp:168
void aggregated_deep_copy(Agg_executor_t &agg_exec, TargetView_t &target, SourceView_t &source)
Convenience function to perform an aggregated deep copy.
Definition kokkos_aggregation_util.hpp:141
CPPUDDLE_HOST_DEVICE_METHOD auto map_views_to_slice(const Integer slice_id, const Integer max_slices, const Agg_view_t &current_arg, const Args &...rest)
Definition kokkos_aggregation_util.hpp:87
CPPUDDLE_HOST_DEVICE_METHOD Agg_view_t::view_type get_slice_subview(const size_t slice_id, const size_t max_slices, const Agg_view_t &agg_view)
Get subview for the current slice.
Definition kokkos_aggregation_util.hpp:73
Definition config.hpp:31