Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_MP_VectorTraits.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef SACADO_MP_VECTOR_TRAITS_HPP
11 #define SACADO_MP_VECTOR_TRAITS_HPP
12 
13 #include "Sacado_Traits.hpp"
14 #include "Sacado_mpl_apply.hpp"
15 
16 //
17 // Currently only the type-style traits classes will work on the device
18 // since the function-style traits don't have the needed specializations
19 // for the device. This is in particular true for the scalar traits.
20 //
21 
22 // Forward declarations
23 namespace Sacado {
24  namespace MP {
25  template <typename S> class Vector;
26  }
27 }
28 
29 namespace Sacado {
30 
32  SACADO_AD_PROMOTE_SPEC( MP, Vector )
33 
34 
35  template <typename S>
36  struct ScalarType< MP::Vector<S> > {
38  };
39 
41  template <typename S>
42  struct ValueType< MP::Vector<S> > {
43  typedef typename MP::Vector<S>::value_type type;
44  };
45 
47  template <typename S>
48  struct IsADType< MP::Vector<S> > {
49  static const bool value = true;
50  };
51 
53  template <typename S>
54  struct IsScalarType< MP::Vector<S> > {
55  static const bool value = S::is_static;
56  };
57 
59  template <typename S>
60  struct Value< MP::Vector<S> > {
61  typedef typename ValueType< MP::Vector<S> >::type value_type;
62  KOKKOS_INLINE_FUNCTION
63  static const value_type& eval(const MP::Vector<S>& x) {
64  return x.val(); }
65  };
66 
68  template <typename S>
69  struct ScalarValue< MP::Vector<S> > {
70  typedef typename ValueType< MP::Vector<S> >::type value_type;
71  typedef typename ScalarType< MP::Vector<S> >::type scalar_type;
72  KOKKOS_INLINE_FUNCTION
73  static const scalar_type& eval(const MP::Vector<S>& x) {
74  return ScalarValue<value_type>::eval(x.val()); }
75  };
76 
78  template <typename S>
79  struct StringName< MP::Vector<S> > {
80  static std::string eval() {
81  return std::string("Sacado::MP::Vector< ") +
82  StringName<S>::eval() + " >"; }
83  };
84 
86  template <typename S>
87  struct IsEqual< MP::Vector<S> > {
88  KOKKOS_INLINE_FUNCTION
89  static bool eval(const MP::Vector<S>& x,
90  const MP::Vector<S>& y) {
91  return x.isEqualTo(y);
92  }
93  };
94 
96  template <typename S>
97  struct IsStaticallySized< MP::Vector<S> > {
98  static const bool value = S::is_static;
99  };
100 
102  template <typename S>
103  struct StaticSize< MP::Vector<S> > {
104  static const unsigned value = S::static_size;
105  };
106 
107 } // namespace Sacado
108 
109 // Define Teuchos traits classes
110 // Note: Stokhos has required dependency on all Teuchos sub-packages
111 #include "Stokhos_ConfigDefs.h"
113 #include "Teuchos_ScalarTraits.hpp"
116 #include "Teuchos_as.hpp"
117 
118 namespace Teuchos {
119 
121  template <typename S>
122  struct PromotionTraits< Sacado::MP::Vector<S>,
123  Sacado::MP::Vector<S> > {
124  typedef typename Sacado::Promote< Sacado::MP::Vector<S>,
125  Sacado::MP::Vector<S> >::type
127  };
128 
130  template <typename S, typename R>
131  struct PromotionTraits< Sacado::MP::Vector<S>, R > {
132  typedef typename Sacado::Promote< Sacado::MP::Vector<S>, R >::type
134  };
135 
137  template <typename L, typename S>
138  struct PromotionTraits< L, Sacado::MP::Vector<S> > {
139  public:
140  typedef typename Sacado::Promote< L, Sacado::MP::Vector<S> >::type
142  };
143 
145 #if defined(HAVE_STOKHOS_ENSEMBLE_REDUCT)
146  template <typename S>
147  struct ScalarTraits< Sacado::MP::Vector<S> > :
148  public Sacado::MP::ScalarTraitsImp<S,true> {};
149 #else
150  template <typename S>
151  struct ScalarTraits< Sacado::MP::Vector<S> > :
152  public Sacado::MP::ScalarTraitsImp<S,false> {};
153 #endif
154 
156  template <typename Ordinal, typename S>
157  struct SerializationTraits<Ordinal, Sacado::MP::Vector<S> > :
158  public Sacado::MP::SerializationTraitsImp< Ordinal,
159  Sacado::MP::Vector<S>,
160  S::is_static > {};
161 
163  template <typename Ordinal, typename S>
164  struct ValueTypeSerializer<Ordinal, Sacado::MP::Vector<S> > :
165  public Sacado::MP::SerializerImp< Ordinal,
166  Sacado::MP::Vector<S>,
167  ValueTypeSerializer<Ordinal,typename Sacado::MP::Vector<S>::value_type> >
168  {
170  typedef typename VecType::value_type value_type;
174  Ordinal sz = 0) :
175  Base(vs, sz) {}
176  };
177 
179 template<class TypeTo, class StorageFrom>
180 class ValueTypeConversionTraits< TypeTo, Sacado::MP::Vector<StorageFrom> > {
181 public:
184  static TypeTo convert (const TypeFrom& t) {
185  // This default implementation is just an implicit conversion and
186  // may generate compiler warnings on dangerous conversions.
187  return Teuchos::as<TypeTo>(t.coeff(0));
188  }
189 
191  static TypeTo safeConvert (const TypeFrom& t) {
192  // This default implementation is just an implicit conversion and
193  // may generate compiler warnings on dangerous conversions. No
194  // runtime checking (e.g., for overflow) can be done by default;
195  // only specializations can define meaningful and portable
196  // run-time checks of conversions.
197  return Teuchos::as<TypeTo>(t.coeff(0));
198  }
199 };
200 
201 template<class TypeTo, class ExprFrom>
202 class ValueTypeConversionTraits< TypeTo, Sacado::MP::Expr<ExprFrom> > {
203 public:
206  static TypeTo convert (const TypeFrom& t) {
207  // This default implementation is just an implicit conversion and
208  // may generate compiler warnings on dangerous conversions.
209  return Teuchos::as<TypeTo>(t.derived().coeff(0));
210  }
211 
213  static TypeTo safeConvert (const TypeFrom& t) {
214  // This default implementation is just an implicit conversion and
215  // may generate compiler warnings on dangerous conversions. No
216  // runtime checking (e.g., for overflow) can be done by default;
217  // only specializations can define meaningful and portable
218  // run-time checks of conversions.
219  return Teuchos::as<TypeTo>(t.derived().coeff(0));
220  }
221 };
222 
223 // Should also do TypeTo, and TypeTo,TypeFrom as MP::Vector, but the real way
224 // to fix is to make sure it is never called at all (requires fixing
225 // magnitudeType)
226 
227 }
228 
229 #endif // SACADO_MP_VECTORTRAITS_HPP
static KOKKOS_INLINE_FUNCTION const scalar_type & eval(const MP::Vector< S > &x)
static TypeTo convert(const TypeFrom &t)
Convert t from a TypeFrom object to a TypeTo object.
static KOKKOS_INLINE_FUNCTION const value_type & eval(const MP::Vector< S > &x)
ValueType< MP::Vector< S > >::type value_type
ScalarType< typename MP::Vector< S >::value_type >::type type
static TypeTo safeConvert(const TypeFrom &t)
Convert t from a TypeFrom object to a TypeTo object, with checks for validity.
ScalarType< MP::Vector< S > >::type scalar_type
static TypeTo convert(const TypeFrom &t)
Convert t from a TypeFrom object to a TypeTo object.
Sacado::Promote< L, Sacado::MP::Vector< S > >::type promote
ValueTypeSerializer(const Teuchos::RCP< const ValueSerializer > &vs, Ordinal sz=0)
Implementation of Teuchos::SerializationTraits for all Vector types.
Sacado::Promote< Sacado::MP::Vector< S >, Sacado::MP::Vector< S > >::type promote
static KOKKOS_INLINE_FUNCTION bool eval(const MP::Vector< S > &x, const MP::Vector< S > &y)
An indirect serialization object for all Vector types.
ValueType< MP::Vector< S > >::type value_type
Sacado::MP::SerializerImp< Ordinal, VecType, ValueSerializer > Base
Sacado::Promote< Sacado::MP::Vector< S >, R >::type promote
static TypeTo safeConvert(const TypeFrom &t)
Convert t from a TypeFrom object to a TypeTo object, with checks for validity.