Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_Fad_Exp_DynamicStorage.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef SACADO_FAD_EXP_DYNAMICSTORAGE_HPP
11 #define SACADO_FAD_EXP_DYNAMICSTORAGE_HPP
12 
13 #include <type_traits>
14 #include <utility>
15 
16 #include "Sacado_ConfigDefs.h"
18 
19 namespace Sacado {
20 
21  namespace Fad {
22  namespace Exp {
23 
25  template <typename T, typename U = T>
27 
28  public:
29 
30  typedef typename std::remove_cv<T>::type value_type;
31  static constexpr bool is_statically_sized = false;
32  static constexpr int static_size = 0;
33  static constexpr bool is_view = false;
34 
36  template <typename TT, typename UU = TT>
37  struct apply {
39  };
40 
42  template <int N>
43  struct apply_N {
45  };
46 
50  val_(), sz_(0), len_(0), dx_(nullptr) {}
51 
54  DynamicStorage(const T & x) :
55  val_(x), sz_(0), len_(0), dx_(nullptr) {}
56 
58 
62  DynamicStorage(const int sz, const T & x,
63  const DerivInit zero_out = InitDerivArray) :
64  val_(x), sz_(sz), len_(sz) {
65  if (zero_out == InitDerivArray)
67  else
69  }
70 
72 
78  DynamicStorage(const int sz, const int i, const value_type & x) :
80  dx_[i]=1.;
81  }
82 
86  val_(x.val_), sz_(x.sz_), len_(x.sz_) {
88  }
89 
93  val_(std::move(x.val_)), sz_(x.sz_), len_(x.len_), dx_(x.dx_) {
94  x.sz_ = 0;
95  x.len_ = 0;
96  x.dx_ = nullptr;
97  }
98 
102  if (len_ != 0)
104  }
105 
109  if (this != &x) {
110  val_ = x.val_;
111  if (sz_ != x.sz_) {
112  sz_ = x.sz_;
113  if (x.sz_ > len_) {
114  if (len_ != 0)
116  len_ = x.sz_;
118  }
119  else
121  }
122  else
124  }
125  return *this;
126  }
127 
131  if (this != &x) {
132  if (len_ != 0)
134  val_ = std::move(x.val_);
135  sz_ = x.sz_; x.sz_ = 0;
136  len_ = x.len_; x.len_ = 0;
137  dx_ = x.dx_; x.dx_ = nullptr;
138  }
139  return *this;
140  }
141 
144  int size() const { return sz_;}
145 
148  int length() const { return len_; }
149 
151 
155  void resize(int sz) {
156  if (sz > len_) {
157  if (len_ != 0)
160  len_ = sz;
161  }
162  sz_ = sz;
163  }
164 
166 
171  void resizeAndZero(int sz) {
172  if (sz > len_) {
173  if (len_ != 0)
176  len_ = sz;
177  }
178  else if (sz > sz_)
180  sz_ = sz;
181  }
182 
184 
189  void expand(int sz) {
190  if (sz > len_) {
191  U* dx_new = ds_array<U>::get_and_fill(sz);
192  ds_array<U>::copy(dx_, dx_new, sz_);
193  if (len_ > 0)
195  dx_ = dx_new;
196  len_ = sz;
197  }
198  else if (sz > sz_)
200  sz_ = sz;
201  }
202 
205  void zero() {
207  }
208 
211  const T& val() const { return val_; }
212 
215  T& val() { return val_; }
216 
219  const U* dx() const { return dx_;}
220 
221 #if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD_STRIDED) && !defined(SACADO_DISABLE_CUDA_IN_KOKKOS) && ( defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) )
222 
225  U dx(int i) const { return sz_ ? dx_[i*blockDim.x] : U(0.); }
226 
229  U& fastAccessDx(int i) { return dx_[i*blockDim.x];}
230 
233  const U& fastAccessDx(int i) const { return dx_[i*blockDim.x];}
234 
235 #else
236 
239  U dx(int i) const { return sz_ ? dx_[i] : U(0.); }
240 
243  U& fastAccessDx(int i) { return dx_[i];}
244 
247  const U& fastAccessDx(int i) const { return dx_[i];}
248 
249 #endif
250 
251  protected:
252 
255 
257  int sz_;
258 
260  int len_;
261 
263  U* dx_;
264 
265  }; // class DynamicStorage
266 
267  } // namespace Exp
268  } // namespace Fad
269 
270 } // namespace Sacado
271 
272 #endif // SACADO_FAD_EXP_DYNAMICSTORAGE_HPP
Turn DynamicStorage into a meta-function class usable with mpl::apply.
SACADO_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
SACADO_INLINE_FUNCTION void resizeAndZero(int sz)
Resize the derivative array to sz.
SACADO_INLINE_FUNCTION DynamicStorage(const DynamicStorage &x)
Copy constructor.
static SACADO_INLINE_FUNCTION void zero(T *dest, int sz)
Zero out array dest of length sz.
SACADO_INLINE_FUNCTION ~DynamicStorage()
Destructor.
SACADO_INLINE_FUNCTION int size() const
Returns number of derivative components.
SACADO_INLINE_FUNCTION DynamicStorage & operator=(const DynamicStorage &x)
Assignment.
SACADO_INLINE_FUNCTION int length() const
Returns array length.
SACADO_INLINE_FUNCTION DynamicStorage(const T &x)
Constructor with value.
#define T
Definition: Sacado_rad.hpp:553
SACADO_INLINE_FUNCTION DynamicStorage(DynamicStorage &&x)
Move constructor.
static SACADO_INLINE_FUNCTION void destroy_and_release(T *m, int sz)
Destroy array elements and release memory.
SACADO_INLINE_FUNCTION T & val()
Returns value.
SACADO_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
Derivative array storage class using dynamic memory allocation.
SACADO_INLINE_FUNCTION U dx(int i) const
Returns derivative component i with bounds checking.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
SACADO_INLINE_FUNCTION DynamicStorage(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz.
SACADO_INLINE_FUNCTION const T & val() const
Returns value.
static SACADO_INLINE_FUNCTION void copy(const T *src, T *dest, int sz)
Copy array from src to dest of length sz.
SACADO_INLINE_FUNCTION DynamicStorage(const int sz, const int i, const value_type &x)
Constructor with size sz, index i, and value x.
SACADO_INLINE_FUNCTION DynamicStorage & operator=(DynamicStorage &&x)
Move assignment.
Initialize the derivative array.
static SACADO_INLINE_FUNCTION T * get(int sz)
Get memory for new array of length sz.
SACADO_INLINE_FUNCTION DynamicStorage()
Default constructor.
SACADO_INLINE_FUNCTION const U & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
#define SACADO_INLINE_FUNCTION
SACADO_INLINE_FUNCTION void zero()
Zero out derivative array.
SACADO_INLINE_FUNCTION U & fastAccessDx(int i)
Returns derivative component i without bounds checking.
SACADO_INLINE_FUNCTION const U * dx() const
Returns derivative array.
static SACADO_INLINE_FUNCTION T * get_and_fill(int sz)
Get memory for new array of length sz and fill with zeros.