| 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_EPETRA_ADAPTER_HPP |
| 11 |
#define BELOS_EPETRA_ADAPTER_HPP |
| 12 |
|
| 13 |
/*! \file BelosEpetraAdapter.hpp |
| 14 |
\brief Provides several interfaces between Belos virtual classes and Epetra concrete classes. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "Epetra_MultiVector.h" |
| 18 |
#include "Epetra_Operator.h" |
| 19 |
#include "Epetra_Map.h" |
| 20 |
#include "Epetra_LocalMap.h" |
| 21 |
#include "Teuchos_SerialDenseMatrix.hpp" |
| 22 |
|
| 23 |
#include "BelosConfigDefs.hpp" |
| 24 |
#include "BelosMultiVec.hpp" |
| 25 |
#include "BelosOperator.hpp" |
| 26 |
#include "BelosTypes.hpp" |
| 27 |
|
| 28 |
#include "BelosSolverFactory_Epetra.hpp" |
| 29 |
|
| 30 |
#ifdef HAVE_BELOS_TSQR |
| 31 |
// This header file actually lives in the Tpetra package. |
| 32 |
// |
| 33 |
// mfh 07 Sep 2012: Back in 2010, I had to put Epetra's TSQR adapter |
| 34 |
// in Tpetra. This was because an external software package was |
| 35 |
// linking to Epetra without using Trilinos' Makefile.export mechanism |
| 36 |
// to consider which Trilinos libraries to use. Making Epetra |
| 37 |
// optionally depend on Kokkos (to make TSQR work) changed the list of |
| 38 |
// libraries, and that software package wasn't doing the right thing |
| 39 |
// to adjust. In any case, I chose at the time to put Epetra's TSQR |
| 40 |
// adapter in Tpetra, since Tpetra already had an optional dependency |
| 41 |
// on Epetra. So, for better or worse, you won't be able to use TSQR |
| 42 |
// with Epetra unless you enable both Epetra and Tpetra in your |
| 43 |
// Trilinos build. HAVE_BELOS_TSQR will correctly reflect this: it |
| 44 |
// won't be defined unless both Epetra and Tpetra are enabled in your |
| 45 |
// Trilinos build. |
| 46 |
# include <Epetra_TsqrAdaptor.hpp> |
| 47 |
#endif // HAVE_BELOS_TSQR |
| 48 |
|
| 49 |
#if defined(HAVE_TEUCHOSCORE_CXX11) |
| 50 |
#define BELOSEPETRACOPY Epetra_DataAccess::Copy |
| 51 |
#define BELOSEPETRAVIEW Epetra_DataAccess::View |
| 52 |
#else |
| 53 |
#define BELOSEPETRACOPY ::Copy |
| 54 |
#define BELOSEPETRAVIEW ::View |
| 55 |
#endif |
| 56 |
|
| 57 |
namespace Belos { |
| 58 |
|
| 59 |
//! @name Epetra Adapter Exceptions |
| 60 |
//@{ |
| 61 |
|
| 62 |
/** \brief EpetraMultiVecFailure is thrown when a return value from an Epetra |
| 63 |
* call on an Epetra_MultiVector is non-zero. |
| 64 |
*/ |
| 65 |
class EpetraMultiVecFailure : public BelosError {public: |
| 66 |
EpetraMultiVecFailure(const std::string& what_arg) : BelosError(what_arg) |
| 67 |
{}}; |
| 68 |
|
| 69 |
/** \brief EpetraOpFailure is thrown when a return value from an Epetra |
| 70 |
* call on an Epetra_Operator is non-zero. |
| 71 |
*/ |
| 72 |
class EpetraOpFailure : public BelosError {public: |
| 73 |
EpetraOpFailure(const std::string& what_arg) : BelosError(what_arg) |
| 74 |
{}}; |
| 75 |
|
| 76 |
//@} |
| 77 |
|
| 78 |
/// \class EpetraMultiVec |
| 79 |
/// \brief Implementation of Belos::MultiVec using Epetra_MultiVector. |
| 80 |
/// |
| 81 |
/// Belos::MultiVec offers a simple abstract interface for |
| 82 |
/// multivector operations in Belos solver algorithms. This class |
| 83 |
/// implements Belos::MultiVec by extending Epetra_MultiVector. |
| 84 |
class EpetraMultiVec : public MultiVec<double>, public Epetra_MultiVector { |
| 85 |
public: |
| 86 |
// constructors |
| 87 |
EpetraMultiVec(const Epetra_BlockMap& Map_in, double * array, const int numvecs, const int stride=0); |
| 88 |
EpetraMultiVec(const Epetra_BlockMap& Map_in, const int numvecs, bool zeroOut=true); |
| 89 |
EpetraMultiVec(Epetra_DataAccess CV_in, const Epetra_MultiVector& P_vec, const std::vector<int>& index); |
| 90 |
EpetraMultiVec& operator=(const EpetraMultiVec& pv) { Epetra_MultiVector::operator=(pv); return *this; } |
| 91 |
EpetraMultiVec(const Epetra_MultiVector & P_vec); |
| 92 |
~EpetraMultiVec(); |
| 93 |
|
| 94 |
//! @name Member functions inherited from Belos::MultiVec |
| 95 |
//@{ |
| 96 |
|
| 97 |
/// A virtual "copy constructor" that returns a pointer to a new |
| 98 |
/// object of the pure virtual class. This vector's entries are |
| 99 |
/// not copied; instead, a new MultiVec is created with the same |
| 100 |
/// data distribution, but with numvecs columns (numvecs > 0). |
| 101 |
/// |
| 102 |
/// \param numvecs [in] The number of columns in the output |
| 103 |
/// multivector. Must be positive. |
| 104 |
MultiVec<double> * Clone ( const int numvecs ) const; |
| 105 |
|
| 106 |
/// A virtual "copy constructor" returning a pointer to a new |
| 107 |
/// object of the pure virtual class. This vector's entries are |
| 108 |
/// copied and a new stand-alone multivector is created. (deep |
| 109 |
/// copy). |
| 110 |
MultiVec<double> * CloneCopy () const; |
| 111 |
|
| 112 |
/// A virtual "copy constructor" returning a pointer to the pure |
| 113 |
/// virtual class. This vector's entries are copied and a new |
| 114 |
/// stand-alone MultiVector is created where only selected columns |
| 115 |
/// are chosen. (deep copy). |
| 116 |
MultiVec<double> * CloneCopy ( const std::vector<int>& index ) const; |
| 117 |
|
| 118 |
/// A virtual view "constructor" returning a pointer to the pure |
| 119 |
/// virtual class. This vector's entries are shared and hence no |
| 120 |
/// memory is allocated for the columns. |
| 121 |
MultiVec<double> * CloneViewNonConst ( const std::vector<int>& index ); |
| 122 |
|
| 123 |
/// A virtual view constructor returning a pointer to the pure |
| 124 |
/// virtual class. This vector's entries are shared and hence no |
| 125 |
/// memory is allocated for the columns. |
| 126 |
const MultiVec<double> * CloneView ( const std::vector<int>& index ) const; |
| 127 |
|
| 128 |
/// Set a subblock of the multivector, which need not be |
| 129 |
/// contiguous, and is given by the indices. |
| 130 |
void SetBlock ( const MultiVec<double>& A, const std::vector<int>& index ); |
| 131 |
|
| 132 |
//! The number of rows in the multivector. |
| 133 |
ptrdiff_t GetGlobalLength () const |
| 134 |
{ |
| 135 |
#ifndef EPETRA_NO_64BIT_GLOBAL_INDICES |
| 136 |
if ( Map().GlobalIndicesLongLong() ) |
| 137 |
return static_cast<ptrdiff_t>( GlobalLength64() ); |
| 138 |
else |
| 139 |
return static_cast<ptrdiff_t>( GlobalLength() ); |
| 140 |
#else |
| 141 |
return static_cast<ptrdiff_t>( GlobalLength() ); |
| 142 |
#endif |
| 143 |
} |
| 144 |
|
| 145 |
//! The number of columns in the multivector. |
| 146 |
int GetNumberVecs () const { return NumVectors(); } |
| 147 |
|
| 148 |
//! *this <- alpha * A * B + beta * (*this) |
| 149 |
void MvTimesMatAddMv ( const double alpha, const MultiVec<double>& A, |
| 150 |
const Teuchos::SerialDenseMatrix<int,double>& B, const double beta ); |
| 151 |
//! *this <- alpha * A + beta * B |
| 152 |
void MvAddMv ( const double alpha, const MultiVec<double>& A, const double beta, |
| 153 |
const MultiVec<double>& B); |
| 154 |
|
| 155 |
//! Scale each element of the vectors in \c *this with \c alpha. |
| 156 |
void MvScale ( const double alpha ) { |
| 157 |
TEUCHOS_TEST_FOR_EXCEPTION( this->Scale( alpha )!=0, EpetraMultiVecFailure, |
| 158 |
"Belos::EpetraMultiVec::MvScale() call to Scale() returned a nonzero value."); } |
| 159 |
|
| 160 |
//! Scale each element of the \c i-th vector in \c *this with \c alpha[i]. |
| 161 |
void MvScale ( const std::vector<double>& alpha ); |
| 162 |
|
| 163 |
//! B <- alpha * A^T * (*this) |
| 164 |
void MvTransMv ( const double alpha, const MultiVec<double>& A, Teuchos::SerialDenseMatrix<int,double>& B ) const; |
| 165 |
|
| 166 |
//! b[i] = A[i]^T * this[i] |
| 167 |
void MvDot ( const MultiVec<double>& A, std::vector<double>& b ) const; |
| 168 |
|
| 169 |
//! alpha[i] = norm of i-th column of (*this) |
| 170 |
void MvNorm ( std::vector<double>& normvec, NormType norm_type = TwoNorm ) const; |
| 171 |
|
| 172 |
//! Fill all columns of *this with random values. |
| 173 |
void MvRandom() { |
| 174 |
TEUCHOS_TEST_FOR_EXCEPTION( Random()!=0, EpetraMultiVecFailure, |
| 175 |
"Belos::EpetraMultiVec::MvRandom() call to Random() returned a nonzero value."); } |
| 176 |
|
| 177 |
//! Initialize each element of (*this) to the scalar value alpha. |
| 178 |
void MvInit ( const double alpha ) { |
| 179 |
TEUCHOS_TEST_FOR_EXCEPTION( PutScalar(alpha)!=0, EpetraMultiVecFailure, |
| 180 |
"Belos::EpetraMultiVec::MvInit() call to PutScalar() returned a nonzero value."); } |
| 181 |
|
| 182 |
//! Print (*this) to the given output stream. |
| 183 |
void MvPrint( std::ostream& os ) const { os << *this << std::endl; }; |
| 184 |
private: |
| 185 |
}; |
| 186 |
|
| 187 |
/// \class EpetraOp |
| 188 |
/// \brief Belos::Operator implementation that wraps an Epetra_Operator instance. |
| 189 |
/// |
| 190 |
/// An instance of this class wraps an Epetra_Operator instance so |
| 191 |
/// that it can be handled as a Belos::Operator. Its Apply() method |
| 192 |
/// just invokes the underlying Epetra_Operator's Apply(). |
| 193 |
/// |
| 194 |
/// Important note on applying the transpose: Epetra_Operator |
| 195 |
/// objects, unlike Tpetra or Thyra operators, have a persistent |
| 196 |
/// "use the transpose" state. This state can be set or unset using |
| 197 |
/// Epetra_Operator::SetUseTranspose(). Epetra_Operator instances |
| 198 |
/// are not required to implement applying the transpose. However, |
| 199 |
/// if the wrapped Epetra_Operator object does implement applying |
| 200 |
/// the transpose, and if its transpose state is set on input, |
| 201 |
/// EpetraOp follows the convention that calling Apply() with |
| 202 |
/// trans=TRANS (or CONJTRANS) applies the transpose, not the |
| 203 |
/// transpose of the transpose. Similarly, calling Apply() with |
| 204 |
/// trans=NOTRANS temporary unsets the transpose state, applies the |
| 205 |
/// operator, and restores the transpose state on exit. This |
| 206 |
/// preserves the historical behavior of Belos' Epetra interface, |
| 207 |
/// without permanently changing the state of the operator. |
| 208 |
/// |
| 209 |
class EpetraOp : public virtual Operator<double> { |
| 210 |
public: |
| 211 |
/// \brief Constructor. |
| 212 |
/// |
| 213 |
/// \param Op [in] The Epetra_Operator instance to wrap. |
| 214 |
EpetraOp (const Teuchos::RCP<Epetra_Operator> &Op); |
| 215 |
|
| 216 |
//! Destructor. |
| 217 |
~EpetraOp () {} |
| 218 |
|
| 219 |
//! Apply the operator (or its transpose) to x and put the result in y. |
| 220 |
void Apply (const MultiVec<double>& x, |
| 221 |
MultiVec<double>& y, |
| 222 |
ETrans trans=NOTRANS) const; |
| 223 |
|
| 224 |
//! Whether the operator knows how to apply its transpose. |
| 225 |
bool HasApplyTranspose() const; |
| 226 |
|
| 227 |
private: |
| 228 |
Teuchos::RCP<Epetra_Operator> Epetra_Op; |
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
/// \class EpetraPrecOp |
| 233 |
/// \brief Belos::Operator implementation that wraps Epetra_Operator as a preconditioner. |
| 234 |
/// |
| 235 |
/// An instance of this class wraps an \c Epetra_Operator, when the |
| 236 |
/// wrapped operator is a preconditioner or other object that is |
| 237 |
/// normally applied using ApplyInverse(). EpetraPrecOp's \c |
| 238 |
/// Apply() method thus invokes the underlying object's |
| 239 |
/// Epetra_Operator::ApplyInverse() method, and its \c |
| 240 |
/// ApplyInverse() method invokes the underlying object's |
| 241 |
/// Epetra_Operator::Apply() method. |
| 242 |
/// |
| 243 |
/// EpetraPrecOp implements both \c Belos::Operator and |
| 244 |
/// Epetra_Operator. Thus, you can use it in Belos' templated |
| 245 |
/// solvers with either the OP = Belos::Operator or OP = |
| 246 |
/// Epetra_Operator specializations. |
| 247 |
/// |
| 248 |
class EpetraPrecOp : |
| 249 |
public virtual Operator<double>, |
| 250 |
public virtual Epetra_Operator |
| 251 |
{ |
| 252 |
public: |
| 253 |
/// Basic constructor. |
| 254 |
/// |
| 255 |
/// \param Op [in/out] The operator to wrap. EpetraPrecOp's |
| 256 |
/// Apply() method will invoke this operator's ApplyInverse() |
| 257 |
/// method, and vice versa. |
| 258 |
EpetraPrecOp (const Teuchos::RCP<Epetra_Operator>& Op); |
| 259 |
|
| 260 |
//! Virtual destructor, for memory safety of derived classes. |
| 261 |
virtual ~EpetraPrecOp() {}; |
| 262 |
|
| 263 |
/// \brief Apply the operator (or its transpose) to x, putting the result in y. |
| 264 |
/// |
| 265 |
/// This method is part of the Belos::MultiVec implementation. |
| 266 |
/// EpetraPrecOp's Apply() methods invoke the underlying |
| 267 |
/// operator's ApplyInverse() method. |
| 268 |
/// |
| 269 |
/// All Epetra operators are real-valued, never complex-valued, so |
| 270 |
/// setting either trans=CONJTRANS or trans=TRANS means that this |
| 271 |
/// method will attempt to apply the transpose. If you attempt to |
| 272 |
/// apply the transpose, EpetraPrecOp will invoke the underlying |
| 273 |
/// operator's SetUseTranspose() method. This may or may not |
| 274 |
/// succeed. If it does <i>not</i> succeed, this method will |
| 275 |
/// throw an \c EpetraOpFailure exception. Furthermore, if the |
| 276 |
/// underlying operator's ApplyInverse() method does not succeed |
| 277 |
/// (i.e., returns a nonzero error code), this method will throw |
| 278 |
/// an \c EpetraOpFailure exception. |
| 279 |
/// |
| 280 |
/// \note The trans argument will always have its literal meaning, |
| 281 |
/// even if the underlying Epetra_Operator's transpose flag is |
| 282 |
/// set (i.e., if UseTranspose()==true). Implementing this |
| 283 |
/// requires temporarily changing the transpose flag of the |
| 284 |
/// underlying operator. However, the flag will be changed back |
| 285 |
/// to its original value before this method returns, whatever |
| 286 |
/// that original value was. This behavior is <i>different</i> |
| 287 |
/// than that of the two-argument version of Apply() below, |
| 288 |
/// because the three-argument version of Apply() here |
| 289 |
/// implements the Belos::Operator and Belos::OperatorTraits |
| 290 |
/// interfaces. Those interfaces expect the transpose-ness of |
| 291 |
/// the operator to be stateless. |
| 292 |
void |
| 293 |
Apply (const MultiVec<double>& x, |
| 294 |
MultiVec<double>& y, |
| 295 |
ETrans trans=NOTRANS) const; |
| 296 |
|
| 297 |
//! Whether the operator knows how to apply its transpose. |
| 298 |
bool HasApplyTranspose() const; |
| 299 |
|
| 300 |
/// \brief Apply the operator to x, putting the result in y. |
| 301 |
/// |
| 302 |
/// This method is part of the Epetra_MultiVector implementation. |
| 303 |
/// EpetraPrecOp's Apply() methods invoke the underlying |
| 304 |
/// operator's ApplyInverse() method. This version of Apply() |
| 305 |
/// does <i>not</i> attempt to check for errors, but it returns |
| 306 |
/// the error code that the underlying operator's ApplyInverse() |
| 307 |
/// method returns. |
| 308 |
/// |
| 309 |
/// \note If the underlying operator's transpose flag is set |
| 310 |
/// (i.e., if UseTranspose() returns true), then EpetraPrecOp |
| 311 |
/// will apply the transpose of the inverse. This behavior is |
| 312 |
/// <i>different</i> than that of the three-argument version of |
| 313 |
/// Apply() above. This is so because the two-argument version |
| 314 |
/// of Apply() implements Epetra_Operator, which expects the |
| 315 |
/// effect of SetUseTranspose() to persist for all subsequent |
| 316 |
/// calls to Apply() and ApplyInverse(). |
| 317 |
/// |
| 318 |
/// \return Zero if successful, else nonzero. The value of the |
| 319 |
/// error code is the same as would be returned by the |
| 320 |
/// underlying operator's ApplyInverse() method. |
| 321 |
int |
| 322 |
Apply (const Epetra_MultiVector &X, |
| 323 |
Epetra_MultiVector &Y) const; |
| 324 |
|
| 325 |
/// \brief Apply inverse method for an Epetra_MultiVector. |
| 326 |
/// |
| 327 |
/// This method is part of the Epetra_MultiVector implementation. |
| 328 |
/// EpetraPrecOp's ApplyInverse() method invokes the underlying |
| 329 |
/// operator's Apply() method. This method does <i>not</i> |
| 330 |
/// attempt to check for errors, but it returns the error code |
| 331 |
/// that the underlying operator's Apply() method returns. |
| 332 |
/// |
| 333 |
/// \note If the underlying operator's transpose flag is set |
| 334 |
/// (i.e., if UseTranspose() returns true), then EpetraPrecOp |
| 335 |
/// will apply the transpose of the underlying operator. This |
| 336 |
/// behavior is the same as that of the two-argument version of |
| 337 |
/// Apply(), for the same reason as discussed in that method's |
| 338 |
/// documentation. |
| 339 |
/// |
| 340 |
/// \return Zero if successful, else nonzero. The value of the |
| 341 |
/// error code is the same as would be returned by the |
| 342 |
/// underlying operator's Apply() method. |
| 343 |
int |
| 344 |
ApplyInverse (const Epetra_MultiVector &X, |
| 345 |
Epetra_MultiVector &Y) const; |
| 346 |
|
| 347 |
//! Return a human-readable string describing the operator. |
| 348 |
const char* Label() const { |
| 349 |
return "Epetra_Operator applying A^{-1} as A"; |
| 350 |
} |
| 351 |
|
| 352 |
//! Return the current UseTranspose setting. |
| 353 |
bool UseTranspose() const { |
| 354 |
return Epetra_Op->UseTranspose (); |
| 355 |
} |
| 356 |
|
| 357 |
/// \brief If set true, the transpose of this operator will be applied. |
| 358 |
/// |
| 359 |
/// \param UseTranspose_in [in] True if you want to apply the |
| 360 |
/// transpose, else false. Epetra operators are real-valued, |
| 361 |
/// never complex-valued, so applying the conjugate transpose is |
| 362 |
/// the same as applying the transpose. |
| 363 |
/// |
| 364 |
/// \return Zero if setting was successful, else nonzero. |
| 365 |
/// |
| 366 |
/// This method invokes the underlying operator's |
| 367 |
/// SetUseTranspose(). That operator returns zero if successful, |
| 368 |
/// else nonzero. Note that SetUseTranspose() affects all |
| 369 |
/// subsequent applications of the operator, until the next |
| 370 |
/// SetUseTranspose() call. |
| 371 |
int SetUseTranspose (bool UseTranspose_in) { |
| 372 |
return Epetra_Op->SetUseTranspose (UseTranspose_in); |
| 373 |
} |
| 374 |
|
| 375 |
/// \brief Return true if this object can provide an approximate inf-norm. |
| 376 |
/// |
| 377 |
/// If this method returns false, then the \c NormInf() method |
| 378 |
/// should not be used. |
| 379 |
bool HasNormInf () const { |
| 380 |
return Epetra_Op->HasNormInf (); |
| 381 |
} |
| 382 |
|
| 383 |
/// \brief Return the infinity norm of the global matrix. |
| 384 |
/// |
| 385 |
/// The returned value only makes sense if HasNormInf() == true. |
| 386 |
double NormInf() const { |
| 387 |
return Epetra_Op->NormInf (); |
| 388 |
} |
| 389 |
|
| 390 |
//! Return the Epetra_Comm communicator associated with this operator. |
| 391 |
const Epetra_Comm& Comm() const { return Epetra_Op->Comm(); }; |
| 392 |
|
| 393 |
//! Return the Epetra_Map object representing the domain of this operator. |
| 394 |
const Epetra_Map& OperatorDomainMap() const { |
| 395 |
return Epetra_Op->OperatorDomainMap(); |
| 396 |
} |
| 397 |
|
| 398 |
//! Return the Epetra_Map object representing the range of this operator. |
| 399 |
const Epetra_Map& OperatorRangeMap() const { |
| 400 |
return Epetra_Op->OperatorRangeMap(); |
| 401 |
} |
| 402 |
|
| 403 |
private: |
| 404 |
//! The underlying operator that this EpetraPrecOp instance wraps. |
| 405 |
Teuchos::RCP<Epetra_Operator> Epetra_Op; |
| 406 |
}; |
| 407 |
|
| 408 |
//////////////////////////////////////////////////////////////////// |
| 409 |
// |
| 410 |
// Implementation of the Belos::MultiVecTraits for Epetra_MultiVector. |
| 411 |
// |
| 412 |
//////////////////////////////////////////////////////////////////// |
| 413 |
|
| 414 |
//! Full specialization of Belos::MultiVecTraits for Epetra_MultiVector. |
| 415 |
template<> |
| 416 |
class MultiVecTraits<double, Epetra_MultiVector> { |
| 417 |
public: |
| 418 |
/// \brief Create a new multivector with \c outNumVecs columns. |
| 419 |
/// |
| 420 |
/// The returned Epetra_MultiVector has the same Epetra_Map |
| 421 |
/// (distribution over one or more parallel processes) as \c mv. |
| 422 |
/// Its entries are not initialized and have undefined values. |
| 423 |
static Teuchos::RCP<Epetra_MultiVector> |
| 424 |
Clone (const Epetra_MultiVector& mv, const int outNumVecs) |
| 425 |
{ |
| 426 |
TEUCHOS_TEST_FOR_EXCEPTION( |
| 427 |
outNumVecs <= 0, std::invalid_argument, |
| 428 |
"Belos::MultiVecTraits<double, Epetra_MultiVector>::" |
| 429 |
"Clone(mv, outNumVecs = " << outNumVecs << "): " |
| 430 |
"outNumVecs must be positive."); |
| 431 |
// NOTE (mfh 13 Jan 2011) Anasazi currently lets Epetra fill in |
| 432 |
// the entries of the returned multivector with zeros, but Belos |
| 433 |
// does not. We retain this different behavior for now, but the |
| 434 |
// two versions should be reconciled. |
| 435 |
// |
| 436 |
// NOTE (mfh 07 Mar 2013) The Tpetra::MultiVector specialization |
| 437 |
// of Belos::MultiVecTraits used to fill the returned |
| 438 |
// multivector with zeros. In fact, Belos' solvers do not |
| 439 |
// require Clone to initialize, because the Epetra_MultiVector |
| 440 |
// specialization did not fill with zeros, and almost all of |
| 441 |
// Belos' solvers were written to pass tests with Epetra. Thus, |
| 442 |
// we should prefer that Clone _not_ initialize the multivector, |
| 443 |
// because it wastes time. (We actually observed that Belos |
| 444 |
// with Tpetra was spending a lot of time initializing |
| 445 |
// multivectors, compared to Belos with Epetra.) |
| 446 |
return Teuchos::rcp (new Epetra_MultiVector (mv.Map(), outNumVecs, false)); |
| 447 |
} |
| 448 |
|
| 449 |
static Teuchos::RCP<Epetra_MultiVector> |
| 450 |
CloneCopy (const Epetra_MultiVector& mv) |
| 451 |
{ |
| 452 |
return Teuchos::rcp (new Epetra_MultiVector (mv)); |
| 453 |
} |
| 454 |
|
| 455 |
static Teuchos::RCP<Epetra_MultiVector> |
| 456 |
CloneCopy (const Epetra_MultiVector& mv, const std::vector<int>& index) |
| 457 |
{ |
| 458 |
const int inNumVecs = GetNumberVecs (mv); |
| 459 |
const int outNumVecs = index.size(); |
| 460 |
|
| 461 |
// Simple, inexpensive tests of the index vector. |
| 462 |
TEUCHOS_TEST_FOR_EXCEPTION(outNumVecs == 0, std::invalid_argument, |
| 463 |
"Belos::MultiVecTraits<double,Epetra_MultiVector>::" |
| 464 |
"CloneCopy(mv, index = {}): At least one vector must be" |
| 465 |
" cloned from mv."); |
| 466 |
if (outNumVecs > inNumVecs) |
| 467 |
{ |
| 468 |
std::ostringstream os; |
| 469 |
os << "Belos::MultiVecTraits<double,Epetra_MultiVector>::" |
| 470 |
"CloneCopy(mv, index = {"; |
| 471 |
for (int k = 0; k < outNumVecs - 1; ++k) |
| 472 |
os << index[k] << ", "; |
| 473 |
os << index[outNumVecs-1] << "}): There are " << outNumVecs |
| 474 |
<< " indices to copy, but only " << inNumVecs << " columns of mv."; |
| 475 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, os.str()); |
| 476 |
} |
| 477 |
#ifdef TEUCHOS_DEBUG |
| 478 |
// In debug mode, we perform more expensive tests of the index |
| 479 |
// vector, to ensure all the elements are in range. |
| 480 |
// Dereferencing the iterator is valid because index has length |
| 481 |
// > 0. |
| 482 |
const int minIndex = *std::min_element (index.begin(), index.end()); |
| 483 |
const int maxIndex = *std::max_element (index.begin(), index.end()); |
| 484 |
|
| 485 |
if (minIndex < 0) |
| 486 |
{ |
| 487 |
std::ostringstream os; |
| 488 |
os << "Belos::MultiVecTraits<double,Epetra_MultiVector>::" |
| 489 |
"CloneCopy(mv, index = {"; |
| 490 |
for (int k = 0; k < outNumVecs - 1; ++k) |
| 491 |
os << index[k] << ", "; |
| 492 |
os << index[outNumVecs-1] << "}): Indices must be nonnegative, but " |
| 493 |
"the smallest index " << minIndex << " is negative."; |
| 494 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, os.str()); |
| 495 |
} |
| 496 |
if (maxIndex >= inNumVecs) |
| 497 |
{ |
| 498 |
std::ostringstream os; |
| 499 |
os << "Belos::MultiVecTraits<double,Epetra_MultiVector>::" |
| 500 |
"CloneCopy(mv, index = {"; |
| 501 |
for (int k = 0; k < outNumVecs - 1; ++k) |
| 502 |
os << index[k] << ", "; |
| 503 |
os << index[outNumVecs-1] << "}): Indices must be strictly less than " |
| 504 |
"the number of vectors " << inNumVecs << " in mv; the largest index " |
| 505 |
<< maxIndex << " is out of bounds."; |
| 506 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, os.str()); |
| 507 |
} |
| 508 |
#endif // TEUCHOS_DEBUG |
| 509 |
// Cast to nonconst, because Epetra_MultiVector's constructor |
| 510 |
// wants a nonconst int array argument. It doesn't actually |
| 511 |
// change the entries of the array. |
| 512 |
std::vector<int>& tmpind = const_cast< std::vector<int>& > (index); |
| 513 |
return Teuchos::rcp (new Epetra_MultiVector (BELOSEPETRACOPY, mv, &tmpind[0], index.size())); |
| 514 |
} |
| 515 |
|
| 516 |
static Teuchos::RCP<Epetra_MultiVector> |
| 517 |
CloneCopy (const Epetra_MultiVector& mv, const Teuchos::Range1D& index) |
| 518 |
{ |
| 519 |
const int inNumVecs = GetNumberVecs (mv); |
| 520 |
const int outNumVecs = index.size(); |
| 521 |
const bool validRange = outNumVecs > 0 && index.lbound() >= 0 && |
| 522 |
index.ubound() < inNumVecs; |
| 523 |
if (! validRange) |
| 524 |
{ |
| 525 |
std::ostringstream os; |
| 526 |
os << "Belos::MultiVecTraits<double,Epetra_MultiVector>::Clone(mv," |
| 527 |
"index=[" << index.lbound() << ", " << index.ubound() << "]): "; |
| 528 |
TEUCHOS_TEST_FOR_EXCEPTION(outNumVecs == 0, std::invalid_argument, |
| 529 |
os.str() << "Column index range must be nonempty."); |
| 530 |
TEUCHOS_TEST_FOR_EXCEPTION(index.lbound() < 0, std::invalid_argument, |
| 531 |
os.str() << "Column index range must be nonnegative."); |
| 532 |
TEUCHOS_TEST_FOR_EXCEPTION(index.ubound() >= inNumVecs, std::invalid_argument, |
| 533 |
os.str() << "Column index range must not exceed " |
| 534 |
"number of vectors " << inNumVecs << " in the " |
| 535 |
"input multivector."); |
| 536 |
} |
| 537 |
return Teuchos::rcp (new Epetra_MultiVector (BELOSEPETRACOPY, mv, index.lbound(), index.size())); |
| 538 |
} |
| 539 |
|
| 540 |
static Teuchos::RCP<Epetra_MultiVector> |
| 541 |
CloneViewNonConst (Epetra_MultiVector& mv, const std::vector<int>& index) |
| 542 |
{ |
| 543 |
const int inNumVecs = GetNumberVecs (mv); |
| 544 |
const int outNumVecs = index.size(); |
| 545 |
|
| 546 |
// Simple, inexpensive tests of the index vector. |
| 547 |
TEUCHOS_TEST_FOR_EXCEPTION(outNumVecs == 0, std::invalid_argument, |
| 548 |
"Belos::MultiVecTraits<double,Epetra_MultiVector>::" |
| 549 |
"CloneViewNonConst(mv, index = {}): The output view " |
| 550 |
"must have at least one column."); |
| 551 |
if (outNumVecs > inNumVecs) |
| 552 |
{ |
| 553 |
std::ostringstream os; |
| 554 |
os << "Belos::MultiVecTraits<double,Epetra_MultiVector>::" |
| 555 |
"CloneViewNonConst(mv, index = {"; |
| 556 |
for (int k = 0; k < outNumVecs - 1; ++k) |
| 557 |
os << index[k] << ", "; |
| 558 |
os << index[outNumVecs-1] << "}): There are " << outNumVecs |
| 559 |
<< " indices to view, but only " << inNumVecs << " columns of mv."; |
| 560 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, os.str()); |
| 561 |
} |
| 562 |
#ifdef TEUCHOS_DEBUG |
| 563 |
// In debug mode, we perform more expensive tests of the index |
| 564 |
// vector, to ensure all the elements are in range. |
| 565 |
// Dereferencing the iterator is valid because index has length |
| 566 |
// > 0. |
| 567 |
const int minIndex = *std::min_element (index.begin(), index.end()); |
| 568 |
const int maxIndex = *std::max_element (index.begin(), index.end()); |
| 569 |
|
| 570 |
if (minIndex < 0) |
| 571 |
{ |
| 572 |
std::ostringstream os; |
| 573 |
os << "Belos::MultiVecTraits<double,Epetra_MultiVector>::" |
| 574 |
"CloneViewNonConst(mv, index = {"; |
| 575 |
for (int k = 0; k < outNumVecs - 1; ++k) |
| 576 |
os << index[k] << ", "; |
| 577 |
os << index[outNumVecs-1] << "}): Indices must be nonnegative, but " |
| 578 |
"the smallest index " << minIndex << " is negative."; |
| 579 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, os.str()); |
| 580 |
} |
| 581 |
if (maxIndex >= inNumVecs) |
| 582 |
{ |
| 583 |
std::ostringstream os; |
| 584 |
os << "Belos::MultiVecTraits<double,Epetra_MultiVector>::" |
| 585 |
"CloneViewNonConst(mv, index = {"; |
| 586 |
for (int k = 0; k < outNumVecs - 1; ++k) |
| 587 |
os << index[k] << ", "; |
| 588 |
os << index[outNumVecs-1] << "}): Indices must be strictly less than " |
| 589 |
"the number of vectors " << inNumVecs << " in mv; the largest index " |
| 590 |
<< maxIndex << " is out of bounds."; |
| 591 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, os.str()); |
| 592 |
} |
| 593 |
#endif // TEUCHOS_DEBUG |
| 594 |
// Cast to nonconst, because Epetra_MultiVector's constructor |
| 595 |
// wants a nonconst int array argument. It doesn't actually |
| 596 |
// change the entries of the array. |
| 597 |
std::vector<int>& tmpind = const_cast< std::vector<int>& > (index); |
| 598 |
return Teuchos::rcp (new Epetra_MultiVector (BELOSEPETRAVIEW, mv, &tmpind[0], index.size())); |
| 599 |
} |
| 600 |
|
| 601 |
static Teuchos::RCP<Epetra_MultiVector> |
| 602 |
CloneViewNonConst (Epetra_MultiVector& mv, const Teuchos::Range1D& index) |
| 603 |
{ |
| 604 |
const bool validRange = index.size() > 0 && |
| 605 |
index.lbound() >= 0 && |
| 606 |
index.ubound() < mv.NumVectors(); |
| 607 |
if (! validRange) |
| 608 |
{ |
| 609 |
std::ostringstream os; |
| 610 |
os << "Belos::MultiVecTraits<double,Epetra_MultiVector>::CloneView" |
| 611 |
"NonConst(mv,index=[" << index.lbound() << ", " << index.ubound() |
| 612 |
<< "]): "; |
| 613 |
TEUCHOS_TEST_FOR_EXCEPTION(index.size() == 0, std::invalid_argument, |
| 614 |
os.str() << "Column index range must be nonempty."); |
| 615 |
TEUCHOS_TEST_FOR_EXCEPTION(index.lbound() < 0, std::invalid_argument, |
| 616 |
os.str() << "Column index range must be nonnegative."); |
| 617 |
TEUCHOS_TEST_FOR_EXCEPTION(index.ubound() >= mv.NumVectors(), |
| 618 |
std::invalid_argument, |
| 619 |
os.str() << "Column index range must not exceed " |
| 620 |
"number of vectors " << mv.NumVectors() << " in " |
| 621 |
"the input multivector."); |
| 622 |
} |
| 623 |
return Teuchos::rcp (new Epetra_MultiVector (BELOSEPETRAVIEW, mv, index.lbound(), index.size())); |
| 624 |
} |
| 625 |
|
| 626 |
static Teuchos::RCP<const Epetra_MultiVector> |
| 627 |
CloneView (const Epetra_MultiVector& mv, const std::vector<int>& index) |
| 628 |
{ |
| 629 |
const int inNumVecs = GetNumberVecs (mv); |
| 630 |
const int outNumVecs = index.size(); |
| 631 |
|
| 632 |
// Simple, inexpensive tests of the index vector. |
| 633 |
TEUCHOS_TEST_FOR_EXCEPTION(outNumVecs == 0, std::invalid_argument, |
| 634 |
"Belos::MultiVecTraits<double,Epetra_MultiVector>::" |
| 635 |
"CloneView(mv, index = {}): The output view " |
| 636 |
"must have at least one column."); |
| 637 |
if (outNumVecs > inNumVecs) |
| 638 |
{ |
| 639 |
std::ostringstream os; |
| 640 |
os << "Belos::MultiVecTraits<double,Epetra_MultiVector>::" |
| 641 |
"CloneView(mv, index = {"; |
| 642 |
for (int k = 0; k < outNumVecs - 1; ++k) |
| 643 |
os << index[k] << ", "; |
| 644 |
os << index[outNumVecs-1] << "}): There are " << outNumVecs |
| 645 |
<< " indices to view, but only " << inNumVecs << " columns of mv."; |
| 646 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, os.str()); |
| 647 |
} |
| 648 |
#ifdef TEUCHOS_DEBUG |
| 649 |
// In debug mode, we perform more expensive tests of the index |
| 650 |
// vector, to ensure all the elements are in range. |
| 651 |
// Dereferencing the iterator is valid because index has length |
| 652 |
// > 0. |
| 653 |
const int minIndex = *std::min_element (index.begin(), index.end()); |
| 654 |
const int maxIndex = *std::max_element (index.begin(), index.end()); |
| 655 |
|
| 656 |
if (minIndex < 0) |
| 657 |
{ |
| 658 |
std::ostringstream os; |
| 659 |
os << "Belos::MultiVecTraits<double,Epetra_MultiVector>::" |
| 660 |
"CloneView(mv, index = {"; |
| 661 |
for (int k = 0; k < outNumVecs - 1; ++k) |
| 662 |
os << index[k] << ", "; |
| 663 |
os << index[outNumVecs-1] << "}): Indices must be nonnegative, but " |
| 664 |
"the smallest index " << minIndex << " is negative."; |
| 665 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, os.str()); |
| 666 |
} |
| 667 |
if (maxIndex >= inNumVecs) |
| 668 |
{ |
| 669 |
std::ostringstream os; |
| 670 |
os << "Belos::MultiVecTraits<double,Epetra_MultiVector>::" |
| 671 |
"CloneView(mv, index = {"; |
| 672 |
for (int k = 0; k < outNumVecs - 1; ++k) |
| 673 |
os << index[k] << ", "; |
| 674 |
os << index[outNumVecs-1] << "}): Indices must be strictly less than " |
| 675 |
"the number of vectors " << inNumVecs << " in mv; the largest index " |
| 676 |
<< maxIndex << " is out of bounds."; |
| 677 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, os.str()); |
| 678 |
} |
| 679 |
#endif // TEUCHOS_DEBUG |
| 680 |
// Cast to nonconst, because Epetra_MultiVector's constructor |
| 681 |
// wants a nonconst int array argument. It doesn't actually |
| 682 |
// change the entries of the array. |
| 683 |
std::vector<int>& tmpind = const_cast< std::vector<int>& > (index); |
| 684 |
return Teuchos::rcp (new Epetra_MultiVector (BELOSEPETRAVIEW, mv, &tmpind[0], index.size())); |
| 685 |
} |
| 686 |
|
| 687 |
static Teuchos::RCP<Epetra_MultiVector> |
| 688 |
CloneView (const Epetra_MultiVector& mv, const Teuchos::Range1D& index) |
| 689 |
{ |
| 690 |
const bool validRange = index.size() > 0 && |
| 691 |
index.lbound() >= 0 && |
| 692 |
index.ubound() < mv.NumVectors(); |
| 693 |
if (! validRange) |
| 694 |
{ |
| 695 |
std::ostringstream os; |
| 696 |
os << "Belos::MultiVecTraits<double,Epetra_MultiVector>::CloneView" |
| 697 |
"(mv,index=[" << index.lbound() << ", " << index.ubound() |
| 698 |
<< "]): "; |
| 699 |
TEUCHOS_TEST_FOR_EXCEPTION(index.size() == 0, std::invalid_argument, |
| 700 |
os.str() << "Column index range must be nonempty."); |
| 701 |
TEUCHOS_TEST_FOR_EXCEPTION(index.lbound() < 0, std::invalid_argument, |
| 702 |
os.str() << "Column index range must be nonnegative."); |
| 703 |
TEUCHOS_TEST_FOR_EXCEPTION(index.ubound() >= mv.NumVectors(), |
| 704 |
std::invalid_argument, |
| 705 |
os.str() << "Column index range must not exceed " |
| 706 |
"number of vectors " << mv.NumVectors() << " in " |
| 707 |
"the input multivector."); |
| 708 |
} |
| 709 |
return Teuchos::rcp (new Epetra_MultiVector(BELOSEPETRAVIEW, mv, index.lbound(), index.size())); |
| 710 |
} |
| 711 |
|
| 712 |
static ptrdiff_t GetGlobalLength( const Epetra_MultiVector& mv ) |
| 713 |
{ |
| 714 |
#ifndef EPETRA_NO_64BIT_GLOBAL_INDICES |
| 715 |
if (mv.Map().GlobalIndicesLongLong()) |
| 716 |
return static_cast<ptrdiff_t>( mv.GlobalLength64() ); |
| 717 |
else |
| 718 |
return static_cast<ptrdiff_t>( mv.GlobalLength() ); |
| 719 |
#else |
| 720 |
return static_cast<ptrdiff_t>( mv.GlobalLength() ); |
| 721 |
#endif |
| 722 |
} |
| 723 |
|
| 724 |
static int GetNumberVecs( const Epetra_MultiVector& mv ) |
| 725 |
{ return mv.NumVectors(); } |
| 726 |
|
| 727 |
static bool HasConstantStride( const Epetra_MultiVector& mv ) |
| 728 |
{ return mv.ConstantStride(); } |
| 729 |
|
| 730 |
static void |
| 731 |
MvTimesMatAddMv (const double alpha, |
| 732 |
const Epetra_MultiVector& A, |
| 733 |
const Teuchos::SerialDenseMatrix<int,double>& B, |
| 734 |
const double beta, |
| 735 |
Epetra_MultiVector& mv) |
| 736 |
{ |
| 737 |
Epetra_LocalMap LocalMap(B.numRows(), 0, mv.Map().Comm()); |
| 738 |
Epetra_MultiVector B_Pvec(BELOSEPETRAVIEW, LocalMap, B.values(), B.stride(), B.numCols()); |
| 739 |
|
| 740 |
const int info = mv.Multiply ('N', 'N', alpha, A, B_Pvec, beta); |
| 741 |
TEUCHOS_TEST_FOR_EXCEPTION( |
| 742 |
info != 0, EpetraMultiVecFailure, |
| 743 |
"Belos::MultiVecTraits<double,Epetra_MultiVector>::MvTimesMatAddMv: " |
| 744 |
"Epetra_MultiVector::Multiply() returned a nonzero value info=" << info |
| 745 |
<< "."); |
| 746 |
} |
| 747 |
|
| 748 |
/// \brief <tt>mv := alpha*A + beta*B</tt> |
| 749 |
/// |
| 750 |
/// The Epetra specialization of this method ignores and |
| 751 |
/// completely overwrites any NaN or Inf entries in A. Thus, it |
| 752 |
/// does <i>not</i> mean the same thing as <tt>mv := 0*mv + |
| 753 |
/// alpha*A + beta*B</tt> in IEEE 754 floating-point arithmetic. |
| 754 |
/// (Remember that NaN*0 = NaN.) |
| 755 |
static void |
| 756 |
MvAddMv (const double alpha, |
| 757 |
const Epetra_MultiVector& A, |
| 758 |
const double beta, |
| 759 |
const Epetra_MultiVector& B, |
| 760 |
Epetra_MultiVector& mv) |
| 761 |
{ |
| 762 |
const int info = mv.Update (alpha, A, beta, B, 0.0); |
| 763 |
TEUCHOS_TEST_FOR_EXCEPTION(info != 0, EpetraMultiVecFailure, |
| 764 |
"Belos::MultiVecTraits<double, Epetra_MultiVector>::MvAddMv: Call to " |
| 765 |
"Update() returned a nonzero value " << info << "."); |
| 766 |
} |
| 767 |
|
| 768 |
static void |
| 769 |
MvScale (Epetra_MultiVector& mv, |
| 770 |
const double alpha) |
| 771 |
{ |
| 772 |
const int info = mv.Scale (alpha); |
| 773 |
TEUCHOS_TEST_FOR_EXCEPTION(info != 0, EpetraMultiVecFailure, |
| 774 |
"Belos::MultiVecTraits<double,Epetra_MultiVector>::MvScale: " |
| 775 |
"Epetra_MultiVector::Scale() returned a nonzero value info=" |
| 776 |
<< info << "."); |
| 777 |
} |
| 778 |
|
| 779 |
//! For all columns j of \c mv, set <tt>mv[j] := alpha[j] * mv[j]</tt>. |
| 780 |
static void |
| 781 |
MvScale (Epetra_MultiVector& mv, |
| 782 |
const std::vector<double>& alpha) |
| 783 |
{ |
| 784 |
// Check to make sure the vector has the same number of entries |
| 785 |
// as the multivector has columns. |
| 786 |
const int numvecs = mv.NumVectors (); |
| 787 |
TEUCHOS_TEST_FOR_EXCEPTION( |
| 788 |
(int) alpha.size () != numvecs, EpetraMultiVecFailure, |
| 789 |
"Belos::MultiVecTraits<double,Epetra_MultiVector>::MvScale: " |
| 790 |
"Array alpha of scaling coefficients has " << alpha.size () |
| 791 |
<< " entries, which is not the same as the number of columns " |
| 792 |
<< numvecs << " in the input multivector mv."); |
| 793 |
|
| 794 |
int info = 0; |
| 795 |
std::vector<int> tmp_index (1, 0); |
| 796 |
for (int i = 0; i < numvecs; ++i) { |
| 797 |
Epetra_MultiVector temp_vec (BELOSEPETRAVIEW, mv, &tmp_index[0], 1); |
| 798 |
info = temp_vec.Scale (alpha[i]); |
| 799 |
TEUCHOS_TEST_FOR_EXCEPTION(info != 0, EpetraMultiVecFailure, |
| 800 |
"Belos::MultiVecTraits<double,Epetra_MultiVector>::MvScale: " |
| 801 |
"On column " << (i+1) << " of " << numvecs << ", Epetra_Multi" |
| 802 |
"Vector::Scale() returned a nonzero value info=" << info << "."); |
| 803 |
tmp_index[0]++; |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
//! <tt>B := alpha * A^T * mv</tt>. |
| 808 |
static void MvTransMv( const double alpha, const Epetra_MultiVector& A, const Epetra_MultiVector& mv, Teuchos::SerialDenseMatrix<int,double>& B ) |
| 809 |
{ |
| 810 |
Epetra_LocalMap LocalMap(B.numRows(), 0, mv.Map().Comm()); |
| 811 |
Epetra_MultiVector B_Pvec(BELOSEPETRAVIEW, LocalMap, B.values(), B.stride(), B.numCols()); |
| 812 |
|
| 813 |
const int info = B_Pvec.Multiply ('T', 'N', alpha, A, mv, 0.0); |
| 814 |
TEUCHOS_TEST_FOR_EXCEPTION(info != 0, EpetraMultiVecFailure, |
| 815 |
"Belos::MultiVecTraits<double,Epetra_MultiVector>::MvTransMv: " |
| 816 |
"Epetra_MultiVector::Multiply() returned a nonzero value info=" |
| 817 |
<< info << "."); |
| 818 |
} |
| 819 |
|
| 820 |
//! For all columns j of mv, set <tt>b[j] := mv[j]^T * A[j]</tt>. |
| 821 |
static void |
| 822 |
MvDot (const Epetra_MultiVector& mv, |
| 823 |
const Epetra_MultiVector& A, |
| 824 |
std::vector<double>& b) |
| 825 |
{ |
| 826 |
const int info = mv.Dot (A, &b[0]); |
| 827 |
TEUCHOS_TEST_FOR_EXCEPTION(info != 0, EpetraMultiVecFailure, |
| 828 |
"Belos::MultiVecTraits<double,Epetra_MultiVector>::MvDot: " |
| 829 |
"Epetra_MultiVector::Dot() returned a nonzero value info=" |
| 830 |
<< info << "."); |
| 831 |
} |
| 832 |
|
| 833 |
//! For all columns j of mv, set <tt>normvec[j] = norm(mv[j])</tt>. |
| 834 |
static void |
| 835 |
MvNorm (const Epetra_MultiVector& mv, |
| 836 |
std::vector<double>& normvec, |
| 837 |
NormType type = TwoNorm) |
| 838 |
{ |
| 839 |
if ((int)normvec.size() >= mv.NumVectors()) { |
| 840 |
int info = 0; |
| 841 |
switch( type ) { |
| 842 |
case ( OneNorm ) : |
| 843 |
info = mv.Norm1(&normvec[0]); |
| 844 |
break; |
| 845 |
case ( TwoNorm ) : |
| 846 |
info = mv.Norm2(&normvec[0]); |
| 847 |
break; |
| 848 |
case ( InfNorm ) : |
| 849 |
info = mv.NormInf(&normvec[0]); |
| 850 |
break; |
| 851 |
default: |
| 852 |
break; |
| 853 |
} |
| 854 |
TEUCHOS_TEST_FOR_EXCEPTION(info != 0, EpetraMultiVecFailure, |
| 855 |
"Belos::MultiVecTraits<double,Epetra_MultiVector>::MvNorm: " |
| 856 |
"Epetra_MultiVector::Norm() returned a nonzero value info=" |
| 857 |
<< info << "."); |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
static void |
| 862 |
SetBlock (const Epetra_MultiVector& A, |
| 863 |
const std::vector<int>& index, |
| 864 |
Epetra_MultiVector& mv) |
| 865 |
{ |
| 866 |
const int inNumVecs = GetNumberVecs (A); |
| 867 |
const int outNumVecs = index.size(); |
| 868 |
|
| 869 |
// NOTE (mfh 13 Jan 2011) Belos allows A to have more columns |
| 870 |
// than index.size(), in which case we just take the first |
| 871 |
// index.size() columns of A. Anasazi requires that A have the |
| 872 |
// same number of columns as index.size(). Changing Anasazi's |
| 873 |
// behavior should not break existing Anasazi solvers, but the |
| 874 |
// tests need to be done. |
| 875 |
if (inNumVecs < outNumVecs) |
| 876 |
{ |
| 877 |
std::ostringstream os; |
| 878 |
os << "Belos::MultiVecTraits<double,Epetra_MultiVector>::" |
| 879 |
"SetBlock(A, mv, index = {"; |
| 880 |
if (outNumVecs > 0) |
| 881 |
{ |
| 882 |
for (int k = 0; k < outNumVecs - 1; ++k) |
| 883 |
os << index[k] << ", "; |
| 884 |
os << index[outNumVecs-1]; |
| 885 |
} |
| 886 |
os << "}): A has only " << inNumVecs << " columns, but there are " |
| 887 |
<< outNumVecs << " indices in the index vector."; |
| 888 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, os.str()); |
| 889 |
} |
| 890 |
// Make a view of the columns of mv indicated by the index std::vector. |
| 891 |
Teuchos::RCP<Epetra_MultiVector> mv_view = CloneViewNonConst (mv, index); |
| 892 |
|
| 893 |
// View of columns [0, outNumVecs-1] of the source multivector A. |
| 894 |
// If A has fewer columns than mv_view, then create a view of |
| 895 |
// the first outNumVecs columns of A. |
| 896 |
Teuchos::RCP<const Epetra_MultiVector> A_view; |
| 897 |
if (outNumVecs == inNumVecs) |
| 898 |
A_view = Teuchos::rcpFromRef (A); // Const, non-owning RCP |
| 899 |
else |
| 900 |
A_view = CloneView (A, Teuchos::Range1D(0, outNumVecs - 1)); |
| 901 |
|
| 902 |
// Assignment calls Epetra_MultiVector::Assign(), which deeply |
| 903 |
// copies the data directly, ignoring the underlying |
| 904 |
// Epetra_Map(s). If A and mv don't have the same data |
| 905 |
// distribution (Epetra_Map), this may result in incorrect or |
| 906 |
// undefined behavior. Epetra_MultiVector::Update() also |
| 907 |
// ignores the Epetra_Maps, so we might as well just use the |
| 908 |
// (perhaps slightly cheaper) Assign() method via operator=(). |
| 909 |
*mv_view = *A_view; |
| 910 |
} |
| 911 |
|
| 912 |
static void |
| 913 |
SetBlock (const Epetra_MultiVector& A, |
| 914 |
const Teuchos::Range1D& index, |
| 915 |
Epetra_MultiVector& mv) |
| 916 |
{ |
| 917 |
const int numColsA = A.NumVectors(); |
| 918 |
const int numColsMv = mv.NumVectors(); |
| 919 |
// 'index' indexes into mv; it's the index set of the target. |
| 920 |
const bool validIndex = index.lbound() >= 0 && index.ubound() < numColsMv; |
| 921 |
// We can't take more columns out of A than A has. |
| 922 |
const bool validSource = index.size() <= numColsA; |
| 923 |
|
| 924 |
if (! validIndex || ! validSource) |
| 925 |
{ |
| 926 |
std::ostringstream os; |
| 927 |
os << "Belos::MultiVecTraits<double, Epetra_MultiVector>::SetBlock" |
| 928 |
"(A, index=[" << index.lbound() << ", " << index.ubound() << "], " |
| 929 |
"mv): "; |
| 930 |
TEUCHOS_TEST_FOR_EXCEPTION(index.lbound() < 0, std::invalid_argument, |
| 931 |
os.str() << "Range lower bound must be nonnegative."); |
| 932 |
TEUCHOS_TEST_FOR_EXCEPTION(index.ubound() >= numColsMv, std::invalid_argument, |
| 933 |
os.str() << "Range upper bound must be less than " |
| 934 |
"the number of columns " << numColsA << " in the " |
| 935 |
"'mv' output argument."); |
| 936 |
TEUCHOS_TEST_FOR_EXCEPTION(index.size() > numColsA, std::invalid_argument, |
| 937 |
os.str() << "Range must have no more elements than" |
| 938 |
" the number of columns " << numColsA << " in the " |
| 939 |
"'A' input argument."); |
| 940 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Should never get here!"); |
| 941 |
} |
| 942 |
|
| 943 |
// View of columns [index.lbound(), index.ubound()] of the |
| 944 |
// target multivector mv. We avoid view creation overhead by |
| 945 |
// only creating a view if the index range is different than [0, |
| 946 |
// (# columns in mv) - 1]. |
| 947 |
Teuchos::RCP<Epetra_MultiVector> mv_view; |
| 948 |
if (index.lbound() == 0 && index.ubound()+1 == numColsMv) |
| 949 |
mv_view = Teuchos::rcpFromRef (mv); // Non-const, non-owning RCP |
| 950 |
else |
| 951 |
mv_view = CloneViewNonConst (mv, index); |
| 952 |
|
| 953 |
// View of columns [0, index.size()-1] of the source multivector |
| 954 |
// A. If A has fewer columns than mv_view, then create a view |
| 955 |
// of the first index.size() columns of A. |
| 956 |
Teuchos::RCP<const Epetra_MultiVector> A_view; |
| 957 |
if (index.size() == numColsA) |
| 958 |
A_view = Teuchos::rcpFromRef (A); // Const, non-owning RCP |
| 959 |
else |
| 960 |
A_view = CloneView (A, Teuchos::Range1D(0, index.size()-1)); |
| 961 |
|
| 962 |
// Assignment calls Epetra_MultiVector::Assign(), which deeply |
| 963 |
// copies the data directly, ignoring the underlying |
| 964 |
// Epetra_Map(s). If A and mv don't have the same data |
| 965 |
// distribution (Epetra_Map), this may result in incorrect or |
| 966 |
// undefined behavior. Epetra_MultiVector::Update() also |
| 967 |
// ignores the Epetra_Maps, so we might as well just use the |
| 968 |
// (perhaps slightly cheaper) Assign() method via operator=(). |
| 969 |
*mv_view = *A_view; |
| 970 |
} |
| 971 |
|
| 972 |
static void |
| 973 |
Assign (const Epetra_MultiVector& A, |
| 974 |
Epetra_MultiVector& mv) |
| 975 |
{ |
| 976 |
const int numColsA = GetNumberVecs (A); |
| 977 |
const int numColsMv = GetNumberVecs (mv); |
| 978 |
if (numColsA > numColsMv) |
| 979 |
{ |
| 980 |
std::ostringstream os; |
| 981 |
os << "Belos::MultiVecTraits<double, Epetra_MultiVector>::Assign" |
| 982 |
"(A, mv): "; |
| 983 |
TEUCHOS_TEST_FOR_EXCEPTION(numColsA > numColsMv, std::invalid_argument, |
| 984 |
os.str() << "Input multivector 'A' has " |
| 985 |
<< numColsA << " columns, but output multivector " |
| 986 |
"'mv' has only " << numColsMv << " columns."); |
| 987 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Should never get here!"); |
| 988 |
} |
| 989 |
// View of the first [0, numColsA-1] columns of mv. |
| 990 |
Teuchos::RCP<Epetra_MultiVector> mv_view; |
| 991 |
if (numColsMv == numColsA) |
| 992 |
mv_view = Teuchos::rcpFromRef (mv); // Non-const, non-owning RCP |
| 993 |
else // numColsMv > numColsA |
| 994 |
mv_view = CloneView (mv, Teuchos::Range1D(0, numColsA - 1)); |
| 995 |
|
| 996 |
// Assignment calls Epetra_MultiVector::Assign(), which deeply |
| 997 |
// copies the data directly, ignoring the underlying |
| 998 |
// Epetra_Map(s). If A and mv don't have the same data |
| 999 |
// distribution (Epetra_Map), this may result in incorrect or |
| 1000 |
// undefined behavior. Epetra_MultiVector::Update() also |
| 1001 |
// ignores the Epetra_Maps, so we might as well just use the |
| 1002 |
// (perhaps slightly cheaper) Assign() method via operator=(). |
| 1003 |
*mv_view = A; |
| 1004 |
} |
| 1005 |
|
| 1006 |
static void MvRandom (Epetra_MultiVector& mv) |
| 1007 |
{ |
| 1008 |
TEUCHOS_TEST_FOR_EXCEPTION( mv.Random()!=0, EpetraMultiVecFailure, |
| 1009 |
"Belos::MultiVecTraits<double,Epetra_MultiVector>::" |
| 1010 |
"MvRandom() call to Random() returned a nonzero value."); |
| 1011 |
} |
| 1012 |
|
| 1013 |
static void MvInit (Epetra_MultiVector& mv, |
| 1014 |
double alpha = Teuchos::ScalarTraits<double>::zero()) |
| 1015 |
{ |
| 1016 |
TEUCHOS_TEST_FOR_EXCEPTION( mv.PutScalar(alpha)!=0, EpetraMultiVecFailure, |
| 1017 |
"Belos::MultiVecTraits<double,Epetra_MultiVector>::" |
| 1018 |
"MvInit() call to PutScalar() returned a nonzero value."); |
| 1019 |
} |
| 1020 |
|
| 1021 |
static void MvPrint (const Epetra_MultiVector& mv, std::ostream& os) |
| 1022 |
{ |
| 1023 |
os << mv << std::endl; |
| 1024 |
} |
| 1025 |
|
| 1026 |
#ifdef HAVE_BELOS_TSQR |
| 1027 |
/// \typedef tsqr_adaptor_type |
| 1028 |
/// \brief TsqrAdaptor specialization for Epetra_MultiVector |
| 1029 |
/// |
| 1030 |
typedef Epetra::TsqrAdaptor tsqr_adaptor_type; |
| 1031 |
#endif // HAVE_BELOS_TSQR |
| 1032 |
}; |
| 1033 |
|
| 1034 |
|
| 1035 |
//////////////////////////////////////////////////////////////////// |
| 1036 |
// |
| 1037 |
// Implementation of the Belos::OperatorTraits for Epetra_Operator. |
| 1038 |
// |
| 1039 |
//////////////////////////////////////////////////////////////////// |
| 1040 |
|
| 1041 |
//! Specialization of OperatorTraits for Epetra_Operator. |
| 1042 |
template <> |
| 1043 |
class OperatorTraits <double, Epetra_MultiVector, Epetra_Operator> |
| 1044 |
{ |
| 1045 |
public: |
| 1046 |
|
| 1047 |
/// \brief Specialization of Apply() for Epetra_Operator. |
| 1048 |
/// |
| 1049 |
/// This method throws an EpetraOpFailure on failure. It fails in |
| 1050 |
/// either of the following two cases: |
| 1051 |
/// 1. If the operator's Epetra_Operator::Apply() method fails |
| 1052 |
/// (which it indicates by returning a nonzero value). |
| 1053 |
/// 2. If you attempt to apply the transpose and the underlying |
| 1054 |
/// operator does not implement the transpose. |
| 1055 |
/// |
| 1056 |
/// Epetra_Operator objects have a "persistent" transpose state, |
| 1057 |
/// which means that the input Op might already be set to use the |
| 1058 |
/// transpose. We assume in that case that trans=NOTRANS means |
| 1059 |
/// don't apply the transpose, and trans=TRANS means apply the |
| 1060 |
/// transpose (not the "transpose of the transpose"). Thus, trans |
| 1061 |
/// has its literal meaning. However, we leave Op on exit of this |
| 1062 |
/// routine with the same transpose setting that it had on entry. |
| 1063 |
/// |
| 1064 |
/// \note If you want to implement an operator that applies the |
| 1065 |
/// transpose to an existing operator A in a reversed sense |
| 1066 |
/// (i.e., "transposed" means not transposed, and "not |
| 1067 |
/// transposed" means transposed), you should write a class |
| 1068 |
/// implementing Epetra_Operator that wraps A. This class |
| 1069 |
/// should implement `Epetra_Operator::UseTranspose()` by |
| 1070 |
/// returning `!(A->UseTranspose())`, and should implement |
| 1071 |
/// `Epetra_Operator::SetUseTranspose(useTrans)` by calling |
| 1072 |
/// `A->SetUseTranspose(!useTrans)`. |
| 1073 |
static void |
| 1074 |
Apply (const Epetra_Operator& Op, |
| 1075 |
const Epetra_MultiVector& x, |
| 1076 |
Epetra_MultiVector& y, |
| 1077 |
ETrans trans=NOTRANS); |
| 1078 |
|
| 1079 |
//! Whether Op implements applying the transpose. |
| 1080 |
static bool |
| 1081 |
HasApplyTranspose (const Epetra_Operator& Op); |
| 1082 |
}; |
| 1083 |
|
| 1084 |
} // end of Belos namespace |
| 1085 |
|
| 1086 |
#endif |
| 1087 |
// end of file BELOS_EPETRA_ADAPTER_HPP |