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 {
67 typedef typename Teuchos::ScalarTraits<value_type>::magnitudeType value_mag_type;
68 typedef typename Teuchos::ScalarTraits<value_type>::halfPrecision value_half_type;
69 typedef typename Teuchos::ScalarTraits<value_type>::doublePrecision value_double_type;
71 typedef typename Sacado::mpl::apply<storage_type,ordinal_type,value_mag_type>::type storage_mag_type;
72 typedef typename Sacado::mpl::apply<storage_type,ordinal_type,value_half_type>::type storage_half_type;
73 typedef typename Sacado::mpl::apply<storage_type,ordinal_type,value_double_type>::type storage_double_type;
75 typedef typename Sacado::mpl::apply<PCEType, storage_mag_type>::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 = Teuchos::ScalarTraits<value_type>::isComplex;
83 static const bool isOrdinal = Teuchos::ScalarTraits<value_type>::isOrdinal;
84 static const bool isComparable =
85 Teuchos::ScalarTraits<value_type>::isComparable;
86 static const bool hasMachineParameters =
87 Teuchos::ScalarTraits<value_type>::hasMachineParameters;
89 static typename Teuchos::ScalarTraits<value_type>::magnitudeType eps() {
90 return Teuchos::ScalarTraits<value_type>::eps();
93 static typename Teuchos::ScalarTraits<value_type>::magnitudeType sfmin() {
94 return Teuchos::ScalarTraits<value_type>::sfmin();
97 static typename Teuchos::ScalarTraits<value_type>::magnitudeType base() {
98 return Teuchos::ScalarTraits<value_type>::base();
101 static typename Teuchos::ScalarTraits<value_type>::magnitudeType prec() {
102 return Teuchos::ScalarTraits<value_type>::prec();
105 static typename Teuchos::ScalarTraits<value_type>::magnitudeType t() {
106 return Teuchos::ScalarTraits<value_type>::t();
109 static typename Teuchos::ScalarTraits<value_type>::magnitudeType
rnd() {
113 static typename Teuchos::ScalarTraits<value_type>::magnitudeType emin() {
114 return Teuchos::ScalarTraits<value_type>::emin();
117 static typename Teuchos::ScalarTraits<value_type>::magnitudeType rmin() {
118 return Teuchos::ScalarTraits<value_type>::rmin();
121 static typename Teuchos::ScalarTraits<value_type>::magnitudeType emax() {
122 return Teuchos::ScalarTraits<value_type>::emax();
125 static typename Teuchos::ScalarTraits<value_type>::magnitudeType rmax() {
126 return Teuchos::ScalarTraits<value_type>::rmax();
129 static magnitudeType magnitude(
const PCEType& a) {
133 static innerProductType innerProduct(
const PCEType& a,
const PCEType& b) {
134 return a.inner_product(b);
150 y.val() = Teuchos::ScalarTraits<value_type>::conjugate(
x.val());
159 y.val() = Teuchos::ScalarTraits<value_type>::real(
x.val());
166 return PCEType(Teuchos::ScalarTraits<value_type>::imag(
x.val()));
171 return Teuchos::ScalarTraits<value_type>::nan();
174 static bool isnaninf(
const PCEType&
x) {
175 for (
int i=0; i<
x.size(); i++)
176 if (Teuchos::ScalarTraits<value_type>::isnaninf(
x.fastAccessCoeff(i)))
181 static void seedrandom(
unsigned int s) {
182 Teuchos::ScalarTraits<value_type>::seedrandom(s);
186 return Teuchos::ScalarTraits<value_type>::random();
189 static const char * name() {
190 return "Sacado::UQ::PCE<>";
213 Teuchos::ScalarTraits<value_type>::magnitude(
x-Teuchos::ScalarTraits<value_type>::real(
x)) == 0;
218 static bool is_pce_real(
const PCEType&
x) {
221 if (Teuchos::ScalarTraits<value_type>::isComplex) {
222 for (
int i=0; i<
x.size(); i++)
223 if (!is_complex_real(
x.fastAccessCoeff(i)))
232 template <
typename TypeTo,
typename PCEType>
233 struct PCEValueTypeConversionTraitsImp {
234 typedef typename Sacado::ValueType<PCEType>::type ValueT;
235 typedef Teuchos::ValueTypeConversionTraits<TypeTo,ValueT> VTCT;
236 static TypeTo convert(
const PCEType t ) {
237 return VTCT::convert(t.val());
239 static TypeTo safeConvert(
const PCEType t ) {
240 return VTCT::safeConvert(t.val());
245 template <
typename Ordinal,
typename PCEType>
246 class PCESerializationTraitsImp {
247 typedef typename Sacado::ValueType<PCEType>::type ValueT;
248 typedef Teuchos::SerializationTraits<Ordinal,ValueT> vSerT;
249 typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
250 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
255 static const bool supportsDirectSerialization =
false;
264 for (
Ordinal i=0; i<count; i++) {
265 int sz = buffer[i].size();
266 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
267 Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
268 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
275 static void serialize (
const Ordinal count,
279 for (
Ordinal i=0; i<count; i++) {
281 int sz = buffer[i].size();
282 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
283 iSerT::serialize(1, &sz, b1, charBuffer);
287 Ordinal b2 = vSerT::fromCountToIndirectBytes(sz, buffer[i].coeff());
288 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
289 oSerT::serialize(1, &b2, b3, charBuffer);
291 vSerT::serialize(sz, buffer[i].coeff(), b2, charBuffer);
298 const char charBuffer[]) {
301 while (bytes_used < bytes) {
304 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
309 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
310 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
322 static void deserialize (
const Ordinal bytes,
323 const char charBuffer[],
326 for (
Ordinal i=0; i<count; i++) {
329 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
330 const int *sz = iSerT::convertFromCharPtr(charBuffer);
336 if (buffer[i].size() != *sz)
337 buffer[i].reset(buffer[i].
cijk(), *sz);
338 buffer[i].copyForWrite();
341 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
342 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
344 vSerT::deserialize(*b2, charBuffer, *sz, buffer[i].coeff());
356 template <
typename Ordinal,
typename PCEType,
typename ValueSerializer>
357 class PCESerializerImp {
362 typedef ValueSerializer value_serializer_type;
365 typedef typename PCEType::cijk_type cijk_type;
369 typedef typename Sacado::ValueType<PCEType>::type ValueT;
370 typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
371 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
374 Teuchos::RCP<const ValueSerializer> vs;
380 static const bool supportsDirectSerialization =
false;
382 PCESerializerImp(
const cijk_type& cijk_,
383 const Teuchos::RCP<const ValueSerializer>& vs_) :
384 cijk(cijk_), vs(vs_), sz(
cijk.dimension()) {}
387 cijk_type getSerializerCijk()
const {
return cijk; }
390 Teuchos::RCP<const value_serializer_type> getValueSerializer()
const {
398 const PCEType buffer[])
const {
402 for (
Ordinal i=0; i<count; i++) {
403 int my_sz = buffer[i].size();
413 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
414 Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
415 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
424 void serialize (
const Ordinal count,
427 char charBuffer[])
const {
430 for (
Ordinal i=0; i<count; i++) {
432 int my_sz = buffer[i].size();
442 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &sz);
443 iSerT::serialize(1, &sz, b1, charBuffer);
447 Ordinal b2 = vs->fromCountToIndirectBytes(sz, cx->coeff());
448 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
449 oSerT::serialize(1, &b2, b3, charBuffer);
451 vs->serialize(sz, cx->coeff(), b2, charBuffer);
460 const char charBuffer[])
const {
463 while (bytes_used < bytes) {
466 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
471 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
472 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
484 void deserialize (
const Ordinal bytes,
485 const char charBuffer[],
488 for (
Ordinal i=0; i<count; i++) {
491 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
492 const int *my_sz = iSerT::convertFromCharPtr(charBuffer);
496 buffer[i].reset(
cijk);
499 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
500 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
502 vs->deserialize(*b2, charBuffer, *my_sz, buffer[i].coeff());
516 #endif // HAVE_SACADO_TEUCHOS 518 #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