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_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_VIEWSTORAGE_HPP
11 #define SACADO_FAD_VIEWSTORAGE_HPP
12 
13 #include "Sacado_ConfigDefs.h"
14 
15 #ifdef SACADO_NEW_FAD_DESIGN_IS_DEFAULT
16 
18 
19 namespace Sacado {
20  namespace Fad {
21 
22  template <typename T, unsigned static_length, unsigned static_stride,
23  typename U>
24  using ViewStorage = Exp::ViewStorage<T,static_length,static_stride,U>;
25 
26  }
27 }
28 
29 #else
30 
31 #include "Sacado_Traits.hpp"
34 
35 namespace Sacado {
36 
37  namespace Fad {
38 
39 #ifndef SACADO_FAD_DERIV_LOOP
40 #if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) && !defined(SACADO_DISABLE_CUDA_IN_KOKKOS) && defined(__CUDA_ARCH__)
41 #define SACADO_FAD_DERIV_LOOP(I,SZ) for (int I=threadIdx.x; I<SZ; I+=blockDim.x)
42 #else
43 #define SACADO_FAD_DERIV_LOOP(I,SZ) for (int I=0; I<SZ; ++I)
44 #endif
45 #endif
46 
52  template <typename T, unsigned static_length, unsigned static_stride, typename U>
53  class ViewStorage {
54 
55  private:
56 
57  // Enumerated flag so logic is evaluated at compile-time
58  enum { stride_one = 1 == static_stride };
59 
60  public:
61 
62  typedef T value_type;
63 
65  template <typename S>
68  sz_(0), stride_(0), val_(0), dx_(0) {}
69 
72  ViewStorage(const int sz, const T & x, const DerivInit zero_out = InitDerivArray) :
73  sz_(0), stride_(0), val_(0), dx_(0) {}
74 
77  ViewStorage(T* v, const int arg_size = 0, const int arg_stride = 0) :
78  sz_(arg_size), stride_(arg_stride), val_(v+sz_.value*stride_.value), dx_(v) {}
79 
82  ViewStorage(T* arg_dx, T* arg_val, const int arg_size = 0,
83  const int arg_stride = 0) :
84  sz_(arg_size), stride_(arg_stride), val_(arg_val), dx_(arg_dx) {}
85 
89  sz_(x.sz_), stride_(x.stride_), val_(x.val_), dx_(x.dx_) {}
90 
94 
98  if (this != &x) {
99  *val_ = *x.val_;
100  if (stride_one)
101  //for (int i=0; i<sz_.value; ++i)
103  dx_[i] = x.dx_[i];
104  else
105  //for (int i=0; i<sz_.value; ++i)
107  dx_[i*stride_.value] = x.dx_[i*x.stride_.value];
108  }
109  return *this;
110  }
111 
114  int size() const { return sz_.value;}
115 
118  int length() const { return sz_.value; }
119 
121 
127  void resize(int sz) {
129  }
130 
132 
138  void resizeAndZero(int /* sz */) {}
139 
142  void expand(int sz) {}
143 
146  void zero() {
148  }
149 
152  const T& val() const { return *val_; }
153 
156  T& val() { return *val_; }
157 
160  const T* dx() const { return dx_;}
161 
164  T dx(int i) const {
165  return unsigned(sz_.value) > 0 ? dx_[ unsigned(stride_one) == 1 ? i : i * stride_.value ] : T(0.);
166  }
167 
170  T& fastAccessDx(int i) {
171  return dx_[ unsigned(stride_one) == 1 ? i : i * stride_.value ];
172  }
173 
176  const T& fastAccessDx(int i) const {
177  return dx_[ unsigned(stride_one) == 1 ? i : i * stride_.value ];
178  }
179 
180  protected:
181 
184 
187 
189  T *val_;
190 
192  T *dx_;
193 
194  }; // class ViewStorage
195 
196  } // namespace Fad
197 
198 } // namespace Sacado
199 
200 #endif // SACADO_NEW_FAD_DESIGN_IS_DEFAULT
201 
202 #endif // SACADO_FAD_VIEWSTORAGE_HPP
SACADO_INLINE_FUNCTION ViewStorage(T *arg_dx, T *arg_val, const int arg_size=0, const int arg_stride=0)
Constructor.
const mpl::integral_nonzero_constant< int, static_length > sz_
Derivative array size.
#define SACADO_ENABLE_VALUE_CTOR_DECL
#define SACADO_FAD_DERIV_LOOP(I, SZ)
SACADO_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
SACADO_INLINE_FUNCTION T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
SACADO_INLINE_FUNCTION int length() const
Returns array length.
SACADO_INLINE_FUNCTION ~ViewStorage()
Destructor.
SACADO_INLINE_FUNCTION const T & val() const
Returns value.
#define T
Definition: Sacado_rad.hpp:553
SACADO_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
SACADO_INLINE_FUNCTION ViewStorage & operator=(const ViewStorage &x)
Assignment.
SACADO_INLINE_FUNCTION ViewStorage(T *v, const int arg_size=0, const int arg_stride=0)
Constructor.
SACADO_INLINE_FUNCTION ViewStorage(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Default constructor (needed to satisfy interface)
SACADO_INLINE_FUNCTION int size() const
Returns number of derivative components.
SACADO_INLINE_FUNCTION void resizeAndZero(int)
Resize the derivative array to sz.
SACADO_INLINE_FUNCTION T & val()
Returns value.
const mpl::integral_nonzero_constant< int, static_stride > stride_
Derivative array stride.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
SACADO_INLINE_FUNCTION T dx(int i) const
Returns derivative component i with bounds checking.
int value
SACADO_INLINE_FUNCTION const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
Initialize the derivative array.
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 ViewStorage(const ViewStorage &x)
Copy constructor.
#define SACADO_INLINE_FUNCTION
SACADO_INLINE_FUNCTION ViewStorage(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz (needed to satisfy interface)
SACADO_INLINE_FUNCTION void zero()
Zero out derivative array.
SACADO_INLINE_FUNCTION const T * dx() const
Returns derivative array.
static SACADO_INLINE_FUNCTION void strided_zero(T *dest, int stride, int sz)
Zero out array dest of length sz.