Sacado Package Browser (Single Doxygen Collection)  Version of the Day
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_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  val_(x), sz_(0), len_(0), dx_(NULL) {}
53 
55 
59  DynamicStorage(const int sz, const T & x, const DerivInit zero_out = InitDerivArray) :
60  val_(x), sz_(sz), len_(sz) {
61  if (zero_out == InitDerivArray)
63  else
65  }
66 
70  val_(x.val_), sz_(x.sz_), len_(x.sz_) {
72  }
73 
77  if (len_ != 0)
79  }
80 
84  if (this != &x) {
85  val_ = x.val_;
86  if (sz_ != x.sz_) {
87  sz_ = x.sz_;
88  if (x.sz_ > len_) {
89  if (len_ != 0)
91  len_ = x.sz_;
93  }
94  else
96  }
97  else
99  }
100  return *this;
101  }
102 
105  int size() const { return sz_;}
106 
109  int length() const { return len_; }
110 
112 
116  void resize(int sz) {
117  if (sz > len_) {
118  if (len_ != 0)
121  len_ = sz;
122  }
123  sz_ = sz;
124  }
125 
127 
132  void resizeAndZero(int sz) {
133  if (sz > len_) {
134  if (len_ != 0)
137  len_ = sz;
138  }
139  else if (sz > sz_)
141  sz_ = sz;
142  }
143 
145 
150  void expand(int sz) {
151  if (sz > len_) {
152  U* dx_new = ds_array<U>::get_and_fill(sz);
153  ds_array<U>::copy(dx_, dx_new, sz_);
154  if (len_ > 0)
156  dx_ = dx_new;
157  len_ = sz;
158  }
159  else if (sz > sz_)
161  sz_ = sz;
162  }
163 
166  void zero() {
168  }
169 
172  const T& val() const { return val_; }
173 
176  T& val() { return val_; }
177 
180  const U* dx() const { return dx_;}
181 
184  U dx(int i) const { return sz_ ? dx_[i] : U(0.); }
185 
188  U& fastAccessDx(int i) { return dx_[i];}
189 
192  const U& fastAccessDx(int i) const { return dx_[i];}
193 
194  protected:
195 
198 
200  int sz_;
201 
203  int len_;
204 
206  U* dx_;
207 
208  }; // class DynamicStorage
209 
210  } // namespace Fad
211 
212 } // namespace Sacado
213 
214 #endif // SACADO_FAD_DYNAMICSTORAGE_HPP
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 void resize(int sz)
Resize the derivative array to sz.
KOKKOS_INLINE_FUNCTION int length() const
Returns array length.
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 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 const T & val() const
Returns value.
KOKKOS_INLINE_FUNCTION int size() const
Returns number of derivative components.
KOKKOS_INLINE_FUNCTION ~DynamicStorage()
Destructor.
KOKKOS_INLINE_FUNCTION const U & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
#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 void zero()
Zero out derivative array.
KOKKOS_INLINE_FUNCTION U dx(int i) const
Returns derivative component i with bounds checking.
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 & 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.