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 
40 namespace Sacado {
41 
42  namespace Fad {
43  namespace Exp {
44 
45 #ifndef SACADO_FAD_DERIV_LOOP
46 #if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) && !defined(SACADO_DISABLE_CUDA_IN_KOKKOS) && defined(__CUDA_ARCH__)
47 #define SACADO_FAD_DERIV_LOOP(I,SZ) for (int I=threadIdx.x; I<SZ; I+=blockDim.x)
48 #else
49 #define SACADO_FAD_DERIV_LOOP(I,SZ) for (int I=0; I<SZ; ++I)
50 #endif
51 #endif
52 
53  // Class representing a pointer to ViewFad so that &ViewFad is supported
54  template <typename T, unsigned sl, unsigned ss, typename U>
55  class ViewFadPtr;
56 
62  template <typename T, unsigned static_length, unsigned static_stride, typename U>
63  class ViewStorage {
64 
65  private:
66 
67  // Enumerated flag so logic is evaluated at compile-time
68  static constexpr bool stride_one = (1 == static_stride);
69 
70  public:
71 
72  typedef typename std::remove_cv<T>::type value_type;
73  static constexpr bool is_statically_sized = (static_length > 0);
74  static constexpr int static_size = static_length;
75  static constexpr bool is_view = true;
76  typedef U base_fad_type;
77 
79  template <typename TT>
80  struct apply {
82  };
83 
85  template <int N>
86  struct apply_N {
88  };
89 
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 
111  // Move does not make sense for this storage since it is always tied to
112  // some preallocated data. Don't define move constructor so compiler will
113  // always fall-back to copy
114 
118 
122  // Can't use std::addressof() on the GPU, so this is equivalent
123  // according to cppreference.com
124  if (this != reinterpret_cast<ViewStorage*>(
125  &const_cast<char&>(
126  reinterpret_cast<const volatile char&>(x)))) {
127  *val_ = *x.val_;
128  if (stride_one)
129  //for (int i=0; i<sz_.value; ++i)
131  dx_[i] = x.dx_[i];
132  else
133  //for (int i=0; i<sz_.value; ++i)
135  dx_[i*stride_.value] = x.dx_[i*x.stride_.value];
136  }
137  return *this;
138  }
139 
140  // Move does not make sense for this storage since it is always tied to
141  // some preallocated data. Don't define move assignment so compiler will
142  // always fall-back to copy
143 
146  constexpr int size() const { return sz_.value;}
147 
150  constexpr int length() const { return sz_.value; }
151 
153 
159  void resize(int sz) {
161  }
162 
164 
170  void resizeAndZero(int sz) {}
171 
174  void expand(int sz) {}
175 
178  void zero() {
180  }
181 
184  const T& val() const { return *val_; }
185 
188  T& val() { return *val_; }
189 
192  const T* dx() const { return dx_;}
193 
196  T dx(int i) const {
197  return unsigned(sz_.value) ? dx_[ stride_one ? i : i * stride_.value ] : T(0.);
198  }
199 
202  T& fastAccessDx(int i) {
203  return dx_[ stride_one ? i : i * stride_.value ];
204  }
205 
208  const T& fastAccessDx(int i) const {
209  return dx_[ stride_one ? i : i * stride_.value ];
210  }
211 
216  this->dx_, this->val_, this->sz_.value, this->stride_.value);
217  }
218 
219  protected:
220 
223 
226 
228  T *val_;
229 
231  T *dx_;
232 
233  }; // class ViewStorage
234 
235  } // namespace Exp
236  } // namespace Fad
237 
238 } // namespace Sacado
239 
240 #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.
#define SACADO_FAD_DERIV_LOOP(I, SZ)
KOKKOS_INLINE_FUNCTION const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
ViewStorage< T, static_length, static_stride, U > type
KOKKOS_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
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.