Sacado Package Browser (Single Doxygen Collection)  Version of the Day
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_Traits.hpp"
35 
36 namespace Sacado {
37 
38  namespace Fad {
39 
41  template <typename T, typename U = T>
43 
44  public:
45 
46  typedef T value_type;
47 
49  template <typename S>
52  v_(x), owns_mem(true), sz_(0), len_(0), stride_(1), val_(&v_), dx_(NULL)
53  {}
54 
56 
60  VectorDynamicStorage(const int sz, const T & x, const DerivInit zero_out = InitDerivArray) :
61  v_(x), owns_mem(true), sz_(sz), len_(sz), stride_(1), val_(&v_) {
62  if (zero_out == InitDerivArray)
64  else
66  }
67 
70  VectorDynamicStorage(const int sz, T* x, U* dx_p, const int stride,
71  bool zero_out) :
72  v_(), owns_mem(false), sz_(sz), len_(sz), stride_(stride),
73  val_(x), dx_(dx_p) {
74  if (zero_out)
75  zero();
76  }
77 
81  v_(*x.val_), owns_mem(true), sz_(x.sz_), len_(x.sz_),
82  stride_(1), val_(&v_) {
84  }
85 
89  if (owns_mem) {
90  if (len_ != 0)
92  }
93  }
94 
98  if (this != &x) {
99  *val_ = *x.val_;
100  if (sz_ != x.sz_) {
101  sz_ = x.sz_;
102  if (x.sz_ > len_) {
103 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
104  if (!owns_mem)
105  throw "Can\'t resize beyond original size when memory isn't owned!";
106 #endif
107  if (len_ != 0)
109  len_ = x.sz_;
111  }
112  else
114  }
115  else
117  }
118  return *this;
119  }
120 
123  int size() const { return sz_;}
124 
127  int length() const { return len_; }
128 
130 
134  void resize(int sz) {
135  if (sz > len_) {
136 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
137  if (!owns_mem)
138  throw "Can\'t resize beyond original size when memory isn't owned!";
139 #endif
140  if (len_ != 0)
142  len_ = sz;
144  }
145  sz_ = sz;
146  }
147 
149 
154  void resizeAndZero(int sz) {
155  if (sz > len_) {
156 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
157  if (!owns_mem)
158  throw "Can\'t resize beyond original size when memory isn't owned!";
159 #endif
160  if (len_ != 0)
162  len_ = sz;
164  }
165  else if (sz > sz_)
167  sz_ = sz;
168  }
169 
171 
176  void expand(int sz) {
177  if (sz > len_) {
178 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
179  if (!owns_mem)
180  throw "Can\'t resize beyond original size when memory isn't owned!";
181 #endif
182  U* dx_new = ds_array<U>::get_and_fill(sz);
183  ds_array<U>::copy(dx_, dx_new, sz_);
184  if (len_ > 0)
186  dx_ = dx_new;
187  len_ = sz;
188  }
189  else if (sz > sz_)
191  sz_ = sz;
192  }
193 
196  void zero() {
198  }
199 
202  void setMemory(int sz, T* x, U* dx_p, int stride) {
203 
204  // Destroy old memory
205  if (owns_mem) {
206  if (len_ != 0)
208  }
209 
210  // Set new values
211  owns_mem = false;
212  sz_ = sz;
213  len_ = sz;
214  stride_ = stride;
215  val_ = x;
216  dx_ = dx_p;
217  }
218 
221  const T& val() const { return *val_; }
222 
225  T& val() { return *val_; }
226 
229  const U* dx() const { return dx_;}
230 
233  U dx(int i) const { return sz_ ? dx_[i*stride_] : T(0.); }
234 
237  U& fastAccessDx(int i) { return dx_[i*stride_];}
238 
241  const U& fastAccessDx(int i) const { return dx_[i*stride_];}
242 
243  private:
244 
245  T v_;
246 
247  private:
248 
250  bool owns_mem;
251 
253  int sz_;
254 
256  int len_;
257 
259  int stride_;
260 
262  T* val_;
263 
265  U* dx_;
266 
267  }; // class VectorDynamicStorage
268 
269  } // namespace Fad
270 
271 } // namespace Sacado
272 
273 #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 int length() const
Returns array length.
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 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
#define T
Definition: Sacado_rad.hpp:573
KOKKOS_INLINE_FUNCTION const U & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION const T & val() const
Returns value.
KOKKOS_INLINE_FUNCTION int size() const
Returns number of derivative components.
bool owns_mem
Do we own the val/dx storage.
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 const U * dx() const
Returns derivative array.
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.
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.