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 //
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_EXP_VECTORDYNAMICSTORAGE_HPP
31 #define SACADO_FAD_EXP_VECTORDYNAMICSTORAGE_HPP
32 
33 #include <type_traits>
34 
35 #include "Sacado_Traits.hpp"
37 
38 namespace Sacado {
39 
40  namespace Fad {
41  namespace Exp {
42 
44  template <typename T, typename U = T>
46 
47  public:
48 
49  typedef typename std::remove_cv<T>::type value_type;
50  static constexpr bool is_statically_sized = false;
51  static constexpr int static_size = 0;
52 
54  template <typename TT, typename UU = TT>
55  struct apply {
57  };
58 
60  template <int N>
61  struct apply_N {
63  };
64 
68  v_(), owns_mem(true), sz_(0), len_(0), stride_(1), val_(&v_), dx_(NULL)
69  {}
70 
73  VectorDynamicStorage(const T & x) :
74  v_(x), owns_mem(true), sz_(0), len_(0), stride_(1), val_(&v_), dx_(NULL)
75  {}
76 
78 
82  VectorDynamicStorage(const int sz, const T & x, const DerivInit zero_out) :
83  v_(x), owns_mem(true), sz_(sz), len_(sz), stride_(1), val_(&v_) {
84  if (zero_out == InitDerivArray)
86  else
88  }
89 
92  VectorDynamicStorage(const int sz, T* x, U* dx_p, const int stride,
93  bool zero_out) :
94  v_(), owns_mem(false), sz_(sz), len_(sz), stride_(stride),
95  val_(x), dx_(dx_p) {
96  if (zero_out)
97  zero();
98  }
99 
103  v_(*x.val_), owns_mem(true), sz_(x.sz_), len_(x.sz_),
104  stride_(1), val_(&v_) {
106  }
107 
111  if (owns_mem) {
112  if (len_ != 0)
114  }
115  }
116 
120  if (this != &x) {
121  *val_ = *x.val_;
122  if (sz_ != x.sz_) {
123  sz_ = x.sz_;
124  if (x.sz_ > len_) {
125 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
126  if (!owns_mem)
127  throw "Can\'t resize beyond original size when memory isn't owned!";
128 #endif
129  if (len_ != 0)
131  len_ = x.sz_;
133  }
134  else
136  }
137  else
139  }
140  return *this;
141  }
142 
145  int size() const { return sz_;}
146 
149  int length() const { return len_; }
150 
152 
156  void resize(int sz) {
157  if (sz > len_) {
158 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
159  if (!owns_mem)
160  throw "Can\'t resize beyond original size when memory isn't owned!";
161 #endif
162  if (len_ != 0)
164  len_ = sz;
166  }
167  sz_ = sz;
168  }
169 
171 
176  void resizeAndZero(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  if (len_ != 0)
184  len_ = sz;
186  }
187  else if (sz > sz_)
189  sz_ = sz;
190  }
191 
193 
198  void expand(int sz) {
199  if (sz > len_) {
200 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
201  if (!owns_mem)
202  throw "Can\'t resize beyond original size when memory isn't owned!";
203 #endif
204  U* dx_new = ds_array<U>::get_and_fill(sz);
205  ds_array<U>::copy(dx_, dx_new, sz_);
206  if (len_ > 0)
208  dx_ = dx_new;
209  len_ = sz;
210  }
211  else if (sz > sz_)
213  sz_ = sz;
214  }
215 
218  void zero() {
220  }
221 
224  void setMemory(int sz, T* x, U* dx_p, int stride) {
225 
226  // Destroy old memory
227  if (owns_mem) {
228  if (len_ != 0)
230  }
231 
232  // Set new values
233  owns_mem = false;
234  sz_ = sz;
235  len_ = sz;
236  stride_ = stride;
237  val_ = x;
238  dx_ = dx_p;
239  }
240 
243  const T& val() const { return *val_; }
244 
247  T& val() { return *val_; }
248 
251  const U* dx() const { return dx_;}
252 
255  U dx(int i) const { return sz_ ? dx_[i*stride_] : T(0.); }
256 
259  U& fastAccessDx(int i) { return dx_[i*stride_];}
260 
263  const U& fastAccessDx(int i) const { return dx_[i*stride_];}
264 
265  private:
266 
267  T v_;
268 
269  private:
270 
272  bool owns_mem;
273 
275  int sz_;
276 
278  int len_;
279 
281  int stride_;
282 
284  T* val_;
285 
287  U* dx_;
288 
289  }; // class VectorDynamicStorage
290 
291  } // namespace Exp
292  } // namespace Fad
293 
294 } // namespace Sacado
295 
296 #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.
KOKKOS_INLINE_FUNCTION VectorDynamicStorage(const int sz, const T &x, const DerivInit zero_out)
Constructor with size sz.
KOKKOS_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size 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 U dx(int i) const
Returns derivative component i with bounds checking.
Turn DynamicStorage into a meta-function class usable with mpl::apply.
static KOKKOS_INLINE_FUNCTION void strided_zero(T *dest, int stride, int sz)
Zero out array dest of length 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 VectorDynamicStorage(const VectorDynamicStorage &x)
Copy constructor.
expr true
KOKKOS_INLINE_FUNCTION int size() const
Returns number of derivative components.
static KOKKOS_INLINE_FUNCTION void destroy_and_release(T *m, int sz)
Destroy array elements and release memory.
KOKKOS_INLINE_FUNCTION int length() const
Returns array length.
KOKKOS_INLINE_FUNCTION VectorDynamicStorage()
Default constructor.
#define KOKKOS_INLINE_FUNCTION
#define T
Definition: Sacado_rad.hpp:573
KOKKOS_INLINE_FUNCTION const T & val() const
Returns value.
KOKKOS_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
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 void zero()
Zero out derivative array.
KOKKOS_INLINE_FUNCTION const U & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION void resizeAndZero(int sz)
Resize the derivative array to sz.
KOKKOS_INLINE_FUNCTION T & val()
Returns value.
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.
KOKKOS_INLINE_FUNCTION U & fastAccessDx(int i)
Returns derivative component i without bounds checking.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
KOKKOS_INLINE_FUNCTION ~VectorDynamicStorage()
Destructor.
KOKKOS_INLINE_FUNCTION VectorDynamicStorage(const T &x)
Constructor with value.
Initialize the derivative array.
KOKKOS_INLINE_FUNCTION void setMemory(int sz, T *x, U *dx_p, int stride)
Set value/derivative array memory.
Derivative array storage class using dynamic memory allocation.
expr expr expr bar false
KOKKOS_INLINE_FUNCTION const U * dx() const
Returns derivative array.
KOKKOS_INLINE_FUNCTION VectorDynamicStorage & operator=(const VectorDynamicStorage &x)
Assignment.