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_StaticFixedStorage.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_STATICFIXEDSTORAGE_HPP
11 #define SACADO_FAD_EXP_STATICFIXEDSTORAGE_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>
31 
32  public:
33 
34  typedef typename std::remove_cv<T>::type value_type;
35  static constexpr bool is_statically_sized = true;
36  static constexpr int static_size = Num;
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 
52 #ifdef SACADO_SFAD_INIT_DEFAULT_CONSTRUCTOR
55  val_(T(0.0)) {
56  ss_array<T>::zero(dx_, Num);
57  }
58 #else
60  StaticFixedStorage() = default;
61 #endif
62 
65  StaticFixedStorage(const T & x) :
66  val_(x) {
67  ss_array<T>::zero(dx_, Num);
68  }
69 
71 
75  StaticFixedStorage(const int sz, const T & x,
76  const DerivInit zero_out = InitDerivArray) :
77  val_(x) {
78 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ ) && !defined(__HIP_DEVICE_COMPILE__)
79  if (sz != Num)
80  throw "StaticFixedStorage::StaticFixedStorage() Error: Supplied derivative dimension does not equal static length.";
81 #endif
82  if (zero_out == InitDerivArray)
83  ss_array<T>::zero(dx_, Num);
84  }
85 
87 
93  StaticFixedStorage(const int sz, const int i, const value_type & x) :
95  dx_[i]=1.;
96  }
97 
99 
106  val_(x.val_) {
107  for (int i=0; i<Num; i++)
108  dx_[i] = x.dx_[i];
109  }
110 
114  val_(std::move(x.val_)) {
115  for (int i=0; i<Num; i++)
116  dx_[i] = std::move(x.dx_[i]);
117  }
118 
121  ~StaticFixedStorage() = default;
122 
124 
131  if (this != &x) {
132  val_ = x.val_;
133  for (int i=0; i<Num; i++)
134  dx_[i] = x.dx_[i];
135  }
136  return *this;
137  }
138 
142  if (this != &x) {
143  val_ = std::move(x.val_);
144  for (int i=0; i<Num; i++)
145  dx_[i] = std::move(x.dx_[i]);
146  }
147  return *this;
148  }
149 
152  constexpr int size() const { return Num; }
153 
156  constexpr int length() const { return Num; }
157 
160  void resize(int sz) {
161 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ ) && !defined(__HIP_DEVICE_COMPILE__)
162  if (sz != 0 && sz != Num)
163  throw "StaticFixedStorage::resize() Error: Cannot resize fixed storage length.";
164 #endif
165  // Because we don't track a "used" length and can't set the length to 0,
166  // we need to instead zero out derivative components if the length
167  // requested is 0
168  if (sz == 0)
169  ss_array<T>::zero(dx_, Num);
170  }
171 
174  void resizeAndZero(int sz) {
175 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ ) && !defined(__HIP_DEVICE_COMPILE__)
176  if (sz != 0 && sz != Num)
177  throw "StaticFixedStorage::resize() Error: Cannot resize fixed storage length.";
178 #endif
179  ss_array<T>::zero(dx_, Num);
180  }
181 
184  void expand(int sz) {
185 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ ) && !defined(__HIP_DEVICE_COMPILE__)
186  if (sz != Num)
187  throw "StaticFixedStorage::expand() Error: Cannot resize fixed storage length.";
188 #endif
189  }
190 
191 
194  void zero() { ss_array<T>::zero(dx_, Num); }
195 
198  const T& val() const { return val_; }
199 
202  T& val() { return val_; }
203 
206  const T* dx() const { return dx_;}
207 
210  T dx(int i) const { return dx_[i]; }
211 
214  T& fastAccessDx(int i) { return dx_[i]; }
215 
218  const T& fastAccessDx(int i) const { return dx_[i]; }
219 
220  protected:
221 
224 
226  T dx_[Num];
227 
228  }; // class StaticFixedStorage
229 
230  } // namespace Exp
231  } // namespace Fad
232 
233 } // namespace Sacado
234 
235 #endif // SACADO_FAD_EXP_STATICFIXEDSTORAGE_HPP
SACADO_INLINE_FUNCTION StaticFixedStorage(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz.
Turn StaticFixedStorage into a meta-function class usable with mpl::apply.
#define SACADO_DEFAULTED_FUNCTION
SACADO_INLINE_FUNCTION StaticFixedStorage & operator=(const StaticFixedStorage &x)
Assignment.
SACADO_INLINE_FUNCTION StaticFixedStorage(const int sz, const int i, const value_type &x)
Constructor with size sz, index i, and value x.
SACADO_INLINE_FUNCTION T dx(int i) const
Returns derivative component i with bounds checking.
SACADO_INLINE_FUNCTION StaticFixedStorage(StaticFixedStorage &&x)
Move constructor.
SACADO_INLINE_FUNCTION T & val()
Returns value.
SACADO_INLINE_FUNCTION StaticFixedStorage & operator=(StaticFixedStorage &&x)
Move assignment.
SACADO_DEFAULTED_FUNCTION ~StaticFixedStorage()=default
Destructor.
SACADO_INLINE_FUNCTION T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
SACADO_INLINE_FUNCTION StaticFixedStorage(const T &x)
Constructor with value.
#define T
Definition: Sacado_rad.hpp:553
Derivative array storage class using static, fixed memory allocation.
SACADO_INLINE_FUNCTION constexpr int length() const
Returns array length.
SACADO_INLINE_FUNCTION const T * dx() const
Returns derivative array.
SACADO_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
SACADO_INLINE_FUNCTION const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
SACADO_DEFAULTED_FUNCTION StaticFixedStorage()=default
Default constructor.
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 & val() const
Returns value.
SACADO_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
SACADO_INLINE_FUNCTION StaticFixedStorage(const StaticFixedStorage &x)
Copy constructor.
#define SACADO_INLINE_FUNCTION
SACADO_INLINE_FUNCTION constexpr int size() const
Returns number of derivative components.
SACADO_INLINE_FUNCTION void resizeAndZero(int sz)
Resize the derivative array to sz.
SACADO_INLINE_FUNCTION void zero()
Zero out derivative array.