17#ifndef REALM_HIP_REDOP_H
18#define REALM_HIP_REDOP_H
23#if defined(__CUDACC__) || defined(__HIPCC__)
25#include <hip/hip_runtime.h>
29#if defined(__CUDACC__) || defined(__HIPCC__)
41 template <
typename Offset_t =
size_t>
42 static __device__
inline void index_to_coords(Offset_t *coords, Offset_t index,
43 const Offset_t *extents,
44 const size_t elem_size)
49 for(
int i = 0; i < n - 1; i++) {
50 size_t div_tmp = div / extents[i];
51 coords[i] = div - div_tmp * extents[i];
55 coords[0] = coords[0] * elem_size;
58 template <
typename Offset_t =
size_t>
59 static __device__
inline size_t coords_to_index(
const Offset_t *coords,
60 const Offset_t *strides,
61 const size_t elem_size)
68 for(; d < n - 1; d++) {
78 template <
typename Offset_t =
size_t>
79 static __device__
inline size_t coords_to_index_transpose(
const Offset_t *coords,
80 const Offset_t *strides)
83 i = coords[1] * strides[0] + coords[2] * strides[1] + coords[0];
87 namespace ReductionKernelsAdvanced {
88 template <
typename REDOP,
bool EXCL>
91 size_t off = blockIdx.x * blockDim.x + threadIdx.x;
93 size_t vol = current_info.
volume;
94 size_t num_elems_rhs = current_info.
src.elem_size /
sizeof(
typename REDOP::RHS);
95 size_t redop_rhs_size =
sizeof(
typename REDOP::RHS);
96 typename REDOP::RHS *dst =
97 reinterpret_cast<typename REDOP::RHS *
>(current_info.
dst.addr);
98 typename REDOP::RHS *
src =
99 reinterpret_cast<typename REDOP::RHS *
>(current_info.
src.addr);
100 for(
size_t idx = off; idx < vol; idx += blockDim.x * gridDim.x) {
102 index_to_coords<size_t>(coords, idx, current_info.
extents, redop_rhs_size);
103 const size_t src_idx =
104 coords_to_index<size_t>(coords, current_info.
src.strides, redop_rhs_size);
105 const size_t dst_idx =
106 coords_to_index<size_t>(coords, current_info.
dst.strides, redop_rhs_size);
107 redop.template fold_hip<EXCL>(
108 *
reinterpret_cast<typename REDOP::RHS *
>(&dst[dst_idx * num_elems_rhs]),
109 *
reinterpret_cast<const typename REDOP::RHS *
>(
110 &
src[src_idx * num_elems_rhs]));
114 template <
typename REDOP,
bool EXCL>
117 size_t off = blockIdx.x * blockDim.x + threadIdx.x;
119 size_t vol = current_info.
volume;
120 size_t num_elems_lhs = current_info.
dst.elem_size /
sizeof(
typename REDOP::LHS);
121 size_t num_elems_rhs = current_info.
src.elem_size /
sizeof(
typename REDOP::RHS);
122 size_t redop_lhs_size =
sizeof(
typename REDOP::LHS);
123 size_t redop_rhs_size =
sizeof(
typename REDOP::RHS);
124 typename REDOP::LHS *dst =
125 reinterpret_cast<typename REDOP::LHS *
>(current_info.
dst.addr);
126 typename REDOP::RHS *
src =
127 reinterpret_cast<typename REDOP::RHS *
>(current_info.
src.addr);
128 for(
size_t idx = off; idx < vol; idx += blockDim.x * gridDim.x) {
130 index_to_coords<size_t>(coords, idx, current_info.
extents, redop_rhs_size);
131 const size_t src_idx =
132 coords_to_index<size_t>(coords, current_info.
src.strides, redop_rhs_size);
133 const size_t dst_idx =
134 coords_to_index<size_t>(coords, current_info.
dst.strides, redop_lhs_size);
135 redop.template apply_hip<EXCL>(
136 *
reinterpret_cast<typename REDOP::LHS *
>(&(dst[dst_idx * num_elems_lhs])),
137 *
reinterpret_cast<const typename REDOP::RHS *
>(
138 &
src[src_idx * num_elems_rhs]));
143 namespace ReductionKernelsTranspose {
144 template <
typename REDOP,
bool EXCL>
148 size_t offset = blockIdx.x * blockDim.x + threadIdx.x;
149 size_t vol = current_info.
volume;
150 size_t num_elems = current_info.
elem_size /
sizeof(
typename REDOP::RHS);
151 typename REDOP::RHS *dst =
152 reinterpret_cast<typename REDOP::RHS *
>(current_info.
dst);
153 typename REDOP::RHS *
src =
154 reinterpret_cast<typename REDOP::RHS *
>(current_info.
src);
155 for(
size_t idx = offset; idx < vol; idx += blockDim.x * gridDim.x) {
157 index_to_coords<size_t>(coords, idx, current_info.
extents, 1);
158 const size_t src_idx =
159 coords_to_index_transpose<size_t>(coords, current_info.
src_strides);
160 const size_t dst_idx =
161 coords_to_index_transpose<size_t>(coords, current_info.
dst_strides);
162 redop.template fold_hip<EXCL>(
163 *
reinterpret_cast<typename REDOP::RHS *
>(&dst[dst_idx * num_elems]),
164 *
reinterpret_cast<const typename REDOP::RHS *
>(&
src[src_idx * num_elems]));
168 template <
typename REDOP,
bool EXCL>
172 const size_t offset = blockIdx.x * blockDim.x + threadIdx.x;
173 size_t vol = current_info.
volume;
174 size_t num_elems = current_info.
elem_size /
sizeof(
typename REDOP::RHS);
175 typename REDOP::LHS *dst =
176 reinterpret_cast<typename REDOP::LHS *
>(current_info.
dst);
177 typename REDOP::RHS *
src =
178 reinterpret_cast<typename REDOP::RHS *
>(current_info.
src);
180 for(
size_t idx = offset; idx < vol; idx += blockDim.x * gridDim.x) {
182 index_to_coords<size_t>(coords, idx, current_info.
extents, 1);
183 const size_t src_idx =
184 coords_to_index_transpose<size_t>(coords, current_info.
src_strides);
185 const size_t dst_idx =
186 coords_to_index_transpose<size_t>(coords, current_info.
dst_strides);
187 redop.template apply_hip<EXCL>(
188 *
reinterpret_cast<typename REDOP::LHS *
>(&dst[dst_idx * num_elems]),
189 *
reinterpret_cast<const typename REDOP::RHS *
>(&
src[src_idx * num_elems]));
196 namespace ReductionKernels {
198 template <
typename LHS,
typename RHS,
typename F>
199 __device__
void iter_hip_kernel(uintptr_t lhs_base, uintptr_t lhs_stride,
200 uintptr_t rhs_base, uintptr_t rhs_stride,
201 size_t count, F func,
void *context =
nullptr)
203 const size_t tid = blockIdx.x * blockDim.x + threadIdx.x;
204 for(
size_t idx = tid; idx < count; idx += blockDim.x * gridDim.x) {
205 (*func)(*
reinterpret_cast<LHS *
>(lhs_base + idx * lhs_stride),
206 *
reinterpret_cast<const RHS *
>(rhs_base + idx * rhs_stride), context);
210 template <
typename REDOP,
bool EXCL>
211 __device__
void redop_apply_wrapper(
typename REDOP::LHS &lhs,
212 const typename REDOP::RHS &rhs,
void *context)
214 REDOP &redop = *
reinterpret_cast<REDOP *
>(context);
215 redop.template apply_hip<EXCL>(lhs, rhs);
217 template <
typename REDOP,
bool EXCL>
218 __device__
void redop_fold_wrapper(
typename REDOP::RHS &rhs1,
219 const typename REDOP::RHS &rhs2,
void *context)
221 REDOP &redop = *
reinterpret_cast<REDOP *
>(context);
222 redop.template fold_hip<EXCL>(rhs1, rhs2);
225 template <
typename REDOP,
bool EXCL>
226 __global__
void apply_hip_kernel(uintptr_t lhs_base, uintptr_t lhs_stride,
227 uintptr_t rhs_base, uintptr_t rhs_stride,
228 size_t count, REDOP redop)
230 iter_hip_kernel<typename REDOP::LHS, typename REDOP::RHS>(
231 lhs_base, lhs_stride, rhs_base, rhs_stride, count,
232 redop_apply_wrapper<REDOP, EXCL>, (
void *)&redop);
235 template <
typename REDOP,
bool EXCL>
236 __global__
void fold_hip_kernel(uintptr_t rhs1_base, uintptr_t rhs1_stride,
237 uintptr_t rhs2_base, uintptr_t rhs2_stride,
238 size_t count, REDOP redop)
240 iter_hip_kernel<typename REDOP::RHS, typename REDOP::RHS>(
241 rhs1_base, rhs1_stride, rhs2_base, rhs2_stride, count,
242 redop_fold_wrapper<REDOP, EXCL>, (
void *)&redop);
246 template <
typename REDOP,
typename T >
247 void add_hip_redop_kernels_advanced(T *redop)
249 redop->hip_apply_excl_fn_advanced =
reinterpret_cast<void *
>(
250 &ReductionKernelsAdvanced::apply_hip_kernel<REDOP, true>);
251 redop->hip_apply_nonexcl_fn_advanced =
reinterpret_cast<void *
>(
252 &ReductionKernelsAdvanced::apply_hip_kernel<REDOP, false>);
253 redop->hip_fold_excl_fn_advanced =
reinterpret_cast<void *
>(
254 &ReductionKernelsAdvanced::fold_hip_kernel<REDOP, true>);
255 redop->hip_fold_nonexcl_fn_advanced =
reinterpret_cast<void *
>(
256 &ReductionKernelsAdvanced::fold_hip_kernel<REDOP, false>);
258 redop->hip_apply_excl_fn_transpose =
reinterpret_cast<void *
>(
259 &ReductionKernelsTranspose::apply_hip_kernel<REDOP, true>);
260 redop->hip_apply_nonexcl_fn_transpose =
reinterpret_cast<void *
>(
261 &ReductionKernelsTranspose::apply_hip_kernel<REDOP, false>);
262 redop->hip_fold_excl_fn_transpose =
reinterpret_cast<void *
>(
263 &ReductionKernelsTranspose::fold_hip_kernel<REDOP, true>);
264 redop->hip_fold_nonexcl_fn_transpose =
reinterpret_cast<void *
>(
265 &ReductionKernelsTranspose::fold_hip_kernel<REDOP, false>);
270 template <
typename REDOP,
typename T >
271 void add_hip_redop_kernels(T *redop)
275 redop->hip_apply_excl_fn =
276 reinterpret_cast<void *
>(&ReductionKernels::apply_hip_kernel<REDOP, true>);
277 redop->hip_apply_nonexcl_fn =
278 reinterpret_cast<void *
>(&ReductionKernels::apply_hip_kernel<REDOP, false>);
279 redop->hip_fold_excl_fn =
280 reinterpret_cast<void *
>(&ReductionKernels::fold_hip_kernel<REDOP, true>);
281 redop->hip_fold_nonexcl_fn =
282 reinterpret_cast<void *
>(&ReductionKernels::fold_hip_kernel<REDOP, false>);
283 add_hip_redop_kernels_advanced<REDOP, T>(redop);
Definition activemsg.h:42
Definition hip_reduc.h:58
AffineReducPair< N > subrects[MAX_RECTS]
Definition hip_reduc.h:64
Definition hip_reduc.h:47
size_t extents[N]
Definition hip_reduc.h:51
AffineReducSubRect< N > dst
Definition hip_reduc.h:49
AffineReducSubRect< N > src
Definition hip_reduc.h:48
size_t volume
Definition hip_reduc.h:54
Definition hip_reduc.h:27
size_t elem_size
Definition hip_reduc.h:34
Offset_t src_strides[2]
Definition hip_reduc.h:29
size_t volume
Definition hip_reduc.h:33
uintptr_t dst
Definition hip_reduc.h:31
uintptr_t src
Definition hip_reduc.h:32
Offset_t dst_strides[2]
Definition hip_reduc.h:30
Offset_t extents[3]
Definition hip_reduc.h:28
NodeID src
Definition ucp_internal.h:1