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 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
29 
30 #ifndef SACADO_FAD_VECTORDYNAMICSTORAGE_HPP
31 #define SACADO_FAD_VECTORDYNAMICSTORAGE_HPP
32 
33 #include "Sacado_ConfigDefs.h"
34 
35 #ifdef SACADO_NEW_FAD_DESIGN_IS_DEFAULT
36 
38 
39 namespace Sacado {
40  namespace Fad {
41 
42  template <typename T, typename U = T>
43  using VectorDynamicStorage = Exp::VectorDynamicStorage<T,U>;
44 
45  }
46 }
47 
48 #else
49 
50 #include "Sacado_Traits.hpp"
52 
53 namespace Sacado {
54 
55  namespace Fad {
56 
58  template <typename T, typename U = T>
60 
61  public:
62 
63  typedef T value_type;
64 
66  template <typename S>
69  v_(x), owns_mem(true), sz_(0), len_(0), stride_(1), val_(&v_), dx_(NULL)
70  {}
71 
73 
77  VectorDynamicStorage(const int sz, const T & x, const DerivInit zero_out = InitDerivArray) :
78  v_(x), owns_mem(true), sz_(sz), len_(sz), stride_(1), val_(&v_) {
79  if (zero_out == InitDerivArray)
81  else
83  }
84 
87  VectorDynamicStorage(const int sz, T* x, U* dx_p, const int stride,
88  bool zero_out) :
89  v_(), owns_mem(false), sz_(sz), len_(sz), stride_(stride),
90  val_(x), dx_(dx_p) {
91  if (zero_out)
92  zero();
93  }
94 
98  v_(*x.val_), owns_mem(true), sz_(x.sz_), len_(x.sz_),
99  stride_(1), val_(&v_) {
101  }
102 
106  if (owns_mem) {
107  if (len_ != 0)
109  }
110  }
111 
115  if (this != &x) {
116  *val_ = *x.val_;
117  if (sz_ != x.sz_) {
118  sz_ = x.sz_;
119  if (x.sz_ > len_) {
120 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
121  if (!owns_mem)
122  throw "Can\'t resize beyond original size when memory isn't owned!";
123 #endif
124  if (len_ != 0)
126  len_ = x.sz_;
128  }
129  else
131  }
132  else
134  }
135  return *this;
136  }
137 
140  int size() const { return sz_;}
141 
144  int length() const { return len_; }
145 
147 
151  void resize(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  sz_ = sz;
163  }
164 
166 
171  void resizeAndZero(int sz) {
172  if (sz > len_) {
173 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
174  if (!owns_mem)
175  throw "Can\'t resize beyond original size when memory isn't owned!";
176 #endif
177  if (len_ != 0)
179  len_ = sz;
181  }
182  else if (sz > sz_)
184  sz_ = sz;
185  }
186 
188 
193  void expand(int sz) {
194  if (sz > len_) {
195 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
196  if (!owns_mem)
197  throw "Can\'t resize beyond original size when memory isn't owned!";
198 #endif
199  U* dx_new = ds_array<U>::get_and_fill(sz);
200  ds_array<U>::copy(dx_, dx_new, sz_);
201  if (len_ > 0)
203  dx_ = dx_new;
204  len_ = sz;
205  }
206  else if (sz > sz_)
208  sz_ = sz;
209  }
210 
213  void zero() {
215  }
216 
219  void setMemory(int sz, T* x, U* dx_p, int stride) {
220 
221  // Destroy old memory
222  if (owns_mem) {
223  if (len_ != 0)
225  }
226 
227  // Set new values
228  owns_mem = false;
229  sz_ = sz;
230  len_ = sz;
231  stride_ = stride;
232  val_ = x;
233  dx_ = dx_p;
234  }
235 
238  const T& val() const { return *val_; }
239 
242  T& val() { return *val_; }
243 
246  const U* dx() const { return dx_;}
247 
250  U dx(int i) const { return sz_ ? dx_[i*stride_] : T(0.); }
251 
254  U& fastAccessDx(int i) { return dx_[i*stride_];}
255 
258  const U& fastAccessDx(int i) const { return dx_[i*stride_];}
259 
260  private:
261 
262  T v_;
263 
264  private:
265 
267  bool owns_mem;
268 
270  int sz_;
271 
273  int len_;
274 
276  int stride_;
277 
279  T* val_;
280 
282  U* dx_;
283 
284  }; // class VectorDynamicStorage
285 
286  } // namespace Fad
287 
288 } // namespace Sacado
289 
290 #endif // SACADO_NEW_FAD_DESIGN_IS_DEFAULT
291 
292 #endif // SACADO_FAD_VECTORDYNAMICSTORAGE_HPP
static KOKKOS_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 KOKKOS_INLINE_FUNCTION T * get(int sz)
Get memory for new array of length sz.
static KOKKOS_INLINE_FUNCTION void copy(const T *src, T *dest, int sz)
Copy array from src to dest of length sz.
KOKKOS_INLINE_FUNCTION T & val()
Returns value.
KOKKOS_INLINE_FUNCTION VectorDynamicStorage(const VectorDynamicStorage &x)
Copy constructor.
KOKKOS_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
#define SACADO_ENABLE_VALUE_CTOR_DECL
static KOKKOS_INLINE_FUNCTION void strided_zero(T *dest, int stride, int sz)
Zero out array dest of length sz.
KOKKOS_INLINE_FUNCTION void setMemory(int sz, T *x, U *dx_p, int stride)
Set value/derivative array memory.
KOKKOS_INLINE_FUNCTION const T & val() const
Returns value.
KOKKOS_INLINE_FUNCTION U dx(int i) const
Returns derivative component i with bounds checking.
expr true
static KOKKOS_INLINE_FUNCTION void destroy_and_release(T *m, int sz)
Destroy array elements and release memory.
KOKKOS_INLINE_FUNCTION VectorDynamicStorage(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz.
#define KOKKOS_INLINE_FUNCTION
KOKKOS_INLINE_FUNCTION const U * dx() const
Returns derivative array.
#define T
Definition: Sacado_rad.hpp:573
bool owns_mem
Do we own the val/dx storage.
KOKKOS_INLINE_FUNCTION const U & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
static KOKKOS_INLINE_FUNCTION T * get_and_fill(int sz)
Get memory for new array of length sz and fill with zeros.
KOKKOS_INLINE_FUNCTION VectorDynamicStorage & operator=(const VectorDynamicStorage &x)
Assignment.
static KOKKOS_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.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
KOKKOS_INLINE_FUNCTION VectorDynamicStorage(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Default constructor.
Derivative array storage class using dynamic memory allocation.
KOKKOS_INLINE_FUNCTION int size() const
Returns number of derivative components.
Initialize the derivative array.
KOKKOS_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
KOKKOS_INLINE_FUNCTION U & fastAccessDx(int i)
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION ~VectorDynamicStorage()
Destructor.
expr expr expr bar false
KOKKOS_INLINE_FUNCTION void resizeAndZero(int sz)
Resize the derivative array to sz.
KOKKOS_INLINE_FUNCTION VectorDynamicStorage(const int sz, T *x, U *dx_p, const int stride, bool zero_out)
Constructor with supplied memory.
KOKKOS_INLINE_FUNCTION void zero()
Zero out derivative array.
KOKKOS_INLINE_FUNCTION int length() const
Returns array length.