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_StaticStorage.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_STATICSTORAGE_HPP
31 #define SACADO_FAD_EXP_STATICSTORAGE_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 
49  template <typename T, int Num>
50  class StaticStorage {
51 
52  public:
53 
54  typedef typename std::remove_cv<T>::type value_type;
55  static constexpr bool is_statically_sized = false;
56  static constexpr int static_size = 0;
57  static constexpr bool is_view = false;
58 
60  template <typename TT>
61  struct apply {
63  };
64 
66  template <int N>
67  struct apply_N {
69  };
70 
74  val_(), sz_(0) {}
75 
78  StaticStorage(const T & x) :
79  val_(x), sz_(0) {}
80 
82 
86  StaticStorage(const int sz, const T & x,
87  const DerivInit zero_out = InitDerivArray) :
88  val_(x), sz_(sz) {
89 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
90  if (sz > Num)
91  throw "StaticStorage::StaticStorage() Error: Supplied derivative dimension exceeds maximum length.";
92 #endif
93  if (zero_out == InitDerivArray)
95  }
96 
98 
104  StaticStorage(const int sz, const int i, const value_type & x) :
105  StaticStorage(sz, x, InitDerivArray) {
106  dx_[i]=1.;
107  }
108 
112  val_(x.val_), sz_(x.sz_) {
113  //ss_array<T>::copy(x.dx_, dx_, sz_);
114  for (int i=0; i<sz_; i++)
115  dx_[i] = x.dx_[i];
116  }
117 
121  val_(std::move(x.val_)), sz_(x.sz_) {
122  for (int i=0; i<sz_; i++)
123  dx_[i] = std::move(x.dx_[i]);
124  }
125 
129 
133  if (this != &x) {
134  val_ = x.val_;
135  sz_ = x.sz_;
136  //ss_array<T>::copy(x.dx_, dx_, sz_);
137  for (int i=0; i<sz_; i++)
138  dx_[i] = x.dx_[i];
139  }
140  return *this;
141  }
142 
146  if (this != &x) {
147  val_ = std::move(x.val_);
148  sz_ = x.sz_;
149  for (int i=0; i<sz_; i++)
150  dx_[i] = std::move(x.dx_[i]);
151  }
152  return *this;
153  }
154 
157  int size() const { return sz_;}
158 
161  int length() const { return Num; }
162 
165  void resize(int sz) {
166 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
167  if (sz > Num)
168  throw "StaticStorage::resize() Error: Supplied derivative dimension exceeds maximum length.";
169 #endif
170  sz_ = sz;
171  }
172 
174 
179  void resizeAndZero(int sz) {
180 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
181  if (sz > Num)
182  throw "StaticStorage::resize() Error: Supplied derivative dimension exceeds maximum length.";
183 #endif
184  if (sz > sz_)
186  sz_ = sz;
187  }
188 
190 
195  void expand(int sz) {
196 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
197  if (sz > Num)
198  throw "StaticStorage::resize() Error: Supplied derivative dimension exceeds maximum length.";
199 #endif
200  if (sz > sz_)
202  sz_ = sz;
203  }
204 
205 
209 
212  const T& val() const { return val_; }
213 
216  T& val() { return val_; }
217 
220  const T* dx() const { return dx_;}
221 
224  T dx(int i) const { return sz_ ? dx_[i] : T(0.); }
225 
228  T& fastAccessDx(int i) { return dx_[i];}
229 
232  const T& fastAccessDx(int i) const { return dx_[i];}
233 
234  protected:
235 
238 
240  T dx_[Num];
241 
243  int sz_;
244 
245  }; // class StaticStorage
246 
247  } // namespace Exp
248  } // namespace Fad
249 
250 } // namespace Sacado
251 
252 #endif // SACADO_FAD_EXP_STATICSTORAGE_HPP
KOKKOS_INLINE_FUNCTION T dx(int i) const
Returns derivative component i with bounds checking.
Turn StaticStorage into a meta-function class usable with mpl::apply.
KOKKOS_INLINE_FUNCTION StaticStorage()
Default constructor.
KOKKOS_INLINE_FUNCTION void resizeAndZero(int sz)
Resize the derivative array to sz.
KOKKOS_INLINE_FUNCTION T & val()
Returns value.
KOKKOS_INLINE_FUNCTION StaticStorage(const int sz, const int i, const value_type &x)
Constructor with size sz, index i, and value x.
KOKKOS_INLINE_FUNCTION void zero()
Zero out derivative array.
KOKKOS_INLINE_FUNCTION int size() const
Returns number of derivative components.
KOKKOS_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
KOKKOS_INLINE_FUNCTION StaticStorage(const T &x)
Constructor with value.
static KOKKOS_INLINE_FUNCTION void zero(T *dest, int sz)
Zero out array dest of length sz.
KOKKOS_INLINE_FUNCTION ~StaticStorage()
Destructor.
#define KOKKOS_INLINE_FUNCTION
#define T
Definition: Sacado_rad.hpp:573
KOKKOS_INLINE_FUNCTION StaticStorage(const StaticStorage &x)
Copy constructor.
KOKKOS_INLINE_FUNCTION StaticStorage & operator=(const StaticStorage &x)
Assignment.
KOKKOS_INLINE_FUNCTION StaticStorage(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz.
KOKKOS_INLINE_FUNCTION const T & val() const
Returns value.
KOKKOS_INLINE_FUNCTION int length() const
Returns array length.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
KOKKOS_INLINE_FUNCTION const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION StaticStorage & operator=(StaticStorage &&x)
Move assignment.
Initialize the derivative array.
Derivative array storage class using static memory allocation.
Replace static derivative length (interpreted as a fixed length)
KOKKOS_INLINE_FUNCTION T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
KOKKOS_INLINE_FUNCTION const T * dx() const
Returns derivative array.
KOKKOS_INLINE_FUNCTION StaticStorage(StaticStorage &&x)
Move constructor.