Belos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BelosMultiVec.hpp
Go to the documentation of this file.
1 //@HEADER
2 // ************************************************************************
3 //
4 // Belos: Block Linear Solvers Package
5 // Copyright 2004 Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 //@HEADER
41 
42 #ifndef BELOS_MULTI_VEC_HPP
43 #define BELOS_MULTI_VEC_HPP
44 
59 
60 #include "BelosMultiVecTraits.hpp"
61 #include "BelosTypes.hpp"
62 #include "BelosConfigDefs.hpp"
63 
64 namespace Belos {
65 
90 template <class ScalarType>
91 class MultiVec {
92 public:
94 
95  MultiVec() {};
97 
99  virtual ~MultiVec () {};
100 
102 
104 
107  virtual MultiVec<ScalarType> * Clone ( const int numvecs ) const = 0;
108 
111  virtual MultiVec<ScalarType> * CloneCopy () const = 0;
112 
119  virtual MultiVec<ScalarType> * CloneCopy ( const std::vector<int>& index ) const = 0;
120 
127  virtual MultiVec<ScalarType> * CloneViewNonConst ( const std::vector<int>& index ) = 0;
128 
135  virtual const MultiVec<ScalarType> * CloneView ( const std::vector<int>& index ) const = 0;
136 
138 
140 
142  virtual ptrdiff_t GetGlobalLength () const = 0;
143 
145  virtual int GetNumberVecs () const = 0;
146 
148 
150 
152  virtual void
153  MvTimesMatAddMv (const ScalarType alpha,
154  const MultiVec<ScalarType>& A,
155  const Teuchos::SerialDenseMatrix<int,ScalarType>& B, const ScalarType beta) = 0;
156 
158  virtual void MvAddMv ( const ScalarType alpha, const MultiVec<ScalarType>& A, const ScalarType beta, const MultiVec<ScalarType>& B ) = 0;
159 
161  virtual void MvScale ( const ScalarType alpha ) = 0;
162 
164  virtual void MvScale ( const std::vector<ScalarType>& alpha ) = 0;
165 
169  virtual void MvTransMv ( const ScalarType alpha, const MultiVec<ScalarType>& A, Teuchos::SerialDenseMatrix<int,ScalarType>& B) const = 0;
170 
176  virtual void MvDot ( const MultiVec<ScalarType>& A, std::vector<ScalarType>& b ) const = 0;
177 
179 
181 
187  virtual void MvNorm ( std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>& normvec, NormType type = TwoNorm ) const = 0;
188 
190 
192 
197  virtual void SetBlock ( const MultiVec<ScalarType>& A, const std::vector<int>& index ) = 0;
198 
200  virtual void MvRandom () = 0;
201 
203  virtual void MvInit ( const ScalarType alpha ) = 0;
204 
206 
208 
210  virtual void MvPrint ( std::ostream& os ) const = 0;
212 
213 #ifdef HAVE_BELOS_TSQR
214 
216 
239  virtual void
240  factorExplicit (MultiVec<ScalarType>& Q,
242  const bool forceNonnegativeDiagonal=false)
243  {
244  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "The Belos::MultiVec<"
245  << Teuchos::TypeNameTraits<ScalarType>::name() << "> subclass which you "
246  "are using does not implement the TSQR-related method factorExplicit().");
247  }
248 
283  virtual int
286  {
287  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "The Belos::MultiVec<"
288  << Teuchos::TypeNameTraits<ScalarType>::name() << "> subclass which you "
289  "are using does not implement the TSQR-related method revealRank().");
290  }
291 
293 #endif // HAVE_BELOS_TSQR
294 };
295 
296 
297 namespace details {
315 template<class ScalarType>
317 public:
319  typedef ScalarType scalar_type;
320  typedef int ordinal_type; // This doesn't matter either
321  typedef int node_type; // Nor does this
324 
326  void
328  MV& Q,
330  const bool forceNonnegativeDiagonal=false)
331  {
332  A.factorExplicit (Q, R, forceNonnegativeDiagonal);
333  }
334 
336  int
339  const magnitude_type& tol)
340  {
341  return Q.revealRank (R, tol);
342  }
343 
345  (void) params;
346  }
347 
349  return Teuchos::parameterList ();
350  }
351 };
352 } // namespace details
353 
363  template<class ScalarType>
364  class MultiVecTraits<ScalarType,MultiVec<ScalarType> > {
365  public:
367 
368 
372  Clone (const MultiVec<ScalarType>& mv, const int numvecs) {
373  return Teuchos::rcp (const_cast<MultiVec<ScalarType>&> (mv).Clone (numvecs));
374  }
377  { return Teuchos::rcp( const_cast<MultiVec<ScalarType>&>(mv).CloneCopy() ); }
379  static Teuchos::RCP<MultiVec<ScalarType> > CloneCopy( const MultiVec<ScalarType>& mv, const std::vector<int>& index )
380  { return Teuchos::rcp( const_cast<MultiVec<ScalarType>&>(mv).CloneCopy(index) ); }
383  CloneViewNonConst (MultiVec<ScalarType>& mv, const std::vector<int>& index)
384  {
385  return Teuchos::rcp( mv.CloneViewNonConst(index) );
386  }
387 
390  {
391  // mfh 02 Mar 2013: For now, we'll just use the above index
392  // vector version of CloneViewNonConst to implement this, since
393  // that doesn't require adding to the MultiVec interface.
394  std::vector<int> indVec (index.size ());
395  for (int k = 0; k < index.size (); ++k) {
396  indVec[k] = k;
397  }
398  return CloneViewNonConst (mv, indVec);
399  }
400 
403  CloneView (const MultiVec<ScalarType>& mv, const std::vector<int>& index) {
404  return Teuchos::rcp( const_cast<MultiVec<ScalarType>&>(mv).CloneView(index) );
405  }
406 
409  {
410  // mfh 02 Mar 2013: For now, we'll just use the above index
411  // vector version of CloneView to implement this, since that
412  // doesn't require adding to the MultiVec interface.
413  std::vector<int> indVec (index.size ());
414  for (int k = 0; k < index.size (); ++k) {
415  indVec[k] = k;
416  }
417  return CloneView (mv, indVec);
418  }
419 
421  static ptrdiff_t GetGlobalLength( const MultiVec<ScalarType>& mv )
422  { return mv.GetGlobalLength(); }
424  static int GetNumberVecs( const MultiVec<ScalarType>& mv )
425  { return mv.GetNumberVecs(); }
427  static void MvTimesMatAddMv( ScalarType alpha, const MultiVec<ScalarType>& A,
429  ScalarType beta, MultiVec<ScalarType>& mv )
430  { mv.MvTimesMatAddMv(alpha, A, B, beta); }
432  static void MvAddMv( ScalarType alpha, const MultiVec<ScalarType>& A, ScalarType beta, const MultiVec<ScalarType>& B, MultiVec<ScalarType>& mv )
433  { mv.MvAddMv(alpha, A, beta, B); }
435  static void MvScale ( MultiVec<ScalarType>& mv, const ScalarType alpha )
436  { mv.MvScale( alpha ); }
437 
438  static void MvScale ( MultiVec<ScalarType>& mv, const std::vector<ScalarType>& alpha )
439  { mv.MvScale(alpha); }
441  static void MvTransMv( const ScalarType alpha, const MultiVec<ScalarType>& A, const MultiVec<ScalarType>& mv, Teuchos::SerialDenseMatrix<int,ScalarType>& B )
442  { mv.MvTransMv(alpha, A, B); }
444  static void MvDot( const MultiVec<ScalarType>& mv, const MultiVec<ScalarType>& A, std::vector<ScalarType>& b )
445  { mv.MvDot( A, b ); }
447  static void MvNorm( const MultiVec<ScalarType>& mv, std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>& normvec, NormType type = TwoNorm )
448  { mv.MvNorm(normvec,type); }
450  static void SetBlock( const MultiVec<ScalarType>& A, const std::vector<int>& index, MultiVec<ScalarType>& mv )
451  { mv.SetBlock(A, index); }
452 
453  static void
456  {
457  // mfh 02 Mar 2013: For now, we'll just use SetBlock to implement this,
458  // since that doesn't require adding to the MultiVec interface.
459  const int numVecsRhs = GetNumberVecs (A);
460  const int numVecsLhs = GetNumberVecs (mv);
461 
463  numVecsLhs != numVecsRhs, std::invalid_argument,
464  "Belos::MultiVecTraits::Assign: Input multivector A has " << numVecsRhs
465  << " columns, which differs from the number of columns " << numVecsLhs
466  << " in the output multivector mv.");
467 
468  // mfh 02 Mar 2013: It's pretty silly to build this each time.
469  // However, at least that makes the code correct.
470  std::vector<int> index (numVecsRhs);
471  for (int k = 0; k < numVecsRhs; ++k) {
472  index[k] = k;
473  }
474  SetBlock (A, index, mv);
475  }
476 
478  static void MvRandom( MultiVec<ScalarType>& mv )
479  { mv.MvRandom(); }
481  static void MvInit( MultiVec<ScalarType>& mv, ScalarType alpha = Teuchos::ScalarTraits<ScalarType>::zero() )
482  { mv.MvInit(alpha); }
484  static void MvPrint( const MultiVec<ScalarType>& mv, std::ostream& os )
485  { mv.MvPrint(os); }
486 
487 #ifdef HAVE_BELOS_TSQR
488  typedef details::MultiVecTsqrAdapter<ScalarType> tsqr_adaptor_type;
496 #endif // HAVE_BELOS_TSQR
497  };
498 
499 
500 } // namespace Belos
501 
502 #endif
503 
504 // end of file BelosMultiVec.hpp
Collection of types and exceptions used within the Belos solvers.
static void MvTimesMatAddMv(ScalarType alpha, const MultiVec< ScalarType > &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, ScalarType beta, MultiVec< ScalarType > &mv)
virtual MultiVec< ScalarType > * Clone(const int numvecs) const =0
Create a new MultiVec with numvecs columns.
virtual MultiVec< ScalarType > * CloneCopy() const =0
Create a new MultiVec and copy contents of *this into it (deep copy).
static Teuchos::RCP< const MultiVec< ScalarType > > CloneView(const MultiVec< ScalarType > &mv, const std::vector< int > &index)
static void MvTransMv(const ScalarType alpha, const MultiVec< ScalarType > &A, const MultiVec< ScalarType > &mv, Teuchos::SerialDenseMatrix< int, ScalarType > &B)
static Teuchos::RCP< MultiVec< ScalarType > > Clone(const MultiVec< ScalarType > &mv, const int numvecs)
Create a new empty MultiVec containing numvecs columns.
static void MvScale(MultiVec< ScalarType > &mv, const ScalarType alpha)
virtual int GetNumberVecs() const =0
The number of vectors (i.e., columns) in the multivector.
virtual const MultiVec< ScalarType > * 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...
Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > dense_matrix_type
MultiVec()
Default constructor.
static void Assign(const MultiVec< ScalarType > &A, MultiVec< ScalarType > &mv)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
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).
Declaration of basic traits for the multivector type.
virtual void SetBlock(const MultiVec< ScalarType > &A, const std::vector< int > &index)=0
Copy the vectors in A to a set of vectors in *this.
virtual MultiVec< ScalarType > * 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 void MvTransMv(const ScalarType alpha, const MultiVec< ScalarType > &A, Teuchos::SerialDenseMatrix< int, ScalarType > &B) const =0
Compute a dense matrix B through the matrix-matrix multiply alpha * A^T * (*this).
virtual void MvScale(const ScalarType alpha)=0
Scale each element of the vectors in *this with alpha.
virtual ptrdiff_t GetGlobalLength() const =0
The number of rows in the multivector.
virtual void MvTimesMatAddMv(const ScalarType alpha, const MultiVec< ScalarType > &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta)=0
Update *this with alpha * A * B + beta * (*this).
static Teuchos::RCP< const MultiVec< ScalarType > > CloneView(const MultiVec< ScalarType > &mv, const Teuchos::Range1D &index)
static void MvDot(const MultiVec< ScalarType > &mv, const MultiVec< ScalarType > &A, std::vector< ScalarType > &b)
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
virtual void MvNorm(std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec, NormType type=TwoNorm) const =0
Compute the norm of each vector in *this.
int revealRank(MV &Q, dense_matrix_type &R, const magnitude_type &tol)
Compute rank-revealing decomposition using results of factorExplicit().
Traits class which defines basic operations on multivectors.
static void MvRandom(MultiVec< ScalarType > &mv)
virtual void MvDot(const MultiVec< ScalarType > &A, std::vector< ScalarType > &b) const =0
Compute the dot product of each column of *this with the corresponding column of A.
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 ptrdiff_t GetGlobalLength(const MultiVec< ScalarType > &mv)
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 Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
static void MvScale(MultiVec< ScalarType > &mv, const std::vector< ScalarType > &alpha)
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
Creates a new MV and copies contents of mv into the new vector (deep copy).
TSQR adapter for MultiVec.
static Teuchos::RCP< MultiVec< ScalarType > > CloneViewNonConst(MultiVec< ScalarType > &mv, const std::vector< int > &index)
static void MvPrint(const MultiVec< ScalarType > &mv, std::ostream &os)
static void MvNorm(const MultiVec< ScalarType > &mv, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec, NormType type=TwoNorm)
static void MvAddMv(ScalarType alpha, const MultiVec< ScalarType > &A, ScalarType beta, const MultiVec< ScalarType > &B, MultiVec< ScalarType > &mv)
NormType
The type of vector norm to compute.
Definition: BelosTypes.hpp:97
void factorExplicit(MV &A, MV &Q, dense_matrix_type &R, const bool forceNonnegativeDiagonal=false)
Compute QR factorization A = QR, using TSQR.
virtual void MvInit(const ScalarType alpha)=0
Replace each element of the vectors in *this with alpha.
static int GetNumberVecs(const MultiVec< ScalarType > &mv)
Teuchos::ScalarTraits< scalar_type >::magnitudeType magnitude_type
Ordinal size() const
virtual void MvAddMv(const ScalarType alpha, const MultiVec< ScalarType > &A, const ScalarType beta, const MultiVec< ScalarType > &B)=0
Replace *this with alpha * A + beta * B.
static void MvInit(MultiVec< ScalarType > &mv, ScalarType alpha=Teuchos::ScalarTraits< ScalarType >::zero())
Interface for multivectors used by Belos&#39; linear solvers.
static Teuchos::RCP< MultiVec< ScalarType > > CloneCopy(const MultiVec< ScalarType > &mv)
virtual ~MultiVec()
Destructor (virtual for memory safety of derived classes).
virtual void MvPrint(std::ostream &os) const =0
Print *this multivector to the os output stream.
static Teuchos::RCP< MultiVec< ScalarType > > CloneViewNonConst(MultiVec< ScalarType > &mv, const Teuchos::Range1D &index)
static Teuchos::RCP< MultiVec< ScalarType > > CloneCopy(const MultiVec< ScalarType > &mv, const std::vector< int > &index)
virtual void MvRandom()=0
Fill all the vectors in *this with random numbers.
Belos header file which uses auto-configuration information to include necessary C++ headers...
static void SetBlock(const MultiVec< ScalarType > &A, const std::vector< int > &index, MultiVec< ScalarType > &mv)
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &params)