Belos  Version of the Day
 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 int GetVecLength () const = 0;
143 
146  virtual ptrdiff_t GetGlobalLength () const { return static_cast<ptrdiff_t>( this->GetVecLength() ); }
147 
149  virtual int GetNumberVecs () const = 0;
150 
152 
154 
156  virtual void
157  MvTimesMatAddMv (const ScalarType alpha,
158  const MultiVec<ScalarType>& A,
159  const Teuchos::SerialDenseMatrix<int,ScalarType>& B, const ScalarType beta) = 0;
160 
162  virtual void MvAddMv ( const ScalarType alpha, const MultiVec<ScalarType>& A, const ScalarType beta, const MultiVec<ScalarType>& B ) = 0;
163 
165  virtual void MvScale ( const ScalarType alpha ) = 0;
166 
168  virtual void MvScale ( const std::vector<ScalarType>& alpha ) = 0;
169 
173  virtual void MvTransMv ( const ScalarType alpha, const MultiVec<ScalarType>& A, Teuchos::SerialDenseMatrix<int,ScalarType>& B) const = 0;
174 
180  virtual void MvDot ( const MultiVec<ScalarType>& A, std::vector<ScalarType>& b ) const = 0;
181 
183 
185 
191  virtual void MvNorm ( std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>& normvec, NormType type = TwoNorm ) const = 0;
192 
194 
196 
201  virtual void SetBlock ( const MultiVec<ScalarType>& A, const std::vector<int>& index ) = 0;
202 
204  virtual void MvRandom () = 0;
205 
207  virtual void MvInit ( const ScalarType alpha ) = 0;
208 
210 
212 
214  virtual void MvPrint ( std::ostream& os ) const = 0;
216 
217 #ifdef HAVE_BELOS_TSQR
218 
220 
243  virtual void
244  factorExplicit (MultiVec<ScalarType>& Q,
246  const bool forceNonnegativeDiagonal=false)
247  {
248  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "The Belos::MultiVec<"
249  << Teuchos::TypeNameTraits<ScalarType>::name() << "> subclass which you "
250  "are using does not implement the TSQR-related method factorExplicit().");
251  }
252 
287  virtual int
290  {
291  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "The Belos::MultiVec<"
292  << Teuchos::TypeNameTraits<ScalarType>::name() << "> subclass which you "
293  "are using does not implement the TSQR-related method revealRank().");
294  }
295 
297 #endif // HAVE_BELOS_TSQR
298 };
299 
300 
301 namespace details {
319 template<class ScalarType>
321 public:
323  typedef ScalarType scalar_type;
324  typedef int ordinal_type; // This doesn't matter either
325  typedef int node_type; // Nor does this
328 
330  void
332  MV& Q,
334  const bool forceNonnegativeDiagonal=false)
335  {
336  A.factorExplicit (Q, R, forceNonnegativeDiagonal);
337  }
338 
340  int
343  const magnitude_type& tol)
344  {
345  return Q.revealRank (R, tol);
346  }
347 
349  (void) params;
350  }
351 
353  return Teuchos::parameterList ();
354  }
355 };
356 } // namespace details
357 
367  template<class ScalarType>
368  class MultiVecTraits<ScalarType,MultiVec<ScalarType> > {
369  public:
371 
372 
376  Clone (const MultiVec<ScalarType>& mv, const int numvecs) {
377  return Teuchos::rcp (const_cast<MultiVec<ScalarType>&> (mv).Clone (numvecs));
378  }
381  { return Teuchos::rcp( const_cast<MultiVec<ScalarType>&>(mv).CloneCopy() ); }
383  static Teuchos::RCP<MultiVec<ScalarType> > CloneCopy( const MultiVec<ScalarType>& mv, const std::vector<int>& index )
384  { return Teuchos::rcp( const_cast<MultiVec<ScalarType>&>(mv).CloneCopy(index) ); }
387  CloneViewNonConst (MultiVec<ScalarType>& mv, const std::vector<int>& index)
388  {
389  return Teuchos::rcp( mv.CloneViewNonConst(index) );
390  }
391 
394  {
395  // mfh 02 Mar 2013: For now, we'll just use the above index
396  // vector version of CloneViewNonConst to implement this, since
397  // that doesn't require adding to the MultiVec interface.
398  std::vector<int> indVec (index.size ());
399  for (int k = 0; k < index.size (); ++k) {
400  indVec[k] = k;
401  }
402  return CloneViewNonConst (mv, indVec);
403  }
404 
407  CloneView (const MultiVec<ScalarType>& mv, const std::vector<int>& index) {
408  return Teuchos::rcp( const_cast<MultiVec<ScalarType>&>(mv).CloneView(index) );
409  }
410 
413  {
414  // mfh 02 Mar 2013: For now, we'll just use the above index
415  // vector version of CloneView to implement this, since that
416  // doesn't require adding to the MultiVec interface.
417  std::vector<int> indVec (index.size ());
418  for (int k = 0; k < index.size (); ++k) {
419  indVec[k] = k;
420  }
421  return CloneView (mv, indVec);
422  }
423 
425  static int GetVecLength( const MultiVec<ScalarType>& mv )
426  { return mv.GetVecLength(); }
428  static int GetNumberVecs( const MultiVec<ScalarType>& mv )
429  { return mv.GetNumberVecs(); }
431  static void MvTimesMatAddMv( ScalarType alpha, const MultiVec<ScalarType>& A,
433  ScalarType beta, MultiVec<ScalarType>& mv )
434  { mv.MvTimesMatAddMv(alpha, A, B, beta); }
436  static void MvAddMv( ScalarType alpha, const MultiVec<ScalarType>& A, ScalarType beta, const MultiVec<ScalarType>& B, MultiVec<ScalarType>& mv )
437  { mv.MvAddMv(alpha, A, beta, B); }
439  static void MvScale ( MultiVec<ScalarType>& mv, const ScalarType alpha )
440  { mv.MvScale( alpha ); }
441 
442  static void MvScale ( MultiVec<ScalarType>& mv, const std::vector<ScalarType>& alpha )
443  { mv.MvScale(alpha); }
445  static void MvTransMv( const ScalarType alpha, const MultiVec<ScalarType>& A, const MultiVec<ScalarType>& mv, Teuchos::SerialDenseMatrix<int,ScalarType>& B )
446  { mv.MvTransMv(alpha, A, B); }
448  static void MvDot( const MultiVec<ScalarType>& mv, const MultiVec<ScalarType>& A, std::vector<ScalarType>& b )
449  { mv.MvDot( A, b ); }
451  static void MvNorm( const MultiVec<ScalarType>& mv, std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>& normvec, NormType type = TwoNorm )
452  { mv.MvNorm(normvec,type); }
454  static void SetBlock( const MultiVec<ScalarType>& A, const std::vector<int>& index, MultiVec<ScalarType>& mv )
455  { mv.SetBlock(A, index); }
456 
457  static void
460  {
461  // mfh 02 Mar 2013: For now, we'll just use SetBlock to implement this,
462  // since that doesn't require adding to the MultiVec interface.
463  const int numVecsRhs = GetNumberVecs (A);
464  const int numVecsLhs = GetNumberVecs (mv);
465 
467  numVecsLhs != numVecsRhs, std::invalid_argument,
468  "Belos::MultiVecTraits::Assign: Input multivector A has " << numVecsRhs
469  << " columns, which differs from the number of columns " << numVecsLhs
470  << " in the output multivector mv.");
471 
472  // mfh 02 Mar 2013: It's pretty silly to build this each time.
473  // However, at least that makes the code correct.
474  std::vector<int> index (numVecsRhs);
475  for (int k = 0; k < numVecsRhs; ++k) {
476  index[k] = k;
477  }
478  SetBlock (A, index, mv);
479  }
480 
482  static void MvRandom( MultiVec<ScalarType>& mv )
483  { mv.MvRandom(); }
485  static void MvInit( MultiVec<ScalarType>& mv, ScalarType alpha = Teuchos::ScalarTraits<ScalarType>::zero() )
486  { mv.MvInit(alpha); }
488  static void MvPrint( const MultiVec<ScalarType>& mv, std::ostream& os )
489  { mv.MvPrint(os); }
490 
491 #ifdef HAVE_BELOS_TSQR
492  typedef details::MultiVecTsqrAdapter<ScalarType> tsqr_adaptor_type;
500 #endif // HAVE_BELOS_TSQR
501  };
502 
503 
504 } // namespace Belos
505 
506 #endif
507 
508 // end of file BelosMultiVec.hpp
static int GetVecLength(const MultiVec< ScalarType > &mv)
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 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...
virtual ptrdiff_t GetGlobalLength() const
The number of rows in the multivector.
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)
virtual int GetVecLength() const =0
The number of rows in the multivector.
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)