// @HEADER // ***************************************************************************** // Stokhos Package // // Copyright 2009 NTESS and the Stokhos contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef SACADO_MP_SCALAR_TRAITS_IMP_HPP #define SACADO_MP_SCALAR_TRAITS_IMP_HPP #include "Teuchos_ScalarTraits.hpp" #include "Teuchos_SerializationTraits.hpp" #include "Teuchos_SerializationTraitsHelpers.hpp" #include "Teuchos_Assert.hpp" #include "Teuchos_TestForException.hpp" #include "Sacado_mpl_apply.hpp" namespace Sacado { namespace MP { template struct ScalarTraitsImp {}; // Implementation of Teuchos::ScalarTraits where reductions are taken // across the components of MP::Vector. In this case magnitudeType is // a scalar template struct ScalarTraitsImp { typedef Sacado::MP::Vector ScalarType; typedef typename S::value_type value_type; typedef typename S::ordinal_type ordinal_type; typedef Teuchos::ScalarTraits TVT; typedef typename TVT::magnitudeType value_mag_type; typedef typename TVT::halfPrecision value_half_type; typedef typename TVT::doublePrecision value_double_type; typedef typename Sacado::mpl::apply::type storage_mag_type; typedef typename Sacado::mpl::apply::type storage_half_type; typedef typename Sacado::mpl::apply::type storage_double_type; typedef value_mag_type magnitudeType; typedef Sacado::MP::Vector halfPrecision; typedef Sacado::MP::Vector doublePrecision; typedef typename Teuchos::ScalarTraits::coordinateType coordinateType; static const bool isComplex = TVT::isComplex; static const bool isOrdinal = TVT::isOrdinal; static const bool isComparable = TVT::isComparable; static const bool hasMachineParameters = TVT::hasMachineParameters; static value_mag_type eps() { return TVT::eps(); } static value_mag_type sfmin() { return TVT::sfmin(); } static value_mag_type base() { return TVT::base(); } static value_mag_type prec() { return TVT::prec(); } static value_mag_type t() { return TVT::t(); } static value_mag_type rnd() { return TVT::rnd(); } static value_mag_type emin() { return TVT::emin(); } static value_mag_type rmin() { return TVT::rmin(); } static value_mag_type emax() { return TVT::emax(); } static value_mag_type rmax() { return TVT::rmax(); } static magnitudeType magnitude(const ScalarType& a) { magnitudeType m = magnitudeType(0.0); const ordinal_type sz = a.size(); for (ordinal_type i=0; i"; } static ScalarType squareroot(const ScalarType& x) { return std::sqrt(x); } static ScalarType pow(const ScalarType& x, const ScalarType& y) { return std::pow(x,y); } static ScalarType log(const ScalarType& x) { return std::log(x); } static ScalarType log10(const ScalarType& x) { return std::log10(x); } }; // class ScalarTraitsImp // Implementation of Teuchos::ScalarTraits where reductions are not taken // across the components of MP::Vector. In this case magnitudeType is // an MP::Vector template struct ScalarTraitsImp { typedef Sacado::MP::Vector ScalarType; typedef typename S::value_type value_type; typedef typename S::ordinal_type ordinal_type; typedef Teuchos::ScalarTraits TVT; typedef typename TVT::magnitudeType value_mag_type; typedef typename TVT::halfPrecision value_half_type; typedef typename TVT::doublePrecision value_double_type; typedef typename Sacado::mpl::apply::type storage_mag_type; typedef typename Sacado::mpl::apply::type storage_half_type; typedef typename Sacado::mpl::apply::type storage_double_type; typedef Sacado::MP::Vector magnitudeType; typedef Sacado::MP::Vector halfPrecision; typedef Sacado::MP::Vector doublePrecision; typedef typename Teuchos::ScalarTraits::coordinateType coordinateType; static const bool isComplex = TVT::isComplex; static const bool isOrdinal = TVT::isOrdinal; static const bool isComparable = TVT::isComparable; static const bool hasMachineParameters = TVT::hasMachineParameters; static value_mag_type eps() { return TVT::eps(); } static value_mag_type sfmin() { return TVT::sfmin(); } static value_mag_type base() { return TVT::base(); } static value_mag_type prec() { return TVT::prec(); } static value_mag_type t() { return TVT::t(); } static value_mag_type rnd() { return TVT::rnd(); } static value_mag_type emin() { return TVT::emin(); } static value_mag_type rmin() { return TVT::rmin(); } static value_mag_type emax() { return TVT::emax(); } static value_mag_type rmax() { return TVT::rmax(); } static magnitudeType magnitude(const ScalarType& a) { return std::fabs(a); } static ScalarType zero() { return ScalarType(0.0); } static ScalarType one() { return ScalarType(1.0); } static ScalarType conjugate(const ScalarType& x) { int sz = x.size(); ScalarType y(sz, value_type(0.0)); for (int i=0; i"; } static ScalarType squareroot(const ScalarType& x) { return std::sqrt(x); } static ScalarType pow(const ScalarType& x, const ScalarType& y) { return std::pow(x,y); } static ScalarType log(const ScalarType& x) { return std::log(x); } static ScalarType log10(const ScalarType& x) { return std::log10(x); } }; // class ScalarTraitsImp //! Serialization implementation for all Vector types template struct SerializationImp { private: //! Value type typedef typename Sacado::ValueType::type ValueT; //! How to serialize ints typedef Teuchos::SerializationTraits iSerT; //! How to serialize ordinals typedef Teuchos::SerializationTraits oSerT; public: /// \brief Whether the type T supports direct serialization. static const bool supportsDirectSerialization = false; //! @name Indirect serialization functions (always defined and supported) //@{ /** \brief Return the number of bytes for count objects. */ static Ordinal fromCountToIndirectBytes(const Serializer& vs, const Ordinal count, const VecType buffer[], const Ordinal sz = 0) { Ordinal bytes = 0; VecType *x = NULL; const VecType *cx; for (Ordinal i=0; ireset(tot_sz); cx = x; } else cx = &(buffer[i]); Ordinal b2 = vs.fromCountToIndirectBytes(tot_sz, cx->coeff()); Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2); bytes += b1+b2+b3; } if (x != NULL) delete x; return bytes; } /** \brief Serialize to an indirect char[] buffer. */ static void serialize (const Serializer& vs, const Ordinal count, const VecType buffer[], const Ordinal bytes, char charBuffer[], const Ordinal sz = 0) { VecType *x = NULL; const VecType *cx; for (Ordinal i=0; ireset(tot_sz); cx = x; } else cx = &(buffer[i]); Ordinal b2 = vs.fromCountToIndirectBytes(tot_sz, cx->coeff()); Ordinal b3 = oSerT::fromCountToIndirectBytes(1, &b2); oSerT::serialize(1, &b2, b3, charBuffer); charBuffer += b3; vs.serialize(tot_sz, cx->coeff(), b2, charBuffer); charBuffer += b2; } if (x != NULL) delete x; } /** \brief Return the number of objects for bytes of storage. */ static Ordinal fromIndirectBytesToCount(const Serializer& vs, const Ordinal bytes, const char charBuffer[], const Ordinal sz = 0) { Ordinal count = 0; Ordinal bytes_used = 0; while (bytes_used < bytes) { // Bytes for size Ordinal b1 = iSerT::fromCountToDirectBytes(1); bytes_used += b1; charBuffer += b1; // Bytes for vector coefficients Ordinal b3 = oSerT::fromCountToDirectBytes(1); const Ordinal *b2 = oSerT::convertFromCharPtr(charBuffer); bytes_used += b3; charBuffer += b3; bytes_used += *b2; charBuffer += *b2; ++count; } return count; } /** \brief Deserialize from an indirect char[] buffer. */ static void deserialize (const Serializer& vs, const Ordinal bytes, const char charBuffer[], const Ordinal count, VecType buffer[], const Ordinal sz = 0) { for (Ordinal i=0; i struct SerializationTraitsImp { private: //! Value type of Vec type typedef typename Sacado::ValueType::type ValueT; //! Default serializer for values typedef Teuchos::DefaultSerializer DS; //! Default serializer type for values typedef typename DS::DefaultSerializerType ValueSerializer; //! Implementation typedef SerializationImp Imp; public: /// \brief Whether the type T supports direct serialization. static const bool supportsDirectSerialization = Imp::supportsDirectSerialization; //! @name Indirect serialization functions (always defined and supported) //@{ /** \brief Return the number of bytes for count objects. */ static Ordinal fromCountToIndirectBytes(const Ordinal count, const VecType buffer[]) { return Imp::fromCountToIndirectBytes( DS::getDefaultSerializer(), count, buffer); } /** \brief Serialize to an indirect char[] buffer. */ static void serialize (const Ordinal count, const VecType buffer[], const Ordinal bytes, char charBuffer[]) { Imp::serialize( DS::getDefaultSerializer(), count, buffer, bytes, charBuffer); } /** \brief Return the number of objects for bytes of storage. */ static Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[]) { return Imp::fromIndirectBytesToCount( DS::getDefaultSerializer(), bytes, charBuffer); } /** \brief Deserialize from an indirect char[] buffer. */ static void deserialize (const Ordinal bytes, const char charBuffer[], const Ordinal count, VecType buffer[]) { Imp::deserialize( DS::getDefaultSerializer(), bytes, charBuffer, count, buffer); } //@} }; //! Implementation of Teuchos::SerializationTraits for all static Vec types template struct SerializationTraitsImp { typedef typename Sacado::ValueType::type ValueT; typedef Teuchos::SerializationTraits vSerT; typedef Teuchos::DirectSerializationTraits DSerT; typedef Sacado::MP::SerializationTraitsImp STI; /// \brief Whether the type T supports direct serialization. static const bool supportsDirectSerialization = vSerT::supportsDirectSerialization; //! @name Direct serialization functions (not defined if supportsDirectSerialization==false) //@{ /** \brief Return the number of bytes for count objects. */ static Ordinal fromCountToDirectBytes(const Ordinal count) { return DSerT::fromCountToDirectBytes(count); } /** \brief Convert the pointer type to char*. */ static char* convertToCharPtr( VecType* ptr ) { return DSerT::convertToCharPtr(ptr); } /** \brief Convert the pointer type to const char*. */ static const char* convertToCharPtr( const VecType* ptr ) { return DSerT::convertToCharPtr(ptr); } /** \brief Return the number of objects for bytes of storage. */ static Ordinal fromDirectBytesToCount(const Ordinal bytes) { return DSerT::fromDirectBytesToCount(bytes); } /** \brief Convert the pointer type from char*. */ static VecType* convertFromCharPtr( char* ptr ) { return DSerT::convertFromCharPtr(ptr); } /** \brief Convert the pointer type from char*. */ static const VecType* convertFromCharPtr( const char* ptr ) { return DSerT::convertFromCharPtr(ptr); } //@} //! @name Indirect serialization functions (always defined and supported) //@{ /** \brief Return the number of bytes for count objects. */ static Ordinal fromCountToIndirectBytes(const Ordinal count, const VecType buffer[]) { if (supportsDirectSerialization) return DSerT::fromCountToIndirectBytes(count, buffer); else return STI::fromCountToIndirectBytes(count, buffer); } /** \brief Serialize to an indirect char[] buffer. */ static void serialize (const Ordinal count, const VecType buffer[], const Ordinal bytes, char charBuffer[]) { if (supportsDirectSerialization) return DSerT::serialize(count, buffer, bytes, charBuffer); else return STI::serialize(count, buffer, bytes, charBuffer); } /** \brief Return the number of objects for bytes of storage. */ static Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[]) { if (supportsDirectSerialization) return DSerT::fromIndirectBytesToCount(bytes, charBuffer); else return STI::fromIndirectBytesToCount(bytes, charBuffer); } /** \brief Deserialize from an indirect char[] buffer. */ static void deserialize (const Ordinal bytes, const char charBuffer[], const Ordinal count, VecType buffer[]) { if (supportsDirectSerialization) return DSerT::deserialize(bytes, charBuffer, count, buffer); else return STI::deserialize(bytes, charBuffer, count, buffer); } //@} }; //! An indirect serialization object for all Vector types template class SerializerImp { private: //! Implementation typedef SerializationImp Imp; //! Serializer for value types Teuchos::RCP vs; //! Specified number of derivative components; Ordinal sz; public: //! Typename of value serializer typedef ValueSerializer value_serializer_type; /// \brief Whether we support direct serialization. static const bool supportsDirectSerialization = Imp::supportsDirectSerialization; //! Constructor SerializerImp(const Teuchos::RCP& vs_, Ordinal sz_ = 0) : vs(vs_), sz(sz_) {} //! Return specified serializer size Ordinal getSerializerSize() const { return sz; } //! Get nested value serializer Teuchos::RCP getValueSerializer() const { return vs; } //! @name Indirect serialization functions (always defined and supported) //@{ /** \brief Return the number of bytes for count objects. */ Ordinal fromCountToIndirectBytes(const Ordinal count, const VecType buffer[]) const { return Imp::fromCountToIndirectBytes(*vs, count, buffer, sz); } /** \brief Serialize to an indirect char[] buffer. */ void serialize (const Ordinal count, const VecType buffer[], const Ordinal bytes, char charBuffer[]) const { Imp::serialize(*vs, count, buffer, bytes, charBuffer, sz); } /** \brief Return the number of objects for bytes of storage. */ Ordinal fromIndirectBytesToCount(const Ordinal bytes, const char charBuffer[]) const { return Imp::fromIndirectBytesToCount(*vs, bytes, charBuffer, sz); } /** \brief Deserialize from an indirect char[] buffer. */ void deserialize (const Ordinal bytes, const char charBuffer[], const Ordinal count, VecType buffer[]) const { return Imp::deserialize(*vs, bytes, charBuffer, count, buffer, sz); } //@} }; } } #endif // SACADO_MP_SCALAR_TRAITS_IMP_HPP