|
Belos
Version of the Day
|
00001 //@HEADER 00002 // ************************************************************************ 00003 // 00004 // Belos: Block Linear Solvers Package 00005 // Copyright 2004 Sandia Corporation 00006 // 00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, 00008 // the U.S. Government retains certain rights in this software. 00009 // 00010 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are 00012 // met: 00013 // 00014 // 1. Redistributions of source code must retain the above copyright 00015 // notice, this list of conditions and the following disclaimer. 00016 // 00017 // 2. Redistributions in binary form must reproduce the above copyright 00018 // notice, this list of conditions and the following disclaimer in the 00019 // documentation and/or other materials provided with the distribution. 00020 // 00021 // 3. Neither the name of the Corporation nor the names of the 00022 // contributors may be used to endorse or promote products derived from 00023 // this software without specific prior written permission. 00024 // 00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY 00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE 00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 // 00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00038 // 00039 // ************************************************************************ 00040 //@HEADER 00041 00042 #ifndef BELOS_MULTI_VEC_HPP 00043 #define BELOS_MULTI_VEC_HPP 00044 00059 00060 #include "BelosMultiVecTraits.hpp" 00061 #include "BelosTypes.hpp" 00062 #include "BelosConfigDefs.hpp" 00063 00064 namespace Belos { 00065 00090 template <class ScalarType> 00091 class MultiVec { 00092 public: 00094 00095 00096 MultiVec() {}; 00097 00099 virtual ~MultiVec () {}; 00100 00102 00103 00104 00107 virtual MultiVec<ScalarType> * Clone ( const int numvecs ) const = 0; 00108 00111 virtual MultiVec<ScalarType> * CloneCopy () const = 0; 00112 00119 virtual MultiVec<ScalarType> * CloneCopy ( const std::vector<int>& index ) const = 0; 00120 00127 virtual MultiVec<ScalarType> * CloneViewNonConst ( const std::vector<int>& index ) = 0; 00128 00135 virtual const MultiVec<ScalarType> * CloneView ( const std::vector<int>& index ) const = 0; 00136 00138 00139 00140 00142 virtual int GetVecLength () const = 0; 00143 00146 virtual ptrdiff_t GetGlobalLength () const { return static_cast<ptrdiff_t>( this->GetVecLength() ); } 00147 00149 virtual int GetNumberVecs () const = 0; 00150 00152 00153 00154 00156 virtual void 00157 MvTimesMatAddMv (const ScalarType alpha, 00158 const MultiVec<ScalarType>& A, 00159 const Teuchos::SerialDenseMatrix<int,ScalarType>& B, const ScalarType beta) = 0; 00160 00162 virtual void MvAddMv ( const ScalarType alpha, const MultiVec<ScalarType>& A, const ScalarType beta, const MultiVec<ScalarType>& B ) = 0; 00163 00165 virtual void MvScale ( const ScalarType alpha ) = 0; 00166 00168 virtual void MvScale ( const std::vector<ScalarType>& alpha ) = 0; 00169 00173 virtual void MvTransMv ( const ScalarType alpha, const MultiVec<ScalarType>& A, Teuchos::SerialDenseMatrix<int,ScalarType>& B) const = 0; 00174 00180 virtual void MvDot ( const MultiVec<ScalarType>& A, std::vector<ScalarType>& b ) const = 0; 00181 00183 00184 00185 00191 virtual void MvNorm ( std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>& normvec, NormType type = TwoNorm ) const = 0; 00192 00194 00195 00196 00201 virtual void SetBlock ( const MultiVec<ScalarType>& A, const std::vector<int>& index ) = 0; 00202 00204 virtual void MvRandom () = 0; 00205 00207 virtual void MvInit ( const ScalarType alpha ) = 0; 00208 00210 00211 00212 00214 virtual void MvPrint ( std::ostream& os ) const = 0; 00216 00217 #ifdef HAVE_BELOS_TSQR 00218 00219 00220 00243 virtual void 00244 factorExplicit (MultiVec<ScalarType>& Q, 00245 Teuchos::SerialDenseMatrix<int, ScalarType>& R, 00246 const bool forceNonnegativeDiagonal=false) 00247 { 00248 TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "The Belos::MultiVec<" 00249 << Teuchos::TypeNameTraits<ScalarType>::name() << "> subclass which you " 00250 "are using does not implement the TSQR-related method factorExplicit()."); 00251 } 00252 00287 virtual int 00288 revealRank (Teuchos::SerialDenseMatrix<int, ScalarType>& R, 00289 const typename Teuchos::ScalarTraits<ScalarType>::magnitudeType& tol) 00290 { 00291 TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "The Belos::MultiVec<" 00292 << Teuchos::TypeNameTraits<ScalarType>::name() << "> subclass which you " 00293 "are using does not implement the TSQR-related method revealRank()."); 00294 } 00295 00297 #endif // HAVE_BELOS_TSQR 00298 }; 00299 00300 00301 namespace details { 00319 template<class ScalarType> 00320 class MultiVecTsqrAdapter { 00321 public: 00322 typedef MultiVec<ScalarType> MV; 00323 typedef ScalarType scalar_type; 00324 typedef int ordinal_type; // This doesn't matter either 00325 typedef int node_type; // Nor does this 00326 typedef Teuchos::SerialDenseMatrix<ordinal_type, scalar_type> dense_matrix_type; 00327 typedef typename Teuchos::ScalarTraits<scalar_type>::magnitudeType magnitude_type; 00328 00330 void 00331 factorExplicit (MV& A, 00332 MV& Q, 00333 dense_matrix_type& R, 00334 const bool forceNonnegativeDiagonal=false) 00335 { 00336 A.factorExplicit (Q, R, forceNonnegativeDiagonal); 00337 } 00338 00340 int 00341 revealRank (MV& Q, 00342 dense_matrix_type& R, 00343 const magnitude_type& tol) 00344 { 00345 return Q.revealRank (R, tol); 00346 } 00347 00348 void setParameterList (const Teuchos::RCP<Teuchos::ParameterList>& params) { 00349 (void) params; 00350 } 00351 00352 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters () const { 00353 return Teuchos::parameterList (); 00354 } 00355 }; 00356 } // namespace details 00357 00367 template<class ScalarType> 00368 class MultiVecTraits<ScalarType,MultiVec<ScalarType> > { 00369 public: 00371 00372 00375 static Teuchos::RCP<MultiVec<ScalarType> > 00376 Clone (const MultiVec<ScalarType>& mv, const int numvecs) { 00377 return Teuchos::rcp (const_cast<MultiVec<ScalarType>&> (mv).Clone (numvecs)); 00378 } 00380 static Teuchos::RCP<MultiVec<ScalarType> > CloneCopy( const MultiVec<ScalarType>& mv ) 00381 { return Teuchos::rcp( const_cast<MultiVec<ScalarType>&>(mv).CloneCopy() ); } 00383 static Teuchos::RCP<MultiVec<ScalarType> > CloneCopy( const MultiVec<ScalarType>& mv, const std::vector<int>& index ) 00384 { return Teuchos::rcp( const_cast<MultiVec<ScalarType>&>(mv).CloneCopy(index) ); } 00386 static Teuchos::RCP<MultiVec<ScalarType> > 00387 CloneViewNonConst (MultiVec<ScalarType>& mv, const std::vector<int>& index) 00388 { 00389 return Teuchos::rcp( mv.CloneViewNonConst(index) ); 00390 } 00391 00392 static Teuchos::RCP<MultiVec<ScalarType> > 00393 CloneViewNonConst (MultiVec<ScalarType>& mv, const Teuchos::Range1D& index) 00394 { 00395 // mfh 02 Mar 2013: For now, we'll just use the above index 00396 // vector version of CloneViewNonConst to implement this, since 00397 // that doesn't require adding to the MultiVec interface. 00398 std::vector<int> indVec (index.size ()); 00399 for (int k = 0; k < index.size (); ++k) { 00400 indVec[k] = k; 00401 } 00402 return CloneViewNonConst (mv, indVec); 00403 } 00404 00406 static Teuchos::RCP<const MultiVec<ScalarType> > 00407 CloneView (const MultiVec<ScalarType>& mv, const std::vector<int>& index) { 00408 return Teuchos::rcp( const_cast<MultiVec<ScalarType>&>(mv).CloneView(index) ); 00409 } 00410 00411 static Teuchos::RCP<const MultiVec<ScalarType> > 00412 CloneView (const MultiVec<ScalarType>& mv, const Teuchos::Range1D& index) 00413 { 00414 // mfh 02 Mar 2013: For now, we'll just use the above index 00415 // vector version of CloneView to implement this, since that 00416 // doesn't require adding to the MultiVec interface. 00417 std::vector<int> indVec (index.size ()); 00418 for (int k = 0; k < index.size (); ++k) { 00419 indVec[k] = k; 00420 } 00421 return CloneView (mv, indVec); 00422 } 00423 00425 static int GetVecLength( const MultiVec<ScalarType>& mv ) 00426 { return mv.GetVecLength(); } 00428 static int GetNumberVecs( const MultiVec<ScalarType>& mv ) 00429 { return mv.GetNumberVecs(); } 00431 static void MvTimesMatAddMv( ScalarType alpha, const MultiVec<ScalarType>& A, 00432 const Teuchos::SerialDenseMatrix<int,ScalarType>& B, 00433 ScalarType beta, MultiVec<ScalarType>& mv ) 00434 { mv.MvTimesMatAddMv(alpha, A, B, beta); } 00436 static void MvAddMv( ScalarType alpha, const MultiVec<ScalarType>& A, ScalarType beta, const MultiVec<ScalarType>& B, MultiVec<ScalarType>& mv ) 00437 { mv.MvAddMv(alpha, A, beta, B); } 00439 static void MvScale ( MultiVec<ScalarType>& mv, const ScalarType alpha ) 00440 { mv.MvScale( alpha ); } 00441 00442 static void MvScale ( MultiVec<ScalarType>& mv, const std::vector<ScalarType>& alpha ) 00443 { mv.MvScale(alpha); } 00445 static void MvTransMv( const ScalarType alpha, const MultiVec<ScalarType>& A, const MultiVec<ScalarType>& mv, Teuchos::SerialDenseMatrix<int,ScalarType>& B ) 00446 { mv.MvTransMv(alpha, A, B); } 00448 static void MvDot( const MultiVec<ScalarType>& mv, const MultiVec<ScalarType>& A, std::vector<ScalarType>& b ) 00449 { mv.MvDot( A, b ); } 00451 static void MvNorm( const MultiVec<ScalarType>& mv, std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>& normvec, NormType type = TwoNorm ) 00452 { mv.MvNorm(normvec,type); } 00454 static void SetBlock( const MultiVec<ScalarType>& A, const std::vector<int>& index, MultiVec<ScalarType>& mv ) 00455 { mv.SetBlock(A, index); } 00456 00457 static void 00458 Assign (const MultiVec<ScalarType>& A, 00459 MultiVec<ScalarType>& mv) 00460 { 00461 // mfh 02 Mar 2013: For now, we'll just use SetBlock to implement this, 00462 // since that doesn't require adding to the MultiVec interface. 00463 const int numVecsRhs = GetNumberVecs (A); 00464 const int numVecsLhs = GetNumberVecs (mv); 00465 00466 TEUCHOS_TEST_FOR_EXCEPTION( 00467 numVecsLhs != numVecsRhs, std::invalid_argument, 00468 "Belos::MultiVecTraits::Assign: Input multivector A has " << numVecsRhs 00469 << " columns, which differs from the number of columns " << numVecsLhs 00470 << " in the output multivector mv."); 00471 00472 // mfh 02 Mar 2013: It's pretty silly to build this each time. 00473 // However, at least that makes the code correct. 00474 std::vector<int> index (numVecsRhs); 00475 for (int k = 0; k < numVecsRhs; ++k) { 00476 index[k] = k; 00477 } 00478 SetBlock (A, index, mv); 00479 } 00480 00482 static void MvRandom( MultiVec<ScalarType>& mv ) 00483 { mv.MvRandom(); } 00485 static void MvInit( MultiVec<ScalarType>& mv, ScalarType alpha = Teuchos::ScalarTraits<ScalarType>::zero() ) 00486 { mv.MvInit(alpha); } 00488 static void MvPrint( const MultiVec<ScalarType>& mv, std::ostream& os ) 00489 { mv.MvPrint(os); } 00490 00491 #ifdef HAVE_BELOS_TSQR 00492 00493 00494 00495 00496 00497 00498 00499 typedef details::MultiVecTsqrAdapter<ScalarType> tsqr_adaptor_type; 00500 #endif // HAVE_BELOS_TSQR 00501 }; 00502 00503 00504 } // namespace Belos 00505 00506 #endif 00507 00508 // end of file BelosMultiVec.hpp
1.7.6.1