RAJA
RAJA provides a collection of platform portability abstractions for C++ HPC applications.
multi_reduce.hpp
Go to the documentation of this file.
1 
14 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
15 // Copyright (c) Lawrence Livermore National Security, LLC and other
16 // RAJA Project Developers. See top-level LICENSE and COPYRIGHT
17 // files for dates and other details. No copyright assignment is required
18 // to contribute to RAJA.
19 //
20 // SPDX-License-Identifier: (BSD-3-Clause)
21 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
22 
23 #ifndef RAJA_sequential_multi_reduce_HPP
24 #define RAJA_sequential_multi_reduce_HPP
25 
26 #include "RAJA/config.hpp"
27 
29 
32 
34 
35 #include "RAJA/util/types.hpp"
36 
37 namespace RAJA
38 {
39 
40 namespace detail
41 {
42 
52 template<typename T, typename t_MultiReduceOp, typename tuning>
54 
64 template<typename T, typename t_MultiReduceOp>
66  T,
67  t_MultiReduceOp,
69  RAJA::sequential::multi_reduce_algorithm::left_fold>>
70 {
71  using value_type = T;
72  using MultiReduceOp = t_MultiReduceOp;
73 
74  MultiReduceDataSeq() = delete;
75 
76  template<typename Container,
77  std::enable_if_t<
78  !std::is_same<Container, MultiReduceDataSeq>::value>* = nullptr>
79  MultiReduceDataSeq(Container const& container, T identity)
80  : m_parent(nullptr),
81  m_num_bins(container.size()),
82  m_identity(identity),
83  m_data(nullptr)
84  {
85  m_data = create_data(container, m_num_bins);
86  }
87 
89  : m_parent(other.m_parent ? other.m_parent : &other),
90  m_num_bins(other.m_num_bins),
91  m_identity(other.m_identity),
92  m_data(other.m_data)
93  {}
94 
98 
100  {
101  if (m_data)
102  {
103  if (!m_parent)
104  {
105  destroy_data(m_data, m_num_bins);
106  }
107  }
108  }
109 
110  template<typename Container>
111  void reset(Container const& container, T identity)
112  {
113  m_identity = identity;
114  size_t new_num_bins = container.size();
115  if (new_num_bins != m_num_bins)
116  {
117  destroy_data(m_data, m_num_bins);
118  m_num_bins = new_num_bins;
119  m_data = create_data(container, m_num_bins);
120  }
121  else
122  {
123  size_t bin = 0;
124  for (auto const& value : container)
125  {
126  m_data[bin] = value;
127  ++bin;
128  }
129  }
130  }
131 
132  size_t num_bins() const { return m_num_bins; }
133 
134  T identity() const { return m_identity; }
135 
136  void combine(size_t bin, T const& val) { MultiReduceOp {}(m_data[bin], val); }
137 
138  T get(size_t bin) const { return m_data[bin]; }
139 
140 private:
141  MultiReduceDataSeq const* m_parent;
142  size_t m_num_bins;
143  T m_identity;
144  T* m_data;
145 
146  template<typename Container>
147  static T* create_data(Container const& container, size_t num_bins)
148  {
149  if (num_bins == size_t(0))
150  {
151  return nullptr;
152  }
153 
154  auto data = static_cast<T*>(malloc(num_bins * sizeof(T)));
155  size_t bin = 0;
156  for (auto const& value : container)
157  {
158  new (&data[bin]) T(value);
159  ++bin;
160  }
161  return data;
162  }
163 
164  static void destroy_data(T*& data, size_t num_bins)
165  {
166  if (num_bins == size_t(0))
167  {
168  return;
169  }
170 
171  for (size_t bin = 0; bin < num_bins; ++bin)
172  {
173  data[bin].~T();
174  }
175  free(data);
176  data = nullptr;
177  }
178 };
179 
180 } // namespace detail
181 
182 RAJA_DECLARE_ALL_MULTI_REDUCERS(policy::sequential::seq_multi_reduce_policy,
183  detail::MultiReduceDataSeq)
184 
185 } // namespace RAJA
186 
187 #endif // closing endif for header file include guard
Header file defining prototypes for routines used to manage memory for CPU reductions and other opera...
Definition: AlignedRangeIndexSetBuilders.cpp:35
Base types used in common for RAJA reducer objects.
#define RAJA_DECLARE_ALL_MULTI_REDUCERS(POL, DATA)
Definition: multi_reduce.hpp:49
Header file providing RAJA reduction declarations.
Header file containing RAJA sequential policy definitions.
Seq multi-reduce data class template.
Definition: multi_reduce.hpp:53
Definition: policy.hpp:37
Header file for RAJA type definitions.