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_VectorDynamicStorage.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_VECTORDYNAMICSTORAGE_HPP
11 #define SACADO_FAD_VECTORDYNAMICSTORAGE_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 VectorDynamicStorage = Exp::VectorDynamicStorage<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  v_(x), owns_mem(true), sz_(0), len_(0), stride_(1), val_(&v_), dx_(NULL)
50  {}
51 
53 
57  VectorDynamicStorage(const int sz, const T & x, const DerivInit zero_out = InitDerivArray) :
58  v_(x), owns_mem(true), sz_(sz), len_(sz), stride_(1), val_(&v_) {
59  if (zero_out == InitDerivArray)
61  else
63  }
64 
67  VectorDynamicStorage(const int sz, T* x, U* dx_p, const int stride,
68  bool zero_out) :
69  v_(), owns_mem(false), sz_(sz), len_(sz), stride_(stride),
70  val_(x), dx_(dx_p) {
71  if (zero_out)
72  zero();
73  }
74 
78  v_(*x.val_), owns_mem(true), sz_(x.sz_), len_(x.sz_),
79  stride_(1), val_(&v_) {
81  }
82 
86  if (owns_mem) {
87  if (len_ != 0)
89  }
90  }
91 
95  if (this != &x) {
96  *val_ = *x.val_;
97  if (sz_ != x.sz_) {
98  sz_ = x.sz_;
99  if (x.sz_ > len_) {
100 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
101  if (!owns_mem)
102  throw "Can\'t resize beyond original size when memory isn't owned!";
103 #endif
104  if (len_ != 0)
106  len_ = x.sz_;
108  }
109  else
111  }
112  else
114  }
115  return *this;
116  }
117 
120  int size() const { return sz_;}
121 
124  int length() const { return len_; }
125 
127 
131  void resize(int sz) {
132  if (sz > len_) {
133 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
134  if (!owns_mem)
135  throw "Can\'t resize beyond original size when memory isn't owned!";
136 #endif
137  if (len_ != 0)
139  len_ = sz;
141  }
142  sz_ = sz;
143  }
144 
146 
151  void resizeAndZero(int sz) {
152  if (sz > len_) {
153 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
154  if (!owns_mem)
155  throw "Can\'t resize beyond original size when memory isn't owned!";
156 #endif
157  if (len_ != 0)
159  len_ = sz;
161  }
162  else if (sz > sz_)
164  sz_ = sz;
165  }
166 
168 
173  void expand(int sz) {
174  if (sz > len_) {
175 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
176  if (!owns_mem)
177  throw "Can\'t resize beyond original size when memory isn't owned!";
178 #endif
179  U* dx_new = ds_array<U>::get_and_fill(sz);
180  ds_array<U>::copy(dx_, dx_new, sz_);
181  if (len_ > 0)
183  dx_ = dx_new;
184  len_ = sz;
185  }
186  else if (sz > sz_)
188  sz_ = sz;
189  }
190 
193  void zero() {
195  }
196 
199  void setMemory(int sz, T* x, U* dx_p, int stride) {
200 
201  // Destroy old memory
202  if (owns_mem) {
203  if (len_ != 0)
205  }
206 
207  // Set new values
208  owns_mem = false;
209  sz_ = sz;
210  len_ = sz;
211  stride_ = stride;
212  val_ = x;
213  dx_ = dx_p;
214  }
215 
218  const T& val() const { return *val_; }
219 
222  T& val() { return *val_; }
223 
226  const U* dx() const { return dx_;}
227 
230  U dx(int i) const { return sz_ ? dx_[i*stride_] : T(0.); }
231 
234  U& fastAccessDx(int i) { return dx_[i*stride_];}
235 
238  const U& fastAccessDx(int i) const { return dx_[i*stride_];}
239 
240  private:
241 
242  T v_;
243 
244  private:
245 
247  bool owns_mem;
248 
250  int sz_;
251 
253  int len_;
254 
256  int stride_;
257 
259  T* val_;
260 
262  U* dx_;
263 
264  }; // class VectorDynamicStorage
265 
266  } // namespace Fad
267 
268 } // namespace Sacado
269 
270 #endif // SACADO_NEW_FAD_DESIGN_IS_DEFAULT
271 
272 #endif // SACADO_FAD_VECTORDYNAMICSTORAGE_HPP
SACADO_INLINE_FUNCTION void setMemory(int sz, T *x, U *dx_p, int stride)
Set value/derivative array memory.
static SACADO_INLINE_FUNCTION void strided_copy(const T *src, int src_stride, T *dest, int dest_stride, int sz)
Copy array from src to dest of length sz.
SACADO_INLINE_FUNCTION U dx(int i) const
Returns derivative component i with bounds checking.
SACADO_INLINE_FUNCTION VectorDynamicStorage(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Default constructor.
#define SACADO_ENABLE_VALUE_CTOR_DECL
SACADO_INLINE_FUNCTION const U * dx() const
Returns derivative array.
expr true
SACADO_INLINE_FUNCTION U & fastAccessDx(int i)
Returns derivative component i without bounds checking.
SACADO_INLINE_FUNCTION ~VectorDynamicStorage()
Destructor.
SACADO_INLINE_FUNCTION T & val()
Returns value.
#define T
Definition: Sacado_rad.hpp:553
SACADO_INLINE_FUNCTION VectorDynamicStorage & operator=(const VectorDynamicStorage &x)
Assignment.
bool owns_mem
Do we own the val/dx storage.
SACADO_INLINE_FUNCTION int size() const
Returns number of derivative components.
static SACADO_INLINE_FUNCTION void destroy_and_release(T *m, int sz)
Destroy array elements and release memory.
SACADO_INLINE_FUNCTION void zero()
Zero out derivative array.
SACADO_INLINE_FUNCTION void resizeAndZero(int sz)
Resize the derivative array to sz.
SACADO_INLINE_FUNCTION const U & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
SACADO_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
SACADO_INLINE_FUNCTION VectorDynamicStorage(const int sz, T *x, U *dx_p, const int stride, bool zero_out)
Constructor with supplied memory.
SACADO_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
static SACADO_INLINE_FUNCTION void copy(const T *src, T *dest, int sz)
Copy array from src to dest of length sz.
Derivative array storage class using dynamic memory allocation.
SACADO_INLINE_FUNCTION VectorDynamicStorage(const VectorDynamicStorage &x)
Copy constructor.
SACADO_INLINE_FUNCTION const T & val() const
Returns value.
Initialize the derivative array.
static SACADO_INLINE_FUNCTION T * strided_get_and_fill(const T *src, int stride, int sz)
Get memory for new array of length sz and fill with entries from src.
static SACADO_INLINE_FUNCTION T * get(int sz)
Get memory for new array of length sz.
expr expr expr bar false
SACADO_INLINE_FUNCTION int length() const
Returns array length.
#define SACADO_INLINE_FUNCTION
static SACADO_INLINE_FUNCTION void strided_zero(T *dest, int stride, int sz)
Zero out array dest of length sz.
SACADO_INLINE_FUNCTION VectorDynamicStorage(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz.
static SACADO_INLINE_FUNCTION T * get_and_fill(int sz)
Get memory for new array of length sz and fill with zeros.