42 #ifndef SACADO_UQ_PCE_SCALARTRAITSIMP_HPP 43 #define SACADO_UQ_PCE_SCALARTRAITSIMP_HPP 45 #ifdef HAVE_SACADO_TEUCHOS 47 #include "Teuchos_ScalarTraits.hpp" 48 #include "Teuchos_SerializationTraits.hpp" 49 #include "Teuchos_RCP.hpp" 50 #include "Teuchos_Assert.hpp" 51 #include "Sacado_mpl_apply.hpp" 52 #include "Teuchos_as.hpp" 61 template <
typename PCEType>
62 struct PCEScalarTraitsImp {
66 typedef Teuchos::ScalarTraits<value_type> TVT;
68 typedef typename TVT::magnitudeType value_mag_type;
69 typedef typename TVT::halfPrecision value_half_type;
70 typedef typename TVT::doublePrecision value_double_type;
72 typedef typename Sacado::mpl::apply<storage_type,ordinal_type,value_mag_type>::type storage_mag_type;
73 typedef typename Sacado::mpl::apply<storage_type,ordinal_type,value_half_type>::type storage_half_type;
74 typedef typename Sacado::mpl::apply<storage_type,ordinal_type,value_double_type>::type storage_double_type;
76 typedef value_mag_type magnitudeType;
77 typedef typename Sacado::mpl::apply<PCEType, storage_half_type>::type halfPrecision;
78 typedef typename Sacado::mpl::apply<PCEType, storage_double_type>::type doublePrecision;
82 static const bool isComplex = TVT::isComplex;
83 static const bool isOrdinal = TVT::isOrdinal;
84 static const bool isComparable = TVT::isComparable;
85 static const bool hasMachineParameters = TVT::hasMachineParameters;
87 static value_mag_type eps() {
return TVT::eps(); }
89 static value_mag_type sfmin() {
return TVT::sfmin(); }
91 static value_mag_type base() {
return TVT::base(); }
93 static value_mag_type prec() {
return TVT::prec(); }
95 static value_mag_type t() {
return TVT::t(); }
99 static value_mag_type emin() {
return TVT::emin(); }
101 static value_mag_type rmin() {
return TVT::rmin(); }
103 static value_mag_type emax() {
return TVT::emax(); }
105 static value_mag_type rmax() {
return TVT::rmax(); }
107 static magnitudeType magnitude(
const PCEType& a) {
111 static innerProductType innerProduct(
const PCEType& a,
const PCEType& b) {
112 return a.inner_product(b);
122 y.val() = TVT::conjugate(
x.val());
126 static magnitudeType real(
const PCEType&
x) {
127 magnitudeType m = magnitudeType(0.0);
130 value_mag_type t = TVT::real(
x.fastAccessCoeff(i));
136 static magnitudeType imag(
const PCEType&
x) {
137 magnitudeType m = magnitudeType(0.0);
140 value_mag_type t = TVT::imag(
x.fastAccessCoeff(i));
147 static value_type nan() {
return TVT::nan(); }
149 static bool isnaninf(
const PCEType&
x) {
150 for (
int i=0; i<
x.size(); i++)
151 if (TVT::isnaninf(
x.fastAccessCoeff(i)))
156 static void seedrandom(
unsigned int s) { TVT::seedrandom(s); }
160 static const char * name() {
return "Sacado::UQ::PCE<>"; }
175 template <
typename TypeTo,
typename PCEType>
176 struct PCEValueTypeConversionTraitsImp {
177 typedef typename Sacado::ValueType<PCEType>::type ValueT;
178 typedef Teuchos::ValueTypeConversionTraits<TypeTo,ValueT> VTCT;
179 static TypeTo convert(
const PCEType t ) {
180 return VTCT::convert(t.val());
182 static TypeTo safeConvert(
const PCEType t ) {
183 return VTCT::safeConvert(t.val());
188 template <
typename Ordinal,
typename PCEType>
189 class PCESerializationTraitsImp {
190 typedef typename Sacado::ValueType<PCEType>::type ValueT;
191 typedef Teuchos::SerializationTraits<Ordinal,ValueT> vSerT;
192 typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
193 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
198 static const bool supportsDirectSerialization =
false;
207 for (
Ordinal i=0; i<count; i++) {
208 int sz = buffer[i].size();
209 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
210 Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
211 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
218 static void serialize (
const Ordinal count,
222 for (
Ordinal i=0; i<count; i++) {
224 int sz = buffer[i].size();
225 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
226 iSerT::serialize(1, &sz, b1, charBuffer);
230 Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
231 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
232 oSerT::serialize(1, &b2, b3, charBuffer);
234 vSerT::serialize(sz, buffer[i].coeff(), b2, charBuffer);
241 const char charBuffer[]) {
244 while (bytes_used < bytes) {
247 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
252 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
253 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
265 static void deserialize (
const Ordinal bytes,
266 const char charBuffer[],
269 for (
Ordinal i=0; i<count; i++) {
272 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
273 const int *sz = iSerT::convertFromCharPtr(charBuffer);
279 if (buffer[i].size() != *sz)
280 buffer[i].reset(buffer[i].
cijk(), *sz);
281 buffer[i].copyForWrite();
284 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
285 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
287 vSerT::deserialize(*b2, charBuffer, *sz, buffer[i].coeff());
299 template <
typename Ordinal,
typename PCEType,
typename ValueSerializer>
300 class PCESerializerImp {
305 typedef ValueSerializer value_serializer_type;
308 typedef typename PCEType::cijk_type cijk_type;
312 typedef typename Sacado::ValueType<PCEType>::type ValueT;
313 typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
314 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
317 Teuchos::RCP<const ValueSerializer> vs;
323 static const bool supportsDirectSerialization =
false;
325 PCESerializerImp(
const cijk_type& cijk_,
326 const Teuchos::RCP<const ValueSerializer>& vs_) :
327 cijk(cijk_), vs(vs_), sz(
cijk.dimension()) {}
330 cijk_type getSerializerCijk()
const {
return cijk; }
333 Teuchos::RCP<const value_serializer_type> getValueSerializer()
const {
341 const PCEType buffer[])
const {
345 for (
Ordinal i=0; i<count; i++) {
346 int my_sz = buffer[i].size();
356 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
357 Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
358 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
367 void serialize (
const Ordinal count,
370 char charBuffer[])
const {
373 for (
Ordinal i=0; i<count; i++) {
375 int my_sz = buffer[i].size();
385 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
386 iSerT::serialize(1, &sz, b1, charBuffer);
390 Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
391 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
392 oSerT::serialize(1, &b2, b3, charBuffer);
394 vs->serialize(sz, cx->coeff(), b2, charBuffer);
403 const char charBuffer[])
const {
406 while (bytes_used < bytes) {
409 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
414 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
415 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
427 void deserialize (
const Ordinal bytes,
428 const char charBuffer[],
431 for (
Ordinal i=0; i<count; i++) {
434 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
435 const int *my_sz = iSerT::convertFromCharPtr(charBuffer);
439 buffer[i].reset(
cijk);
442 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
443 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
445 vs->deserialize(*b2, charBuffer, *my_sz, buffer[i].coeff());
459 #endif // HAVE_SACADO_TEUCHOS 461 #endif // SACADO_FAD_SCALARTRAITSIMP_HPP KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
Stokhos::StandardStorage< int, double > storage_type
KOKKOS_INLINE_FUNCTION PCE< Storage > pow(const PCE< Storage > &a, const PCE< Storage > &b)
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
Sacado::Random< double > rnd
KOKKOS_INLINE_FUNCTION constexpr std::enable_if< is_view_uq_pce< view_type >::value, typename CijkType< view_type >::type >::type cijk(const view_type &view)
Sacado::UQ::PCE< storage_type > PCEType
KOKKOS_INLINE_FUNCTION PCE< Storage > log(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > log10(const PCE< Storage > &a)
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType ValueType * y