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 <utility>
35 #include <memory>
36 
39 #include "Sacado_mpl_apply.hpp"
40 
41 namespace Sacado {
42 
43  namespace Fad {
44  namespace Exp {
45 
46 #ifndef SACADO_FAD_DERIV_LOOP
47 #if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) && !defined(SACADO_DISABLE_CUDA_IN_KOKKOS) && ( defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) )
48 #define SACADO_FAD_DERIV_LOOP(I,SZ) for (int I=threadIdx.x; I<SZ; I+=blockDim.x)
49 #else
50 #define SACADO_FAD_DERIV_LOOP(I,SZ) for (int I=0; I<SZ; ++I)
51 #endif
52 #endif
53 
54  // Class representing a pointer to ViewFad so that &ViewFad is supported
55  template <typename T, unsigned sl, unsigned ss, typename U>
56  class ViewFadPtr;
57 
63  template <typename T, unsigned static_length, unsigned static_stride, typename U>
64  class ViewStorage {
65 
66  private:
67 
68  // Enumerated flag so logic is evaluated at compile-time
69  static constexpr bool stride_one = (1 == static_stride);
70 
71  public:
72 
73  typedef typename std::remove_cv<T>::type value_type;
74  static constexpr bool is_statically_sized = (static_length > 0);
75  static constexpr int static_size = static_length;
76  static constexpr bool is_view = true;
77  typedef U base_fad_type;
78 
80  template <typename TT>
81  struct apply {
82  typedef typename std::remove_cv<TT>::type non_const_TT;
86  };
87 
89  template <int N>
90  struct apply_N {
92  };
93 
97  sz_(0), stride_(0), val_(0), dx_(0) {}
98 
101  ViewStorage(T* v, const int arg_size = 0, const int arg_stride = 0) :
102  sz_(arg_size), stride_(arg_stride), val_(v+sz_.value*stride_.value), dx_(v) {}
103 
106  ViewStorage(T* arg_dx, T* arg_val, const int arg_size = 0,
107  const int arg_stride = 0) :
108  sz_(arg_size), stride_(arg_stride), val_(arg_val), dx_(arg_dx) {}
109 
111 
117  template <typename TT>
121  typename std::enable_if< std::is_same<TT,T>::value ||
122  std::is_same<const TT,T>::value >::type* = 0)
123  : sz_(x.sz_), stride_(x.stride_), val_(x.val_), dx_(x.dx_) {}
124 
125  // Move does not make sense for this storage since it is always tied to
126  // some preallocated data. Don't define move constructor so compiler will
127  // always fall-back to copy
128 
132 
136  // Can't use std::addressof() on the GPU, so this is equivalent
137  // according to cppreference.com
138  if (this != reinterpret_cast<ViewStorage*>(
139  &const_cast<char&>(
140  reinterpret_cast<const volatile char&>(x)))) {
141  *val_ = *x.val_;
142  if (stride_one)
143  //for (int i=0; i<sz_.value; ++i)
145  dx_[i] = x.dx_[i];
146  else
147  //for (int i=0; i<sz_.value; ++i)
149  dx_[i*stride_.value] = x.dx_[i*x.stride_.value];
150  }
151  return *this;
152  }
153 
154  // Move does not make sense for this storage since it is always tied to
155  // some preallocated data. Don't define move assignment so compiler will
156  // always fall-back to copy
157 
160  constexpr int size() const { return sz_.value;}
161 
164  constexpr int length() const { return sz_.value; }
165 
167 
173  void resize(int sz) {
175  }
176 
178 
184  void resizeAndZero(int sz) {}
185 
188  void expand(int sz) {}
189 
192  void zero() {
194  }
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 {
211  return unsigned(sz_.value) ? dx_[ stride_one ? i : i * stride_.value ] : T(0.);
212  }
213 
216  T& fastAccessDx(int i) {
217  return dx_[ stride_one ? i : i * stride_.value ];
218  }
219 
222  const T& fastAccessDx(int i) const {
223  return dx_[ stride_one ? i : i * stride_.value ];
224  }
225 
230  this->dx_, this->val_, this->sz_.value, this->stride_.value);
231  }
232 
233  protected:
234 
235  template <typename, unsigned, unsigned, typename>
236  friend class ViewStorage;
237 
240 
243 
245  T *val_;
246 
248  T *dx_;
249 
250  }; // class ViewStorage
251 
252  } // namespace Exp
253  } // namespace Fad
254 
255 } // namespace Sacado
256 
257 #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:573
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.