Sacado Package Browser (Single Doxygen Collection)  Version of the Day
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 #include <utility>
35 
36 #include "Sacado_ConfigDefs.h"
38 
39 namespace Sacado {
40 
41  namespace Fad {
42  namespace Exp {
43 
45  template <typename T, typename U = T>
47 
48  public:
49 
50  typedef typename std::remove_cv<T>::type value_type;
51  static constexpr bool is_statically_sized = false;
52  static constexpr int static_size = 0;
53  static constexpr bool is_view = false;
54 
56  template <typename TT, typename UU = TT>
57  struct apply {
59  };
60 
62  template <int N>
63  struct apply_N {
65  };
66 
70  val_(), sz_(0), len_(0), dx_(nullptr) {}
71 
74  DynamicStorage(const T & x) :
75  val_(x), sz_(0), len_(0), dx_(nullptr) {}
76 
78 
82  DynamicStorage(const int sz, const T & x,
83  const DerivInit zero_out = InitDerivArray) :
84  val_(x), sz_(sz), len_(sz) {
85  if (zero_out == InitDerivArray)
87  else
89  }
90 
92 
98  DynamicStorage(const int sz, const int i, const value_type & x) :
100  dx_[i]=1.;
101  }
102 
106  val_(x.val_), sz_(x.sz_), len_(x.sz_) {
108  }
109 
113  val_(std::move(x.val_)), sz_(x.sz_), len_(x.len_), dx_(x.dx_) {
114  x.sz_ = 0;
115  x.len_ = 0;
116  x.dx_ = nullptr;
117  }
118 
122  if (len_ != 0)
124  }
125 
129  if (this != &x) {
130  val_ = x.val_;
131  if (sz_ != x.sz_) {
132  sz_ = x.sz_;
133  if (x.sz_ > len_) {
134  if (len_ != 0)
136  len_ = x.sz_;
138  }
139  else
140  ds_array<U>::copy(x.dx_, dx_, sz_);
141  }
142  else
143  ds_array<U>::copy(x.dx_, dx_, sz_);
144  }
145  return *this;
146  }
147 
151  if (this != &x) {
152  if (len_ != 0)
154  val_ = std::move(x.val_);
155  sz_ = x.sz_; x.sz_ = 0;
156  len_ = x.len_; x.len_ = 0;
157  dx_ = x.dx_; x.dx_ = nullptr;
158  }
159  return *this;
160  }
161 
164  int size() const { return sz_;}
165 
168  int length() const { return len_; }
169 
171 
175  void resize(int sz) {
176  if (sz > len_) {
177  if (len_ != 0)
180  len_ = sz;
181  }
182  sz_ = sz;
183  }
184 
186 
191  void resizeAndZero(int sz) {
192  if (sz > len_) {
193  if (len_ != 0)
196  len_ = sz;
197  }
198  else if (sz > sz_)
200  sz_ = sz;
201  }
202 
204 
209  void expand(int sz) {
210  if (sz > len_) {
211  U* dx_new = ds_array<U>::get_and_fill(sz);
212  ds_array<U>::copy(dx_, dx_new, sz_);
213  if (len_ > 0)
215  dx_ = dx_new;
216  len_ = sz;
217  }
218  else if (sz > sz_)
220  sz_ = sz;
221  }
222 
225  void zero() {
227  }
228 
231  const T& val() const { return val_; }
232 
235  T& val() { return val_; }
236 
239  const U* dx() const { return dx_;}
240 
241 #if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD_STRIDED) && !defined(SACADO_DISABLE_CUDA_IN_KOKKOS) && defined(__CUDA_ARCH__)
242 
245  U dx(int i) const { return sz_ ? dx_[i*blockDim.x] : U(0.); }
246 
249  U& fastAccessDx(int i) { return dx_[i*blockDim.x];}
250 
253  const U& fastAccessDx(int i) const { return dx_[i*blockDim.x];}
254 
255 #else
256 
259  U dx(int i) const { return sz_ ? dx_[i] : U(0.); }
260 
263  U& fastAccessDx(int i) { return dx_[i];}
264 
267  const U& fastAccessDx(int i) const { return dx_[i];}
268 
269 #endif
270 
271  protected:
272 
275 
277  int sz_;
278 
280  int len_;
281 
283  U* dx_;
284 
285  }; // class DynamicStorage
286 
287  } // namespace Exp
288  } // namespace Fad
289 
290 } // namespace Sacado
291 
292 #endif // SACADO_FAD_EXP_DYNAMICSTORAGE_HPP
SACADO_INLINE_FUNCTION const U & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
Turn DynamicStorage into a meta-function class usable with mpl::apply.
SACADO_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
SACADO_INLINE_FUNCTION void resizeAndZero(int sz)
Resize the derivative array to sz.
SACADO_INLINE_FUNCTION DynamicStorage(const DynamicStorage &x)
Copy constructor.
SACADO_INLINE_FUNCTION int length() const
Returns array length.
static SACADO_INLINE_FUNCTION void zero(T *dest, int sz)
Zero out array dest of length sz.
SACADO_INLINE_FUNCTION ~DynamicStorage()
Destructor.
SACADO_INLINE_FUNCTION DynamicStorage & operator=(const DynamicStorage &x)
Assignment.
SACADO_INLINE_FUNCTION U dx(int i) const
Returns derivative component i with bounds checking.
SACADO_INLINE_FUNCTION DynamicStorage(const T &x)
Constructor with value.
#define T
Definition: Sacado_rad.hpp:573
SACADO_INLINE_FUNCTION int size() const
Returns number of derivative components.
SACADO_INLINE_FUNCTION DynamicStorage(DynamicStorage &&x)
Move constructor.
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.
SACADO_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
Derivative array storage class using dynamic memory allocation.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
SACADO_INLINE_FUNCTION const T & val() const
Returns value.
SACADO_INLINE_FUNCTION const U * dx() const
Returns derivative array.
SACADO_INLINE_FUNCTION DynamicStorage(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz.
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 DynamicStorage(const int sz, const int i, const value_type &x)
Constructor with size sz, index i, and value x.
SACADO_INLINE_FUNCTION DynamicStorage & operator=(DynamicStorage &&x)
Move assignment.
Initialize the derivative array.
static SACADO_INLINE_FUNCTION T * get(int sz)
Get memory for new array of length sz.
SACADO_INLINE_FUNCTION DynamicStorage()
Default constructor.
#define SACADO_INLINE_FUNCTION
SACADO_INLINE_FUNCTION void zero()
Zero out derivative array.
SACADO_INLINE_FUNCTION U & fastAccessDx(int i)
Returns derivative component i without bounds checking.
static SACADO_INLINE_FUNCTION T * get_and_fill(int sz)
Get memory for new array of length sz and fill with zeros.