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_ViewFad.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_VIEWFAD_HPP
31 #define SACADO_FAD_EXP_VIEWFAD_HPP
32 
35 
36 #if defined(HAVE_SACADO_KOKKOSCORE)
37 #include "Kokkos_Atomic.hpp"
38 #include "impl/Kokkos_Error.hpp"
39 #endif
40 
41 namespace Sacado {
42 
43  namespace Fad {
44  namespace Exp {
45 
46  template <typename T, unsigned static_length, unsigned static_stride, typename U>
48 
49  // Class representing a pointer to ViewFad so that &ViewFad is supported
50  template <typename T, unsigned sl, unsigned ss, typename U>
51  class ViewFadPtr : public ViewFad<T,sl,ss,U> {
52  public:
53 
54  // Storage type base class
56 
57  // Bring in constructors
58  using view_fad_type::view_fad_type;
59 
60  // Add overload of dereference operator
62  view_fad_type* operator->() { return this; }
63 
64  // Add overload of dereference operator
66  view_fad_type& operator*() { *this; }
67  };
68 
69 #if defined(HAVE_SACADO_KOKKOSCORE)
70  // Overload of Kokkos::atomic_add for ViewFad types.
71  template <typename ValT, unsigned sl, unsigned ss, typename U, typename T>
73  void atomic_add(ViewFadPtr<ValT,sl,ss,U> dst, const Expr<T>& xx) {
74  using Kokkos::atomic_add;
75 
76  const typename Expr<T>::derived_type& x = xx.derived();
77 
78  const int xsz = x.size();
79  const int sz = dst->size();
80 
81  // We currently cannot handle resizing since that would need to be
82  // done atomically.
83  if (xsz > sz)
84  Kokkos::abort(
85  "Sacado error: Fad resize within atomic_add() not supported!");
86 
87  if (xsz != sz && sz > 0 && xsz > 0)
88  Kokkos::abort(
89  "Sacado error: Fad assignment of incompatiable sizes!");
90 
91 
92  if (sz > 0 && xsz > 0) {
94  atomic_add(&(dst->fastAccessDx(i)), x.fastAccessDx(i));
95  }
97  atomic_add(&(dst->val()), x.val());
98  }
99 #endif
100 
101  } // namespace Exp
102  } // namespace Fad
103 
104  template <typename,unsigned,unsigned> struct ViewFadType;
105 
107  template< class S, unsigned length, unsigned stride >
108  struct ViewFadType< Fad::Exp::GeneralFad<S>, length, stride > {
110  };
111 
113 
116  template< class S, unsigned length, unsigned stride >
117  struct ViewFadType< const Fad::Exp::GeneralFad<S>, length, stride > {
119  };
120 
121  // Specialization of BaseExprType for ViewFad, to use the base fad type
122  template <typename T, unsigned static_length, unsigned static_stride, typename U>
123  struct BaseExprType< Fad::Exp::GeneralFad< Fad::Exp::ViewStorage<T,static_length,static_stride,U> > > {
124  typedef U type;
125  };
126 
128 
132  template <typename ValueT, unsigned Size, unsigned Stride, typename Base>
133  struct ScalarType< Fad::Exp::ViewFad<ValueT,Size,Stride,Base> > {
134  typedef typename ScalarType<ValueT>::type type;
135  };
136 
141  template <typename ValueT, unsigned Size, unsigned Stride, typename Base>
142  struct ValueType< Fad::Exp::ViewFad<ValueT,Size,Stride,Base> > {
143  typedef ValueT type;
144  };
145 
146 } // namespace Sacado
147 
148 #endif // SACADO_FAD_EXP_VIEWFAD_HPP
Wrapper for a generic expression template.
Base template specification for ScalarType.
#define SACADO_FAD_THREAD_SINGLE
KOKKOS_INLINE_FUNCTION view_fad_type * operator->()
expr val()
#define KOKKOS_INLINE_FUNCTION
Forward-mode AD class templated on the storage for the derivative array.
#define SACADO_FAD_DERIV_LOOP(I, SZ)
Get the base Fad type from a view/expression.
ViewFad< T, sl, ss, U > view_fad_type
Fad::Exp::ViewFad< const typename S::value_type, length, stride, Fad::Exp::GeneralFad< S > > type
expr expr expr fastAccessDx(i)) FAD_UNARYOP_MACRO(exp
Fad::Exp::ViewFad< typename S::value_type, length, stride, Fad::Exp::GeneralFad< S > > type
GeneralFad< ViewStorage< T, static_length, static_stride, U > > ViewFad
Base template specification for ValueType.
KOKKOS_INLINE_FUNCTION view_fad_type & operator*()
Get view type for any Fad type.