RAJA
RAJA provides a collection of platform portability abstractions for C++ HPC applications.
Reduce.hpp
Go to the documentation of this file.
1 
11 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
12 // Copyright (c) Lawrence Livermore National Security, LLC and other
13 // RAJA Project Developers. See top-level LICENSE and COPYRIGHT
14 // files for dates and other details. No copyright assignment is required
15 // to contribute to RAJA.
16 //
17 // SPDX-License-Identifier: (BSD-3-Clause)
18 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
19 
20 
21 #ifndef RAJA_policy_hip_kernel_Reduce_HPP
22 #define RAJA_policy_hip_kernel_Reduce_HPP
23 
24 #include "RAJA/config.hpp"
25 
27 
28 namespace RAJA
29 {
30 
31 namespace internal
32 {
33 
34 
35 //
36 // Executor that handles reductions across a single HIP thread block
37 //
38 template<typename Data,
39  template<typename...> class ReduceOperator,
40  typename ParamId,
41  typename... EnclosedStmts,
42  typename Types>
43 struct HipStatementExecutor<Data,
44  statement::Reduce<RAJA::hip_block_reduce,
45  ReduceOperator,
46  ParamId,
47  EnclosedStmts...>,
48  Types>
49 {
50 
51  using stmt_list_t = StatementList<EnclosedStmts...>;
52 
53  using enclosed_stmts_t = HipStatementListExecutor<Data, stmt_list_t, Types>;
54 
55  static inline RAJA_DEVICE void exec(Data& data, bool thread_active)
56  {
57  // block reduce on the specified parameter
58  auto value = data.template get_param<ParamId>();
59  using value_t = decltype(value);
60  value_t ident = value_t();
61 
62  // if this thread isn't active, just set it to the identity
63  if (!thread_active)
64  {
65  value = ident;
66  }
67 
68  // Call out existing block reduction algorithm that we use for
69  // reduction objects
70  using combiner_t =
72  value_t new_value = RAJA::hip::impl::block_reduce<combiner_t>(value, ident);
73 
74 
75  // execute enclosed statements, and mask off everyone but thread 0
76  thread_active = threadIdx.x == 0 && threadIdx.y == 0 && threadIdx.z == 0;
77  if (thread_active)
78  {
79  // Only update to new value on root thread
80  data.template assign_param<ParamId>(new_value);
81  }
82  enclosed_stmts_t::exec(data, thread_active);
83  }
84 
85  static inline LaunchDims calculateDimensions(Data const& data)
86  {
87  // combine with enclosed statements
88  LaunchDims enclosed_dims = enclosed_stmts_t::calculateDimensions(data);
89  return enclosed_dims;
90  }
91 };
92 
93 //
94 // Executor that handles reductions across a single HIP thread warp
95 //
96 template<typename Data,
97  template<typename...> class ReduceOperator,
98  typename ParamId,
99  typename... EnclosedStmts,
100  typename Types>
101 struct HipStatementExecutor<Data,
102  statement::Reduce<RAJA::hip_warp_reduce,
103  ReduceOperator,
104  ParamId,
105  EnclosedStmts...>,
106  Types>
107 {
108 
109  using stmt_list_t = StatementList<EnclosedStmts...>;
110 
111  using enclosed_stmts_t = HipStatementListExecutor<Data, stmt_list_t, Types>;
112 
113  static inline RAJA_DEVICE void exec(Data& data, bool thread_active)
114  {
115  // block reduce on the specified parameter
116  auto value = data.template get_param<ParamId>();
117  using value_t = decltype(value);
118  value_t ident = value_t();
119 
120  // if this thread isn't active, just set it to the identity
121  if (!thread_active)
122  {
123  value = ident;
124  }
125 
126  // Call warp reduction routine
127  using combiner_t =
129  value_t new_value = RAJA::hip::impl::warp_reduce<combiner_t>(value, ident);
130  data.template assign_param<ParamId>(new_value);
131 
132  // execute enclosed statements, and mask off everyone but lane 0
133  thread_active = threadIdx.x == 0;
134  if (thread_active)
135  {
136  // Only update to new value on root thread
137  data.template assign_param<ParamId>(new_value);
138  }
139  enclosed_stmts_t::exec(data, thread_active);
140  }
141 
142  static inline LaunchDims calculateDimensions(Data const& data)
143  {
144  // combine with enclosed statements
145  LaunchDims enclosed_dims = enclosed_stmts_t::calculateDimensions(data);
146  return enclosed_dims;
147  }
148 };
149 
150 
151 } // namespace internal
152 } // end namespace RAJA
153 
154 
155 #endif /* RAJA_policy_hip_kernel_Reduce_HPP */
#define RAJA_DEVICE
Definition: macros.hpp:66
constexpr auto Reduce(T *target)
Definition: reducer.hpp:231
camp::list< Stmts... > StatementList
Definition: StatementList.hpp:41
Definition: AlignedRangeIndexSetBuilders.cpp:35
RAJA header file containing constructs used to run kernel traversals on GPU with HIP.
Definition: reduce.hpp:70