// @HEADER // ***************************************************************************** // Anasazi: Block Eigensolvers Package // // Copyright 2004 NTESS and the Anasazi contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /*! \file AnasaziThyraDebugAdapter.hpp \brief Declarations of Anasazi multi-vector and operator classes using Thyra_MultiVectorBase and Thyra_LinearOpBase classes */ #ifndef ANASAZI_THYRA_DEBUG_ADAPTER_HPP #define ANASAZI_THYRA_DEBUG_ADAPTER_HPP #include "AnasaziConfigDefs.hpp" #include "AnasaziTypes.hpp" #include "AnasaziMultiVec.hpp" #include "AnasaziOperator.hpp" #include "AnasaziThyraAdapter.hpp" #include #include #include #include "Teuchos_Assert.hpp" #include "Teuchos_SerialDenseMatrix.hpp" #include "Teuchos_RCP.hpp" #include "Teuchos_TimeMonitor.hpp" namespace Anasazi { //@} /////////////////////////////////////////////////////////////// // //--------template class AnasaziThyraMultiVec----------------- // /////////////////////////////////////////////////////////////// /*! \brief Basic adapter class for Anasazi::MultiVec that uses Thyra::MultiVectorBase. \note This adapter is only to be used for debugging purposes. For production use Anasazi::MultiVecTraits templated on Thyra::MultiVectorBase<> and Thyra::LinearOpBase<>. */ template class ThyraMultiVec : public MultiVec { public: typedef MultiVecTraits > MVT; typedef Teuchos::ScalarTraits SCT; typedef typename Teuchos::ScalarTraits::magnitudeType MagnitudeType; typedef Teuchos::ScalarTraits MT; //! @name Constructors/Destructors //@{ //! Basic ThyraMultiVec constructor (wraps Thyra::MultiVectorBase<> object). /*! @param mv [in] a reference-counted pointer to a Thyra::MultiVectorBase<> object. \returns Pointer to a ThyraMultiVec object. */ ThyraMultiVec( const Teuchos::RCP > & mv ) : _timerCreate(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::create")), _timerClone(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::clone")), _timerDestroy(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::destroy")), _timerMvTimesMatAddMv(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvtimesmataddmv")), _timerMvTransMv(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvtransmv")), _timerMvAddMv(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvaddmv")), _timerMvDot(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvdot")), _timerMvNorm(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvnorm")), _timerMvScale(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvscale")), _timerSetBlock(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::setblock")), _timerMvInit(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvinit")), _timerMvRandom(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvrandom")) { Teuchos::TimeMonitor timer(*_timerCreate); Thyra_MV = mv; } //! Basic ThyraMultiVec constructor (wraps Thyra::MultiVectorBase<> object). /*! @param mv [in] a reference-counted pointer to a Thyra::MultiVectorBase<> object. @param timers [in] a vector containing timers for this wrapper to use. \returns Pointer to a ThyraMultiVec object. */ ThyraMultiVec( const Teuchos::RCP > & mv, std::vector >& timers ) { copyTimers( timers ); Teuchos::TimeMonitor timer(*_timerCreate); Thyra_MV = mv; } //! Copy constructor. /*! @param mv [in] a ThyraMultiVec object. \returns Pointer to a ThyraMultiVec object, where the underlying Thyra::MultiVectorBase<> object has been deep copied. */ ThyraMultiVec( const ThyraMultiVec & mv ) { copyTimers( mv.getTimers() ); Teuchos::TimeMonitor timer(*_timerCreate); Thyra_MV = MVT::CloneCopy( *(mv.getRCP()) ); } //! Destructor virtual ~ThyraMultiVec() { Teuchos::TimeMonitor timer(*_timerDestroy); } //@} //! @name Creation methods //@{ /*! \brief Creates a new empty ThyraMultiVec containing \c numvecs columns. \returns Pointer to an ThyraMultiVec */ MultiVec * Clone ( const int numvecs ) const { Teuchos::TimeMonitor timer(*_timerClone); std::vector > myTimers = getTimers(); return new ThyraMultiVec( MVT::Clone( *Thyra_MV, numvecs ), myTimers ); } /*! \brief Creates a new ThyraMultiVec and copies contents of \c *this into the new vector (deep copy). \returns Pointer to an ThyraMultiVec */ MultiVec * CloneCopy () const { Teuchos::TimeMonitor timer(*_timerClone); std::vector > myTimers = getTimers(); return new ThyraMultiVec( MVT::CloneCopy( *Thyra_MV ), myTimers ); } /*! \brief Creates a new ThyraMultiVec and copies the selected contents of \c *this into the new vector (deep copy). The copied vectors from \c *this are indicated by the \c index.size() indices in \c index. \returns Pointer to an ThyraMultiVec */ MultiVec * CloneCopy ( const std::vector& index ) const { Teuchos::TimeMonitor timer(*_timerClone); std::vector > myTimers = getTimers(); return new ThyraMultiVec( MVT::CloneCopy( *Thyra_MV, index ), myTimers ); } /*! \brief Creates a new ThyraMultiVec that shares the selected contents of \c *this. The index of the \c numvecs vectors shallow copied from \c *this are indicated by the indices given in \c index. \returns Pointer to an ThyraMultiVec */ MultiVec * CloneViewNonConst ( const std::vector& index ) { Teuchos::TimeMonitor timer(*_timerClone); std::vector > myTimers = getTimers(); return new ThyraMultiVec( MVT::CloneViewNonConst( *Thyra_MV, index ), myTimers ); } /*! \brief Creates a new ThyraMultiVec that shares the selected contents of \c *this. The index of the \c numvecs vectors shallow copied from \c *this are indicated by the indices given in \c index. \returns Pointer to an ThyraMultiVec */ const MultiVec * CloneView ( const std::vector& index ) const { Teuchos::TimeMonitor timer(*_timerClone); std::vector > myTimers = getTimers(); Teuchos::RCP > nonconst_ptr_to_const_view = Teuchos::rcp_const_cast >( MVT::CloneView(*Thyra_MV,index) ); const MultiVec * const_ret = new ThyraMultiVec( nonconst_ptr_to_const_view, myTimers ); return const_ret; } //@} //! @name Attribute methods //@{ //! Obtain the vector length of *this. int GetNumberVecs () const { return MVT::GetNumberVecs( *Thyra_MV ); } //! Obtain the number of vectors in *this. ptrdiff_t GetGlobalLength () const { return MVT::GetGlobalLength( *Thyra_MV ); } //@} //! @name Update methods //@{ /*! \brief Update \c *this with \f$\alpha AB + \beta (*this)\f$. */ void MvTimesMatAddMv ( ScalarType alpha, const MultiVec& A, const Teuchos::SerialDenseMatrix& B, ScalarType beta ) { Teuchos::TimeMonitor timer(*_timerMvTimesMatAddMv); const Anasazi::ThyraMultiVec* vec_A = dynamic_cast* >(&A); MVT::MvTimesMatAddMv( alpha, *(vec_A->getRCP()), B, beta, *Thyra_MV ); } /*! \brief Replace \c *this with \f$\alpha A + \beta B\f$. */ void MvAddMv ( ScalarType alpha, const MultiVec& A, ScalarType beta, const MultiVec& B) { Teuchos::TimeMonitor timer(*_timerMvAddMv); const Anasazi::ThyraMultiVec* vec_A = dynamic_cast* >(&A); const Anasazi::ThyraMultiVec* vec_B = dynamic_cast* >(&B); MVT::MvAddMv( alpha, *(vec_A->getRCP()), beta, *(vec_B->getRCP()), *Thyra_MV ); } /*! \brief Compute a dense matrix \c B through the matrix-matrix multiply \f$\alpha A^T(*this)\f$. */ void MvTransMv ( ScalarType alpha, const MultiVec& A, Teuchos::SerialDenseMatrix& B #ifdef HAVE_ANASAZI_EXPERIMENTAL , ConjType conj = Anasazi::CONJ #endif ) const { Teuchos::TimeMonitor timer(*_timerMvTransMv); const Anasazi::ThyraMultiVec* vec_A = dynamic_cast* >(&A); MVT::MvTransMv( alpha, *(vec_A->getRCP()), *Thyra_MV, B ); } /*! \brief Compute a vector \c b where the components are the individual dot-products, i.e. \f$ b[i] = A[i]^H(this[i])\f$ where \c A[i] is the i-th column of \c A. */ void MvDot ( const MultiVec& A, std::vector &b #ifdef HAVE_ANASAZI_EXPERIMENTAL , ConjType conj = Anasazi::CONJ #endif ) const { Teuchos::TimeMonitor timer(*_timerMvDot); const Anasazi::ThyraMultiVec* vec_A = dynamic_cast* >(&A); MVT::MvDot( *Thyra_MV, *(vec_A->getRCP()), b ); } /*! \brief Scale each element of the vectors in \c *this with \c alpha. */ void MvScale ( ScalarType alpha ) { Teuchos::TimeMonitor timer(*_timerMvScale); MVT::MvScale( *Thyra_MV, alpha ); } /*! \brief Scale each element of the \c i-th vector in \c *this with \c alpha[i]. */ void MvScale ( const std::vector& alpha ) { Teuchos::TimeMonitor timer(*_timerMvScale); MVT::MvScale( *Thyra_MV, alpha ); } //@} //! @name Norm method //@{ /*! \brief Compute the 2-norm of each individual vector of \c *this. Upon return, \c normvec[i] holds the 2-norm of the \c i-th vector of \c *this */ void MvNorm ( std::vector::magnitudeType> &normvec ) const { Teuchos::TimeMonitor timer(*_timerMvNorm); MVT::MvNorm( *Thyra_MV, normvec ); } //@} //! @name Initialization methods //@{ /*! \brief Copy the vectors in \c A to a set of vectors in \c *this. The \c numvecs vectors in \c A are copied to a subset of vectors in \c *this indicated by the indices given in \c index. */ void SetBlock ( const MultiVec& A, const std::vector& index ) { Teuchos::TimeMonitor timer(*_timerSetBlock); const Anasazi::ThyraMultiVec* vec_A = dynamic_cast* >(&A); MVT::SetBlock( *(vec_A->getRCP()), index, *Thyra_MV ); } /*! \brief Fill the vectors in \c *this with random numbers. */ void MvRandom() { Teuchos::TimeMonitor timer(*_timerMvRandom); MVT::MvRandom( *Thyra_MV ); } /*! \brief Replace each element of the vectors in \c *this with \c alpha. */ void MvInit ( ScalarType alpha ) { Teuchos::TimeMonitor timer(*_timerMvInit); MVT::MvInit( *Thyra_MV, alpha ); } //!{ @name Accessor methods //@{ /*! \brief Return the reference-counted pointer held by this object */ Teuchos::RCP< Thyra::MultiVectorBase > getRCP() { return Thyra_MV; } /*! \brief Return the const reference-counted pointer held by this object */ Teuchos::RCP< const Thyra::MultiVectorBase > getRCP() const { return Thyra_MV; } /*! \brief Return a std::vector<> of timers held by this object */ std::vector > getTimers() const { std::vector > timers; timers.push_back( _timerCreate ); timers.push_back( _timerClone ); timers.push_back( _timerDestroy ); timers.push_back( _timerMvTimesMatAddMv ); timers.push_back( _timerMvTransMv ); timers.push_back( _timerMvAddMv ); timers.push_back( _timerMvDot ); timers.push_back( _timerMvNorm ); timers.push_back( _timerMvScale ); timers.push_back( _timerSetBlock ); timers.push_back( _timerMvInit ); timers.push_back( _timerMvRandom ); return timers; } /*! \brief Copy a std::vector<> of timers into this object */ void copyTimers( const std::vector >& timers ) { _timerCreate = timers[0]; _timerClone = timers[1]; _timerDestroy = timers[2]; _timerMvTimesMatAddMv = timers[3]; _timerMvTransMv = timers[4]; _timerMvAddMv = timers[5]; _timerMvDot = timers[6]; _timerMvNorm = timers[7]; _timerMvScale = timers[8]; _timerSetBlock = timers[9]; _timerMvInit = timers[10]; _timerMvRandom = timers[11]; } //@} //@} //! @name Print method //@{ /*! \brief Print \c *this ThyraMultiVec. */ void MvPrint( std::ostream& os ) const { MVT::MvPrint( *Thyra_MV, os ); } //@} private: Teuchos::RCP > Thyra_MV; Teuchos::RCP _timerCreate, _timerClone, _timerDestroy; Teuchos::RCP _timerMvTimesMatAddMv, _timerMvTransMv, _timerMvAddMv, _timerMvDot; Teuchos::RCP _timerMvNorm, _timerMvScale, _timerSetBlock, _timerMvInit, _timerMvRandom; }; //------------------------------------------------------------- /////////////////////////////////////////////////////////////// // //--------template class AnasaziThyraOp--------------------- // /////////////////////////////////////////////////////////////// /*! \brief Basic adapter class for Anasazi::Operator that uses Thyra_Operator. \note The Thyra package performs double-precision arithmetic, so the use of Thyra with Anasazi will only provide a double-precision eigensolver. */ template class ThyraOp : public virtual Operator { public: typedef OperatorTraits,Thyra::LinearOpBase > OPT; //! @name Constructor/Destructor //@{ //! Basic constructor. Accepts reference-counted pointer to an Thyra_Operator. ThyraOp(const Teuchos::RCP > &Op ) { Thyra_Op = Op; } //! Destructor ~ThyraOp() {} //@} //! @name Operator application method //@{ /*! \brief This method takes the Anasazi::MultiVec \c X and applies the operator to it resulting in the Anasazi::MultiVec \c Y. */ void Apply ( const MultiVec& X, MultiVec& Y ) const { const Anasazi::ThyraMultiVec* vec_X = dynamic_cast* >(&X); Anasazi::ThyraMultiVec* vec_Y = dynamic_cast* >(&Y); OPT::Apply( *Thyra_Op, *(vec_X->getRCP()), *(vec_Y->getRCP()) ); } //@} private: Teuchos::RCP > Thyra_Op; }; } // end of Anasazi namespace #endif // end of file ANASAZI_THYRA_DEBUG_ADAPTER_HPP