Anasazi  Version of the Day
AnasaziThyraDebugAdapter.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Anasazi: Block Eigensolvers Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
25 //
26 // ***********************************************************************
27 // @HEADER
28 
33 #ifndef ANASAZI_THYRA_DEBUG_ADAPTER_HPP
34 #define ANASAZI_THYRA_DEBUG_ADAPTER_HPP
35 
36 #include "AnasaziConfigDefs.hpp"
37 #include "AnasaziTypes.hpp"
38 #include "AnasaziMultiVec.hpp"
39 #include "AnasaziOperator.hpp"
40 
41 #include "AnasaziThyraAdapter.hpp"
42 #include <Thyra_DetachedMultiVectorView.hpp>
43 #include <Thyra_MultiVectorBase.hpp>
44 #include <Thyra_MultiVectorStdOps.hpp>
45 
46 #include "Teuchos_Assert.hpp"
47 #include "Teuchos_SerialDenseMatrix.hpp"
48 #include "Teuchos_RCP.hpp"
49 #include "Teuchos_TimeMonitor.hpp"
50 
51 namespace Anasazi {
52 
54 
56  //
57  //--------template class AnasaziThyraMultiVec-----------------
58  //
60 
68  template<class ScalarType>
69  class ThyraMultiVec : public MultiVec<ScalarType> {
70  public:
71 
73  typedef Teuchos::ScalarTraits<ScalarType> SCT;
74  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
75  typedef Teuchos::ScalarTraits<MagnitudeType> MT;
76 
78 
79 
81 
85  ThyraMultiVec( const Teuchos::RCP<Thyra::MultiVectorBase< ScalarType > > & mv ) :
86  _timerCreate(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::create")),
87  _timerClone(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::clone")),
88  _timerDestroy(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::destroy")),
89  _timerMvTimesMatAddMv(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvtimesmataddmv")),
90  _timerMvTransMv(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvtransmv")),
91  _timerMvAddMv(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvaddmv")),
92  _timerMvDot(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvdot")),
93  _timerMvNorm(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvnorm")),
94  _timerMvScale(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvscale")),
95  _timerSetBlock(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::setblock")),
96  _timerMvInit(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvinit")),
97  _timerMvRandom(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvrandom"))
98  {
99  Teuchos::TimeMonitor timer(*_timerCreate);
100  Thyra_MV = mv;
101  }
102 
104 
109  ThyraMultiVec( const Teuchos::RCP<Thyra::MultiVectorBase< ScalarType > > & mv, std::vector<Teuchos::RCP<Teuchos::Time> >& timers )
110  {
111  copyTimers( timers );
112  Teuchos::TimeMonitor timer(*_timerCreate);
113  Thyra_MV = mv;
114  }
115 
117 
122  {
123  copyTimers( mv.getTimers() );
124  Teuchos::TimeMonitor timer(*_timerCreate);
125  Thyra_MV = MVT::CloneCopy( *(mv.getRCP()) );
126  }
127 
129  virtual ~ThyraMultiVec() { Teuchos::TimeMonitor timer(*_timerDestroy); }
130 
132 
134 
135 
140  MultiVec<ScalarType> * Clone ( const int numvecs ) const
141  {
142  Teuchos::TimeMonitor timer(*_timerClone);
143  std::vector<Teuchos::RCP<Teuchos::Time> > myTimers = getTimers();
144  return new ThyraMultiVec<ScalarType>( MVT::Clone( *Thyra_MV, numvecs ), myTimers ); }
145 
152  {
153  Teuchos::TimeMonitor timer(*_timerClone);
154  std::vector<Teuchos::RCP<Teuchos::Time> > myTimers = getTimers();
155  return new ThyraMultiVec<ScalarType>( MVT::CloneCopy( *Thyra_MV ), myTimers );
156  }
157 
165  MultiVec<ScalarType> * CloneCopy ( const std::vector<int>& index ) const
166  {
167  Teuchos::TimeMonitor timer(*_timerClone);
168  std::vector<Teuchos::RCP<Teuchos::Time> > myTimers = getTimers();
169  return new ThyraMultiVec<ScalarType>( MVT::CloneCopy( *Thyra_MV, index ), myTimers );
170  }
171 
179  MultiVec<ScalarType> * CloneViewNonConst ( const std::vector<int>& index )
180  {
181  Teuchos::TimeMonitor timer(*_timerClone);
182  std::vector<Teuchos::RCP<Teuchos::Time> > myTimers = getTimers();
183  return new ThyraMultiVec<ScalarType>( MVT::CloneViewNonConst( *Thyra_MV, index ), myTimers );
184  }
185 
193  const MultiVec<ScalarType> * CloneView ( const std::vector<int>& index ) const
194  {
195  Teuchos::TimeMonitor timer(*_timerClone);
196  std::vector<Teuchos::RCP<Teuchos::Time> > myTimers = getTimers();
197  Teuchos::RCP<Thyra::MultiVectorBase<ScalarType> > nonconst_ptr_to_const_view = Teuchos::rcp_const_cast<Thyra::MultiVectorBase<ScalarType> >( MVT::CloneView(*Thyra_MV,index) );
198  const MultiVec<ScalarType> * const_ret = new ThyraMultiVec<ScalarType>( nonconst_ptr_to_const_view, myTimers );
199  return const_ret;
200  }
201 
203 
205 
206 
208  int GetNumberVecs () const { return MVT::GetNumberVecs( *Thyra_MV ); }
209 
211  ptrdiff_t GetGlobalLength () const { return MVT::GetGlobalLength( *Thyra_MV ); }
212 
214 
216 
217 
219  void MvTimesMatAddMv ( ScalarType alpha, const MultiVec<ScalarType>& A,
220  const Teuchos::SerialDenseMatrix<int,ScalarType>& B,
221  ScalarType beta )
222  {
223  Teuchos::TimeMonitor timer(*_timerMvTimesMatAddMv);
224  const Anasazi::ThyraMultiVec<ScalarType>* vec_A = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&A);
225  MVT::MvTimesMatAddMv( alpha, *(vec_A->getRCP()), B, beta, *Thyra_MV );
226  }
227 
230  void MvAddMv ( ScalarType alpha, const MultiVec<ScalarType>& A,
231  ScalarType beta, const MultiVec<ScalarType>& B)
232  {
233  Teuchos::TimeMonitor timer(*_timerMvAddMv);
234  const Anasazi::ThyraMultiVec<ScalarType>* vec_A = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&A);
235  const Anasazi::ThyraMultiVec<ScalarType>* vec_B = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&B);
236  MVT::MvAddMv( alpha, *(vec_A->getRCP()), beta, *(vec_B->getRCP()), *Thyra_MV );
237  }
238 
241  void MvTransMv ( ScalarType alpha, const MultiVec<ScalarType>& A, Teuchos::SerialDenseMatrix<int,ScalarType>& B
242 #ifdef HAVE_ANASAZI_EXPERIMENTAL
243  , ConjType conj = Anasazi::CONJ
244 #endif
245  ) const
246  {
247  Teuchos::TimeMonitor timer(*_timerMvTransMv);
248  const Anasazi::ThyraMultiVec<ScalarType>* vec_A = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&A);
249  MVT::MvTransMv( alpha, *(vec_A->getRCP()), *Thyra_MV, B );
250  }
251 
254  void MvDot ( const MultiVec<ScalarType>& A, std::vector<ScalarType> &b
255 #ifdef HAVE_ANASAZI_EXPERIMENTAL
256  , ConjType conj = Anasazi::CONJ
257 #endif
258  ) const
259  {
260  Teuchos::TimeMonitor timer(*_timerMvDot);
261  const Anasazi::ThyraMultiVec<ScalarType>* vec_A = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&A);
262  MVT::MvDot( *Thyra_MV, *(vec_A->getRCP()), b );
263  }
264 
267  void MvScale ( ScalarType alpha ) { Teuchos::TimeMonitor timer(*_timerMvScale); MVT::MvScale( *Thyra_MV, alpha ); }
268 
271  void MvScale ( const std::vector<ScalarType>& alpha ) { Teuchos::TimeMonitor timer(*_timerMvScale); MVT::MvScale( *Thyra_MV, alpha ); }
272 
274 
276 
280  void MvNorm ( std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> &normvec ) const { Teuchos::TimeMonitor timer(*_timerMvNorm); MVT::MvNorm( *Thyra_MV, normvec ); }
282 
284 
285 
290  void SetBlock ( const MultiVec<ScalarType>& A, const std::vector<int>& index )
291  {
292  Teuchos::TimeMonitor timer(*_timerSetBlock);
293  const Anasazi::ThyraMultiVec<ScalarType>* vec_A = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&A);
294  MVT::SetBlock( *(vec_A->getRCP()), index, *Thyra_MV );
295  }
296 
299  void MvRandom() { Teuchos::TimeMonitor timer(*_timerMvRandom); MVT::MvRandom( *Thyra_MV ); }
300 
303  void MvInit ( ScalarType alpha ) { Teuchos::TimeMonitor timer(*_timerMvInit); MVT::MvInit( *Thyra_MV, alpha ); }
304 
306 
307 
309  Teuchos::RCP< Thyra::MultiVectorBase<ScalarType> > getRCP() { return Thyra_MV; }
310 
313  Teuchos::RCP< const Thyra::MultiVectorBase<ScalarType> > getRCP() const { return Thyra_MV; }
314 
317  std::vector<Teuchos::RCP<Teuchos::Time> > getTimers() const {
318  std::vector<Teuchos::RCP<Teuchos::Time> > timers;
319  timers.push_back( _timerCreate );
320  timers.push_back( _timerClone );
321  timers.push_back( _timerDestroy );
322  timers.push_back( _timerMvTimesMatAddMv );
323  timers.push_back( _timerMvTransMv );
324  timers.push_back( _timerMvAddMv );
325  timers.push_back( _timerMvDot );
326  timers.push_back( _timerMvNorm );
327  timers.push_back( _timerMvScale );
328  timers.push_back( _timerSetBlock );
329  timers.push_back( _timerMvInit );
330  timers.push_back( _timerMvRandom );
331 
332  return timers;
333  }
334 
337  void copyTimers( const std::vector<Teuchos::RCP<Teuchos::Time> >& timers ) {
338  _timerCreate = timers[0];
339  _timerClone = timers[1];
340  _timerDestroy = timers[2];
341  _timerMvTimesMatAddMv = timers[3];
342  _timerMvTransMv = timers[4];
343  _timerMvAddMv = timers[5];
344  _timerMvDot = timers[6];
345  _timerMvNorm = timers[7];
346  _timerMvScale = timers[8];
347  _timerSetBlock = timers[9];
348  _timerMvInit = timers[10];
349  _timerMvRandom = timers[11];
350  }
352 
354 
356 
358  void MvPrint( std::ostream& os ) const { MVT::MvPrint( *Thyra_MV, os ); }
360 
361  private:
362 
363  Teuchos::RCP<Thyra::MultiVectorBase<ScalarType> > Thyra_MV;
364  Teuchos::RCP<Teuchos::Time> _timerCreate, _timerClone, _timerDestroy;
365  Teuchos::RCP<Teuchos::Time> _timerMvTimesMatAddMv, _timerMvTransMv, _timerMvAddMv, _timerMvDot;
366  Teuchos::RCP<Teuchos::Time> _timerMvNorm, _timerMvScale, _timerSetBlock, _timerMvInit, _timerMvRandom;
367  };
368  //-------------------------------------------------------------
369 
371  //
372  //--------template class AnasaziThyraOp---------------------
373  //
375 
382  template<class ScalarType>
383  class ThyraOp : public virtual Operator<ScalarType> {
384  public:
385 
386  typedef OperatorTraits<ScalarType,Thyra::MultiVectorBase<ScalarType>,Thyra::LinearOpBase<ScalarType> > OPT;
387 
389 
390 
392  ThyraOp(const Teuchos::RCP<const Thyra::LinearOpBase<ScalarType> > &Op ) { Thyra_Op = Op; }
393 
395  ~ThyraOp() {}
397 
399 
400 
404  void Apply ( const MultiVec<ScalarType>& X, MultiVec<ScalarType>& Y ) const
405  {
406  const Anasazi::ThyraMultiVec<ScalarType>* vec_X = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&X);
408  OPT::Apply( *Thyra_Op, *(vec_X->getRCP()), *(vec_Y->getRCP()) );
409  }
410 
412 
413  private:
414  Teuchos::RCP<const Thyra::LinearOpBase<ScalarType> > Thyra_Op;
415  };
416 
417 } // end of Anasazi namespace
418 
419 #endif
420 // end of file ANASAZI_THYRA_DEBUG_ADAPTER_HPP
static void MvNorm(const TMVB &mv, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec)
Compute the 2-norm of each individual vector of mv. Upon return, normvec[i] holds the value of ...
int GetNumberVecs() const
Obtain the vector length of *this.
const MultiVec< ScalarType > * CloneView(const std::vector< int > &index) const
Creates a new ThyraMultiVec that shares the selected contents of *this.
MultiVec< ScalarType > * CloneViewNonConst(const std::vector< int > &index)
Creates a new ThyraMultiVec that shares the selected contents of *this.
static void MvDot(const TMVB &mv, const TMVB &A, std::vector< ScalarType > &b)
Compute a vector b where the components are the individual dot-products of the i-th columns of A and ...
ThyraOp(const Teuchos::RCP< const Thyra::LinearOpBase< ScalarType > > &Op)
Basic constructor. Accepts reference-counted pointer to an Thyra_Operator.
void Apply(const MultiVec< ScalarType > &X, MultiVec< ScalarType > &Y) const
This method takes the Anasazi::MultiVec X and applies the operator to it resulting in the Anasazi::Mu...
Teuchos::RCP< Thyra::MultiVectorBase< ScalarType > > getRCP()
Return the reference-counted pointer held by this object.
ptrdiff_t GetGlobalLength() const
Obtain the number of vectors in *this.
Virtual base class which defines basic traits for the operator type.
Template specialization of Anasazi::OperatorTraits class using the Thyra::LinearOpBase virtual base c...
void MvDot(const MultiVec< ScalarType > &A, std::vector< ScalarType > &b) const
Compute a vector b where the components are the individual dot-products, i.e. where A[i] is the i-th...
void copyTimers(const std::vector< Teuchos::RCP< Teuchos::Time > > &timers)
Copy a std::vector<> of timers into this object.
static void MvTimesMatAddMv(const ScalarType alpha, const TMVB &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, TMVB &mv)
Update mv with .
void MvInit(ScalarType alpha)
Replace each element of the vectors in *this with alpha.
Template specialization of Anasazi::MultiVecTraits class using the Thyra::MultiVectorBase class...
Interface for multivectors used by Anasazi&#39; linear solvers.
void MvNorm(std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec) const
Compute the 2-norm of each individual vector of *this. Upon return, normvec[i] holds the 2-norm of th...
Namespace Anasazi contains the classes, structs, enums and utilities used by the Anasazi package...
void MvScale(const std::vector< ScalarType > &alpha)
Scale each element of the i-th vector in *this with alpha[i].
static void MvPrint(const TMVB &mv, std::ostream &os)
Print the mv multi-vector to the os output stream.
ConjType
Enumerated types used to specify conjugation arguments.
void MvRandom()
Fill the vectors in *this with random numbers.
void MvTransMv(ScalarType alpha, const MultiVec< ScalarType > &A, Teuchos::SerialDenseMatrix< int, ScalarType > &B) const
Compute a dense matrix B through the matrix-matrix multiply .
static void MvTransMv(const ScalarType alpha, const TMVB &A, const TMVB &mv, Teuchos::SerialDenseMatrix< int, ScalarType > &B)
Compute a dense matrix B through the matrix-matrix multiply .
MultiVec< ScalarType > * CloneCopy(const std::vector< int > &index) const
Creates a new ThyraMultiVec and copies the selected contents of *this into the new vector (deep copy)...
Basic adapter class for Anasazi::MultiVec that uses Thyra::MultiVectorBase<ScalarType>.
Traits class which defines basic operations on multivectors.
ThyraMultiVec(const Teuchos::RCP< Thyra::MultiVectorBase< ScalarType > > &mv, std::vector< Teuchos::RCP< Teuchos::Time > > &timers)
Basic ThyraMultiVec constructor (wraps Thyra::MultiVectorBase<> object).
Specializations of the Anasazi multi-vector and operator traits classes using Thyra base classes Line...
void MvPrint(std::ostream &os) const
Print *this ThyraMultiVec.
static void MvAddMv(const ScalarType alpha, const TMVB &A, const ScalarType beta, const TMVB &B, TMVB &mv)
Replace mv with .
Anasazi header file which uses auto-configuration information to include necessary C++ headers...
static void MvRandom(TMVB &mv)
Replace the vectors in mv with random vectors.
void MvAddMv(ScalarType alpha, const MultiVec< ScalarType > &A, ScalarType beta, const MultiVec< ScalarType > &B)
Replace *this with .
Basic adapter class for Anasazi::Operator that uses Thyra_Operator.
std::vector< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return a std::vector<> of timers held by this object.
void MvScale(ScalarType alpha)
Scale each element of the vectors in *this with alpha.
static int GetNumberVecs(const TMVB &mv)
Obtain the number of vectors in mv.
Templated virtual class for creating operators that can interface with the Anasazi::OperatorTraits cl...
MultiVec< ScalarType > * CloneCopy() const
Creates a new ThyraMultiVec and copies contents of *this into the new vector (deep copy)...
static void SetBlock(const TMVB &A, const std::vector< int > &index, TMVB &mv)
Copy the vectors in A to a set of vectors in mv indicated by the indices given in index...
static Teuchos::RCP< TMVB > CloneViewNonConst(TMVB &mv, const std::vector< int > &index)
Creates a new MultiVectorBase that shares the selected contents of mv (shallow copy).
static void MvInit(TMVB &mv, ScalarType alpha=Teuchos::ScalarTraits< ScalarType >::zero())
Replace each element of the vectors in mv with alpha.
Types and exceptions used within Anasazi solvers and interfaces.
ThyraMultiVec(const ThyraMultiVec< ScalarType > &mv)
Copy constructor.
static Teuchos::RCP< TMVB > CloneCopy(const TMVB &mv)
Creates a new MultiVectorBase and copies contents of mv into the new vector (deep copy)...
virtual ~ThyraMultiVec()
Destructor.
MultiVec< ScalarType > * Clone(const int numvecs) const
Creates a new empty ThyraMultiVec containing numvecs columns.
void SetBlock(const MultiVec< ScalarType > &A, const std::vector< int > &index)
Copy the vectors in A to a set of vectors in *this.
void MvTimesMatAddMv(ScalarType alpha, const MultiVec< ScalarType > &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, ScalarType beta)
Update *this with .
static ptrdiff_t GetGlobalLength(const TMVB &mv)
Obtain the vector length of mv.
Interface for multivectors used by Anasazi&#39;s linear solvers.
Teuchos::RCP< const Thyra::MultiVectorBase< ScalarType > > getRCP() const
Return the const reference-counted pointer held by this object.
static Teuchos::RCP< const TMVB > CloneView(const TMVB &mv, const std::vector< int > &index)
Creates a new const MultiVectorBase that shares the selected contents of mv (shallow copy)...
ThyraMultiVec(const Teuchos::RCP< Thyra::MultiVectorBase< ScalarType > > &mv)
Basic ThyraMultiVec constructor (wraps Thyra::MultiVectorBase<> object).
Anasazi&#39;s templated virtual class for constructing an operator that can interface with the OperatorTr...
static void MvScale(TMVB &mv, const ScalarType alpha)
Scale each element of the vectors in *this with alpha.
static Teuchos::RCP< TMVB > Clone(const TMVB &mv, const int numvecs)
Creates a new empty MultiVectorBase containing numvecs columns.