Debian Codegraph @

packages/anasazi/thyra/src/AnasaziThyraDebugAdapter.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Anasazi: Block Eigensolvers Package
4 //
5 // Copyright 2004 NTESS and the Anasazi contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 /*! \file AnasaziThyraDebugAdapter.hpp
11 \brief Declarations of Anasazi multi-vector and operator classes using Thyra_MultiVectorBase and Thyra_LinearOpBase classes
12 */
13
14 #ifndef ANASAZI_THYRA_DEBUG_ADAPTER_HPP
15 #define ANASAZI_THYRA_DEBUG_ADAPTER_HPP
16
17 #include "AnasaziConfigDefs.hpp"
18 #include "AnasaziTypes.hpp"
19 #include "AnasaziMultiVec.hpp"
20 #include "AnasaziOperator.hpp"
21
22 #include "AnasaziThyraAdapter.hpp"
23 #include <Thyra_DetachedMultiVectorView.hpp>
24 #include <Thyra_MultiVectorBase.hpp>
25 #include <Thyra_MultiVectorStdOps.hpp>
26
27 #include "Teuchos_Assert.hpp"
28 #include "Teuchos_SerialDenseMatrix.hpp"
29 #include "Teuchos_RCP.hpp"
30 #include "Teuchos_TimeMonitor.hpp"
31
32 namespace Anasazi {
33
34 //@}
35
36 ///////////////////////////////////////////////////////////////
37 //
38 //--------template class AnasaziThyraMultiVec-----------------
39 //
40 ///////////////////////////////////////////////////////////////
41
42 /*!
43 \brief Basic adapter class for Anasazi::MultiVec that uses Thyra::MultiVectorBase<ScalarType>.
44
45 \note This adapter is only to be used for debugging purposes. For production use Anasazi::MultiVecTraits templated
46 on Thyra::MultiVectorBase<> and Thyra::LinearOpBase<>.
47
48 */
49 template<class ScalarType>
50 class ThyraMultiVec : public MultiVec<ScalarType> {
51 public:
52
53 typedef MultiVecTraits<ScalarType,Thyra::MultiVectorBase<ScalarType> > MVT;
54 typedef Teuchos::ScalarTraits<ScalarType> SCT;
55 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
56 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
57
58 //! @name Constructors/Destructors
59 //@{
60
61 //! Basic ThyraMultiVec constructor (wraps Thyra::MultiVectorBase<> object).
62 /*! @param mv [in] a reference-counted pointer to a Thyra::MultiVectorBase<> object.
63
64 \returns Pointer to a ThyraMultiVec object.
65 */
66 ThyraMultiVec( const Teuchos::RCP<Thyra::MultiVectorBase< ScalarType > > & mv ) :
67 _timerCreate(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::create")),
68 _timerClone(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::clone")),
69 _timerDestroy(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::destroy")),
70 _timerMvTimesMatAddMv(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvtimesmataddmv")),
71 _timerMvTransMv(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvtransmv")),
72 _timerMvAddMv(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvaddmv")),
73 _timerMvDot(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvdot")),
74 _timerMvNorm(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvnorm")),
75 _timerMvScale(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvscale")),
76 _timerSetBlock(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::setblock")),
77 _timerMvInit(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvinit")),
78 _timerMvRandom(Teuchos::TimeMonitor::getNewTimer("ThyraMultiVec::mvrandom"))
79 {
80 Teuchos::TimeMonitor timer(*_timerCreate);
81 Thyra_MV = mv;
82 }
83
84 //! Basic ThyraMultiVec constructor (wraps Thyra::MultiVectorBase<> object).
85 /*! @param mv [in] a reference-counted pointer to a Thyra::MultiVectorBase<> object.
86 @param timers [in] a vector containing timers for this wrapper to use.
87
88 \returns Pointer to a ThyraMultiVec object.
89 */
90 ThyraMultiVec( const Teuchos::RCP<Thyra::MultiVectorBase< ScalarType > > & mv, std::vector<Teuchos::RCP<Teuchos::Time> >& timers )
91 {
92 copyTimers( timers );
93 Teuchos::TimeMonitor timer(*_timerCreate);
94 Thyra_MV = mv;
95 }
96
97 //! Copy constructor.
98 /*! @param mv [in] a ThyraMultiVec object.
99
100 \returns Pointer to a ThyraMultiVec object, where the underlying Thyra::MultiVectorBase<> object has been deep copied.
101 */
102 ThyraMultiVec( const ThyraMultiVec<ScalarType> & mv )
103 {
104 copyTimers( mv.getTimers() );
105 Teuchos::TimeMonitor timer(*_timerCreate);
106 Thyra_MV = MVT::CloneCopy( *(mv.getRCP()) );
107 }
108
109 //! Destructor
110 virtual ~ThyraMultiVec() { Teuchos::TimeMonitor timer(*_timerDestroy); }
111
112 //@}
113
114 //! @name Creation methods
115 //@{
116
117 /*! \brief Creates a new empty ThyraMultiVec containing \c numvecs columns.
118
119 \returns Pointer to an ThyraMultiVec
120 */
121 MultiVec<ScalarType> * Clone ( const int numvecs ) const
122 {
123 Teuchos::TimeMonitor timer(*_timerClone);
124 std::vector<Teuchos::RCP<Teuchos::Time> > myTimers = getTimers();
125 return new ThyraMultiVec<ScalarType>( MVT::Clone( *Thyra_MV, numvecs ), myTimers ); }
126
127 /*! \brief Creates a new ThyraMultiVec and copies contents of \c *this into
128 the new vector (deep copy).
129
130 \returns Pointer to an ThyraMultiVec
131 */
132 MultiVec<ScalarType> * CloneCopy () const
133 {
134 Teuchos::TimeMonitor timer(*_timerClone);
135 std::vector<Teuchos::RCP<Teuchos::Time> > myTimers = getTimers();
136 return new ThyraMultiVec<ScalarType>( MVT::CloneCopy( *Thyra_MV ), myTimers );
137 }
138
139 /*! \brief Creates a new ThyraMultiVec and copies the selected contents of \c *this
140 into the new vector (deep copy).
141
142 The copied vectors from \c *this are indicated by the \c index.size() indices in \c index.
143
144 \returns Pointer to an ThyraMultiVec
145 */
146 MultiVec<ScalarType> * CloneCopy ( const std::vector<int>& index ) const
147 {
148 Teuchos::TimeMonitor timer(*_timerClone);
149 std::vector<Teuchos::RCP<Teuchos::Time> > myTimers = getTimers();
150 return new ThyraMultiVec<ScalarType>( MVT::CloneCopy( *Thyra_MV, index ), myTimers );
151 }
152
153 /*! \brief Creates a new ThyraMultiVec that shares the selected contents of \c *this.
154
155 The index of the \c numvecs vectors shallow copied from \c *this are indicated by the
156 indices given in \c index.
157
158 \returns Pointer to an ThyraMultiVec
159 */
160 MultiVec<ScalarType> * CloneViewNonConst ( const std::vector<int>& index )
161 {
162 Teuchos::TimeMonitor timer(*_timerClone);
163 std::vector<Teuchos::RCP<Teuchos::Time> > myTimers = getTimers();
164 return new ThyraMultiVec<ScalarType>( MVT::CloneViewNonConst( *Thyra_MV, index ), myTimers );
165 }
166
167 /*! \brief Creates a new ThyraMultiVec that shares the selected contents of \c *this.
168
169 The index of the \c numvecs vectors shallow copied from \c *this are indicated by the
170 indices given in \c index.
171
172 \returns Pointer to an ThyraMultiVec
173 */
174 const MultiVec<ScalarType> * CloneView ( const std::vector<int>& index ) const
175 {
176 Teuchos::TimeMonitor timer(*_timerClone);
177 std::vector<Teuchos::RCP<Teuchos::Time> > myTimers = getTimers();
178 Teuchos::RCP<Thyra::MultiVectorBase<ScalarType> > nonconst_ptr_to_const_view = Teuchos::rcp_const_cast<Thyra::MultiVectorBase<ScalarType> >( MVT::CloneView(*Thyra_MV,index) );
179 const MultiVec<ScalarType> * const_ret = new ThyraMultiVec<ScalarType>( nonconst_ptr_to_const_view, myTimers );
180 return const_ret;
181 }
182
183 //@}
184
185 //! @name Attribute methods
186 //@{
187
188 //! Obtain the vector length of *this.
189 int GetNumberVecs () const { return MVT::GetNumberVecs( *Thyra_MV ); }
190
191 //! Obtain the number of vectors in *this.
192 ptrdiff_t GetGlobalLength () const { return MVT::GetGlobalLength( *Thyra_MV ); }
193
194 //@}
195
196 //! @name Update methods
197 //@{
198 /*! \brief Update \c *this with \f$\alpha AB + \beta (*this)\f$.
199 */
200 void MvTimesMatAddMv ( ScalarType alpha, const MultiVec<ScalarType>& A,
201 const Teuchos::SerialDenseMatrix<int,ScalarType>& B,
202 ScalarType beta )
203 {
204 Teuchos::TimeMonitor timer(*_timerMvTimesMatAddMv);
205 const Anasazi::ThyraMultiVec<ScalarType>* vec_A = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&A);
206 MVT::MvTimesMatAddMv( alpha, *(vec_A->getRCP()), B, beta, *Thyra_MV );
207 }
208
209 /*! \brief Replace \c *this with \f$\alpha A + \beta B\f$.
210 */
211 void MvAddMv ( ScalarType alpha, const MultiVec<ScalarType>& A,
212 ScalarType beta, const MultiVec<ScalarType>& B)
213 {
214 Teuchos::TimeMonitor timer(*_timerMvAddMv);
215 const Anasazi::ThyraMultiVec<ScalarType>* vec_A = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&A);
216 const Anasazi::ThyraMultiVec<ScalarType>* vec_B = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&B);
217 MVT::MvAddMv( alpha, *(vec_A->getRCP()), beta, *(vec_B->getRCP()), *Thyra_MV );
218 }
219
220 /*! \brief Compute a dense matrix \c B through the matrix-matrix multiply \f$\alpha A^T(*this)\f$.
221 */
222 void MvTransMv ( ScalarType alpha, const MultiVec<ScalarType>& A, Teuchos::SerialDenseMatrix<int,ScalarType>& B
223 #ifdef HAVE_ANASAZI_EXPERIMENTAL
224 , ConjType conj = Anasazi::CONJ
225 #endif
226 ) const
227 {
228 Teuchos::TimeMonitor timer(*_timerMvTransMv);
229 const Anasazi::ThyraMultiVec<ScalarType>* vec_A = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&A);
230 MVT::MvTransMv( alpha, *(vec_A->getRCP()), *Thyra_MV, B );
231 }
232
233 /*! \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.
234 */
235 void MvDot ( const MultiVec<ScalarType>& A, std::vector<ScalarType> &b
236 #ifdef HAVE_ANASAZI_EXPERIMENTAL
237 , ConjType conj = Anasazi::CONJ
238 #endif
239 ) const
240 {
241 Teuchos::TimeMonitor timer(*_timerMvDot);
242 const Anasazi::ThyraMultiVec<ScalarType>* vec_A = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&A);
243 MVT::MvDot( *Thyra_MV, *(vec_A->getRCP()), b );
244 }
245
246 /*! \brief Scale each element of the vectors in \c *this with \c alpha.
247 */
248 void MvScale ( ScalarType alpha ) { Teuchos::TimeMonitor timer(*_timerMvScale); MVT::MvScale( *Thyra_MV, alpha ); }
249
250 /*! \brief Scale each element of the \c i-th vector in \c *this with \c alpha[i].
251 */
252 void MvScale ( const std::vector<ScalarType>& alpha ) { Teuchos::TimeMonitor timer(*_timerMvScale); MVT::MvScale( *Thyra_MV, alpha ); }
253
254 //@}
255 //! @name Norm method
256 //@{
257
258 /*! \brief Compute the 2-norm of each individual vector of \c *this.
259 Upon return, \c normvec[i] holds the 2-norm of the \c i-th vector of \c *this
260 */
261 void MvNorm ( std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> &normvec ) const { Teuchos::TimeMonitor timer(*_timerMvNorm); MVT::MvNorm( *Thyra_MV, normvec ); }
262 //@}
263
264 //! @name Initialization methods
265 //@{
266 /*! \brief Copy the vectors in \c A to a set of vectors in \c *this.
267
268 The \c numvecs vectors in \c A are copied to a subset of vectors in \c *this
269 indicated by the indices given in \c index.
270 */
271 void SetBlock ( const MultiVec<ScalarType>& A, const std::vector<int>& index )
272 {
273 Teuchos::TimeMonitor timer(*_timerSetBlock);
274 const Anasazi::ThyraMultiVec<ScalarType>* vec_A = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&A);
275 MVT::SetBlock( *(vec_A->getRCP()), index, *Thyra_MV );
276 }
277
278 /*! \brief Fill the vectors in \c *this with random numbers.
279 */
280 void MvRandom() { Teuchos::TimeMonitor timer(*_timerMvRandom); MVT::MvRandom( *Thyra_MV ); }
281
282 /*! \brief Replace each element of the vectors in \c *this with \c alpha.
283 */
284 void MvInit ( ScalarType alpha ) { Teuchos::TimeMonitor timer(*_timerMvInit); MVT::MvInit( *Thyra_MV, alpha ); }
285
286 //!{ @name Accessor methods
287 //@{
288 /*! \brief Return the reference-counted pointer held by this object
289 */
290 Teuchos::RCP< Thyra::MultiVectorBase<ScalarType> > getRCP() { return Thyra_MV; }
291
292 /*! \brief Return the const reference-counted pointer held by this object
293 */
294 Teuchos::RCP< const Thyra::MultiVectorBase<ScalarType> > getRCP() const { return Thyra_MV; }
295
296 /*! \brief Return a std::vector<> of timers held by this object
297 */
298 std::vector<Teuchos::RCP<Teuchos::Time> > getTimers() const {
299 std::vector<Teuchos::RCP<Teuchos::Time> > timers;
300 timers.push_back( _timerCreate );
301 timers.push_back( _timerClone );
302 timers.push_back( _timerDestroy );
303 timers.push_back( _timerMvTimesMatAddMv );
304 timers.push_back( _timerMvTransMv );
305 timers.push_back( _timerMvAddMv );
306 timers.push_back( _timerMvDot );
307 timers.push_back( _timerMvNorm );
308 timers.push_back( _timerMvScale );
309 timers.push_back( _timerSetBlock );
310 timers.push_back( _timerMvInit );
311 timers.push_back( _timerMvRandom );
312
313 return timers;
314 }
315
316 /*! \brief Copy a std::vector<> of timers into this object
317 */
318 void copyTimers( const std::vector<Teuchos::RCP<Teuchos::Time> >& timers ) {
319 _timerCreate = timers[0];
320 _timerClone = timers[1];
321 _timerDestroy = timers[2];
322 _timerMvTimesMatAddMv = timers[3];
323 _timerMvTransMv = timers[4];
324 _timerMvAddMv = timers[5];
325 _timerMvDot = timers[6];
326 _timerMvNorm = timers[7];
327 _timerMvScale = timers[8];
328 _timerSetBlock = timers[9];
329 _timerMvInit = timers[10];
330 _timerMvRandom = timers[11];
331 }
332 //@}
333
334 //@}
335 //! @name Print method
336 //@{
337 /*! \brief Print \c *this ThyraMultiVec.
338 */
339 void MvPrint( std::ostream& os ) const { MVT::MvPrint( *Thyra_MV, os ); }
340 //@}
341
342 private:
343
344 Teuchos::RCP<Thyra::MultiVectorBase<ScalarType> > Thyra_MV;
345 Teuchos::RCP<Teuchos::Time> _timerCreate, _timerClone, _timerDestroy;
346 Teuchos::RCP<Teuchos::Time> _timerMvTimesMatAddMv, _timerMvTransMv, _timerMvAddMv, _timerMvDot;
347 Teuchos::RCP<Teuchos::Time> _timerMvNorm, _timerMvScale, _timerSetBlock, _timerMvInit, _timerMvRandom;
348 };
349 //-------------------------------------------------------------
350
351 ///////////////////////////////////////////////////////////////
352 //
353 //--------template class AnasaziThyraOp---------------------
354 //
355 ///////////////////////////////////////////////////////////////
356
357 /*!
358 \brief Basic adapter class for Anasazi::Operator that uses Thyra_Operator.
359
360 \note The Thyra package performs double-precision arithmetic, so the use of Thyra with Anasazi will
361 only provide a double-precision eigensolver.
362 */
363 template<class ScalarType>
364 class ThyraOp : public virtual Operator<ScalarType> {
365 public:
366
367 typedef OperatorTraits<ScalarType,Thyra::MultiVectorBase<ScalarType>,Thyra::LinearOpBase<ScalarType> > OPT;
368
369 //! @name Constructor/Destructor
370 //@{
371
372 //! Basic constructor. Accepts reference-counted pointer to an Thyra_Operator.
373 ThyraOp(const Teuchos::RCP<const Thyra::LinearOpBase<ScalarType> > &Op ) { Thyra_Op = Op; }
374
375 //! Destructor
376 ~ThyraOp() {}
377 //@}
378
379 //! @name Operator application method
380 //@{
381
382 /*! \brief This method takes the Anasazi::MultiVec \c X and
383 applies the operator to it resulting in the Anasazi::MultiVec \c Y.
384 */
385 void Apply ( const MultiVec<ScalarType>& X, MultiVec<ScalarType>& Y ) const
386 {
387 const Anasazi::ThyraMultiVec<ScalarType>* vec_X = dynamic_cast<const Anasazi::ThyraMultiVec<ScalarType>* >(&X);
388 Anasazi::ThyraMultiVec<ScalarType>* vec_Y = dynamic_cast<Anasazi::ThyraMultiVec<ScalarType>* >(&Y);
389 OPT::Apply( *Thyra_Op, *(vec_X->getRCP()), *(vec_Y->getRCP()) );
390 }
391
392 //@}
393
394 private:
395 Teuchos::RCP<const Thyra::LinearOpBase<ScalarType> > Thyra_Op;
396 };
397
398 } // end of Anasazi namespace
399
400 #endif
401 // end of file ANASAZI_THYRA_DEBUG_ADAPTER_HPP