RAJA
RAJA provides a collection of platform portability abstractions for C++ HPC applications.
reducer.hpp
Go to the documentation of this file.
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
2 // Copyright (c) Lawrence Livermore National Security, LLC and other
3 // RAJA Project Developers. See top-level LICENSE and COPYRIGHT
4 // files for dates and other details. No copyright assignment is required
5 // to contribute to RAJA.
6 //
7 // SPDX-License-Identifier: (BSD-3-Clause)
8 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
9 
10 #ifndef NEW_REDUCE_HPP
11 #define NEW_REDUCE_HPP
12 
13 #include <type_traits>
14 
17 #include "RAJA/util/SoAPtr.hpp"
18 
19 #if defined(RAJA_CUDA_ACTIVE)
21 #elif defined(RAJA_HIP_ACTIVE)
23 #elif defined(RAJA_SYCL_ACTIVE)
25 #endif
26 
27 namespace RAJA
28 {
29 
30 namespace operators
31 {
32 
33 template<typename T, typename IndexType>
34 struct limits<RAJA::expt::ValLoc<T, IndexType>>
35 {
36  RAJA_INLINE RAJA_HOST_DEVICE static constexpr RAJA::expt::ValLoc<T, IndexType>
37  min()
38  {
40  }
41 
42  RAJA_INLINE RAJA_HOST_DEVICE static constexpr RAJA::expt::ValLoc<T, IndexType>
43  max()
44  {
46  }
47 };
48 
49 } // namespace operators
50 
51 } // namespace RAJA
52 
53 namespace RAJA
54 {
55 
56 namespace expt
57 {
58 namespace detail
59 {
60 
61 #if defined(RAJA_CUDA_ACTIVE)
62 using device_mem_pool_t = RAJA::cuda::device_mempool_type;
63 #elif defined(RAJA_HIP_ACTIVE)
64 using device_mem_pool_t = RAJA::hip::device_mempool_type;
65 #elif defined(RAJA_SYCL_ACTIVE)
66 using device_mem_pool_t = RAJA::sycl::device_mempool_type;
67 #endif
68 
69 //
70 //
71 // Basic Reducer
72 //
73 //
74 
75 // Basic data type Reducer
76 // T must be a basic data type
77 // VOp must be ValOp<T, Op>
78 template<typename Op, typename T, typename VOp>
79 struct Reducer : public ForallParamBase
80 {
81  using op = Op;
82  using value_type = T; // This is a basic data type
83 
84  Reducer() = default;
85 
86  // Basic data type constructor
88  : m_valop(VOp {}),
89  target(target_in)
90  {}
91 
92  Reducer(Reducer const&) = default;
93  Reducer(Reducer&&) = default;
94  Reducer& operator=(Reducer const&) = default;
95  Reducer& operator=(Reducer&&) = default;
96 
97  // Internal ValOp object that is used within RAJA::forall/launch
98  VOp m_valop = VOp {};
99 
100  // Points to the user specified result variable
101  value_type* target = nullptr;
102 
103  // combineTarget() performs the final op on the target data and location in
104  // param_resolve()
106  {
107  value_type temp = op {}(*target, in);
108  *target = temp;
109  }
110 
112  value_type& getVal() { return m_valop.val; }
113 
114 #if defined(RAJA_CUDA_ACTIVE) || defined(RAJA_HIP_ACTIVE) || \
115  defined(RAJA_SYCL_ACTIVE)
116  // Device related attributes.
117  value_type* devicetarget = nullptr;
119  unsigned int* device_count = nullptr;
120 #endif
121 
122  // These are types and parameters extracted from this struct, and given to the
123  // forall.
124  using ARG_TUP_T = camp::tuple<VOp*>;
125  using ARG_T = VOp;
126 
128  {
129  return camp::make_tuple(&m_valop);
130  }
131 
133 
134  using ARG_LIST_T = typename ARG_TUP_T::TList;
135  static constexpr size_t num_lambda_args = camp::tuple_size<ARG_TUP_T>::value;
136 };
137 
138 // Partial specialization of Reducer for ValLoc
139 // T is a deduced basic data type
140 // I is a deduced index type
141 template<typename T,
142  typename I,
143  template<typename, typename, typename> class Op>
144 struct Reducer<Op<ValLoc<T, I>, ValLoc<T, I>, ValLoc<T, I>>,
145  ValLoc<T, I>,
146  ValOp<ValLoc<T, I>, Op>> : public ForallParamBase
147 {
148  using target_value_type = T;
149  using target_index_type = I;
151  using op = Op<value_type, value_type, value_type>;
153 
154  Reducer() = default;
155 
156  // ValLoc constructor
157  // Note that the target_ variables point to the val and loc within the user
158  // defined target ValLoc
160  : m_valop(VOp {}),
161  target_value(&target_in->val),
162  target_index(&target_in->loc)
163  {}
164 
165  // Dual input constructor for ReduceLoc<>(data, index) case
166  // The target_ variables point to vars defined by the user
168  target_index_type* index_in)
169  : m_valop(VOp {}),
170  target_value(data_in),
171  target_index(index_in)
172  {}
173 
174  Reducer(Reducer const&) = default;
175  Reducer(Reducer&&) = default;
176  Reducer& operator=(Reducer const&) = default;
177  Reducer& operator=(Reducer&&) = default;
178 
179  // The ValLoc within m_valop is initialized with data and location values from
180  // either a ValLoc, or dual data and location values, passed into the
181  // constructor
183 
184  // Points to either dual value and index defined by the user, or value and
185  // index within a ValLoc defined by the user
186  target_value_type* target_value = nullptr;
187  target_index_type* target_index = nullptr;
188 
189  // combineTarget() performs the final op on the target data and location in
190  // param_resolve()
192  {
193  // Create a different temp ValLoc solely for combining
194  value_type temp(*target_value, *target_index);
195  temp = op {}(temp, in);
196  *target_value = temp.val;
197  *target_index = temp.loc;
198  }
199 
201  value_type& getVal() { return m_valop.val; }
202 
203 #if defined(RAJA_CUDA_ACTIVE) || defined(RAJA_HIP_ACTIVE) || \
204  defined(RAJA_SYCL_ACTIVE)
205  // Device related attributes.
206  value_type* devicetarget = nullptr;
208  unsigned int* device_count = nullptr;
209 #endif
210 
211  // These are types and parameters extracted from this struct, and given to the
212  // forall.
213  using ARG_TUP_T = camp::tuple<VOp*>;
214  using ARG_T = VOp;
215 
217 
219  {
220  return camp::make_tuple(&m_valop);
221  }
222 
223  using ARG_LIST_T = typename ARG_TUP_T::TList;
224  static constexpr size_t num_lambda_args = camp::tuple_size<ARG_TUP_T>::value;
225 };
226 
227 } // namespace detail
228 
229 // Standard use case.
230 template<template<typename, typename, typename> class Op, typename T>
231 auto constexpr Reduce(T* target)
232 {
233  return detail::Reducer<Op<T, T, T>, T, ValOp<T, Op>>(target);
234 }
235 
236 // User-defined ValLoc case.
237 template<template<typename, typename, typename> class Op,
238  typename T,
239  typename IndexType>
240 auto constexpr Reduce(ValLoc<T, IndexType>* target)
241 {
242  using VL = ValLoc<T, IndexType>;
244  target);
245 }
246 
247 // Dual input use case where reduction value and location are separate,
248 // non-ValLoc types supplied by the user.
249 template<template<typename, typename, typename> class Op,
250  typename T,
251  typename IndexType>
252 auto constexpr ReduceLoc(T* target, IndexType* index)
253 {
254  using VL = ValLoc<T, IndexType>;
256  target, index);
257 }
258 
259 
260 } // namespace expt
261 
262 
263 } // namespace RAJA
264 
265 #endif // NEW_REDUCE_HPP
Header file defining prototypes for routines used to manage memory for CUDA reductions and other oper...
Header file defining prototypes for routines used to manage memory for HIP reductions and other opera...
Header file defining prototypes for routines used to manage memory for SYCL reductions and other oper...
Header file for common RAJA internal definitions.
Pointer class specialized for Struct of Array data layout allocated via RAJA basic_mempools.
Definition: SoAPtr.hpp:52
Header containing helper type traits for work with Reducers.
#define RAJA_HOST_DEVICE
Definition: macros.hpp:65
constexpr auto Reduce(T *target)
Definition: reducer.hpp:231
constexpr auto ReduceLoc(T *target, IndexType *index)
Definition: reducer.hpp:252
Definition: AlignedRangeIndexSetBuilders.cpp:35
Definition: params_base.hpp:20
index_type loc
Definition: params_base.hpp:61
value_type val
Definition: params_base.hpp:60
Definition: params_base.hpp:66
Definition: params_base.hpp:282
Definition: params_base.hpp:266
camp::tuple<> ARG_TUP_T
Definition: params_base.hpp:270
typename ARG_TUP_T::TList ARG_LIST_T
Definition: params_base.hpp:272
RAJA_HOST_DEVICE Reducer(target_value_type *data_in, target_index_type *index_in)
Definition: reducer.hpp:167
Definition: reducer.hpp:80
Reducer & operator=(Reducer const &)=default
VOp m_valop
Definition: reducer.hpp:98
Op op
Definition: reducer.hpp:81
RAJA_HOST_DEVICE value_type & getVal()
Definition: reducer.hpp:112
RAJA_HOST_DEVICE Reducer(value_type *target_in)
Definition: reducer.hpp:87
value_type * target
Definition: reducer.hpp:101
Reducer(Reducer &&)=default
Reducer(Reducer const &)=default
RAJA_HOST_DEVICE void combineTarget(value_type in)
Definition: reducer.hpp:105
Reducer & operator=(Reducer &&)=default
T value_type
Definition: reducer.hpp:82
RAJA_HOST_DEVICE ARG_T * get_lambda_arg()
Definition: reducer.hpp:132
static constexpr size_t num_lambda_args
Definition: reducer.hpp:135
RAJA_HOST_DEVICE ARG_TUP_T get_lambda_arg_tup()
Definition: reducer.hpp:127
RAJA_INLINE static constexpr RAJA_HOST_DEVICE RAJA::expt::ValLoc< T, IndexType > max()
Definition: reducer.hpp:43
RAJA_INLINE static constexpr RAJA_HOST_DEVICE RAJA::expt::ValLoc< T, IndexType > min()
Definition: reducer.hpp:37
Definition: Operators.hpp:250