Belos Version of the Day
Loading...
Searching...
No Matches
BelosMultiVec.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Belos: Block Linear Solvers Package
4//
5// Copyright 2004-2016 NTESS and the Belos contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef BELOS_MULTI_VEC_HPP
11#define BELOS_MULTI_VEC_HPP
12
27
29#include "BelosTypes.hpp"
30#include "BelosConfigDefs.hpp"
31
32namespace Belos {
33
58template <class ScalarType, class DM = DefaultDenseMatrix<int, ScalarType>>
59class MultiVec {
60public:
62
63
65
67 virtual ~MultiVec () {};
68
70
72
75 virtual MultiVec<ScalarType,DM> * Clone ( const int numvecs ) const = 0;
76
79 virtual MultiVec<ScalarType,DM> * CloneCopy () const = 0;
80
87 virtual MultiVec<ScalarType,DM> * CloneCopy ( const std::vector<int>& index ) const = 0;
88
95 virtual MultiVec<ScalarType,DM> * CloneViewNonConst ( const std::vector<int>& index ) = 0;
96
103 virtual const MultiVec<ScalarType,DM> * CloneView ( const std::vector<int>& index ) const = 0;
104
106
108
110 virtual ptrdiff_t GetGlobalLength () const = 0;
111
113 virtual int GetNumberVecs () const = 0;
114
116
118
120 virtual void
123 const DM& B, const ScalarType beta) = 0;
124
126 virtual void MvAddMv ( const ScalarType alpha,
128 const ScalarType beta,
129 const MultiVec<ScalarType,DM>& B ) = 0;
130
132 virtual void MvScale ( const ScalarType alpha ) = 0;
133
135 virtual void MvScale ( const std::vector<ScalarType>& alpha ) = 0;
136
140 virtual void MvTransMv ( const ScalarType alpha, const MultiVec<ScalarType,DM>& A, DM& B) const = 0;
141
147 virtual void MvDot ( const MultiVec<ScalarType,DM>& A, std::vector<ScalarType>& b ) const = 0;
148
150
152
158 virtual void MvNorm ( std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>& normvec, NormType type = TwoNorm ) const = 0;
159
161
163
168 virtual void SetBlock ( const MultiVec<ScalarType,DM>& A, const std::vector<int>& index ) = 0;
169
171 virtual void MvRandom () = 0;
172
174 virtual void MvInit ( const ScalarType alpha ) = 0;
175
177
179
181 virtual void MvPrint ( std::ostream& os ) const = 0;
183
184#ifdef HAVE_BELOS_TSQR
186
187
210 virtual void
211 factorExplicit (MultiVec<ScalarType,DM>& Q,
212 DM& R,
213 const bool forceNonnegativeDiagonal=false)
214 {
215 TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "The Belos::MultiVec<"
216 << Teuchos::TypeNameTraits<ScalarType>::name() << "> subclass which you "
217 "are using does not implement the TSQR-related method factorExplicit().");
218 }
219
254 virtual int
255 revealRank (DM& R,
256 const typename Teuchos::ScalarTraits<ScalarType>::magnitudeType& tol)
257 {
258 TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "The Belos::MultiVec<"
259 << Teuchos::TypeNameTraits<ScalarType>::name() << "> subclass which you "
260 "are using does not implement the TSQR-related method revealRank().");
261 }
262
264#endif // HAVE_BELOS_TSQR
265};
266
267
268namespace details {
286template<class ScalarType, class DM = DefaultDenseMatrix<int, ScalarType>>
288public:
291 typedef int ordinal_type; // This doesn't matter either
292 typedef int node_type; // Nor does this
293 typedef typename Teuchos::ScalarTraits<scalar_type>::magnitudeType magnitude_type;
294
296 void
298 MV& Q,
299 DM& R,
300 const bool forceNonnegativeDiagonal=false)
301 {
302 A.factorExplicit (Q, R, forceNonnegativeDiagonal);
303 }
304
306 int
308 DM& R,
309 const magnitude_type& tol)
310 {
311 return Q.revealRank (R, tol);
312 }
313
314 void setParameterList (const Teuchos::RCP<Teuchos::ParameterList>& params) {
315 (void) params;
316 }
317
318 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters () const {
319 return Teuchos::parameterList ();
320 }
321};
322} // namespace details
323
333 template<class ScalarType, class DM>
335 public:
337
338
341 static Teuchos::RCP<MultiVec<ScalarType,DM> >
343 return Teuchos::rcp (const_cast<MultiVec<ScalarType,DM>&> (mv).Clone (numvecs));
344 }
346 static Teuchos::RCP<MultiVec<ScalarType,DM> > CloneCopy( const MultiVec<ScalarType,DM>& mv )
347 { return Teuchos::rcp( const_cast<MultiVec<ScalarType,DM>&>(mv).CloneCopy() ); }
349 static Teuchos::RCP<MultiVec<ScalarType,DM> > CloneCopy( const MultiVec<ScalarType,DM>& mv, const std::vector<int>& index )
350 { return Teuchos::rcp( const_cast<MultiVec<ScalarType,DM>&>(mv).CloneCopy(index) ); }
352 static Teuchos::RCP<MultiVec<ScalarType,DM> >
353 CloneViewNonConst (MultiVec<ScalarType,DM>& mv, const std::vector<int>& index)
354 {
355 return Teuchos::rcp( mv.CloneViewNonConst(index) );
356 }
357
358 static Teuchos::RCP<MultiVec<ScalarType,DM> >
359 CloneViewNonConst (MultiVec<ScalarType,DM>& mv, const Teuchos::Range1D& index)
360 {
361 // mfh 02 Mar 2013: For now, we'll just use the above index
362 // vector version of CloneViewNonConst to implement this, since
363 // that doesn't require adding to the MultiVec interface.
364 std::vector<int> indVec (index.size ());
365 for (int k = 0; k < index.size (); ++k) {
366 indVec[k] = k;
367 }
368 return CloneViewNonConst (mv, indVec);
369 }
370
372 static Teuchos::RCP<const MultiVec<ScalarType,DM> >
373 CloneView (const MultiVec<ScalarType,DM>& mv, const std::vector<int>& index) {
374 return Teuchos::rcp( const_cast<MultiVec<ScalarType,DM>&>(mv).CloneView(index) );
375 }
376
377 static Teuchos::RCP<const MultiVec<ScalarType,DM> >
378 CloneView (const MultiVec<ScalarType,DM>& mv, const Teuchos::Range1D& index)
379 {
380 // mfh 02 Mar 2013: For now, we'll just use the above index
381 // vector version of CloneView to implement this, since that
382 // doesn't require adding to the MultiVec interface.
383 std::vector<int> indVec (index.size ());
384 for (int k = 0; k < index.size (); ++k) {
385 indVec[k] = k;
386 }
387 return CloneView (mv, indVec);
388 }
389
392 { return mv.GetGlobalLength(); }
395 { return mv.GetNumberVecs(); }
398 const DM& B,
400 { mv.MvTimesMatAddMv(alpha, A, B, beta); }
403 { mv.MvAddMv(alpha, A, beta, B); }
405 static void MvScale ( MultiVec<ScalarType,DM>& mv, const ScalarType alpha )
406 { mv.MvScale( alpha ); }
407
408 static void MvScale ( MultiVec<ScalarType,DM>& mv, const std::vector<ScalarType>& alpha )
409 { mv.MvScale(alpha); }
411 static void MvTransMv( const ScalarType alpha, const MultiVec<ScalarType,DM>& A, const MultiVec<ScalarType,DM>& mv, DM& B )
412 { mv.MvTransMv(alpha, A, B); }
414 static void MvDot( const MultiVec<ScalarType,DM>& mv, const MultiVec<ScalarType,DM>& A, std::vector<ScalarType>& b )
415 { mv.MvDot( A, b ); }
417 static void MvNorm( const MultiVec<ScalarType,DM>& mv, std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>& normvec, NormType type = TwoNorm )
418 { mv.MvNorm(normvec,type); }
420 static void SetBlock( const MultiVec<ScalarType,DM>& A, const std::vector<int>& index, MultiVec<ScalarType,DM>& mv )
421 { mv.SetBlock(A, index); }
422
423 static void
426 {
427 // mfh 02 Mar 2013: For now, we'll just use SetBlock to implement this,
428 // since that doesn't require adding to the MultiVec interface.
429 const int numVecsRhs = GetNumberVecs (A);
430 const int numVecsLhs = GetNumberVecs (mv);
431
433 numVecsLhs != numVecsRhs, std::invalid_argument,
434 "Belos::MultiVecTraits::Assign: Input multivector A has " << numVecsRhs
435 << " columns, which differs from the number of columns " << numVecsLhs
436 << " in the output multivector mv.");
437
438 // mfh 02 Mar 2013: It's pretty silly to build this each time.
439 // However, at least that makes the code correct.
440 std::vector<int> index (numVecsRhs);
441 for (int k = 0; k < numVecsRhs; ++k) {
442 index[k] = k;
443 }
444 SetBlock (A, index, mv);
445 }
446
449 { mv.MvRandom(); }
451 static void MvInit( MultiVec<ScalarType, DM>& mv, ScalarType alpha = Teuchos::ScalarTraits<ScalarType>::zero() )
452 { mv.MvInit(alpha); }
454 static void MvPrint( const MultiVec<ScalarType, DM>& mv, std::ostream& os )
455 { mv.MvPrint(os); }
456
457#ifdef HAVE_BELOS_TSQR
465 typedef details::MultiVecTsqrAdapter<ScalarType,DM> tsqr_adaptor_type;
466#endif // HAVE_BELOS_TSQR
467 };
468
469
470} // namespace Belos
471
472#endif
473
474// end of file BelosMultiVec.hpp
Belos header file which uses auto-configuration information to include necessary C++ headers.
Declaration of basic traits for the multivector type.
Collection of types and exceptions used within the Belos solvers.
Interface for multivectors used by Belos' linear solvers.
virtual ~MultiVec()
Destructor (virtual for memory safety of derived classes).
virtual MultiVec< ScalarType, DM > * CloneCopy() const =0
Create a new MultiVec and copy contents of *this into it (deep copy).
virtual void MvInit(const ScalarType alpha)=0
Replace each element of the vectors in *this with alpha.
virtual MultiVec< ScalarType, DM > * Clone(const int numvecs) const =0
Create a new MultiVec with numvecs columns.
virtual void MvPrint(std::ostream &os) const =0
Print *this multivector to the os output stream.
virtual void MvDot(const MultiVec< ScalarType, DM > &A, std::vector< ScalarType > &b) const =0
Compute the dot product of each column of *this with the corresponding column of A.
virtual void MvNorm(std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec, NormType type=TwoNorm) const =0
Compute the norm of each vector in *this.
virtual void MvTransMv(const ScalarType alpha, const MultiVec< ScalarType, DM > &A, DM &B) const =0
Compute a dense matrix B through the matrix-matrix multiply alpha * A^T * (*this).
virtual const MultiVec< ScalarType, DM > * CloneView(const std::vector< int > &index) const =0
Creates a new Belos::MultiVec that shares the selected contents of *this. The index of the numvecs ve...
virtual ptrdiff_t GetGlobalLength() const =0
The number of rows in the multivector.
MultiVec()
Default constructor.
virtual void MvTimesMatAddMv(const ScalarType alpha, const MultiVec< ScalarType, DM > &A, const DM &B, const ScalarType beta)=0
Update *this with alpha * A * B + beta * (*this).
virtual void MvRandom()=0
Fill all the vectors in *this with random numbers.
virtual MultiVec< ScalarType, DM > * CloneViewNonConst(const std::vector< int > &index)=0
Creates a new Belos::MultiVec that shares the selected contents of *this. The index of the numvecs ve...
virtual int GetNumberVecs() const =0
The number of vectors (i.e., columns) in the multivector.
virtual MultiVec< ScalarType, DM > * CloneCopy(const std::vector< int > &index) const =0
Creates a new Belos::MultiVec and copies the selected contents of *this into the new multivector (dee...
virtual void MvScale(const ScalarType alpha)=0
Scale each element of the vectors in *this with alpha.
virtual void MvAddMv(const ScalarType alpha, const MultiVec< ScalarType, DM > &A, const ScalarType beta, const MultiVec< ScalarType, DM > &B)=0
Replace *this with alpha * A + beta * B.
virtual void SetBlock(const MultiVec< ScalarType, DM > &A, const std::vector< int > &index)=0
Copy the vectors in A to a set of vectors in *this.
virtual void MvScale(const std::vector< ScalarType > &alpha)=0
Scale each element of the i-th vector in *this with alpha[i].
static void SetBlock(const MultiVec< ScalarType, DM > &A, const std::vector< int > &index, MultiVec< ScalarType, DM > &mv)
static Teuchos::RCP< const MultiVec< ScalarType, DM > > CloneView(const MultiVec< ScalarType, DM > &mv, const Teuchos::Range1D &index)
static Teuchos::RCP< MultiVec< ScalarType, DM > > CloneCopy(const MultiVec< ScalarType, DM > &mv, const std::vector< int > &index)
static void Assign(const MultiVec< ScalarType, DM > &A, MultiVec< ScalarType, DM > &mv)
static void MvAddMv(ScalarType alpha, const MultiVec< ScalarType, DM > &A, ScalarType beta, const MultiVec< ScalarType, DM > &B, MultiVec< ScalarType, DM > &mv)
static Teuchos::RCP< MultiVec< ScalarType, DM > > CloneCopy(const MultiVec< ScalarType, DM > &mv)
static Teuchos::RCP< MultiVec< ScalarType, DM > > Clone(const MultiVec< ScalarType, DM > &mv, const int numvecs)
Create a new empty MultiVec containing numvecs columns.
static void MvScale(MultiVec< ScalarType, DM > &mv, const std::vector< ScalarType > &alpha)
static void MvInit(MultiVec< ScalarType, DM > &mv, ScalarType alpha=Teuchos::ScalarTraits< ScalarType >::zero())
static void MvDot(const MultiVec< ScalarType, DM > &mv, const MultiVec< ScalarType, DM > &A, std::vector< ScalarType > &b)
static Teuchos::RCP< const MultiVec< ScalarType, DM > > CloneView(const MultiVec< ScalarType, DM > &mv, const std::vector< int > &index)
static void MvNorm(const MultiVec< ScalarType, DM > &mv, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec, NormType type=TwoNorm)
static int GetNumberVecs(const MultiVec< ScalarType, DM > &mv)
static Teuchos::RCP< MultiVec< ScalarType, DM > > CloneViewNonConst(MultiVec< ScalarType, DM > &mv, const std::vector< int > &index)
static void MvPrint(const MultiVec< ScalarType, DM > &mv, std::ostream &os)
static ptrdiff_t GetGlobalLength(const MultiVec< ScalarType, DM > &mv)
static void MvTimesMatAddMv(ScalarType alpha, const MultiVec< ScalarType, DM > &A, const DM &B, ScalarType beta, MultiVec< ScalarType, DM > &mv)
static void MvTransMv(const ScalarType alpha, const MultiVec< ScalarType, DM > &A, const MultiVec< ScalarType, DM > &mv, DM &B)
static void MvScale(MultiVec< ScalarType, DM > &mv, const ScalarType alpha)
static Teuchos::RCP< MultiVec< ScalarType, DM > > CloneViewNonConst(MultiVec< ScalarType, DM > &mv, const Teuchos::Range1D &index)
Traits class which defines basic operations on multivectors.
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
Creates a new MV and copies contents of mv into the new vector (deep copy).
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
static Teuchos::RCP< const MV > CloneView(const MV &mv, const std::vector< int > &index)
Creates a new const MV that shares the selected contents of mv (shallow copy).
static void SetBlock(const MV &A, const std::vector< int > &index, MV &mv)
Copy the vectors in A to a set of vectors in mv indicated by the indices given in index.
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
Creates a new MV that shares the selected contents of mv (shallow copy).
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
Alternative run-time polymorphic interface for operators.
void factorExplicit(MV &A, MV &Q, DM &R, const bool forceNonnegativeDiagonal=false)
Compute QR factorization A = QR, using TSQR.
MultiVec< ScalarType, DM > MV
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
int revealRank(MV &Q, DM &R, const magnitude_type &tol)
Compute rank-revealing decomposition using results of factorExplicit().
Teuchos::ScalarTraits< scalar_type >::magnitudeType magnitude_type
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &params)
NormType
The type of vector norm to compute.
Namespace containing implementation details of Belos solvers.

Generated for Belos by doxygen 1.9.8