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 //
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_STATICFIXEDSTORAGE_HPP
31 #define SACADO_FAD_EXP_STATICFIXEDSTORAGE_HPP
32 
33 #include <type_traits>
34 
35 #include "Sacado_ConfigDefs.h"
37 
38 namespace Sacado {
39 
40  namespace Fad {
41  namespace Exp {
42 
44 
48  template <typename T, int Num>
50 
51  public:
52 
53  typedef typename std::remove_cv<T>::type value_type;
54  static constexpr bool is_statically_sized = true;
55  static constexpr int static_size = Num;
56 
58  template <typename TT>
59  struct apply {
61  };
62 
64  template <int N>
65  struct apply_N {
67  };
68 
70  StaticFixedStorage() = default;
71 
74  StaticFixedStorage(const T & x) :
75  val_(x) {
76  ss_array<T>::zero(dx_, Num);
77  }
78 
80 
84  StaticFixedStorage(const int sz, const T & x, const DerivInit zero_out) :
85  val_(x) {
86 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
87  if (sz != Num)
88  throw "StaticFixedStorage::StaticFixedStorage() Error: Supplied derivative dimension does not equal static length.";
89 #endif
90  if (zero_out == InitDerivArray)
91  ss_array<T>::zero(dx_, Num);
92  }
93 
95 
102  val_(x.val_) {
103  for (int i=0; i<Num; i++)
104  dx_[i] = x.dx_[i];
105  }
106 
108  ~StaticFixedStorage() = default;
109 
111 
118  if (this != &x) {
119  val_ = x.val_;
120  for (int i=0; i<Num; i++)
121  dx_[i] = x.dx_[i];
122  }
123  return *this;
124  }
125 
128  constexpr int size() const { return Num; }
129 
132  constexpr int length() const { return Num; }
133 
136  void resize(int sz) {
137 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
138  if (sz != 0 && sz != Num)
139  throw "StaticFixedStorage::resize() Error: Cannot resize fixed storage length.";
140 #endif
141  // Because we don't track a "used" length and can't set the length to 0,
142  // we need to instead zero out derivative components if the length
143  // requested is 0
144  if (sz == 0)
145  ss_array<T>::zero(dx_, Num);
146  }
147 
150  void resizeAndZero(int sz) {
151 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
152  if (sz != 0 && sz != Num)
153  throw "StaticFixedStorage::resize() Error: Cannot resize fixed storage length.";
154 #endif
155  ss_array<T>::zero(dx_, Num);
156  }
157 
160  void expand(int sz) {
161 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
162  if (sz != Num)
163  throw "StaticFixedStorage::expand() Error: Cannot resize fixed storage length.";
164 #endif
165  }
166 
167 
170  void zero() { ss_array<T>::zero(dx_, Num); }
171 
174  const T& val() const { return val_; }
175 
178  T& val() { return val_; }
179 
182  const T* dx() const { return dx_;}
183 
186  T dx(int i) const { return dx_[i]; }
187 
190  T& fastAccessDx(int i) { return dx_[i]; }
191 
194  const T& fastAccessDx(int i) const { return dx_[i]; }
195 
196  protected:
197 
200 
202  T dx_[Num];
203 
204  }; // class StaticFixedStorage
205 
206  } // namespace Exp
207  } // namespace Fad
208 
209 } // namespace Sacado
210 
211 #endif // SACADO_FAD_EXP_STATICFIXEDSTORAGE_HPP
Turn StaticFixedStorage into a meta-function class usable with mpl::apply.
KOKKOS_INLINE_FUNCTION StaticFixedStorage(const StaticFixedStorage &x)
Copy constructor.
KOKKOS_INLINE_FUNCTION const T * dx() const
Returns derivative array.
KOKKOS_INLINE_FUNCTION StaticFixedStorage(const T &x)
Constructor with value.
KOKKOS_INLINE_FUNCTION constexpr int size() const
Returns number of derivative components.
KOKKOS_INLINE_FUNCTION T 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.
KOKKOS_INLINE_FUNCTION constexpr int length() const
Returns array length.
#define KOKKOS_INLINE_FUNCTION
#define T
Definition: Sacado_rad.hpp:573
~StaticFixedStorage()=default
Destructor.
KOKKOS_INLINE_FUNCTION const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
Derivative array storage class using static, fixed memory allocation.
KOKKOS_INLINE_FUNCTION T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION void zero()
Zero out derivative array.
KOKKOS_INLINE_FUNCTION const T & val() const
Returns value.
KOKKOS_INLINE_FUNCTION StaticFixedStorage & operator=(const StaticFixedStorage &x)
Assignment.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
KOKKOS_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
Initialize the derivative array.
KOKKOS_INLINE_FUNCTION StaticFixedStorage(const int sz, const T &x, const DerivInit zero_out)
Constructor with size sz.
KOKKOS_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
KOKKOS_INLINE_FUNCTION T & val()
Returns value.
KOKKOS_INLINE_FUNCTION void resizeAndZero(int sz)
Resize the derivative array to sz.
StaticFixedStorage()=default
Default constructor.