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_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_EXP_VECTORDYNAMICSTORAGE_HPP
11 #define SACADO_FAD_EXP_VECTORDYNAMICSTORAGE_HPP
12 
13 #include <type_traits>
14 #include <utility>
15 
16 #include "Sacado_Traits.hpp"
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  v_(), owns_mem(true), sz_(0), len_(0), stride_(1), val_(&v_),
51  dx_(nullptr)
52  {}
53 
57  v_(x), owns_mem(true), sz_(0), len_(0), stride_(1), val_(&v_),
58  dx_(nullptr)
59  {}
60 
62 
66  VectorDynamicStorage(const int sz, const T & x,
67  const DerivInit zero_out = InitDerivArray) :
68  v_(x), owns_mem(true), sz_(sz), len_(sz), stride_(1), val_(&v_) {
69  if (zero_out == InitDerivArray)
71  else
73  }
74 
76 
82  VectorDynamicStorage(const int sz, const int i, const value_type & x) :
84  dx_[i]=1.;
85  }
86 
89  VectorDynamicStorage(const int sz, T* x, U* dx_p, const int stride,
90  bool zero_out) :
91  v_(), owns_mem(false), sz_(sz), len_(sz), stride_(stride),
92  val_(x), dx_(dx_p) {
93  if (zero_out)
94  zero();
95  }
96 
100  v_(*x.val_), owns_mem(true), sz_(x.sz_), len_(x.sz_),
101  stride_(1), val_(&v_) {
103  }
104 
105  // Move does not make sense for this storage since it is always tied to
106  // some preallocated data. Don't define move constructor so compiler will
107  // always fall-back to copy
108 
112  if (owns_mem) {
113  if (len_ != 0)
115  }
116  }
117 
121  if (this != &x) {
122  *val_ = *x.val_;
123  if (sz_ != x.sz_) {
124  sz_ = x.sz_;
125  if (x.sz_ > len_) {
126 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ ) && !defined(__HIP_DEVICE_COMPILE__)
127  if (!owns_mem)
128  throw "Can\'t resize beyond original size when memory isn't owned!";
129 #endif
130  if (len_ != 0)
132  len_ = x.sz_;
134  }
135  else
137  }
138  else
140  }
141  return *this;
142  }
143 
144  // Move does not make sense for this storage since it is always tied to
145  // some preallocated data. Don't define move assignment so compiler will
146  // always fall-back to copy
147 
150  int size() const { return sz_;}
151 
154  int length() const { return len_; }
155 
157 
161  void resize(int sz) {
162  if (sz > len_) {
163 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ ) && !defined(__HIP_DEVICE_COMPILE__)
164  if (!owns_mem)
165  throw "Can\'t resize beyond original size when memory isn't owned!";
166 #endif
167  if (len_ != 0)
169  len_ = sz;
171  }
172  sz_ = sz;
173  }
174 
176 
181  void resizeAndZero(int sz) {
182  if (sz > len_) {
183 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ ) && !defined(__HIP_DEVICE_COMPILE__)
184  if (!owns_mem)
185  throw "Can\'t resize beyond original size when memory isn't owned!";
186 #endif
187  if (len_ != 0)
189  len_ = sz;
191  }
192  else if (sz > sz_)
194  sz_ = sz;
195  }
196 
198 
203  void expand(int sz) {
204  if (sz > len_) {
205 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ ) && !defined(__HIP_DEVICE_COMPILE__)
206  if (!owns_mem)
207  throw "Can\'t resize beyond original size when memory isn't owned!";
208 #endif
209  U* dx_new = ds_array<U>::get_and_fill(sz);
210  ds_array<U>::copy(dx_, dx_new, sz_);
211  if (len_ > 0)
213  dx_ = dx_new;
214  len_ = sz;
215  }
216  else if (sz > sz_)
218  sz_ = sz;
219  }
220 
223  void zero() {
225  }
226 
229  void setMemory(int sz, T* x, U* dx_p, int stride) {
230 
231  // Destroy old memory
232  if (owns_mem) {
233  if (len_ != 0)
235  }
236 
237  // Set new values
238  owns_mem = false;
239  sz_ = sz;
240  len_ = sz;
241  stride_ = stride;
242  val_ = x;
243  dx_ = dx_p;
244  }
245 
248  const T& val() const { return *val_; }
249 
252  T& val() { return *val_; }
253 
256  const U* dx() const { return dx_;}
257 
260  U dx(int i) const { return sz_ ? dx_[i*stride_] : T(0.); }
261 
264  U& fastAccessDx(int i) { return dx_[i*stride_];}
265 
268  const U& fastAccessDx(int i) const { return dx_[i*stride_];}
269 
270  private:
271 
272  T v_;
273 
274  private:
275 
277  bool owns_mem;
278 
280  int sz_;
281 
283  int len_;
284 
286  int stride_;
287 
289  T* val_;
290 
292  U* dx_;
293 
294  }; // class VectorDynamicStorage
295 
296  } // namespace Exp
297  } // namespace Fad
298 
299 } // namespace Sacado
300 
301 #endif // SACADO_FAD_VECTORDYNAMICSTORAGE_HPP
SACADO_INLINE_FUNCTION VectorDynamicStorage & operator=(const VectorDynamicStorage &x)
Assignment.
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 VectorDynamicStorage(const int sz, const int i, const value_type &x)
Constructor with size sz, index i, and value x.
SACADO_INLINE_FUNCTION VectorDynamicStorage()
Default constructor.
Turn DynamicStorage into a meta-function class usable with mpl::apply.
SACADO_INLINE_FUNCTION const U * dx() const
Returns derivative array.
SACADO_INLINE_FUNCTION U & fastAccessDx(int i)
Returns derivative component i without bounds checking.
SACADO_INLINE_FUNCTION void setMemory(int sz, T *x, U *dx_p, int stride)
Set value/derivative array memory.
SACADO_INLINE_FUNCTION void resizeAndZero(int sz)
Resize the derivative array to sz.
expr true
SACADO_INLINE_FUNCTION const T & val() const
Returns value.
SACADO_INLINE_FUNCTION int length() const
Returns array length.
SACADO_INLINE_FUNCTION int size() const
Returns number of derivative components.
#define T
Definition: Sacado_rad.hpp:553
SACADO_INLINE_FUNCTION VectorDynamicStorage(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz.
SACADO_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
SACADO_INLINE_FUNCTION void zero()
Zero out derivative array.
SACADO_INLINE_FUNCTION VectorDynamicStorage(const VectorDynamicStorage &x)
Copy constructor.
SACADO_INLINE_FUNCTION VectorDynamicStorage(const int sz, T *x, U *dx_p, const int stride, bool zero_out)
Constructor with supplied memory.
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.
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 ~VectorDynamicStorage()
Destructor.
Initialize the derivative array.
Derivative array storage class using dynamic memory allocation.
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.
SACADO_INLINE_FUNCTION VectorDynamicStorage(const T &x)
Constructor with value.
static SACADO_INLINE_FUNCTION T * get(int sz)
Get memory for new array of length sz.
expr expr expr bar false
#define SACADO_INLINE_FUNCTION
SACADO_INLINE_FUNCTION const U & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
static SACADO_INLINE_FUNCTION void strided_zero(T *dest, int stride, int sz)
Zero out array dest of length sz.
static SACADO_INLINE_FUNCTION T * get_and_fill(int sz)
Get memory for new array of length sz and fill with zeros.
SACADO_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
SACADO_INLINE_FUNCTION U dx(int i) const
Returns derivative component i with bounds checking.