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_ViewFad_tmpl.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 #include "Sacado_mpl_apply.hpp"
11 #include "Sacado_mpl_if.hpp"
12 #include "Sacado_mpl_type_wrap.hpp"
13 
14 #if defined(HAVE_SACADO_KOKKOS)
15 #include "Kokkos_Atomic.hpp"
16 #include "impl/Kokkos_Error.hpp"
17 #endif
18 
19 namespace Sacado {
20 
21  namespace FAD_NS {
22 
23  // Class representing a pointer to ViewFad so that &ViewFad is supported
24  template <typename T, unsigned sl, unsigned ss, typename U>
25  class ViewFadPtr;
26 
37  template <typename ValueT, unsigned length, unsigned stride,
38  typename BaseFadT>
39  class ViewFad :
40  public Expr< GeneralFad<ValueT,Fad::ViewStorage<ValueT,length,stride,BaseFadT> > > {
41 
42  public:
43 
46  typedef GeneralFad<ValueT,StorageType> GeneralFadType;
47  typedef Expr<GeneralFadType> ExprType;
48 
50  typedef typename ExprType::value_type value_type;
51 
53  typedef typename ExprType::scalar_type scalar_type;
54 
57 
59  typedef BaseFadT base_fad_type;
60 
62  template <typename T>
63  struct apply {
64  // BaseExprType<T>::type is T if T is not a view
68  };
69 
74 
75  // ViewFad cannot be created with the usual constructors, so we remove
76  // them here.
77 
80  ViewFad(const StorageType& s) :
81  ExprType(s) {}
82 
85  ViewFad(ValueT* v, const int arg_size = 0, const int arg_stride = 0) :
86  ExprType( StorageType(v,arg_size,arg_stride) ) {}
87 
90  ViewFad(ValueT* dx_ptr, ValueT* val_ptr, const int arg_size = 0,
91  const int arg_stride = 0) :
92  ExprType( StorageType(dx_ptr,val_ptr,arg_size,arg_stride) ) {}
93 
95 
98  ~ViewFad() {}
99 
101  template <typename S>
103  SACADO_ENABLE_VALUE_FUNC(ViewFad&) operator=(const S& v) {
104  GeneralFadType::operator=(v);
105  return *this;
106  }
107 
110  ViewFad& operator=(const ViewFad& x) {
111  GeneralFadType::operator=(static_cast<const GeneralFadType&>(x));
112  return *this;
113  }
114 
116  template <typename S>
118  SACADO_ENABLE_EXPR_FUNC(ViewFad&) operator=(const Expr<S>& x)
119  {
120  GeneralFadType::operator=(x);
121  return *this;
122  }
123 
125 
130 
132  template <typename S>
134  SACADO_ENABLE_VALUE_FUNC(ViewFad&) operator += (const S& x) {
135  GeneralFadType::operator+=(x);
136  return *this;
137  }
138 
140  template <typename S>
142  SACADO_ENABLE_VALUE_FUNC(ViewFad&) operator -= (const S& x) {
143  GeneralFadType::operator-=(x);
144  return *this;
145  }
146 
148  template <typename S>
150  SACADO_ENABLE_VALUE_FUNC(ViewFad&) operator *= (const S& x) {
151  GeneralFadType::operator*=(x);
152  return *this;
153  }
154 
156  template <typename S>
158  SACADO_ENABLE_VALUE_FUNC(ViewFad&) operator /= (const S& x) {
159  GeneralFadType::operator/=(x);
160  return *this;
161  }
162 
165  ViewFad& operator += (const ViewFad& x) {
166  GeneralFadType::operator+=(static_cast<const GeneralFadType&>(x));
167  return *this;
168  }
169 
172  ViewFad& operator -= (const ViewFad& x) {
173  GeneralFadType::operator-=(static_cast<const GeneralFadType&>(x));
174  return *this;
175  }
176 
179  ViewFad& operator *= (const ViewFad& x) {
180  GeneralFadType::operator*=(static_cast<const GeneralFadType&>(x));
181  return *this;
182  }
183 
186  ViewFad& operator /= (const ViewFad& x) {
187  GeneralFadType::operator/=(static_cast<const GeneralFadType&>(x));
188  return *this;
189  }
190 
192  template <typename S>
194  SACADO_ENABLE_EXPR_FUNC(ViewFad&) operator += (const Expr<S>& x) {
195  GeneralFadType::operator+=(x);
196  return *this;
197  }
198 
200  template <typename S>
202  SACADO_ENABLE_EXPR_FUNC(ViewFad&) operator -= (const Expr<S>& x) {
203  GeneralFadType::operator-=(x);
204  return *this;
205  }
206 
208  template <typename S>
210  SACADO_ENABLE_EXPR_FUNC(ViewFad&) operator *= (const Expr<S>& x) {
211  GeneralFadType::operator*=(x);
212  return *this;
213  }
214 
216  template <typename S>
218  SACADO_ENABLE_EXPR_FUNC(ViewFad&) operator /= (const Expr<S>& x) {
219  GeneralFadType::operator/=(x);
220  return *this;
221  }
222 
223  // Overload of addressof operator
225  ViewFadPtr<ValueT,length,stride,BaseFadT> operator&() const {
226  return ViewFadPtr<ValueT,length,stride,BaseFadT>(
227  this->dx_, this->val_, this->sz_.value, this->stride_.value);
228  }
229 
231 
232  }; // class ViewFad<ValueT>
233 
234  // Class representing a pointer to ViewFad so that &ViewFad is supported
235  template <typename T, unsigned sl, unsigned ss, typename U>
236  class ViewFadPtr : public ViewFad<T,sl,ss,U> {
237  public:
238 
239  // Storage type base class
241 
242  // Bring in constructors
243  using view_fad_type::view_fad_type;
244 
245  // Add overload of dereference operator
247  view_fad_type* operator->() { return this; }
248 
249  // Add overload of dereference operator
251  view_fad_type& operator*() { *this; }
252  };
253 
254 #if defined(HAVE_SACADO_KOKKOS)
255  // Overload of Kokkos::atomic_add for ViewFad types.
256  template <typename ValT, unsigned sl, unsigned ss, typename U, typename T>
258  void atomic_add(ViewFadPtr<ValT,sl,ss,U> dst, const Expr<T>& x) {
259  using Kokkos::atomic_add;
260 
261  x.cache();
262 
263  const int xsz = x.size();
264  const int sz = dst->size();
265 
266  // We currently cannot handle resizing since that would need to be
267  // done atomically.
268  if (xsz > sz)
269  Kokkos::abort(
270  "Sacado error: Fad resize within atomic_add() not supported!");
271 
272  if (xsz != sz && sz > 0 && xsz > 0)
273  Kokkos::abort(
274  "Sacado error: Fad assignment of incompatiable sizes!");
275 
276 
277  if (sz > 0 && xsz > 0) {
279  atomic_add(&(dst->fastAccessDx(i)), x.fastAccessDx(i));
280  }
282  atomic_add(&(dst->val()), x.val());
283  }
284 #endif
285 
286  template <typename T, unsigned l, unsigned s, typename U>
287  struct BaseExpr< GeneralFad<T,Fad::ViewStorage<T,l,s,U> > > {
288  //typedef ViewFad<T,l,s,U> type;
289  typedef U type;
290  };
291 
292  template <typename T, unsigned l, unsigned s, typename U>
293  struct ExprLevel< ViewFad<T,l,s,U> > {
294  static const unsigned value =
295  ExprLevel< typename ViewFad<T,l,s,U>::value_type >::value + 1;
296  };
297 
298  template <typename T, unsigned l, unsigned s, typename U>
299  struct IsFadExpr< ViewFad<T,l,s,U> > {
300  static const bool value = true;
301  };
302 
303  } // namespace Fad
304 
305  template <typename T, unsigned l, unsigned s, typename U>
306  struct IsView< Sacado::FAD_NS::ViewFad<T,l,s,U> > {
307  static const bool value = true;
308  };
309 
310  template <typename T, unsigned l, unsigned s, typename U>
311  struct IsFad< FAD_NS::ViewFad<T,l,s,U> > {
312  static const bool value = true;
313  };
314 
315  template <typename T, unsigned l, unsigned s, typename U>
316  struct IsExpr< FAD_NS::ViewFad<T,l,s,U> > {
317  static const bool value = true;
318  };
319 
320  template <typename T, unsigned l, unsigned s, typename U>
321  struct BaseExprType< FAD_NS::ViewFad<T,l,s,U> > {
323  };
324 
325 } // namespace Sacado
Expr< GeneralFadType > ExprType
SACADO_INLINE_FUNCTION view_fad_type * operator->()
Fad::ViewStorage< ValueT, length, stride, BaseFadT > StorageType
Base classes.
#define SACADO_FAD_THREAD_SINGLE
Base template specification for whether a type is a Fad type.
ScalarType< ValueT >::type ScalarT
Typename of scalar&#39;s (which may be different from ValueT)
Forward-mode AD class using dynamic memory allocation and expression templates.
ViewFad< T, length, stride, new_base_fad_type > type
Is a type an expression.
F::template apply< A1, A2, A3, A4, A5 >::type type
expr val()
#define T
Definition: Sacado_rad.hpp:553
#define SACADO_ENABLE_VALUE_FUNC(RETURN_TYPE)
Determine whether a given type is a view.
ExprType::scalar_type scalar_type
Typename of scalar&#39;s (which may be different from value_type)
#define SACADO_FAD_DERIV_LOOP(I, SZ)
Get the base Fad type from a view/expression.
SACADO_INLINE_FUNCTION ViewFad(ValueT *v, const int arg_size=0, const int arg_stride=0)
View-specific constructor.
mpl::apply< base_fad_type, T_for_base >::type new_base_fad_type
SACADO_INLINE_FUNCTION view_fad_type & operator*()
SACADO_INLINE_FUNCTION ViewFad(const StorageType &s)
Constructor with supplied storage s.
#define SACADO_ENABLE_EXPR_FUNC(RETURN_TYPE)
FAD_NS::ViewFad< T, l, s, U >::base_expr_type type
ViewFad< T, sl, ss, U > view_fad_type
int value
expr expr expr fastAccessDx(i)) FAD_UNARYOP_MACRO(exp
BaseFadT base_fad_type
Fad type view is based on.
Turn ViewFad into a meta-function class usable with mpl::apply.
SACADO_INLINE_FUNCTION ViewFad(ValueT *dx_ptr, ValueT *val_ptr, const int arg_size=0, const int arg_stride=0)
View-specific constructor.
SACADO_INLINE_FUNCTION ~ViewFad()
Destructor.
ExprType::value_type value_type
Typename of values.
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.
GeneralFad< ViewStorage< T, static_length, static_stride, U > > ViewFad
#define SACADO_INLINE_FUNCTION
GeneralFad< ValueT, StorageType > GeneralFadType
#define FAD_NS