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 //
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 #include "Sacado_mpl_apply.hpp"
31 #include "Sacado_mpl_if.hpp"
32 #include "Sacado_mpl_type_wrap.hpp"
33 
34 #if defined(HAVE_SACADO_KOKKOSCORE)
35 #include "Kokkos_Atomic.hpp"
36 #include "impl/Kokkos_Error.hpp"
37 #endif
38 
39 namespace Sacado {
40 
41  namespace FAD_NS {
42 
43  // Class representing a pointer to ViewFad so that &ViewFad is supported
44  template <typename T, unsigned sl, unsigned ss, typename U>
45  class ViewFadPtr;
46 
57  template <typename ValueT, unsigned length, unsigned stride,
58  typename BaseFadT>
59  class ViewFad :
60  public Expr< GeneralFad<ValueT,Fad::ViewStorage<ValueT,length,stride,BaseFadT> > > {
61 
62  public:
63 
66  typedef GeneralFad<ValueT,StorageType> GeneralFadType;
67  typedef Expr<GeneralFadType> ExprType;
68 
70  typedef typename ExprType::value_type value_type;
71 
73  typedef typename ExprType::scalar_type scalar_type;
74 
77 
79  typedef BaseFadT base_fad_type;
80 
82  template <typename T>
83  struct apply {
84  // BaseExprType<T>::type is T if T is not a view
88  };
89 
94 
95  // ViewFad cannot be created with the usual constructors, so we remove
96  // them here.
97 
100  ViewFad(const StorageType& s) :
101  ExprType(s) {}
102 
105  ViewFad(ValueT* v, const int arg_size = 0, const int arg_stride = 0) :
106  ExprType( StorageType(v,arg_size,arg_stride) ) {}
107 
110  ViewFad(ValueT* dx_ptr, ValueT* val_ptr, const int arg_size = 0,
111  const int arg_stride = 0) :
112  ExprType( StorageType(dx_ptr,val_ptr,arg_size,arg_stride) ) {}
113 
115 
118  ~ViewFad() {}
119 
121  template <typename S>
123  SACADO_ENABLE_VALUE_FUNC(ViewFad&) operator=(const S& v) {
124  GeneralFadType::operator=(v);
125  return *this;
126  }
127 
130  ViewFad& operator=(const ViewFad& x) {
131  GeneralFadType::operator=(static_cast<const GeneralFadType&>(x));
132  return *this;
133  }
134 
136  template <typename S>
138  SACADO_ENABLE_EXPR_FUNC(ViewFad&) operator=(const Expr<S>& x)
139  {
140  GeneralFadType::operator=(x);
141  return *this;
142  }
143 
145 
150 
152  template <typename S>
154  SACADO_ENABLE_VALUE_FUNC(ViewFad&) operator += (const S& x) {
155  GeneralFadType::operator+=(x);
156  return *this;
157  }
158 
160  template <typename S>
162  SACADO_ENABLE_VALUE_FUNC(ViewFad&) operator -= (const S& x) {
163  GeneralFadType::operator-=(x);
164  return *this;
165  }
166 
168  template <typename S>
170  SACADO_ENABLE_VALUE_FUNC(ViewFad&) operator *= (const S& x) {
171  GeneralFadType::operator*=(x);
172  return *this;
173  }
174 
176  template <typename S>
178  SACADO_ENABLE_VALUE_FUNC(ViewFad&) operator /= (const S& x) {
179  GeneralFadType::operator/=(x);
180  return *this;
181  }
182 
185  ViewFad& operator += (const ViewFad& x) {
186  GeneralFadType::operator+=(static_cast<const GeneralFadType&>(x));
187  return *this;
188  }
189 
192  ViewFad& operator -= (const ViewFad& x) {
193  GeneralFadType::operator-=(static_cast<const GeneralFadType&>(x));
194  return *this;
195  }
196 
199  ViewFad& operator *= (const ViewFad& x) {
200  GeneralFadType::operator*=(static_cast<const GeneralFadType&>(x));
201  return *this;
202  }
203 
206  ViewFad& operator /= (const ViewFad& x) {
207  GeneralFadType::operator/=(static_cast<const GeneralFadType&>(x));
208  return *this;
209  }
210 
212  template <typename S>
214  SACADO_ENABLE_EXPR_FUNC(ViewFad&) operator += (const Expr<S>& x) {
215  GeneralFadType::operator+=(x);
216  return *this;
217  }
218 
220  template <typename S>
222  SACADO_ENABLE_EXPR_FUNC(ViewFad&) operator -= (const Expr<S>& x) {
223  GeneralFadType::operator-=(x);
224  return *this;
225  }
226 
228  template <typename S>
230  SACADO_ENABLE_EXPR_FUNC(ViewFad&) operator *= (const Expr<S>& x) {
231  GeneralFadType::operator*=(x);
232  return *this;
233  }
234 
236  template <typename S>
238  SACADO_ENABLE_EXPR_FUNC(ViewFad&) operator /= (const Expr<S>& x) {
239  GeneralFadType::operator/=(x);
240  return *this;
241  }
242 
243  // Overload of addressof operator
245  ViewFadPtr<ValueT,length,stride,BaseFadT> operator&() const {
246  return ViewFadPtr<ValueT,length,stride,BaseFadT>(
247  this->dx_, this->val_, this->sz_.value, this->stride_.value);
248  }
249 
251 
252  }; // class ViewFad<ValueT>
253 
254  // Class representing a pointer to ViewFad so that &ViewFad is supported
255  template <typename T, unsigned sl, unsigned ss, typename U>
256  class ViewFadPtr : public ViewFad<T,sl,ss,U> {
257  public:
258 
259  // Storage type base class
261 
262  // Bring in constructors
263  using view_fad_type::view_fad_type;
264 
265  // Add overload of dereference operator
267  view_fad_type* operator->() { return this; }
268 
269  // Add overload of dereference operator
271  view_fad_type& operator*() { *this; }
272  };
273 
274 #if defined(HAVE_SACADO_KOKKOSCORE)
275  // Overload of Kokkos::atomic_add for ViewFad types.
276  template <typename ValT, unsigned sl, unsigned ss, typename U, typename T>
278  void atomic_add(ViewFadPtr<ValT,sl,ss,U> dst, const Expr<T>& x) {
279  using Kokkos::atomic_add;
280 
281  x.cache();
282 
283  const int xsz = x.size();
284  const int sz = dst->size();
285 
286  // We currently cannot handle resizing since that would need to be
287  // done atomically.
288  if (xsz > sz)
289  Kokkos::abort(
290  "Sacado error: Fad resize within atomic_add() not supported!");
291 
292  if (xsz != sz && sz > 0 && xsz > 0)
293  Kokkos::abort(
294  "Sacado error: Fad assignment of incompatiable sizes!");
295 
296 
297  if (sz > 0 && xsz > 0) {
299  atomic_add(&(dst->fastAccessDx(i)), x.fastAccessDx(i));
300  }
302  atomic_add(&(dst->val()), x.val());
303  }
304 #endif
305 
306  template <typename T, unsigned l, unsigned s, typename U>
307  struct BaseExpr< GeneralFad<T,Fad::ViewStorage<T,l,s,U> > > {
308  //typedef ViewFad<T,l,s,U> type;
309  typedef U type;
310  };
311 
312  template <typename T, unsigned l, unsigned s, typename U>
313  struct ExprLevel< ViewFad<T,l,s,U> > {
314  static const unsigned value =
315  ExprLevel< typename ViewFad<T,l,s,U>::value_type >::value + 1;
316  };
317 
318  template <typename T, unsigned l, unsigned s, typename U>
319  struct IsFadExpr< ViewFad<T,l,s,U> > {
320  static const bool value = true;
321  };
322 
323  } // namespace Fad
324 
325  template <typename T, unsigned l, unsigned s, typename U>
326  struct IsView< Sacado::FAD_NS::ViewFad<T,l,s,U> > {
327  static const bool value = true;
328  };
329 
330  template <typename T, unsigned l, unsigned s, typename U>
331  struct IsFad< FAD_NS::ViewFad<T,l,s,U> > {
332  static const bool value = true;
333  };
334 
335  template <typename T, unsigned l, unsigned s, typename U>
336  struct IsExpr< FAD_NS::ViewFad<T,l,s,U> > {
337  static const bool value = true;
338  };
339 
340  template <typename T, unsigned l, unsigned s, typename U>
341  struct BaseExprType< FAD_NS::ViewFad<T,l,s,U> > {
343  };
344 
345 } // namespace Sacado
Expr< GeneralFadType > ExprType
KOKKOS_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)
KOKKOS_INLINE_FUNCTION ~ViewFad()
Destructor.
Forward-mode AD class using dynamic memory allocation and expression templates.
KOKKOS_INLINE_FUNCTION ViewFad(const StorageType &s)
Constructor with supplied storage s.
KOKKOS_INLINE_FUNCTION view_fad_type & operator*()
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 KOKKOS_INLINE_FUNCTION
#define T
Definition: Sacado_rad.hpp:573
#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.
mpl::apply< base_fad_type, T_for_base >::type new_base_fad_type
KOKKOS_INLINE_FUNCTION ViewFad(ValueT *v, const int arg_size=0, const int arg_stride=0)
View-specific constructor.
#define SACADO_ENABLE_EXPR_FUNC(RETURN_TYPE)
FAD_NS::ViewFad< T, l, s, U >::base_expr_type type
KOKKOS_INLINE_FUNCTION ViewFad(ValueT *dx_ptr, ValueT *val_ptr, const int arg_size=0, const int arg_stride=0)
View-specific constructor.
ViewFad< T, sl, ss, U > view_fad_type
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.
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
GeneralFad< ValueT, StorageType > GeneralFadType
#define FAD_NS