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_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_DYNAMICSTORAGE_HPP
11 #define SACADO_FAD_DYNAMICSTORAGE_HPP
12 
13 #include "Sacado_ConfigDefs.h"
14 
15 #ifdef SACADO_NEW_FAD_DESIGN_IS_DEFAULT
16 
18 
19 namespace Sacado {
20  namespace Fad {
21 
22  template <typename T, typename U = T>
23  using DynamicStorage = Exp::DynamicStorage<T,U>;
24 
25  }
26 }
27 
28 #else
29 
30 #include "Sacado_Traits.hpp"
32 
33 namespace Sacado {
34 
35  namespace Fad {
36 
38  template <typename T, typename U = T>
40 
41  public:
42 
43  typedef T value_type;
44 
46  template <typename S>
49  val_(x), sz_(0), len_(0), dx_(NULL) {}
50 
52 
56  DynamicStorage(const int sz, const T & x, const DerivInit zero_out = InitDerivArray) :
57  val_(x), sz_(sz), len_(sz) {
58  if (zero_out == InitDerivArray)
60  else
62  }
63 
67  val_(x.val_), sz_(x.sz_), len_(x.sz_) {
69  }
70 
74  if (len_ != 0)
76  }
77 
81  if (this != &x) {
82  val_ = x.val_;
83  if (sz_ != x.sz_) {
84  sz_ = x.sz_;
85  if (x.sz_ > len_) {
86  if (len_ != 0)
88  len_ = x.sz_;
90  }
91  else
93  }
94  else
96  }
97  return *this;
98  }
99 
102  int size() const { return sz_;}
103 
106  int length() const { return len_; }
107 
109 
113  void resize(int sz) {
114  if (sz > len_) {
115  if (len_ != 0)
118  len_ = sz;
119  }
120  sz_ = sz;
121  }
122 
124 
129  void resizeAndZero(int sz) {
130  if (sz > len_) {
131  if (len_ != 0)
134  len_ = sz;
135  }
136  else if (sz > sz_)
138  sz_ = sz;
139  }
140 
142 
147  void expand(int sz) {
148  if (sz > len_) {
149  U* dx_new = ds_array<U>::get_and_fill(sz);
150  ds_array<U>::copy(dx_, dx_new, sz_);
151  if (len_ > 0)
153  dx_ = dx_new;
154  len_ = sz;
155  }
156  else if (sz > sz_)
158  sz_ = sz;
159  }
160 
163  void zero() {
165  }
166 
169  const T& val() const { return val_; }
170 
173  T& val() { return val_; }
174 
177  const U* dx() const { return dx_;}
178 
179 #if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD_STRIDED) && !defined(SACADO_DISABLE_CUDA_IN_KOKKOS) && defined(__CUDA_ARCH__)
180 
183  U dx(int i) const { return sz_ ? dx_[i*blockDim.x] : U(0.); }
184 
187  U& fastAccessDx(int i) { return dx_[i*blockDim.x];}
188 
191  const U& fastAccessDx(int i) const { return dx_[i*blockDim.x];}
192 
193 #else
194 
197  U dx(int i) const { return sz_ ? dx_[i] : U(0.); }
198 
201  U& fastAccessDx(int i) { return dx_[i];}
202 
205  const U& fastAccessDx(int i) const { return dx_[i];}
206 
207 #endif
208 
209  protected:
210 
213 
215  int sz_;
216 
218  int len_;
219 
221  U* dx_;
222 
223  }; // class DynamicStorage
224 
225  } // namespace Fad
226 
227 } // namespace Sacado
228 
229 #endif // SACADO_NEW_FAD_DESIGN_IS_DEFAULT
230 
231 #endif // SACADO_FAD_DYNAMICSTORAGE_HPP
SACADO_INLINE_FUNCTION ~DynamicStorage()
Destructor.
SACADO_INLINE_FUNCTION T & val()
Returns value.
Derivative array storage class using dynamic memory allocation.
#define SACADO_ENABLE_VALUE_CTOR_DECL
static SACADO_INLINE_FUNCTION void zero(T *dest, int sz)
Zero out array dest of length sz.
SACADO_INLINE_FUNCTION U dx(int i) const
Returns derivative component i with bounds checking.
SACADO_INLINE_FUNCTION void resizeAndZero(int sz)
Resize the derivative array to sz.
#define T
Definition: Sacado_rad.hpp:553
SACADO_INLINE_FUNCTION DynamicStorage & operator=(const DynamicStorage &x)
Assignment.
SACADO_INLINE_FUNCTION DynamicStorage(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Default constructor.
SACADO_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
int len_
Derivative array length.
SACADO_INLINE_FUNCTION U & fastAccessDx(int i)
Returns derivative component i without bounds checking.
SACADO_INLINE_FUNCTION DynamicStorage(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz.
static SACADO_INLINE_FUNCTION void destroy_and_release(T *m, int sz)
Destroy array elements and release memory.
SACADO_INLINE_FUNCTION const T & val() const
Returns value.
SACADO_INLINE_FUNCTION int length() const
Returns array length.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
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 DynamicStorage &x)
Copy constructor.
SACADO_INLINE_FUNCTION int size() const
Returns number of derivative components.
Initialize the derivative array.
SACADO_INLINE_FUNCTION const U & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
static SACADO_INLINE_FUNCTION T * get(int sz)
Get memory for new array of length sz.
#define SACADO_INLINE_FUNCTION
SACADO_INLINE_FUNCTION void zero()
Zero out derivative array.
SACADO_INLINE_FUNCTION const U * dx() const
Returns derivative array.
SACADO_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
static SACADO_INLINE_FUNCTION T * get_and_fill(int sz)
Get memory for new array of length sz and fill with zeros.