Anasazi Version of the Day
AnasaziThyraDebugAdapter.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                 Anasazi: Block Eigensolvers Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00025 //
00026 // ***********************************************************************
00027 // @HEADER
00028 
00033 #ifndef ANASAZI_THYRA_DEBUG_ADAPTER_HPP
00034 #define ANASAZI_THYRA_DEBUG_ADAPTER_HPP
00035 
00036 #include "AnasaziConfigDefs.hpp"
00037 #include "AnasaziTypes.hpp"
00038 #include "AnasaziMultiVec.hpp"
00039 #include "AnasaziOperator.hpp"
00040 
00041 #include "AnasaziThyraAdapter.hpp"
00042 #include <Thyra_DetachedMultiVectorView.hpp>
00043 #include <Thyra_MultiVectorBase.hpp>
00044 #include <Thyra_MultiVectorStdOps.hpp>
00045 
00046 #include "Teuchos_Assert.hpp"
00047 #include "Teuchos_SerialDenseMatrix.hpp"
00048 #include "Teuchos_RefCountPtr.hpp"
00049 #include "Teuchos_TimeMonitor.hpp"
00050 
00051 namespace Anasazi {
00052 
00054   
00056   //
00057   //--------template class AnasaziThyraMultiVec-----------------
00058   //
00060   
00068   template<class ScalarType>
00069   class ThyraMultiVec : public MultiVec<ScalarType> {
00070   public:
00071   
00072     typedef MultiVecTraits<ScalarType,Thyra::MultiVectorBase<ScalarType> > MVT;
00073     typedef Teuchos::ScalarTraits<ScalarType> SCT;
00074     typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
00075     typedef Teuchos::ScalarTraits<MagnitudeType> MT;
00076 
00078 
00079 
00081 
00085     ThyraMultiVec( const Teuchos::RCP<Thyra::MultiVectorBase< ScalarType > > & mv ) :
00086       _timerCreate(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::create")),
00087       _timerClone(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::clone")),
00088       _timerDestroy(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::destroy")),
00089       _timerMvTimesMatAddMv(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvtimesmataddmv")),
00090       _timerMvTransMv(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvtransmv")),
00091       _timerMvAddMv(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvaddmv")),
00092       _timerMvDot(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvdot")),
00093       _timerMvNorm(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvnorm")),
00094       _timerMvScale(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvscale")),
00095       _timerSetBlock(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::setblock")),
00096       _timerMvInit(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvinit")),
00097       _timerMvRandom(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvrandom"))
00098     {
00099       Teuchos::TimeMonitor timer(*_timerCreate);
00100       Thyra_MV = mv;
00101     }
00102 
00104 
00109     ThyraMultiVec( const Teuchos::RCP<Thyra::MultiVectorBase< ScalarType > > & mv, std::vector<Teuchos::RCP<Teuchos::Time> >& timers ) 
00110     {
00111       copyTimers( timers );
00112       Teuchos::TimeMonitor timer(*_timerCreate);
00113       Thyra_MV = mv;
00114     }
00115 
00117 
00121     ThyraMultiVec( const ThyraMultiVec<ScalarType> & mv ) 
00122     { 
00123       copyTimers( mv.getTimers() );
00124       Teuchos::TimeMonitor timer(*_timerCreate);
00125       Thyra_MV = MVT::CloneCopy( *(mv.getRCP()) );      
00126     }
00127     
00129     virtual ~ThyraMultiVec() { Teuchos::TimeMonitor timer(*_timerDestroy); }
00130 
00132 
00134 
00135 
00140     MultiVec<ScalarType> * Clone ( const int numvecs ) const 
00141     { 
00142       Teuchos::TimeMonitor timer(*_timerClone); 
00143       std::vector<Teuchos::RCP<Teuchos::Time> >  myTimers = getTimers();
00144       return new ThyraMultiVec<ScalarType>( MVT::Clone( *Thyra_MV, numvecs ), myTimers ); }
00145 
00151     MultiVec<ScalarType> * CloneCopy () const 
00152     { 
00153       Teuchos::TimeMonitor timer(*_timerClone);
00154       std::vector<Teuchos::RCP<Teuchos::Time> >  myTimers = getTimers();
00155       return new ThyraMultiVec<ScalarType>( MVT::CloneCopy( *Thyra_MV ), myTimers ); 
00156     }
00157 
00165     MultiVec<ScalarType> * CloneCopy ( const std::vector<int>& index ) const 
00166     { 
00167       Teuchos::TimeMonitor timer(*_timerClone);
00168       std::vector<Teuchos::RCP<Teuchos::Time> >  myTimers = getTimers();
00169       return new ThyraMultiVec<ScalarType>( MVT::CloneCopy( *Thyra_MV, index ), myTimers ); 
00170     }
00171     
00179     MultiVec<ScalarType> * CloneViewNonConst ( const std::vector<int>& index ) 
00180     {
00181       Teuchos::TimeMonitor timer(*_timerClone); 
00182       std::vector<Teuchos::RCP<Teuchos::Time> >  myTimers = getTimers();
00183       return new ThyraMultiVec<ScalarType>( MVT::CloneViewNonConst( *Thyra_MV, index ), myTimers ); 
00184     }
00185 
00193     const MultiVec<ScalarType> * CloneView ( const std::vector<int>& index ) const
00194     {
00195       Teuchos::TimeMonitor timer(*_timerClone); 
00196       std::vector<Teuchos::RCP<Teuchos::Time> >  myTimers = getTimers();
00197       Teuchos::RCP<Thyra::MultiVectorBase<ScalarType> > nonconst_ptr_to_const_view = Teuchos::rcp_const_cast<Thyra::MultiVectorBase<ScalarType> >( MVT::CloneView(*Thyra_MV,index) );
00198       const MultiVec<ScalarType> * const_ret = new ThyraMultiVec<ScalarType>( nonconst_ptr_to_const_view, myTimers );
00199       return const_ret;
00200     }
00201 
00203 
00205 
00206 
00208     int GetNumberVecs () const { return MVT::GetNumberVecs( *Thyra_MV ); }
00209 
00211     int GetVecLength () const { return MVT::GetVecLength( *Thyra_MV ); }
00212 
00214 
00216 
00217 
00219     void MvTimesMatAddMv ( ScalarType alpha, const MultiVec<ScalarType>& A, 
00220                            const Teuchos::SerialDenseMatrix<int,ScalarType>& B, 
00221                            ScalarType beta )
00222     { 
00223       Teuchos::TimeMonitor timer(*_timerMvTimesMatAddMv);
00224       const Anasazi::ThyraMultiVec<ScalarType>* vec_A = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&A);
00225       MVT::MvTimesMatAddMv( alpha, *(vec_A->getRCP()), B, beta, *Thyra_MV ); 
00226     }
00227 
00230     void MvAddMv ( ScalarType alpha, const MultiVec<ScalarType>& A, 
00231                    ScalarType beta, const MultiVec<ScalarType>& B)
00232     { 
00233       Teuchos::TimeMonitor timer(*_timerMvAddMv);
00234       const Anasazi::ThyraMultiVec<ScalarType>* vec_A = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&A);
00235       const Anasazi::ThyraMultiVec<ScalarType>* vec_B = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&B);
00236       MVT::MvAddMv( alpha, *(vec_A->getRCP()), beta, *(vec_B->getRCP()), *Thyra_MV ); 
00237     }
00238 
00241     void MvTransMv ( ScalarType alpha, const MultiVec<ScalarType>& A, Teuchos::SerialDenseMatrix<int,ScalarType>& B 
00242 #ifdef HAVE_ANASAZI_EXPERIMENTAL
00243         , ConjType conj = Anasazi::CONJ
00244 #endif
00245         ) const
00246     { 
00247       Teuchos::TimeMonitor timer(*_timerMvTransMv);
00248       const Anasazi::ThyraMultiVec<ScalarType>* vec_A = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&A);
00249       MVT::MvTransMv( alpha, *(vec_A->getRCP()), *Thyra_MV, B ); 
00250     }
00251   
00254     void MvDot ( const MultiVec<ScalarType>& A, std::vector<ScalarType> &b
00255 #ifdef HAVE_ANASAZI_EXPERIMENTAL
00256         , ConjType conj = Anasazi::CONJ
00257 #endif
00258         ) const
00259     { 
00260       Teuchos::TimeMonitor timer(*_timerMvDot);
00261       const Anasazi::ThyraMultiVec<ScalarType>* vec_A = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&A);
00262       MVT::MvDot( *Thyra_MV, *(vec_A->getRCP()), b ); 
00263     }
00264 
00267     void MvScale ( ScalarType alpha ) { Teuchos::TimeMonitor timer(*_timerMvScale); MVT::MvScale( *Thyra_MV, alpha ); }
00268 
00271     void MvScale ( const std::vector<ScalarType>& alpha ) { Teuchos::TimeMonitor timer(*_timerMvScale); MVT::MvScale( *Thyra_MV, alpha ); }
00272 
00274 
00275 
00276     
00280     void MvNorm ( std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> &normvec ) const { Teuchos::TimeMonitor timer(*_timerMvNorm); MVT::MvNorm( *Thyra_MV, normvec ); }
00282     
00284 
00285 
00290     void SetBlock ( const MultiVec<ScalarType>& A, const std::vector<int>& index )
00291     {
00292       Teuchos::TimeMonitor timer(*_timerSetBlock);
00293       const Anasazi::ThyraMultiVec<ScalarType>* vec_A = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&A);
00294       MVT::SetBlock( *(vec_A->getRCP()), index, *Thyra_MV );
00295     }
00296 
00299     void MvRandom() { Teuchos::TimeMonitor timer(*_timerMvRandom); MVT::MvRandom( *Thyra_MV ); }
00300 
00303     void MvInit ( ScalarType alpha ) { Teuchos::TimeMonitor timer(*_timerMvInit);  MVT::MvInit( *Thyra_MV, alpha ); }
00304 
00306 
00307 
00309     Teuchos::RCP< Thyra::MultiVectorBase<ScalarType> > getRCP() { return Thyra_MV; }
00310 
00313     Teuchos::RCP< const Thyra::MultiVectorBase<ScalarType> > getRCP() const { return Thyra_MV; }
00314 
00317     std::vector<Teuchos::RCP<Teuchos::Time> > getTimers() const {
00318      std::vector<Teuchos::RCP<Teuchos::Time> > timers;
00319      timers.push_back( _timerCreate ); 
00320      timers.push_back( _timerClone ); 
00321      timers.push_back( _timerDestroy ); 
00322      timers.push_back( _timerMvTimesMatAddMv ); 
00323      timers.push_back( _timerMvTransMv ); 
00324      timers.push_back( _timerMvAddMv ); 
00325      timers.push_back( _timerMvDot ); 
00326      timers.push_back( _timerMvNorm ); 
00327      timers.push_back( _timerMvScale ); 
00328      timers.push_back( _timerSetBlock ); 
00329      timers.push_back( _timerMvInit ); 
00330      timers.push_back( _timerMvRandom ); 
00331  
00332      return timers;
00333    }
00334 
00337    void copyTimers( std::vector<Teuchos::RCP<Teuchos::Time> >& timers ) {
00338      _timerCreate = timers[0];
00339      _timerClone = timers[1];
00340      _timerDestroy = timers[2];
00341      _timerMvTimesMatAddMv = timers[3];
00342      _timerMvTransMv = timers[4];
00343      _timerMvAddMv = timers[5];
00344      _timerMvDot = timers[6];
00345      _timerMvNorm = timers[7];
00346      _timerMvScale = timers[8];
00347      _timerSetBlock = timers[9];
00348      _timerMvInit = timers[10];
00349      _timerMvRandom = timers[11];
00350    } 
00352 
00354 
00355 
00356 
00358     void MvPrint( std::ostream& os ) const { MVT::MvPrint( *Thyra_MV, os ); }
00360 
00361   private:
00362 
00363     Teuchos::RCP<Thyra::MultiVectorBase<ScalarType> > Thyra_MV;
00364     Teuchos::RCP<Teuchos::Time> _timerCreate, _timerClone, _timerDestroy;
00365     Teuchos::RCP<Teuchos::Time> _timerMvTimesMatAddMv, _timerMvTransMv, _timerMvAddMv, _timerMvDot;
00366     Teuchos::RCP<Teuchos::Time> _timerMvNorm, _timerMvScale, _timerSetBlock, _timerMvInit, _timerMvRandom;
00367   };
00368   //-------------------------------------------------------------
00369   
00371   //
00372   //--------template class AnasaziThyraOp---------------------
00373   //
00375   
00382   template<class ScalarType>
00383   class ThyraOp : public virtual Operator<ScalarType> {
00384   public:
00385 
00386     typedef OperatorTraits<ScalarType,Thyra::MultiVectorBase<ScalarType>,Thyra::LinearOpBase<ScalarType> > OPT;
00387 
00389 
00390     
00392     ThyraOp(const Teuchos::RCP<const Thyra::LinearOpBase<ScalarType> > &Op ) { Thyra_Op = Op; }
00393     
00395     ~ThyraOp() {}
00397     
00399 
00400     
00404     void Apply ( const MultiVec<ScalarType>& X, MultiVec<ScalarType>& Y ) const 
00405     {
00406       const Anasazi::ThyraMultiVec<ScalarType>* vec_X = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&X);
00407       Anasazi::ThyraMultiVec<ScalarType>* vec_Y = dynamic_cast<Anasazi::ThyraMultiVec<ScalarType>* >(&Y);
00408       OPT::Apply( *Thyra_Op, *(vec_X->getRCP()), *(vec_Y->getRCP()) );
00409     }
00410     
00412     
00413   private:
00414     Teuchos::RCP<const Thyra::LinearOpBase<ScalarType> > Thyra_Op;
00415   };
00416   
00417 } // end of Anasazi namespace 
00418 
00419 #endif 
00420 // end of file ANASAZI_THYRA_DEBUG_ADAPTER_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends