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 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef SACADO_MP_VECTOR_TRAITS_HPP
43 #define SACADO_MP_VECTOR_TRAITS_HPP
44 
45 #include "Sacado_Traits.hpp"
46 #include "Sacado_mpl_apply.hpp"
47 
48 //
49 // Currently only the type-style traits classes will work on the device
50 // since the function-style traits don't have the needed specializations
51 // for the device. This is in particular true for the scalar traits.
52 //
53 
54 // Forward declarations
55 namespace Sacado {
56  namespace MP {
57  template <typename S> class Vector;
58  }
59 }
60 
61 namespace Sacado {
62 
64  SACADO_AD_PROMOTE_SPEC( MP, Vector )
65 
66 
67  template <typename S>
68  struct ScalarType< MP::Vector<S> > {
70  };
71 
73  template <typename S>
74  struct ValueType< MP::Vector<S> > {
75  typedef typename MP::Vector<S>::value_type type;
76  };
77 
79  template <typename S>
80  struct IsADType< MP::Vector<S> > {
81  static const bool value = true;
82  };
83 
85  template <typename S>
86  struct IsScalarType< MP::Vector<S> > {
87  static const bool value = S::is_static;
88  };
89 
91  template <typename S>
92  struct Value< MP::Vector<S> > {
93  typedef typename ValueType< MP::Vector<S> >::type value_type;
94  KOKKOS_INLINE_FUNCTION
95  static const value_type& eval(const MP::Vector<S>& x) {
96  return x.val(); }
97  };
98 
100  template <typename S>
101  struct ScalarValue< MP::Vector<S> > {
102  typedef typename ValueType< MP::Vector<S> >::type value_type;
103  typedef typename ScalarType< MP::Vector<S> >::type scalar_type;
104  KOKKOS_INLINE_FUNCTION
105  static const scalar_type& eval(const MP::Vector<S>& x) {
106  return ScalarValue<value_type>::eval(x.val()); }
107  };
108 
110  template <typename S>
111  struct StringName< MP::Vector<S> > {
112  static std::string eval() {
113  return std::string("Sacado::MP::Vector< ") +
114  StringName<S>::eval() + " >"; }
115  };
116 
118  template <typename S>
119  struct IsEqual< MP::Vector<S> > {
120  KOKKOS_INLINE_FUNCTION
121  static bool eval(const MP::Vector<S>& x,
122  const MP::Vector<S>& y) {
123  return x.isEqualTo(y);
124  }
125  };
126 
128  template <typename S>
129  struct IsStaticallySized< MP::Vector<S> > {
130  static const bool value = S::is_static;
131  };
132 
134  template <typename S>
135  struct StaticSize< MP::Vector<S> > {
136  static const unsigned value = S::static_size;
137  };
138 
139 } // namespace Sacado
140 
141 // Define Teuchos traits classes
142 // Note: Stokhos has required dependency on all Teuchos sub-packages
143 #include "Stokhos_ConfigDefs.h"
145 #include "Teuchos_ScalarTraits.hpp"
148 #include "Teuchos_as.hpp"
149 
150 namespace Teuchos {
151 
153  template <typename S>
154  struct PromotionTraits< Sacado::MP::Vector<S>,
155  Sacado::MP::Vector<S> > {
156  typedef typename Sacado::Promote< Sacado::MP::Vector<S>,
157  Sacado::MP::Vector<S> >::type
159  };
160 
162  template <typename S, typename R>
163  struct PromotionTraits< Sacado::MP::Vector<S>, R > {
164  typedef typename Sacado::Promote< Sacado::MP::Vector<S>, R >::type
166  };
167 
169  template <typename L, typename S>
170  struct PromotionTraits< L, Sacado::MP::Vector<S> > {
171  public:
172  typedef typename Sacado::Promote< L, Sacado::MP::Vector<S> >::type
174  };
175 
177 #if defined(HAVE_STOKHOS_ENSEMBLE_REDUCT)
178  template <typename S>
179  struct ScalarTraits< Sacado::MP::Vector<S> > :
180  public Sacado::MP::ScalarTraitsImp<S,true> {};
181 #else
182  template <typename S>
183  struct ScalarTraits< Sacado::MP::Vector<S> > :
184  public Sacado::MP::ScalarTraitsImp<S,false> {};
185 #endif
186 
188  template <typename Ordinal, typename S>
189  struct SerializationTraits<Ordinal, Sacado::MP::Vector<S> > :
190  public Sacado::MP::SerializationTraitsImp< Ordinal,
191  Sacado::MP::Vector<S>,
192  S::is_static > {};
193 
195  template <typename Ordinal, typename S>
196  struct ValueTypeSerializer<Ordinal, Sacado::MP::Vector<S> > :
197  public Sacado::MP::SerializerImp< Ordinal,
198  Sacado::MP::Vector<S>,
199  ValueTypeSerializer<Ordinal,typename Sacado::MP::Vector<S>::value_type> >
200  {
202  typedef typename VecType::value_type value_type;
206  Ordinal sz = 0) :
207  Base(vs, sz) {}
208  };
209 
211 template<class TypeTo, class StorageFrom>
212 class ValueTypeConversionTraits< TypeTo, Sacado::MP::Vector<StorageFrom> > {
213 public:
216  static TypeTo convert (const TypeFrom& t) {
217  // This default implementation is just an implicit conversion and
218  // may generate compiler warnings on dangerous conversions.
219  return Teuchos::as<TypeTo>(t.coeff(0));
220  }
221 
223  static TypeTo safeConvert (const TypeFrom& t) {
224  // This default implementation is just an implicit conversion and
225  // may generate compiler warnings on dangerous conversions. No
226  // runtime checking (e.g., for overflow) can be done by default;
227  // only specializations can define meaningful and portable
228  // run-time checks of conversions.
229  return Teuchos::as<TypeTo>(t.coeff(0));
230  }
231 };
232 
233 template<class TypeTo, class ExprFrom>
234 class ValueTypeConversionTraits< TypeTo, Sacado::MP::Expr<ExprFrom> > {
235 public:
238  static TypeTo convert (const TypeFrom& t) {
239  // This default implementation is just an implicit conversion and
240  // may generate compiler warnings on dangerous conversions.
241  return Teuchos::as<TypeTo>(t.derived().coeff(0));
242  }
243 
245  static TypeTo safeConvert (const TypeFrom& t) {
246  // This default implementation is just an implicit conversion and
247  // may generate compiler warnings on dangerous conversions. No
248  // runtime checking (e.g., for overflow) can be done by default;
249  // only specializations can define meaningful and portable
250  // run-time checks of conversions.
251  return Teuchos::as<TypeTo>(t.derived().coeff(0));
252  }
253 };
254 
255 // Should also do TypeTo, and TypeTo,TypeFrom as MP::Vector, but the real way
256 // to fix is to make sure it is never called at all (requires fixing
257 // magnitudeType)
258 
259 }
260 
261 #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.