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 // 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_VIEWSTORAGE_HPP
11 #define SACADO_FAD_EXP_VIEWSTORAGE_HPP
12 
13 #include <type_traits>
14 #include <utility>
15 #include <memory>
16 
19 #include "Sacado_mpl_apply.hpp"
20 
21 namespace Sacado {
22 
23  namespace Fad {
24  namespace Exp {
25 
26 #ifndef SACADO_FAD_DERIV_LOOP
27 #if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) && !defined(SACADO_DISABLE_CUDA_IN_KOKKOS) && ( defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) )
28 #define SACADO_FAD_DERIV_LOOP(I,SZ) for (int I=threadIdx.x; I<SZ; I+=blockDim.x)
29 #else
30 #define SACADO_FAD_DERIV_LOOP(I,SZ) for (int I=0; I<SZ; ++I)
31 #endif
32 #endif
33 
34  // Class representing a pointer to ViewFad so that &ViewFad is supported
35  template <typename T, unsigned sl, unsigned ss, typename U>
36  class ViewFadPtr;
37 
43  template <typename T, unsigned static_length, unsigned static_stride, typename U>
44  class ViewStorage {
45 
46  private:
47 
48  // Enumerated flag so logic is evaluated at compile-time
49  static constexpr bool stride_one = (1 == static_stride);
50 
51  public:
52 
53  typedef typename std::remove_cv<T>::type value_type;
54  static constexpr bool is_statically_sized = (static_length > 0);
55  static constexpr int static_size = static_length;
56  static constexpr bool is_view = true;
57  typedef U base_fad_type;
58 
60  template <typename TT>
61  struct apply {
62  typedef typename std::remove_cv<TT>::type non_const_TT;
66  };
67 
69  template <int N>
70  struct apply_N {
72  };
73 
77  sz_(0), stride_(0), val_(0), dx_(0) {}
78 
81  ViewStorage(T* v, const int arg_size = 0, const int arg_stride = 0) :
82  sz_(arg_size), stride_(arg_stride), val_(v+sz_.value*static_cast<int>(stride_.value)), dx_(v) {}
83 
86  ViewStorage(T* arg_dx, T* arg_val, const int arg_size = 0,
87  const int arg_stride = 0) :
88  sz_(arg_size), stride_(arg_stride), val_(arg_val), dx_(arg_dx) {}
89 
91 
97  template <typename TT>
101  typename std::enable_if< std::is_same<TT,T>::value ||
102  std::is_same<const TT,T>::value >::type* = 0)
103  : sz_(x.sz_), stride_(x.stride_), val_(x.val_), dx_(x.dx_) {}
104 
105  // Move does not make sense for this storage since it is always tied to
106  // some preallocated data. Don't define move constructor so compiler will
107  // always fall-back to copy
108 
112 
116  // Can't use std::addressof() on the GPU, so this is equivalent
117  // according to cppreference.com
118  if (this != reinterpret_cast<ViewStorage*>(
119  &const_cast<char&>(
120  reinterpret_cast<const volatile char&>(x)))) {
121  *val_ = *x.val_;
122  if (stride_one)
123  //for (int i=0; i<sz_.value; ++i)
125  dx_[i] = x.dx_[i];
126  else
127  //for (int i=0; i<sz_.value; ++i)
129  dx_[i*stride_.value] = x.dx_[i*x.stride_.value];
130  }
131  return *this;
132  }
133 
134  // Move does not make sense for this storage since it is always tied to
135  // some preallocated data. Don't define move assignment so compiler will
136  // always fall-back to copy
137 
140  constexpr int size() const { return sz_.value;}
141 
144  constexpr int length() const { return sz_.value; }
145 
147 
153  void resize(int sz) {
155  }
156 
158 
164  void resizeAndZero(int sz) {}
165 
168  void expand(int sz) {}
169 
172  void zero() {
174  }
175 
178  const T& val() const { return *val_; }
179 
182  T& val() { return *val_; }
183 
186  const T* dx() const { return dx_;}
187 
190  T dx(int i) const {
191  return unsigned(sz_.value) ? dx_[ stride_one ? i : i * stride_.value ] : T(0.);
192  }
193 
196  T& fastAccessDx(int i) {
197  return dx_[ stride_one ? i : i * stride_.value ];
198  }
199 
202  const T& fastAccessDx(int i) const {
203  return dx_[ stride_one ? i : i * stride_.value ];
204  }
205 
210  this->dx_, this->val_, this->sz_.value, this->stride_.value);
211  }
212 
213  protected:
214 
215  template <typename, unsigned, unsigned, typename>
216  friend class ViewStorage;
217 
220 
223 
225  T *val_;
226 
228  T *dx_;
229 
230  }; // class ViewStorage
231 
232  } // namespace Exp
233  } // namespace Fad
234 
235 } // namespace Sacado
236 
237 #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.
SACADO_INLINE_FUNCTION const T & val() const
Returns value.
mpl::apply< U, base_expr_TT >::type UU
BaseExprType< non_const_TT >::type base_expr_TT
SACADO_INLINE_FUNCTION ViewStorage & operator=(const ViewStorage &x)
Assignment.
SACADO_INLINE_FUNCTION T dx(int i) const
Returns derivative component i with bounds checking.
SACADO_INLINE_FUNCTION ViewFadPtr< T, static_length, static_stride, U > operator&() const
Overload of addressof operator.
SACADO_INLINE_FUNCTION T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
SACADO_INLINE_FUNCTION const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
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.
SACADO_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
F::template apply< A1, A2, A3, A4, A5 >::type type
SACADO_INLINE_FUNCTION const T * dx() const
Returns derivative array.
SACADO_INLINE_FUNCTION constexpr int length() const
Returns array length.
#define T
Definition: Sacado_rad.hpp:553
SACADO_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
SACADO_INLINE_FUNCTION T & val()
Returns value.
SACADO_INLINE_FUNCTION void resizeAndZero(int sz)
Resize the derivative array to sz.
#define SACADO_FAD_DERIV_LOOP(I, SZ)
SACADO_INLINE_FUNCTION void zero()
Zero out derivative array.
ViewStorage< T, static_length, static_stride, U > type
SACADO_INLINE_FUNCTION ViewStorage(T *arg_dx, T *arg_val, const int arg_size=0, const int arg_stride=0)
Constructor.
int value
SACADO_INLINE_FUNCTION ViewStorage()
Default constructor (needed to satisfy interface)
SACADO_INLINE_FUNCTION ViewStorage(const ViewStorage< TT, static_length, static_stride, U > &x, typename std::enable_if< std::is_same< TT, T >::value||std::is_same< const TT, T >::value >::type *=0)
Copy constructor.
SACADO_INLINE_FUNCTION ~ViewStorage()
Destructor.
#define SACADO_INLINE_FUNCTION
ViewStorage< TT, static_length, static_stride, UU > type
SACADO_INLINE_FUNCTION ViewStorage(T *v, const int arg_size=0, const int arg_stride=0)
Constructor.
static SACADO_INLINE_FUNCTION void strided_zero(T *dest, int stride, int sz)
Zero out array dest of length sz.
const mpl::integral_nonzero_constant< int, static_stride > stride_
Derivative array stride.
SACADO_INLINE_FUNCTION constexpr int size() const
Returns number of derivative components.