29#ifndef KOKKOS_AGGREGATION_UTIL_HPP
30#define KOKKOS_AGGREGATION_UTIL_HPP
31#include <hpx/futures/future.hpp>
33#include <hpx/kokkos/executors.hpp>
34#include <Kokkos_Core.hpp>
35#include <hpx/kokkos.hpp>
40#include <cuda/std/tuple>
41#if defined(HPX_CUDA_VERSION) && (HPX_CUDA_VERSION < 1202)
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...>> {};
59#if defined(__CUDACC__)
60#define CPPUDDLE_HOST_DEVICE_METHOD __host__ __device__
61#elif (defined(__clang__) && defined(__HIP__))
62#define CPPUDDLE_HOST_DEVICE_METHOD __host__ __device__
64#define CPPUDDLE_HOST_DEVICE_METHOD
68namespace kernel_aggregation {
71template <
typename Agg_view_t>
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));
83template <
typename Integer,
84 std::enable_if_t<std::is_integral<Integer>::value,
bool> =
true,
85 typename Agg_view_t,
typename... Args>
88 const Agg_view_t ¤t_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(
98 return cuda::std::make_tuple(
102 if constexpr (
sizeof...(Args) > 0) {
103 return std::tuple_cat(
107 return std::make_tuple(
116 typename Agg_executor_t,
typename Agg_view_t,
117 std::enable_if_t<Kokkos::is_view<typename Agg_view_t::view_type>::value,
122 const Agg_view_t ¤t_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(
134 return std::make_tuple(
140template <
typename Agg_executor_t,
typename TargetView_t,
typename SourceView_t>
142 SourceView_t &source) {
143 if (agg_exec.sync_aggregation_slices()) {
144 Kokkos::deep_copy(agg_exec.get_underlying_executor().instance(), target,
150template <
typename Agg_executor_t,
typename TargetView_t,
typename SourceView_t>
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(
157 std::make_pair<size_t, size_t>(0, number_slices * elements_per_slice));
158 auto source_slices = Kokkos::subview(
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);
167template <
typename executor_t,
typename TargetView_t,
typename SourceView_t>
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);
179 return agg_exec.wrap_async(launch_copy_lambda, target, source,
180 agg_exec.get_underlying_executor());
184template <
typename executor_t,
typename TargetView_t,
typename SourceView_t>
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> {
197 auto target_slices = Kokkos::subview(
199 std::make_pair<size_t, size_t>(0, number_slices * elements_per_slice));
200 auto source_slices = Kokkos::subview(
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,
206 return agg_exec.wrap_async(launch_copy_lambda, target, source,
207 agg_exec.get_underlying_executor());
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 ¤t_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