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 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef SACADO_FAD_EXP_STATICSTORAGE_HPP
11 #define SACADO_FAD_EXP_STATICSTORAGE_HPP
12 
13 #include <type_traits>
14 #include <utility>
15 
16 #include "Sacado_ConfigDefs.h"
18 
19 namespace Sacado {
20 
21  namespace Fad {
22  namespace Exp {
23 
25 
29  template <typename T, int Num>
30  class StaticStorage {
31 
32  public:
33 
34  typedef typename std::remove_cv<T>::type value_type;
35  static constexpr bool is_statically_sized = false;
36  static constexpr int static_size = 0;
37  static constexpr bool is_view = false;
38 
40  template <typename TT>
41  struct apply {
43  };
44 
46  template <int N>
47  struct apply_N {
49  };
50 
54  val_(), sz_(0) {}
55 
58  StaticStorage(const T & x) :
59  val_(x), sz_(0) {}
60 
62 
66  StaticStorage(const int sz, const T & x,
67  const DerivInit zero_out = InitDerivArray) :
68  val_(x), sz_(sz) {
69 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ ) && !defined(__HIP_DEVICE_COMPILE__ )
70  if (sz > Num)
71  throw "StaticStorage::StaticStorage() Error: Supplied derivative dimension exceeds maximum length.";
72 #endif
73  if (zero_out == InitDerivArray)
75  }
76 
78 
84  StaticStorage(const int sz, const int i, const value_type & x) :
86  dx_[i]=1.;
87  }
88 
92  val_(x.val_), sz_(x.sz_) {
93  //ss_array<T>::copy(x.dx_, dx_, sz_);
94  for (int i=0; i<sz_; i++)
95  dx_[i] = x.dx_[i];
96  }
97 
101  val_(std::move(x.val_)), sz_(x.sz_) {
102  for (int i=0; i<sz_; i++)
103  dx_[i] = std::move(x.dx_[i]);
104  }
105 
109 
113  if (this != &x) {
114  val_ = x.val_;
115  sz_ = x.sz_;
116  //ss_array<T>::copy(x.dx_, dx_, sz_);
117  for (int i=0; i<sz_; i++)
118  dx_[i] = x.dx_[i];
119  }
120  return *this;
121  }
122 
126  if (this != &x) {
127  val_ = std::move(x.val_);
128  sz_ = x.sz_;
129  for (int i=0; i<sz_; i++)
130  dx_[i] = std::move(x.dx_[i]);
131  }
132  return *this;
133  }
134 
137  int size() const { return sz_;}
138 
141  int length() const { return Num; }
142 
145  void resize(int sz) {
146 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ ) && !defined(__HIP_DEVICE_COMPILE__ )
147  if (sz > Num)
148  throw "StaticStorage::resize() Error: Supplied derivative dimension exceeds maximum length.";
149 #endif
150  sz_ = sz;
151  }
152 
154 
159  void resizeAndZero(int sz) {
160 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ ) && !defined(__HIP_DEVICE_COMPILE__ )
161  if (sz > Num)
162  throw "StaticStorage::resize() Error: Supplied derivative dimension exceeds maximum length.";
163 #endif
164  if (sz > sz_)
166  sz_ = sz;
167  }
168 
170 
175  void expand(int sz) {
176 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ ) && !defined(__HIP_DEVICE_COMPILE__ )
177  if (sz > Num)
178  throw "StaticStorage::resize() Error: Supplied derivative dimension exceeds maximum length.";
179 #endif
180  if (sz > sz_)
182  sz_ = sz;
183  }
184 
185 
189 
192  const T& val() const { return val_; }
193 
196  T& val() { return val_; }
197 
200  const T* dx() const { return dx_;}
201 
204  T dx(int i) const { return sz_ ? dx_[i] : T(0.); }
205 
208  T& fastAccessDx(int i) { return dx_[i];}
209 
212  const T& fastAccessDx(int i) const { return dx_[i];}
213 
214  protected:
215 
218 
220  T dx_[Num];
221 
223  int sz_;
224 
225  }; // class StaticStorage
226 
227  } // namespace Exp
228  } // namespace Fad
229 
230 } // namespace Sacado
231 
232 #endif // SACADO_FAD_EXP_STATICSTORAGE_HPP
SACADO_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
SACADO_INLINE_FUNCTION T & val()
Returns value.
SACADO_INLINE_FUNCTION const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
Turn StaticStorage into a meta-function class usable with mpl::apply.
SACADO_INLINE_FUNCTION StaticStorage(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz.
SACADO_INLINE_FUNCTION StaticStorage & operator=(const StaticStorage &x)
Assignment.
SACADO_INLINE_FUNCTION const T & val() const
Returns value.
SACADO_INLINE_FUNCTION StaticStorage(const T &x)
Constructor with value.
SACADO_INLINE_FUNCTION void resizeAndZero(int sz)
Resize the derivative array to sz.
SACADO_INLINE_FUNCTION StaticStorage & operator=(StaticStorage &&x)
Move assignment.
#define T
Definition: Sacado_rad.hpp:553
SACADO_INLINE_FUNCTION StaticStorage()
Default constructor.
SACADO_INLINE_FUNCTION T dx(int i) const
Returns derivative component i with bounds checking.
SACADO_INLINE_FUNCTION int length() const
Returns array length.
SACADO_INLINE_FUNCTION ~StaticStorage()
Destructor.
SACADO_INLINE_FUNCTION StaticStorage(StaticStorage &&x)
Move constructor.
SACADO_INLINE_FUNCTION void zero()
Zero out derivative array.
SACADO_INLINE_FUNCTION StaticStorage(const int sz, const int i, const value_type &x)
Constructor with size sz, index i, and value x.
SACADO_INLINE_FUNCTION T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
static SACADO_INLINE_FUNCTION void zero(T *dest, int sz)
Zero out array dest of length sz.
Initialize the derivative array.
SACADO_INLINE_FUNCTION const T * dx() const
Returns derivative array.
#define SACADO_INLINE_FUNCTION
SACADO_INLINE_FUNCTION int size() const
Returns number of derivative components.
SACADO_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
Derivative array storage class using static memory allocation.
SACADO_INLINE_FUNCTION StaticStorage(const StaticStorage &x)
Copy constructor.
Replace static derivative length (interpreted as a fixed length)