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_ViewStorage.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_VIEWSTORAGE_HPP
31 #define SACADO_FAD_EXP_VIEWSTORAGE_HPP
32 
33 #include <type_traits>
34 #include <memory>
35 
38 
39 namespace Sacado {
40 
41  namespace Fad {
42  namespace Exp {
43 
44  // Class representing a pointer to ViewFad so that &ViewFad is supported
45  template <typename T, unsigned sl, unsigned ss, typename U>
46  class ViewFadPtr;
47 
53  template <typename T, unsigned static_length, unsigned static_stride, typename U>
54  class ViewStorage {
55 
56  private:
57 
58  // Enumerated flag so logic is evaluated at compile-time
59  static constexpr bool stride_one = (1 == static_stride);
60 
61  public:
62 
63  typedef typename std::remove_cv<T>::type value_type;
64  static constexpr bool is_statically_sized = (static_length > 0);
65  static constexpr int static_size = static_length;
66  typedef U base_fad_type;
67 
69  template <typename TT>
70  struct apply {
72  };
73 
75  template <int N>
76  struct apply_N {
78  };
79 
83  sz_(0), stride_(0), val_(0), dx_(0) {}
84 
87  ViewStorage(const T & x) :
88  sz_(0), stride_(0), val_(0), dx_(0) {}
89 
92  ViewStorage(const int sz, const T & x, const DerivInit zero_out) :
93  sz_(0), stride_(0), val_(0), dx_(0) {}
94 
97  ViewStorage(T* v, const int arg_size = 0, const int arg_stride = 0) :
98  sz_(arg_size), stride_(arg_stride), val_(v+sz_.value*stride_.value), dx_(v) {}
99 
102  ViewStorage(T* arg_dx, T* arg_val, const int arg_size = 0,
103  const int arg_stride = 0) :
104  sz_(arg_size), stride_(arg_stride), val_(arg_val), dx_(arg_dx) {}
105 
109  sz_(x.sz_), stride_(x.stride_), val_(x.val_), dx_(x.dx_) {}
110 
114 
118  if (this != std::addressof(x)) {
119  *val_ = *x.val_;
120  if (stride_one)
121  for (int i=0; i<sz_.value; ++i)
122  dx_[i] = x.dx_[i];
123  else
124  for (int i=0; i<sz_.value; ++i)
125  dx_[i*stride_.value] = x.dx_[i*x.stride_.value];
126  }
127  return *this;
128  }
129 
132  constexpr int size() const { return sz_.value;}
133 
136  constexpr int length() const { return sz_.value; }
137 
139 
145  void resize(int sz) {
147  }
148 
150 
156  void resizeAndZero(int sz) {}
157 
160  void expand(int sz) {}
161 
164  void zero() {
166  }
167 
170  const T& val() const { return *val_; }
171 
174  T& val() { return *val_; }
175 
178  const T* dx() const { return dx_;}
179 
182  T dx(int i) const {
183  return unsigned(sz_.value) ? dx_[ stride_one ? i : i * stride_.value ] : T(0.);
184  }
185 
188  T& fastAccessDx(int i) {
189  return dx_[ stride_one ? i : i * stride_.value ];
190  }
191 
194  const T& fastAccessDx(int i) const {
195  return dx_[ stride_one ? i : i * stride_.value ];
196  }
197 
202  this->dx_, this->val_, this->sz_.value, this->stride_.value);
203  }
204 
205  protected:
206 
209 
212 
214  T *val_;
215 
217  T *dx_;
218 
219  }; // class ViewStorage
220 
221  } // namespace Exp
222  } // namespace Fad
223 
224 } // namespace Sacado
225 
226 #endif // SACADO_FAD_EXP_VIEWSTORAGE_HPP
const mpl::integral_nonzero_constant< int, static_length > sz_
Derivative array size.
Turn ViewStorage into a meta-function class usable with mpl::apply.
KOKKOS_INLINE_FUNCTION constexpr int length() const
Returns array length.
KOKKOS_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
KOKKOS_INLINE_FUNCTION ViewStorage(T *arg_dx, T *arg_val, const int arg_size=0, const int arg_stride=0)
Constructor.
KOKKOS_INLINE_FUNCTION T dx(int i) const
Returns derivative component i with bounds checking.
KOKKOS_INLINE_FUNCTION const T * dx() const
Returns derivative array.
static KOKKOS_INLINE_FUNCTION void strided_zero(T *dest, int stride, int sz)
Zero out array dest of length sz.
KOKKOS_INLINE_FUNCTION ViewStorage & operator=(const ViewStorage &x)
Assignment.
KOKKOS_INLINE_FUNCTION void resizeAndZero(int sz)
Resize the derivative array to sz.
KOKKOS_INLINE_FUNCTION ViewFadPtr< T, static_length, static_stride, U > operator&() const
Overload of addressof operator.
KOKKOS_INLINE_FUNCTION T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION ViewStorage(T *v, const int arg_size=0, const int arg_stride=0)
Constructor.
Derivative array storage class that is a view into a contiguous memory allocation. It does not provide proper value semantics and thus should not be used in a general-purpose scalar type.
ViewStorage< TT, static_length, static_stride, U > type
#define KOKKOS_INLINE_FUNCTION
#define T
Definition: Sacado_rad.hpp:573
KOKKOS_INLINE_FUNCTION T & val()
Returns value.
KOKKOS_INLINE_FUNCTION ViewStorage(const T &x)
Constructor with value (needed to satisfy interface)
KOKKOS_INLINE_FUNCTION const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
ViewStorage< T, static_length, static_stride, U > type
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
KOKKOS_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
KOKKOS_INLINE_FUNCTION ViewStorage(const int sz, const T &x, const DerivInit zero_out)
Constructor with size sz (needed to satisfy interface)
KOKKOS_INLINE_FUNCTION ViewStorage()
Default constructor (needed to satisfy interface)
KOKKOS_INLINE_FUNCTION void zero()
Zero out derivative array.
KOKKOS_INLINE_FUNCTION constexpr int size() const
Returns number of derivative components.
KOKKOS_INLINE_FUNCTION ViewStorage(const ViewStorage &x)
Copy constructor.
KOKKOS_INLINE_FUNCTION ~ViewStorage()
Destructor.
KOKKOS_INLINE_FUNCTION const T & val() const
Returns value.
const mpl::integral_nonzero_constant< int, static_stride > stride_
Derivative array stride.