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 #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>
51 
52  public:
53 
54  typedef typename std::remove_cv<T>::type value_type;
55  static constexpr bool is_statically_sized = true;
56  static constexpr int static_size = Num;
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 
72 #ifdef SACADO_SFAD_INIT_DEFAULT_CONSTRUCTOR
75  val_(T(0.0)) {
76  ss_array<T>::zero(dx_, Num);
77  }
78 #else
80  StaticFixedStorage() = default;
81 #endif
82 
85  StaticFixedStorage(const T & x) :
86  val_(x) {
87  ss_array<T>::zero(dx_, Num);
88  }
89 
91 
95  StaticFixedStorage(const int sz, const T & x,
96  const DerivInit zero_out = InitDerivArray) :
97  val_(x) {
98 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
99  if (sz != Num)
100  throw "StaticFixedStorage::StaticFixedStorage() Error: Supplied derivative dimension does not equal static length.";
101 #endif
102  if (zero_out == InitDerivArray)
103  ss_array<T>::zero(dx_, Num);
104  }
105 
107 
113  StaticFixedStorage(const int sz, const int i, const value_type & x) :
115  dx_[i]=1.;
116  }
117 
119 
126  val_(x.val_) {
127  for (int i=0; i<Num; i++)
128  dx_[i] = x.dx_[i];
129  }
130 
134  val_(std::move(x.val_)) {
135  for (int i=0; i<Num; i++)
136  dx_[i] = std::move(x.dx_[i]);
137  }
138 
141  ~StaticFixedStorage() = default;
142 
144 
151  if (this != &x) {
152  val_ = x.val_;
153  for (int i=0; i<Num; i++)
154  dx_[i] = x.dx_[i];
155  }
156  return *this;
157  }
158 
162  if (this != &x) {
163  val_ = std::move(x.val_);
164  for (int i=0; i<Num; i++)
165  dx_[i] = std::move(x.dx_[i]);
166  }
167  return *this;
168  }
169 
172  constexpr int size() const { return Num; }
173 
176  constexpr int length() const { return Num; }
177 
180  void resize(int sz) {
181 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
182  if (sz != 0 && sz != Num)
183  throw "StaticFixedStorage::resize() Error: Cannot resize fixed storage length.";
184 #endif
185  // Because we don't track a "used" length and can't set the length to 0,
186  // we need to instead zero out derivative components if the length
187  // requested is 0
188  if (sz == 0)
189  ss_array<T>::zero(dx_, Num);
190  }
191 
194  void resizeAndZero(int sz) {
195 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
196  if (sz != 0 && sz != Num)
197  throw "StaticFixedStorage::resize() Error: Cannot resize fixed storage length.";
198 #endif
199  ss_array<T>::zero(dx_, Num);
200  }
201 
204  void expand(int sz) {
205 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
206  if (sz != Num)
207  throw "StaticFixedStorage::expand() Error: Cannot resize fixed storage length.";
208 #endif
209  }
210 
211 
214  void zero() { ss_array<T>::zero(dx_, Num); }
215 
218  const T& val() const { return val_; }
219 
222  T& val() { return val_; }
223 
226  const T* dx() const { return dx_;}
227 
230  T dx(int i) const { return dx_[i]; }
231 
234  T& fastAccessDx(int i) { return dx_[i]; }
235 
238  const T& fastAccessDx(int i) const { return dx_[i]; }
239 
240  protected:
241 
244 
246  T dx_[Num];
247 
248  }; // class StaticFixedStorage
249 
250  } // namespace Exp
251  } // namespace Fad
252 
253 } // namespace Sacado
254 
255 #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 StaticFixedStorage & operator=(StaticFixedStorage &&x)
Move assignment.
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
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.
#define KOKKOS_DEFAULTED_FUNCTION
KOKKOS_INLINE_FUNCTION T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION StaticFixedStorage(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz.
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 int i, const value_type &x)
Constructor with size sz, index i, and value x.
KOKKOS_INLINE_FUNCTION StaticFixedStorage(StaticFixedStorage &&x)
Move constructor.
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.
KOKKOS_DEFAULTED_FUNCTION StaticFixedStorage()=default
Default constructor.
KOKKOS_DEFAULTED_FUNCTION ~StaticFixedStorage()=default
Destructor.