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_DynamicStorage.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_DYNAMICSTORAGE_HPP
31 #define SACADO_FAD_EXP_DYNAMICSTORAGE_HPP
32 
33 #include <type_traits>
34 
35 #include "Sacado_ConfigDefs.h"
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  val_(), sz_(0), len_(0), dx_(NULL) {}
69 
72  DynamicStorage(const T & x) :
73  val_(x), sz_(0), len_(0), dx_(NULL) {}
74 
76 
80  DynamicStorage(const int sz, const T & x, const DerivInit zero_out) :
81  val_(x), sz_(sz), len_(sz) {
82  if (zero_out == InitDerivArray)
84  else
86  }
87 
91  val_(x.val_), sz_(x.sz_), len_(x.sz_) {
93  }
94 
98  if (len_ != 0)
100  }
101 
105  if (this != &x) {
106  val_ = x.val_;
107  if (sz_ != x.sz_) {
108  sz_ = x.sz_;
109  if (x.sz_ > len_) {
110  if (len_ != 0)
112  len_ = x.sz_;
114  }
115  else
117  }
118  else
120  }
121  return *this;
122  }
123 
126  int size() const { return sz_;}
127 
130  int length() const { return len_; }
131 
133 
137  void resize(int sz) {
138  if (sz > len_) {
139  if (len_ != 0)
142  len_ = sz;
143  }
144  sz_ = sz;
145  }
146 
148 
153  void resizeAndZero(int sz) {
154  if (sz > len_) {
155  if (len_ != 0)
158  len_ = sz;
159  }
160  else if (sz > sz_)
162  sz_ = sz;
163  }
164 
166 
171  void expand(int sz) {
172  if (sz > len_) {
173  U* dx_new = ds_array<U>::get_and_fill(sz);
174  ds_array<U>::copy(dx_, dx_new, sz_);
175  if (len_ > 0)
177  dx_ = dx_new;
178  len_ = sz;
179  }
180  else if (sz > sz_)
182  sz_ = sz;
183  }
184 
187  void zero() {
189  }
190 
193  const T& val() const { return val_; }
194 
197  T& val() { return val_; }
198 
201  const U* dx() const { return dx_;}
202 
203 #if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD_STRIDED) && !defined(SACADO_DISABLE_CUDA_IN_KOKKOS) && defined(__CUDA_ARCH__)
204 
207  U dx(int i) const { return sz_ ? dx_[i*blockDim.x] : U(0.); }
208 
211  U& fastAccessDx(int i) { return dx_[i*blockDim.x];}
212 
215  const U& fastAccessDx(int i) const { return dx_[i*blockDim.x];}
216 
217 #else
218 
221  U dx(int i) const { return sz_ ? dx_[i] : U(0.); }
222 
225  U& fastAccessDx(int i) { return dx_[i];}
226 
229  const U& fastAccessDx(int i) const { return dx_[i];}
230 
231 #endif
232 
233  protected:
234 
237 
239  int sz_;
240 
242  int len_;
243 
245  U* dx_;
246 
247  }; // class DynamicStorage
248 
249  } // namespace Exp
250  } // namespace Fad
251 
252 } // namespace Sacado
253 
254 #endif // SACADO_FAD_EXP_DYNAMICSTORAGE_HPP
Turn DynamicStorage into a meta-function class usable with mpl::apply.
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 DynamicStorage & operator=(const DynamicStorage &x)
Assignment.
KOKKOS_INLINE_FUNCTION ~DynamicStorage()
Destructor.
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 void resize(int sz)
Resize the derivative array to sz.
KOKKOS_INLINE_FUNCTION DynamicStorage(const DynamicStorage &x)
Copy constructor.
#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 DynamicStorage(const int sz, const T &x, const DerivInit zero_out)
Constructor with size 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 int length() const
Returns array length.
KOKKOS_INLINE_FUNCTION const T & val() const
Returns value.
KOKKOS_INLINE_FUNCTION T & val()
Returns value.
KOKKOS_INLINE_FUNCTION void resizeAndZero(int sz)
Resize the derivative array to sz.
KOKKOS_INLINE_FUNCTION void zero()
Zero out derivative array.
Derivative array storage class using dynamic memory allocation.
static KOKKOS_INLINE_FUNCTION void zero(T *dest, int sz)
Zero out array dest of length sz.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
KOKKOS_INLINE_FUNCTION DynamicStorage()
Default constructor.
KOKKOS_INLINE_FUNCTION U & fastAccessDx(int i)
Returns derivative component i without bounds checking.
Initialize the derivative array.
KOKKOS_INLINE_FUNCTION DynamicStorage(const T &x)
Constructor with value.
KOKKOS_INLINE_FUNCTION U dx(int i) const
Returns derivative component i with bounds checking.
KOKKOS_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
KOKKOS_INLINE_FUNCTION const U * dx() const
Returns derivative array.