| 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 |
|
| 31 |
//! Specialization of %Promote to Vector types |
| 32 |
SACADO_AD_PROMOTE_SPEC( MP, Vector ) |
| 33 |
|
| 34 |
//! Specialization of %ScalarType to Vector types |
| 35 |
template <typename S> |
| 36 |
struct ScalarType< MP::Vector<S> > { |
| 37 |
typedef typename ScalarType<typename MP::Vector<S>::value_type>::type type; |
| 38 |
}; |
| 39 |
|
| 40 |
//! Specialization of %ValueType to Vector types |
| 41 |
template <typename S> |
| 42 |
struct ValueType< MP::Vector<S> > { |
| 43 |
typedef typename MP::Vector<S>::value_type type; |
| 44 |
}; |
| 45 |
|
| 46 |
//! Specialization of %IsADType to Vector types |
| 47 |
template <typename S> |
| 48 |
struct IsADType< MP::Vector<S> > { |
| 49 |
static const bool value = true; |
| 50 |
}; |
| 51 |
|
| 52 |
//! Specialization of %IsADType to Vector types |
| 53 |
template <typename S> |
| 54 |
struct IsScalarType< MP::Vector<S> > { |
| 55 |
static const bool value = S::is_static; |
| 56 |
}; |
| 57 |
|
| 58 |
//! Specialization of %Value to Vector types |
| 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 |
|
| 67 |
//! Specialization of %ScalarValue to Vector types |
| 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 |
|
| 77 |
//! Specialization of %StringName to Vector types |
| 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 |
|
| 85 |
//! Specialization of %IsEqual to Vector types |
| 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 |
|
| 95 |
//! Specialization of %IsStaticallySized to Vector types |
| 96 |
template <typename S> |
| 97 |
struct IsStaticallySized< MP::Vector<S> > { |
| 98 |
static const bool value = S::is_static; |
| 99 |
}; |
| 100 |
|
| 101 |
//! Specialization of %StaticSize to Vector types |
| 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" |
| 112 |
#include "Teuchos_PromotionTraits.hpp" |
| 113 |
#include "Teuchos_ScalarTraits.hpp" |
| 114 |
#include "Sacado_MP_ScalarTraitsImp.hpp" |
| 115 |
#include "Teuchos_SerializationTraits.hpp" |
| 116 |
#include "Teuchos_as.hpp" |
| 117 |
|
| 118 |
namespace Teuchos { |
| 119 |
|
| 120 |
//! Specialization of %Teuchos::PromotionTraits to Vector types |
| 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 |
| 126 |
promote; |
| 127 |
}; |
| 128 |
|
| 129 |
//! Specialization of %Teuchos::PromotionTraits to Vector types |
| 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 |
| 133 |
promote; |
| 134 |
}; |
| 135 |
|
| 136 |
//! Specialization of %Teuchos::PromotionTraits to Vector types |
| 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 |
| 141 |
promote; |
| 142 |
}; |
| 143 |
|
| 144 |
//! Specializtion of Teuchos::ScalarTraits |
| 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 |
|
| 155 |
//! Specialization of %Teuchos::SerializationTraits |
| 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 |
|
| 162 |
//! Specialization of %Teuchos::ValueTypeSerializer |
| 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 |
{ |
| 169 |
typedef Sacado::MP::Vector<S> VecType; |
| 170 |
typedef typename VecType::value_type value_type; |
| 171 |
typedef ValueTypeSerializer<Ordinal,value_type> ValueSerializer; |
| 172 |
typedef Sacado::MP::SerializerImp< Ordinal,VecType,ValueSerializer> Base; |
| 173 |
ValueTypeSerializer(const Teuchos::RCP<const ValueSerializer>& vs, |
| 174 |
Ordinal sz = 0) : |
| 175 |
Base(vs, sz) {} |
| 176 |
}; |
| 177 |
|
| 178 |
//! Specializations for Teuchos::as<T> |
| 179 |
template<class TypeTo, class StorageFrom> |
| 180 |
class ValueTypeConversionTraits< TypeTo, Sacado::MP::Vector<StorageFrom> > { |
| 181 |
public: |
| 182 |
typedef Sacado::MP::Vector<StorageFrom> TypeFrom; |
| 183 |
//! Convert t from a TypeFrom object to a TypeTo object. |
| 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 |
|
| 190 |
//! Convert t from a TypeFrom object to a TypeTo object, with checks for validity. |
| 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: |
| 204 |
typedef Sacado::MP::Expr<ExprFrom> TypeFrom; |
| 205 |
//! Convert t from a TypeFrom object to a TypeTo object. |
| 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 |
|
| 212 |
//! Convert t from a TypeFrom object to a TypeTo object, with checks for validity. |
| 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 |