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_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_DYNAMICSTORAGE_HPP
31 #define SACADO_FAD_DYNAMICSTORAGE_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 DynamicStorage = Exp::DynamicStorage<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  val_(x), sz_(0), len_(0), dx_(NULL) {}
70 
72 
76  DynamicStorage(const int sz, const T & x, const DerivInit zero_out = InitDerivArray) :
77  val_(x), sz_(sz), len_(sz) {
78  if (zero_out == InitDerivArray)
80  else
82  }
83 
87  val_(x.val_), sz_(x.sz_), len_(x.sz_) {
89  }
90 
94  if (len_ != 0)
96  }
97 
101  if (this != &x) {
102  val_ = x.val_;
103  if (sz_ != x.sz_) {
104  sz_ = x.sz_;
105  if (x.sz_ > len_) {
106  if (len_ != 0)
108  len_ = x.sz_;
110  }
111  else
113  }
114  else
116  }
117  return *this;
118  }
119 
122  int size() const { return sz_;}
123 
126  int length() const { return len_; }
127 
129 
133  void resize(int sz) {
134  if (sz > len_) {
135  if (len_ != 0)
138  len_ = sz;
139  }
140  sz_ = sz;
141  }
142 
144 
149  void resizeAndZero(int sz) {
150  if (sz > len_) {
151  if (len_ != 0)
154  len_ = sz;
155  }
156  else if (sz > sz_)
158  sz_ = sz;
159  }
160 
162 
167  void expand(int sz) {
168  if (sz > len_) {
169  U* dx_new = ds_array<U>::get_and_fill(sz);
170  ds_array<U>::copy(dx_, dx_new, sz_);
171  if (len_ > 0)
173  dx_ = dx_new;
174  len_ = sz;
175  }
176  else if (sz > sz_)
178  sz_ = sz;
179  }
180 
183  void zero() {
185  }
186 
189  const T& val() const { return val_; }
190 
193  T& val() { return val_; }
194 
197  const U* dx() const { return dx_;}
198 
199 #if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD_STRIDED) && !defined(SACADO_DISABLE_CUDA_IN_KOKKOS) && defined(__CUDA_ARCH__)
200 
203  U dx(int i) const { return sz_ ? dx_[i*blockDim.x] : U(0.); }
204 
207  U& fastAccessDx(int i) { return dx_[i*blockDim.x];}
208 
211  const U& fastAccessDx(int i) const { return dx_[i*blockDim.x];}
212 
213 #else
214 
217  U dx(int i) const { return sz_ ? dx_[i] : U(0.); }
218 
221  U& fastAccessDx(int i) { return dx_[i];}
222 
225  const U& fastAccessDx(int i) const { return dx_[i];}
226 
227 #endif
228 
229  protected:
230 
233 
235  int sz_;
236 
238  int len_;
239 
241  U* dx_;
242 
243  }; // class DynamicStorage
244 
245  } // namespace Fad
246 
247 } // namespace Sacado
248 
249 #endif // SACADO_NEW_FAD_DESIGN_IS_DEFAULT
250 
251 #endif // SACADO_FAD_DYNAMICSTORAGE_HPP
static KOKKOS_INLINE_FUNCTION T * get(int sz)
Get memory for new array of length sz.
KOKKOS_INLINE_FUNCTION int size() const
Returns number of derivative components.
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 void resize(int sz)
Resize the derivative array to sz.
KOKKOS_INLINE_FUNCTION U dx(int i) const
Returns derivative component i with bounds checking.
Derivative array storage class using dynamic memory allocation.
KOKKOS_INLINE_FUNCTION DynamicStorage(const DynamicStorage &x)
Copy constructor.
#define SACADO_ENABLE_VALUE_CTOR_DECL
KOKKOS_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
KOKKOS_INLINE_FUNCTION const T & val() const
Returns value.
KOKKOS_INLINE_FUNCTION DynamicStorage(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz.
static KOKKOS_INLINE_FUNCTION void destroy_and_release(T *m, int sz)
Destroy array elements and release memory.
KOKKOS_INLINE_FUNCTION ~DynamicStorage()
Destructor.
#define KOKKOS_INLINE_FUNCTION
#define T
Definition: Sacado_rad.hpp:573
KOKKOS_INLINE_FUNCTION T & val()
Returns value.
KOKKOS_INLINE_FUNCTION const U * dx() const
Returns derivative array.
int len_
Derivative array length.
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 & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION void zero()
Zero out derivative array.
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 int length() const
Returns array length.
KOKKOS_INLINE_FUNCTION DynamicStorage & operator=(const DynamicStorage &x)
Assignment.
Initialize the derivative array.
KOKKOS_INLINE_FUNCTION void resizeAndZero(int sz)
Resize the derivative array to sz.
KOKKOS_INLINE_FUNCTION U & fastAccessDx(int i)
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION DynamicStorage(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Default constructor.