42 #ifndef SACADO_MP_SCALAR_TRAITS_IMP_HPP 43 #define SACADO_MP_SCALAR_TRAITS_IMP_HPP 45 #ifdef HAVE_SACADO_TEUCHOS 47 #include "Teuchos_ScalarTraits.hpp" 48 #include "Teuchos_SerializationTraits.hpp" 49 #include "Teuchos_SerializationTraitsHelpers.hpp" 50 #include "Teuchos_Assert.hpp" 51 #include "Teuchos_TestForException.hpp" 52 #include "Sacado_mpl_apply.hpp" 57 template <
typename S,
bool reduct_across_vector>
58 struct ScalarTraitsImp {};
64 struct ScalarTraitsImp<S,
true> {
68 typedef Teuchos::ScalarTraits<value_type> TVT;
70 typedef typename TVT::magnitudeType value_mag_type;
71 typedef typename TVT::halfPrecision value_half_type;
72 typedef typename TVT::doublePrecision value_double_type;
74 typedef typename Sacado::mpl::apply<S,ordinal_type,value_mag_type>::type storage_mag_type;
75 typedef typename Sacado::mpl::apply<S,ordinal_type,value_half_type>::type storage_half_type;
76 typedef typename Sacado::mpl::apply<S,ordinal_type,value_double_type>::type storage_double_type;
78 typedef value_mag_type magnitudeType;
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 ScalarType& a) {
108 magnitudeType m = magnitudeType(0.0);
111 value_mag_type t = TVT::magnitude(a.fastAccessCoeff(i));
117 static ScalarType zero() {
return ScalarType(0.0); }
119 static ScalarType one() {
return ScalarType(1.0); }
122 static ScalarType conjugate(
const ScalarType&
x) {
125 for (
int i=0; i<sz; i++)
126 y.fastAccessCoeff(i) = TVT::conjugate(
x.fastAccessCoeff(i));
131 static magnitudeType real(
const ScalarType&
x) {
132 magnitudeType m = magnitudeType(0.0);
135 value_mag_type t = TVT::real(
x.fastAccessCoeff(i));
142 static magnitudeType imag(
const ScalarType&
x) {
143 magnitudeType m = magnitudeType(0.0);
146 value_mag_type t = TVT::imag(
x.fastAccessCoeff(i));
152 static value_type nan() {
return TVT::nan(); }
154 static bool isnaninf(
const ScalarType&
x) {
155 for (
int i=0; i<
x.size(); i++)
156 if (TVT::isnaninf(
x.fastAccessCoeff(i)))
161 static void seedrandom(
unsigned int s) { TVT::seedrandom(s); }
163 static ScalarType random() {
return ScalarType(TVT::random()); }
165 static const char * name() {
return "Sacado::MP::Vector<>"; }
167 static ScalarType squareroot(
const ScalarType&
x) {
return std::sqrt(
x); }
169 static ScalarType
pow(
const ScalarType&
x,
const ScalarType&
y) {
173 static ScalarType
log(
const ScalarType&
x) {
return std::log(
x); }
182 template <
typename S>
183 struct ScalarTraitsImp<S,false> {
187 typedef Teuchos::ScalarTraits<value_type> TVT;
189 typedef typename TVT::magnitudeType value_mag_type;
190 typedef typename TVT::halfPrecision value_half_type;
191 typedef typename TVT::doublePrecision value_double_type;
193 typedef typename Sacado::mpl::apply<S,ordinal_type,value_mag_type>::type storage_mag_type;
194 typedef typename Sacado::mpl::apply<S,ordinal_type,value_half_type>::type storage_half_type;
195 typedef typename Sacado::mpl::apply<S,ordinal_type,value_double_type>::type storage_double_type;
201 static const bool isComplex = TVT::isComplex;
202 static const bool isOrdinal = TVT::isOrdinal;
203 static const bool isComparable = TVT::isComparable;
204 static const bool hasMachineParameters = TVT::hasMachineParameters;
206 static value_mag_type eps() {
return TVT::eps(); }
208 static value_mag_type sfmin() {
return TVT::sfmin(); }
210 static value_mag_type base() {
return TVT::base(); }
212 static value_mag_type prec() {
return TVT::prec(); }
214 static value_mag_type t() {
return TVT::t(); }
218 static value_mag_type emin() {
return TVT::emin(); }
220 static value_mag_type rmin() {
return TVT::rmin(); }
222 static value_mag_type emax() {
return TVT::emax(); }
224 static value_mag_type rmax() {
return TVT::rmax(); }
226 static magnitudeType magnitude(
const ScalarType& a) {
230 static ScalarType zero() {
return ScalarType(0.0); }
232 static ScalarType one() {
return ScalarType(1.0); }
235 static ScalarType conjugate(
const ScalarType&
x) {
238 for (
int i=0; i<sz; i++)
239 y.fastAccessCoeff(i) = TVT::conjugate(
x.fastAccessCoeff(i));
243 static ScalarType real(
const ScalarType&
x) {
246 for (
int i=0; i<sz; i++)
247 y.fastAccessCoeff(i) = TVT::real(
x.fastAccessCoeff(i));
251 static ScalarType imag(
const ScalarType&
x) {
254 for (
int i=0; i<sz; i++)
255 y.fastAccessCoeff(i) = TVT::imag(
x.fastAccessCoeff(i));
259 static value_type nan() {
return TVT::nan(); }
261 static bool isnaninf(
const ScalarType&
x) {
262 for (
int i=0; i<
x.size(); i++)
263 if (TVT::isnaninf(
x.fastAccessCoeff(i)))
268 static void seedrandom(
unsigned int s) { TVT::seedrandom(s); }
270 static ScalarType random() {
return ScalarType(TVT::random()); }
272 static const char * name() {
return "Sacado::MP::Vector<>"; }
274 static ScalarType squareroot(
const ScalarType&
x) {
return std::sqrt(
x); }
276 static ScalarType
pow(
const ScalarType&
x,
const ScalarType&
y) {
280 static ScalarType
log(
const ScalarType&
x) {
return std::log(
x); }
287 template <
typename Ordinal,
typename VecType,
typename Serializer>
288 struct SerializationImp {
293 typedef typename Sacado::ValueType<VecType>::type ValueT;
296 typedef Teuchos::SerializationTraits<Ordinal,int> iSerT;
299 typedef Teuchos::SerializationTraits<Ordinal,Ordinal> oSerT;
304 static const bool supportsDirectSerialization =
false;
310 static Ordinal fromCountToIndirectBytes(
const Serializer& vs,
312 const VecType buffer[],
317 for (
Ordinal i=0; i<count; i++) {
318 int my_sz = buffer[i].size();
320 if (sz == 0) tot_sz = my_sz;
321 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
322 if (tot_sz != my_sz) {
331 Ordinal b2 = vs.fromCountToIndirectBytes(tot_sz, cx->coeff());
332 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
341 static void serialize (
const Serializer& vs,
343 const VecType buffer[],
349 for (
Ordinal i=0; i<count; i++) {
351 int my_sz = buffer[i].size();
353 if (sz == 0) tot_sz = my_sz;
354 Ordinal b1 = iSerT::fromCountToIndirectBytes(1, &tot_sz);
355 iSerT::serialize(1, &tot_sz, b1, charBuffer);
359 if (tot_sz != my_sz) {
368 Ordinal b2 = vs.fromCountToIndirectBytes(tot_sz, cx->coeff());
369 Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2);
370 oSerT::serialize(1, &b2, b3, charBuffer);
372 vs.serialize(tot_sz, cx->coeff(), b2, charBuffer);
380 static Ordinal fromIndirectBytesToCount(
const Serializer& vs,
382 const char charBuffer[],
386 while (bytes_used < bytes) {
389 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
394 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
395 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
407 static void deserialize (
const Serializer& vs,
409 const char charBuffer[],
413 for (
Ordinal i=0; i<count; i++) {
416 Ordinal b1 = iSerT::fromCountToDirectBytes(1);
417 const int *my_sz = iSerT::convertFromCharPtr(charBuffer);
422 if (sz == 0) tot_sz = *my_sz;
423 buffer[i] = VecType(tot_sz, ValueT(0.0));
426 Ordinal b3 = oSerT::fromCountToDirectBytes(1);
427 const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer);
429 vs.deserialize(*b2, charBuffer, *my_sz, buffer[i].coeff());
439 template <
typename Ordinal,
typename VecType,
bool is_static = false>
440 struct SerializationTraitsImp {
445 typedef typename Sacado::ValueType<VecType>::type ValueT;
448 typedef Teuchos::DefaultSerializer<Ordinal,ValueT> DS;
451 typedef typename DS::DefaultSerializerType ValueSerializer;
454 typedef SerializationImp<Ordinal,VecType,ValueSerializer> Imp;
459 static const bool supportsDirectSerialization =
460 Imp::supportsDirectSerialization;
467 const VecType buffer[]) {
468 return Imp::fromCountToIndirectBytes(
469 DS::getDefaultSerializer(), count, buffer);
473 static void serialize (
const Ordinal count,
474 const VecType buffer[],
478 DS::getDefaultSerializer(), count, buffer, bytes, charBuffer);
483 const char charBuffer[]) {
484 return Imp::fromIndirectBytesToCount(
485 DS::getDefaultSerializer(), bytes, charBuffer);
489 static void deserialize (
const Ordinal bytes,
490 const char charBuffer[],
494 DS::getDefaultSerializer(), bytes, charBuffer, count, buffer);
502 template <
typename Ordinal,
typename VecType>
503 struct SerializationTraitsImp<
Ordinal, VecType,
true> {
504 typedef typename Sacado::ValueType<VecType>::type ValueT;
505 typedef Teuchos::SerializationTraits<Ordinal,ValueT> vSerT;
506 typedef Teuchos::DirectSerializationTraits<Ordinal,VecType> DSerT;
507 typedef Sacado::MP::SerializationTraitsImp<Ordinal,VecType> STI;
510 static const bool supportsDirectSerialization =
511 vSerT::supportsDirectSerialization;
518 return DSerT::fromCountToDirectBytes(count);
522 static char* convertToCharPtr( VecType* ptr ) {
523 return DSerT::convertToCharPtr(ptr);
527 static const char* convertToCharPtr(
const VecType* ptr ) {
528 return DSerT::convertToCharPtr(ptr);
533 return DSerT::fromDirectBytesToCount(bytes);
537 static VecType* convertFromCharPtr(
char* ptr ) {
538 return DSerT::convertFromCharPtr(ptr);
542 static const VecType* convertFromCharPtr(
const char* ptr ) {
543 return DSerT::convertFromCharPtr(ptr);
553 const VecType buffer[]) {
554 if (supportsDirectSerialization)
555 return DSerT::fromCountToIndirectBytes(count, buffer);
557 return STI::fromCountToIndirectBytes(count, buffer);
561 static void serialize (
const Ordinal count,
562 const VecType buffer[],
565 if (supportsDirectSerialization)
566 return DSerT::serialize(count, buffer, bytes, charBuffer);
568 return STI::serialize(count, buffer, bytes, charBuffer);
573 const char charBuffer[]) {
574 if (supportsDirectSerialization)
575 return DSerT::fromIndirectBytesToCount(bytes, charBuffer);
577 return STI::fromIndirectBytesToCount(bytes, charBuffer);
581 static void deserialize (
const Ordinal bytes,
582 const char charBuffer[],
585 if (supportsDirectSerialization)
586 return DSerT::deserialize(bytes, charBuffer, count, buffer);
588 return STI::deserialize(bytes, charBuffer, count, buffer);
596 template <
typename Ordinal,
typename VecType,
typename ValueSerializer>
597 class SerializerImp {
602 typedef SerializationImp<Ordinal,VecType,ValueSerializer> Imp;
605 Teuchos::RCP<const ValueSerializer> vs;
613 typedef ValueSerializer value_serializer_type;
616 static const bool supportsDirectSerialization =
617 Imp::supportsDirectSerialization;
620 SerializerImp(
const Teuchos::RCP<const ValueSerializer>& vs_,
625 Ordinal getSerializerSize()
const {
return sz; }
628 Teuchos::RCP<const value_serializer_type> getValueSerializer()
const {
636 const VecType buffer[])
const {
637 return Imp::fromCountToIndirectBytes(*vs, count, buffer, sz);
641 void serialize (
const Ordinal count,
642 const VecType buffer[],
644 char charBuffer[])
const {
645 Imp::serialize(*vs, count, buffer, bytes, charBuffer, sz);
650 const char charBuffer[])
const {
651 return Imp::fromIndirectBytesToCount(*vs, bytes, charBuffer, sz);
655 void deserialize (
const Ordinal bytes,
656 const char charBuffer[],
658 VecType buffer[])
const {
659 return Imp::deserialize(*vs, bytes, charBuffer, count, buffer, sz);
670 #endif // HAVE_SACADO_TEUCHOS 672 #endif // SACADO_MP_SCALAR_TRAITS_IMP_HPP KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > fabs(const PCE< Storage > &a)
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 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