RAJA
RAJA provides a collection of platform portability abstractions for C++ HPC applications.
SoAPtr.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 #ifndef RAJA_SOA_PTR_HPP
21 #define RAJA_SOA_PTR_HPP
22 
23 #include "RAJA/config.hpp"
24 
25 #include <type_traits>
26 
27 // for RAJA::reduce::detail::ValueLoc
29 #include "RAJA/util/types.hpp"
30 
31 // for RAJA::expt::ValLoc
33 
34 namespace RAJA
35 {
36 
37 namespace detail
38 {
39 
47 template<typename T,
48  typename mempool = RAJA::basic_mempool::MemPool<
50  typename accessor = DefaultAccessor>
51 class SoAPtr
52 {
53  template<typename, typename, typename>
54  friend class SoAPtr; // friend other instantiations of this class
55 
56 public:
57  using value_type = T;
58 
59  template<typename rhs_accessor>
61 
62  SoAPtr() = default;
63  SoAPtr(SoAPtr const&) = default;
64  SoAPtr(SoAPtr&&) = default;
65  SoAPtr& operator=(SoAPtr const&) = default;
66  SoAPtr& operator=(SoAPtr&&) = default;
67 
68  explicit SoAPtr(size_t size)
69  : mem(mempool::getInstance().template malloc<value_type>(size))
70  {}
71 
72  template<
73  typename rhs_accessor,
74  std::enable_if_t<!std::is_same<accessor, rhs_accessor>::value>* = nullptr>
77  : mem(rhs.mem)
78  {}
79 
80  SoAPtr& allocate(size_t size)
81  {
82  mem = mempool::getInstance().template malloc<value_type>(size);
83  return *this;
84  }
85 
87  {
88  mempool::getInstance().free(mem);
89  mem = nullptr;
90  return *this;
91  }
92 
93  RAJA_HOST_DEVICE bool allocated() const { return mem != nullptr; }
94 
95  constexpr RAJA_HOST_DEVICE value_type get(size_t i) const
96  {
97  return accessor::get(mem, i);
98  }
99 
100  constexpr RAJA_HOST_DEVICE void set(size_t i, value_type val)
101  {
102  accessor::set(mem, i, val);
103  }
104 
105 private:
106  value_type* mem = nullptr;
107 };
108 
112 template<typename T,
113  typename IndexType,
114  bool doing_min,
115  typename mempool,
116  typename accessor>
117 class SoAPtr<RAJA::reduce::detail::ValueLoc<T, IndexType, doing_min>,
118  mempool,
119  accessor>
120 {
121  using first_type = T;
122  using second_type = IndexType;
123 
124  template<typename, typename, typename>
125  friend class SoAPtr; // fiend other instantiations of this class
126 
127 public:
129 
130  template<typename rhs_accessor>
132 
133  SoAPtr() = default;
134  SoAPtr(SoAPtr const&) = default;
135  SoAPtr(SoAPtr&&) = default;
136  SoAPtr& operator=(SoAPtr const&) = default;
137  SoAPtr& operator=(SoAPtr&&) = default;
138 
139  explicit SoAPtr(size_t size)
140  : mem(mempool::getInstance().template malloc<first_type>(size)),
141  mem_idx(mempool::getInstance().template malloc<second_type>(size))
142  {}
143 
144  template<
145  typename rhs_accessor,
146  std::enable_if_t<!std::is_same<accessor, rhs_accessor>::value>* = nullptr>
149  : mem(rhs.mem),
150  mem_idx(rhs.mem_idx)
151  {}
152 
153  SoAPtr& allocate(size_t size)
154  {
155  mem = mempool::getInstance().template malloc<first_type>(size);
156  mem_idx = mempool::getInstance().template malloc<second_type>(size);
157  return *this;
158  }
159 
161  {
162  mempool::getInstance().free(mem);
163  mem = nullptr;
164  mempool::getInstance().free(mem_idx);
165  mem_idx = nullptr;
166  return *this;
167  }
168 
169  RAJA_HOST_DEVICE bool allocated() const { return mem != nullptr; }
170 
171  constexpr RAJA_HOST_DEVICE value_type get(size_t i) const
172  {
173  return value_type(accessor::get(mem, i), accessor::get(mem_idx, i));
174  }
175 
176  constexpr RAJA_HOST_DEVICE void set(size_t i, value_type val)
177  {
178  accessor::set(mem, i, first_type(val));
179  accessor::set(mem_idx, i, val.getLoc());
180  }
181 
182 private:
183  first_type* mem = nullptr;
184  second_type* mem_idx = nullptr;
185 };
186 
190 template<typename T, typename IndexType, typename mempool, typename accessor>
191 class SoAPtr<RAJA::expt::ValLoc<T, IndexType>, mempool, accessor>
192 {
193  using first_type = T;
194  using second_type = IndexType;
195 
196  template<typename, typename, typename>
197  friend class SoAPtr; // friend other instantiations of this class
198 
199 public:
201 
202  template<typename rhs_accessor>
204 
205  SoAPtr() = default;
206  SoAPtr(SoAPtr const&) = default;
207  SoAPtr(SoAPtr&&) = default;
208  SoAPtr& operator=(SoAPtr const&) = default;
209  SoAPtr& operator=(SoAPtr&&) = default;
210 
211  explicit SoAPtr(size_t size)
212  : mem(mempool::getInstance().template malloc<first_type>(size)),
213  mem_idx(mempool::getInstance().template malloc<second_type>(size))
214  {}
215 
216  template<
217  typename rhs_accessor,
218  std::enable_if_t<!std::is_same<accessor, rhs_accessor>::value>* = nullptr>
221  : mem(rhs.mem),
222  mem_idx(rhs.mem_idx)
223  {}
224 
225  SoAPtr& allocate(size_t size)
226  {
227  mem = mempool::getInstance().template malloc<first_type>(size);
228  mem_idx = mempool::getInstance().template malloc<second_type>(size);
229  return *this;
230  }
231 
233  {
234  mempool::getInstance().free(mem);
235  mem = nullptr;
236  mempool::getInstance().free(mem_idx);
237  mem_idx = nullptr;
238  return *this;
239  }
240 
241  RAJA_HOST_DEVICE bool allocated() const { return mem != nullptr; }
242 
243  constexpr RAJA_HOST_DEVICE value_type get(size_t i) const
244  {
245  return value_type(accessor::get(mem, i), accessor::get(mem_idx, i));
246  }
247 
248  constexpr RAJA_HOST_DEVICE void set(size_t i, value_type val)
249  {
250  accessor::set(mem, i, val.getVal());
251  accessor::set(mem_idx, i, val.getLoc());
252  }
253 
254 private:
255  first_type* mem = nullptr;
256  second_type* mem_idx = nullptr;
257 };
258 
259 } // namespace detail
260 
261 } // namespace RAJA
262 
263 #endif /* RAJA_SOA_PTR_HPP */
MemPool pre-allocates a large chunk of memory and provides generic malloc/free for the user to alloca...
Definition: basic_mempool.hpp:306
constexpr RAJA_HOST_DEVICE void set(size_t i, value_type val)
Definition: SoAPtr.hpp:248
constexpr RAJA_HOST_DEVICE value_type get(size_t i) const
Definition: SoAPtr.hpp:243
RAJA_HOST_DEVICE SoAPtr(SoAPtr< value_type, mempool, rhs_accessor > const &rhs)
Definition: SoAPtr.hpp:219
RAJA_HOST_DEVICE bool allocated() const
Definition: SoAPtr.hpp:241
constexpr RAJA_HOST_DEVICE void set(size_t i, value_type val)
Definition: SoAPtr.hpp:176
constexpr RAJA_HOST_DEVICE value_type get(size_t i) const
Definition: SoAPtr.hpp:171
RAJA_HOST_DEVICE SoAPtr(SoAPtr< value_type, mempool, rhs_accessor > const &rhs)
Definition: SoAPtr.hpp:147
Pointer class specialized for Struct of Array data layout allocated via RAJA basic_mempools.
Definition: SoAPtr.hpp:52
SoAPtr & allocate(size_t size)
Definition: SoAPtr.hpp:80
RAJA_HOST_DEVICE bool allocated() const
Definition: SoAPtr.hpp:93
constexpr RAJA_HOST_DEVICE void set(size_t i, value_type val)
Definition: SoAPtr.hpp:100
SoAPtr & deallocate()
Definition: SoAPtr.hpp:86
SoAPtr(SoAPtr const &)=default
SoAPtr & operator=(SoAPtr const &)=default
constexpr RAJA_HOST_DEVICE value_type get(size_t i) const
Definition: SoAPtr.hpp:95
SoAPtr(SoAPtr &&)=default
SoAPtr(size_t size)
Definition: SoAPtr.hpp:68
SoAPtr & operator=(SoAPtr &&)=default
T value_type
Definition: SoAPtr.hpp:57
RAJA_HOST_DEVICE SoAPtr(SoAPtr< value_type, mempool, rhs_accessor > const &rhs)
Definition: SoAPtr.hpp:75
Definition: reduce.hpp:131
RAJA_HOST_DEVICE IndexType getLoc()
Definition: reduce.hpp:170
#define RAJA_HOST_DEVICE
Definition: macros.hpp:65
Definition: AlignedRangeIndexSetBuilders.cpp:35
RAJA_HOST_DEVICE constexpr RAJA_INLINE RAJA::zip_tuple_element_t< I, zip_tuple< is_val, Ts... > > & get(zip_tuple< is_val, Ts... > &z) noexcept
Definition: zip_tuple.hpp:56
Base types used in common for RAJA reducer objects.
example allocator for basic_mempool using malloc/free
Definition: basic_mempool.hpp:426
Definition: params_base.hpp:20
constexpr RAJA_HOST_DEVICE const value_type & getVal() const
Definition: params_base.hpp:46
constexpr RAJA_HOST_DEVICE const index_type & getLoc() const
Definition: params_base.hpp:48
Header file for RAJA type definitions.