| 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 |
|
| 11 |
/*! \file BelosEpetraAdapter.cpp |
| 12 |
\brief Implementation of the interfaces between Belos virtual classes and Epetra concrete classes. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "BelosEpetraAdapter.hpp" |
| 16 |
|
| 17 |
// mfh 19 Oct 2011: Enabling this compile-time option might help you |
| 18 |
// debug tricky issues involving Epetra operators and preconditioners. |
| 19 |
// For example, today we used this option to figure out that a user's |
| 20 |
// wrapped preconditioner was not correctly initializing its |
| 21 |
// UseTranspose() flag. |
| 22 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 23 |
# undef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 24 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 25 |
|
| 26 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 27 |
# include "Teuchos_oblackholestream.hpp" |
| 28 |
// We have to include this file, because we're calling a member |
| 29 |
// function of Epetra_Comm. |
| 30 |
# include "Epetra_Comm.h" |
| 31 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 32 |
|
| 33 |
|
| 34 |
namespace Belos { |
| 35 |
|
| 36 |
// An anonymous namespace restricts the scope of its definitions to |
| 37 |
// this file. |
| 38 |
namespace { |
| 39 |
|
| 40 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 41 |
/// \fn getErrStream |
| 42 |
/// \brief Get a stream for debugging output that prints only on MPI Rank 0. |
| 43 |
/// |
| 44 |
/// Rank 0 is relative to the Epetra_Operator's MPI communicator, |
| 45 |
/// which is why this function wants an Epetra_Operator input. |
| 46 |
/// |
| 47 |
/// \warning This function is NOT reentrant, because it has static data. |
| 48 |
Teuchos::RCP<std::ostream> |
| 49 |
getErrStream (const Epetra_Operator& Op) |
| 50 |
{ |
| 51 |
using Teuchos::RCP; |
| 52 |
using Teuchos::rcp; |
| 53 |
using Teuchos::rcpFromRef; |
| 54 |
|
| 55 |
// The static object makes this function NOT reentrant. |
| 56 |
static RCP<std::ostream> errStream; |
| 57 |
|
| 58 |
if (errStream.is_null()) { |
| 59 |
if (Op.Comm().MyPID() == 0) { |
| 60 |
errStream = rcpFromRef (std::cerr); |
| 61 |
} else { |
| 62 |
errStream = rcp (new Teuchos::oblackholestream); |
| 63 |
} |
| 64 |
} |
| 65 |
return errStream; |
| 66 |
} |
| 67 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 68 |
|
| 69 |
//! Return a string representation of the given transpose enum value. |
| 70 |
std::string |
| 71 |
etransToString (const ETrans trans) |
| 72 |
{ |
| 73 |
if (trans == NOTRANS) { |
| 74 |
return "NOTRANS"; |
| 75 |
} else if (trans == TRANS) { |
| 76 |
return "TRANS"; |
| 77 |
} else if (trans == CONJTRANS) { |
| 78 |
return "CONJTRANS"; |
| 79 |
} |
| 80 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, |
| 81 |
"Invalid ETrans value trans = " << trans << "."); |
| 82 |
} |
| 83 |
|
| 84 |
/// \fn implementsApplyTranspose |
| 85 |
/// \brief Whether Op implements applying the transpose. |
| 86 |
/// |
| 87 |
/// Epetra_Operator instances are not required to implement |
| 88 |
/// applying the transpose (or conjugate transpose, if |
| 89 |
/// applicable). This function lets you ask. |
| 90 |
/// |
| 91 |
/// We need this function because Epetra_Operator doesn't have a |
| 92 |
/// direct query method. We query by asking it to set using the |
| 93 |
/// transpose, and examine the returned error code. Epetra |
| 94 |
/// operators have a persistent "use the transpose" state that you |
| 95 |
/// can set or unset using Epetra_Operator::SetUseTranspose(). |
| 96 |
/// |
| 97 |
/// \note This function is not thread-safe, because it makes no |
| 98 |
/// effort to protect Op against simultaneous calls to its |
| 99 |
/// Apply() or SetUseTranspose() methods. |
| 100 |
/// |
| 101 |
/// We use this function multiple times in this file, so it's |
| 102 |
/// worthwhile breaking it out into a separate function, rather |
| 103 |
/// than copying and pasting. |
| 104 |
bool |
| 105 |
implementsApplyTranspose (const Epetra_Operator& Op) |
| 106 |
{ |
| 107 |
// We must first check Op's current transpose state, so that we |
| 108 |
// leave Op in the same state as it was given to us. |
| 109 |
const bool transposed = Op.UseTranspose (); |
| 110 |
|
| 111 |
// If Op is already transposed, then obviously it implements |
| 112 |
// applying the transpose. |
| 113 |
if (transposed) { |
| 114 |
return true; |
| 115 |
} |
| 116 |
|
| 117 |
// SetUseTranspose() is a nonconst method, so we have to cast |
| 118 |
// away const-ness of Op in order to call it. Epetra_Operator |
| 119 |
// gives us no other way to query whether it knows how to apply |
| 120 |
// the transpose. |
| 121 |
const int transposeFailed = |
| 122 |
const_cast<Epetra_Operator&>(Op).SetUseTranspose (true); |
| 123 |
|
| 124 |
// SetUseTranspose() follows the POSIX convention that returning |
| 125 |
// zero means success. If SetUseTranspose() failed, the best we can |
| 126 |
// do is assume that the state of Op was not changed. Any |
| 127 |
// reasonable implementation of SetUseTranspose() should not change |
| 128 |
// the state of the operator if setting the transpose failed. |
| 129 |
if (transposeFailed != 0) { |
| 130 |
// Make sure that the failed call didn't managed to change the |
| 131 |
// state of Op. It shouldn't have, but if it did, then Op is |
| 132 |
// broken. |
| 133 |
const bool currentTransposedState = Op.UseTranspose(); |
| 134 |
TEUCHOS_TEST_FOR_EXCEPTION(currentTransposedState != transposed, std::logic_error, |
| 135 |
"implementsApplyTranspose: Op.SetUseTranspose(true) " |
| 136 |
"failed (with error code " << transposeFailed << "), " |
| 137 |
"but nevertheless managed to change the transposed " |
| 138 |
"state of Op. On input, Op.UseTranspose() = " |
| 139 |
<< transposed << ", but now, Op.UseTranspose() = " |
| 140 |
<< currentTransposedState << ". This likely indicates" |
| 141 |
" a bug in the implementation of the operator Op."); |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
// Now we need to restore the original transpose state. We can |
| 146 |
// do this unconditionally because if we're here, Op's state is |
| 147 |
// not transposed. |
| 148 |
{ |
| 149 |
const int errcode = |
| 150 |
const_cast<Epetra_Operator&>(Op).SetUseTranspose (false); |
| 151 |
TEUCHOS_TEST_FOR_EXCEPTION(errcode != 0, std::logic_error, |
| 152 |
"implementsApplyTranspose: Op.SetUseTranspose(true) " |
| 153 |
"succeeded, but Op.SetUseTranspose(false) failed with " |
| 154 |
"error code " << errcode << ". This likely indicates " |
| 155 |
"a bug in the implementation of the operator Op."); |
| 156 |
} |
| 157 |
// If we made it this far, then Op implements transpose. |
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
/// \class EpetraOperatorTransposeScopeGuard |
| 162 |
/// \brief Safely sets and unsets an Epetra_Operator's transpose state. |
| 163 |
/// |
| 164 |
/// This class uses the RAII (Resource Acquisition Is |
| 165 |
/// Instantiation) technique to set the transpose state of an |
| 166 |
/// Epetra_Operator instance on construction, and restore its |
| 167 |
/// original transpose state on destruction. |
| 168 |
class EpetraOperatorTransposeScopeGuard { |
| 169 |
private: |
| 170 |
//! Reference to the Epetra_Operator instance to protect. |
| 171 |
const Epetra_Operator& Op_; |
| 172 |
|
| 173 |
//! The original transpose state of the operator. |
| 174 |
const bool originalTransposeFlag_; |
| 175 |
|
| 176 |
/// \brief Whether we want to apply the transpose of the operator. |
| 177 |
/// |
| 178 |
/// Recall that Epetra operators are always real-valued, never |
| 179 |
/// complex-valued, so the conjugate transpose means the same |
| 180 |
/// thing as the transpose. |
| 181 |
const bool newTransposeFlag_; |
| 182 |
|
| 183 |
public: |
| 184 |
/// \brief Constructor. |
| 185 |
/// |
| 186 |
/// \param Op [in/out] The operator whose transpose state to guard. |
| 187 |
/// |
| 188 |
/// \param trans [in] True if we want to set Op to apply the |
| 189 |
/// transpose, else false. |
| 190 |
EpetraOperatorTransposeScopeGuard (const Epetra_Operator& Op, |
| 191 |
const ETrans trans) |
| 192 |
: Op_ (Op), |
| 193 |
originalTransposeFlag_ (Op.UseTranspose ()), |
| 194 |
newTransposeFlag_ (trans != NOTRANS) |
| 195 |
{ |
| 196 |
// If necessary, set (or unset) the transpose flag to the value |
| 197 |
// corresponding to 'trans'. |
| 198 |
if (newTransposeFlag_ != originalTransposeFlag_) { |
| 199 |
|
| 200 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 201 |
using std::endl; |
| 202 |
std::ostream& err = *(getErrStream (Op_)); |
| 203 |
err << "---- EpetraOperatorTransposeScopeGuard constructor:" << endl |
| 204 |
<< "------ Toggling transpose flag from " << originalTransposeFlag_ |
| 205 |
<< " to " << newTransposeFlag_ << endl; |
| 206 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 207 |
|
| 208 |
// Toggle the transpose flag. The destructor will restore |
| 209 |
// its original value. |
| 210 |
const int info = |
| 211 |
const_cast<Epetra_Operator &>(Op_).SetUseTranspose (newTransposeFlag_); |
| 212 |
TEUCHOS_TEST_FOR_EXCEPTION(info != 0, EpetraOpFailure, |
| 213 |
"Toggling the transpose flag of the operator failed, " |
| 214 |
"returning a nonzero error code of " << info |
| 215 |
<< ". That probably means that the Epetra_Operator " |
| 216 |
"subclass instance doesn't know how to apply its " |
| 217 |
"transpose. Are you perhaps using a subclass of " |
| 218 |
"Epetra_Operator for which applying the transpose " |
| 219 |
"is not implemented? Anyway, just to help with " |
| 220 |
"debugging, you specified trans=" |
| 221 |
<< etransToString (trans) |
| 222 |
<< ", and the original operator was set " |
| 223 |
<< (originalTransposeFlag_ ? "" : "NOT ") |
| 224 |
<< "to apply the transpose."); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
~EpetraOperatorTransposeScopeGuard () |
| 229 |
{ |
| 230 |
// SetUseTranspose() changes the state of the operator, so if |
| 231 |
// applicable, we have to change the state back. |
| 232 |
if (newTransposeFlag_ != originalTransposeFlag_) { |
| 233 |
|
| 234 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 235 |
using std::endl; |
| 236 |
std::ostream& err = *(getErrStream (Op_)); |
| 237 |
err << "---- EpetraOperatorTransposeScopeGuard destructor:" << endl |
| 238 |
<< "------ Toggling transpose flag from " << newTransposeFlag_ |
| 239 |
<< " to " << originalTransposeFlag_ << endl; |
| 240 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 241 |
|
| 242 |
const int info = |
| 243 |
const_cast<Epetra_Operator &>(Op_).SetUseTranspose (originalTransposeFlag_); |
| 244 |
TEUCHOS_TEST_FOR_TERMINATION(info != 0, |
| 245 |
"Resetting the original " |
| 246 |
"transpose flag value of the Epetra_Operator failed, " |
| 247 |
"returning a nonzero error code of " << info << ". " |
| 248 |
"This is an unusual error, since we were able to call " |
| 249 |
"its SetUseTranspose() method successfully before. " |
| 250 |
"This suggests a bug in the subclass of Epetra_Operator " |
| 251 |
"which you are currently using. This is probably not a " |
| 252 |
"Belos bug."); |
| 253 |
} |
| 254 |
|
| 255 |
// Make sure that the transpose flag has its original value. If |
| 256 |
// not, we throw std::logic_error instead of EpetraOpFailure, since |
| 257 |
// that's definitely a bug. It's safe to do this check whether or |
| 258 |
// not we actually had to toggle the transpose flag. Any reasonable |
| 259 |
// implementation of Epetra_Operator should make calling |
| 260 |
// UseTranspose() cheap. |
| 261 |
// |
| 262 |
// Note to code maintainers: The reason we capture the value of |
| 263 |
// UseTranspose() instead of just calling it twice, is that if the |
| 264 |
// exception test really does trigger, then there is something |
| 265 |
// fundamentally wrong. If something is that wrong, then we want to |
| 266 |
// avoid further calls to the operator's methods. For example, it |
| 267 |
// could be that the UseTranspose() method is erroneously changing |
| 268 |
// the value of the flag, so if we call that method twice, it might |
| 269 |
// have the right value on the second call. This would make the |
| 270 |
// resulting exception message confusing. |
| 271 |
const bool finalTransposeFlag = Op_.UseTranspose (); |
| 272 |
TEUCHOS_TEST_FOR_TERMINATION(originalTransposeFlag_ != finalTransposeFlag, |
| 273 |
"Belos::OperatorTraits::Apply: The value of the " |
| 274 |
"Epetra_Operator's transpose flag changed unexpectedly!" |
| 275 |
" The original value at the top of this method was " |
| 276 |
<< originalTransposeFlag_ << ", and its new value is " |
| 277 |
<< finalTransposeFlag << ". This suggests either a " |
| 278 |
"bug in Belos (in the implementation of this routine), " |
| 279 |
"or a bug in the operator."); |
| 280 |
} |
| 281 |
}; |
| 282 |
|
| 283 |
} // namespace (anonymous) |
| 284 |
|
| 285 |
|
| 286 |
EpetraMultiVec::EpetraMultiVec (const Epetra_BlockMap& Map_in, |
| 287 |
double * array, |
| 288 |
const int numvecs, |
| 289 |
const int stride) |
| 290 |
: Epetra_MultiVector(BELOSEPETRACOPY, Map_in, array, stride, numvecs) |
| 291 |
{ |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
EpetraMultiVec::EpetraMultiVec (const Epetra_BlockMap& Map_in, |
| 296 |
const int numvecs, |
| 297 |
bool zeroOut) |
| 298 |
: Epetra_MultiVector(Map_in, numvecs, zeroOut) |
| 299 |
{ |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
EpetraMultiVec::EpetraMultiVec (Epetra_DataAccess CV_in, |
| 304 |
const Epetra_MultiVector& P_vec, |
| 305 |
const std::vector<int>& index ) |
| 306 |
: Epetra_MultiVector(CV_in, P_vec, &(const_cast<std::vector<int> &>(index))[0], index.size()) |
| 307 |
{ |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
EpetraMultiVec::EpetraMultiVec(const Epetra_MultiVector& P_vec) |
| 312 |
: Epetra_MultiVector(P_vec) |
| 313 |
{ |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
EpetraMultiVec::~EpetraMultiVec() |
| 318 |
{ |
| 319 |
} |
| 320 |
// |
| 321 |
// member functions inherited from Belos::MultiVec |
| 322 |
// |
| 323 |
// |
| 324 |
// Simulating a virtual copy constructor. If we could rely on the co-variance |
| 325 |
// of virtual functions, we could return a pointer to EpetraMultiVec |
| 326 |
// (the derived type) instead of a pointer to the pure virtual base class. |
| 327 |
// |
| 328 |
|
| 329 |
MultiVec<double>* EpetraMultiVec::Clone ( const int numvecs ) const |
| 330 |
{ |
| 331 |
EpetraMultiVec * ptr_apv = new EpetraMultiVec(Map(), numvecs, false); |
| 332 |
return ptr_apv; // safe upcast. |
| 333 |
} |
| 334 |
// |
| 335 |
// the following is a virtual copy constructor returning |
| 336 |
// a pointer to the pure virtual class. vector values are |
| 337 |
// copied. |
| 338 |
// |
| 339 |
|
| 340 |
MultiVec<double>* EpetraMultiVec::CloneCopy() const |
| 341 |
{ |
| 342 |
EpetraMultiVec *ptr_apv = new EpetraMultiVec(*this); |
| 343 |
return ptr_apv; // safe upcast |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
MultiVec<double>* EpetraMultiVec::CloneCopy ( const std::vector<int>& index ) const |
| 348 |
{ |
| 349 |
EpetraMultiVec * ptr_apv = new EpetraMultiVec(BELOSEPETRACOPY, *this, index); |
| 350 |
return ptr_apv; // safe upcast. |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
MultiVec<double>* EpetraMultiVec::CloneViewNonConst ( const std::vector<int>& index ) |
| 355 |
{ |
| 356 |
EpetraMultiVec * ptr_apv = new EpetraMultiVec(BELOSEPETRAVIEW, *this, index); |
| 357 |
return ptr_apv; // safe upcast. |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
const MultiVec<double>* |
| 362 |
EpetraMultiVec::CloneView (const std::vector<int>& index) const |
| 363 |
{ |
| 364 |
EpetraMultiVec * ptr_apv = new EpetraMultiVec(BELOSEPETRAVIEW, *this, index); |
| 365 |
return ptr_apv; // safe upcast. |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
void |
| 370 |
EpetraMultiVec::SetBlock (const MultiVec<double>& A, |
| 371 |
const std::vector<int>& index) |
| 372 |
{ |
| 373 |
EpetraMultiVec temp_vec(BELOSEPETRAVIEW, *this, index); |
| 374 |
|
| 375 |
int numvecs = index.size(); |
| 376 |
if ( A.GetNumberVecs() != numvecs ) { |
| 377 |
std::vector<int> index2( numvecs ); |
| 378 |
for(int i=0; i<numvecs; i++) |
| 379 |
index2[i] = i; |
| 380 |
EpetraMultiVec *tmp_vec = |
| 381 |
dynamic_cast<EpetraMultiVec *>(&const_cast<MultiVec<double> &>(A)); |
| 382 |
TEUCHOS_TEST_FOR_EXCEPTION(tmp_vec==NULL, EpetraMultiVecFailure, |
| 383 |
"Belos::EpetraMultiVec::SetBlock: Dynamic cast from " |
| 384 |
"Belos::MultiVec<double> to Belos::EpetraMultiVec " |
| 385 |
"failed."); |
| 386 |
EpetraMultiVec A_vec(BELOSEPETRAVIEW, *tmp_vec, index2); |
| 387 |
temp_vec.MvAddMv( 1.0, A_vec, 0.0, A_vec ); |
| 388 |
} |
| 389 |
else { |
| 390 |
temp_vec.MvAddMv( 1.0, A, 0.0, A ); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
//------------------------------------------------------------- |
| 395 |
// |
| 396 |
// *this <- alpha * A * B + beta * (*this) |
| 397 |
// |
| 398 |
//------------------------------------------------------------- |
| 399 |
|
| 400 |
void EpetraMultiVec::MvTimesMatAddMv ( const double alpha, const MultiVec<double>& A, |
| 401 |
const Teuchos::SerialDenseMatrix<int,double>& B, const double beta ) |
| 402 |
{ |
| 403 |
Epetra_LocalMap LocalMap(B.numRows(), 0, Map().Comm()); |
| 404 |
Epetra_MultiVector B_Pvec(BELOSEPETRAVIEW, LocalMap, B.values(), B.stride(), B.numCols()); |
| 405 |
|
| 406 |
EpetraMultiVec *A_vec = dynamic_cast<EpetraMultiVec *>(&const_cast<MultiVec<double> &>(A)); |
| 407 |
TEUCHOS_TEST_FOR_EXCEPTION(A_vec==NULL, EpetraMultiVecFailure, |
| 408 |
"Belos::EpetraMultiVec::MvTimesMatAddMv cast from Belos::MultiVec<> to Belos::EpetraMultiVec failed."); |
| 409 |
|
| 410 |
int info = Multiply( 'N', 'N', alpha, *A_vec, B_Pvec, beta ); |
| 411 |
TEUCHOS_TEST_FOR_EXCEPTION(info!=0, EpetraMultiVecFailure, |
| 412 |
"Belos::EpetraMultiVec::MvTimesMatAddMv call to Multiply() returned a nonzero value."); |
| 413 |
|
| 414 |
} |
| 415 |
|
| 416 |
//------------------------------------------------------------- |
| 417 |
// |
| 418 |
// *this <- alpha * A + beta * B |
| 419 |
// |
| 420 |
//------------------------------------------------------------- |
| 421 |
|
| 422 |
void EpetraMultiVec::MvAddMv ( const double alpha , const MultiVec<double>& A, |
| 423 |
const double beta, const MultiVec<double>& B) |
| 424 |
{ |
| 425 |
EpetraMultiVec *A_vec = dynamic_cast<EpetraMultiVec *>(&const_cast<MultiVec<double> &>(A)); |
| 426 |
TEUCHOS_TEST_FOR_EXCEPTION( A_vec==NULL, EpetraMultiVecFailure, |
| 427 |
"Belos::EpetraMultiVec::MvAddMv cast from Belos::MultiVec<> to Belos::EpetraMultiVec failed."); |
| 428 |
EpetraMultiVec *B_vec = dynamic_cast<EpetraMultiVec *>(&const_cast<MultiVec<double> &>(B)); |
| 429 |
TEUCHOS_TEST_FOR_EXCEPTION( B_vec==NULL, EpetraMultiVecFailure, |
| 430 |
"Belos::EpetraMultiVec::MvAddMv cast from Belos::MultiVec<> to Belos::EpetraMultiVec failed."); |
| 431 |
|
| 432 |
int info = Update( alpha, *A_vec, beta, *B_vec, 0.0 ); |
| 433 |
TEUCHOS_TEST_FOR_EXCEPTION(info!=0, EpetraMultiVecFailure, |
| 434 |
"Belos::EpetraMultiVec::MvAddMv call to Update() returned a nonzero value."); |
| 435 |
} |
| 436 |
|
| 437 |
//------------------------------------------------------------- |
| 438 |
// |
| 439 |
// this[i] = alpha[i] * this[i] |
| 440 |
// |
| 441 |
//------------------------------------------------------------- |
| 442 |
void EpetraMultiVec::MvScale ( const std::vector<double>& alpha ) |
| 443 |
{ |
| 444 |
// Check to make sure the input vector of scale factors has the same |
| 445 |
// number of entries as the number of columns in the multivector. |
| 446 |
int numvecs = this->NumVectors(); |
| 447 |
TEUCHOS_TEST_FOR_EXCEPTION((int)alpha.size() != numvecs, EpetraMultiVecFailure, |
| 448 |
"Belos::MultiVecTraits<double,Epetra_MultiVec>::MvScale: " |
| 449 |
"Vector (alpha) of scaling factors has length " |
| 450 |
<< alpha.size() << ", which is different than the number " |
| 451 |
"of columns " << numvecs << " in the input multivector " |
| 452 |
"(mv)."); |
| 453 |
int ret = 0; |
| 454 |
std::vector<int> tmp_index( 1, 0 ); |
| 455 |
for (int i=0; i<numvecs; i++) { |
| 456 |
Epetra_MultiVector temp_vec(BELOSEPETRAVIEW, *this, &tmp_index[0], 1); |
| 457 |
ret = temp_vec.Scale( alpha[i] ); |
| 458 |
TEUCHOS_TEST_FOR_EXCEPTION(ret!=0, EpetraMultiVecFailure, |
| 459 |
"Belos::MultiVecTraits<double,Epetra_MultiVec>::MvScale: " |
| 460 |
"Call to Epetra_MultiVector::Scale() returned a nonzero " |
| 461 |
"error code " << ret << "."); |
| 462 |
tmp_index[0]++; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
//------------------------------------------------------------- |
| 467 |
// |
| 468 |
// dense B <- alpha * A^T * (*this) |
| 469 |
// |
| 470 |
//------------------------------------------------------------- |
| 471 |
|
| 472 |
void EpetraMultiVec::MvTransMv ( const double alpha, const MultiVec<double>& A, |
| 473 |
Teuchos::SerialDenseMatrix<int,double>& B) const |
| 474 |
{ |
| 475 |
EpetraMultiVec *A_vec = dynamic_cast<EpetraMultiVec *>(&const_cast<MultiVec<double> &>(A)); |
| 476 |
|
| 477 |
if (A_vec) { |
| 478 |
Epetra_LocalMap LocalMap(B.numRows(), 0, Map().Comm()); |
| 479 |
Epetra_MultiVector B_Pvec(BELOSEPETRAVIEW, LocalMap, B.values(), B.stride(), B.numCols()); |
| 480 |
|
| 481 |
int info = B_Pvec.Multiply( 'T', 'N', alpha, *A_vec, *this, 0.0 ); |
| 482 |
TEUCHOS_TEST_FOR_EXCEPTION(info!=0, EpetraMultiVecFailure, |
| 483 |
"Belos::EpetraMultiVec::MvTransMv call to Multiply() returned a nonzero value."); |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
//------------------------------------------------------------- |
| 488 |
// |
| 489 |
// b[i] = A[i]^T * this[i] |
| 490 |
// |
| 491 |
//------------------------------------------------------------- |
| 492 |
|
| 493 |
void EpetraMultiVec::MvDot ( const MultiVec<double>& A, std::vector<double>& b ) const |
| 494 |
{ |
| 495 |
EpetraMultiVec *A_vec = dynamic_cast<EpetraMultiVec *>(&const_cast<MultiVec<double> &>(A)); |
| 496 |
TEUCHOS_TEST_FOR_EXCEPTION(A_vec==NULL, EpetraMultiVecFailure, |
| 497 |
"Belos::EpetraMultiVec::MvDot: Dynamic cast from Belos::" |
| 498 |
"MultiVec<double> to Belos::EpetraMultiVec failed."); |
| 499 |
if (A_vec && ( (int)b.size() >= A_vec->NumVectors() ) ) { |
| 500 |
int info = this->Dot( *A_vec, &b[0] ); |
| 501 |
TEUCHOS_TEST_FOR_EXCEPTION(info!=0, EpetraMultiVecFailure, |
| 502 |
"Belos::EpetraMultiVec::MvDot: Call to Epetra_Multi" |
| 503 |
"Vector::Dot() returned a nonzero error code " |
| 504 |
<< info << "."); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
//------------------------------------------------------------- |
| 509 |
// |
| 510 |
// alpha[i] = norm of i-th column of (*this) |
| 511 |
// |
| 512 |
//------------------------------------------------------------- |
| 513 |
|
| 514 |
void EpetraMultiVec::MvNorm ( std::vector<double>& normvec, NormType norm_type ) const { |
| 515 |
if ((int)normvec.size() >= GetNumberVecs()) { |
| 516 |
int info = 0; |
| 517 |
switch( norm_type ) { |
| 518 |
case ( OneNorm ) : |
| 519 |
info = Norm1(&normvec[0]); |
| 520 |
break; |
| 521 |
case ( TwoNorm ) : |
| 522 |
info = Norm2(&normvec[0]); |
| 523 |
break; |
| 524 |
case ( InfNorm ) : |
| 525 |
info = NormInf(&normvec[0]); |
| 526 |
break; |
| 527 |
default: |
| 528 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, |
| 529 |
"Belos::EpetraMultiVec::MvNorm: Invalid norm_type " |
| 530 |
<< norm_type << ". The current list of valid norm " |
| 531 |
"types is {OneNorm, TwoNorm, InfNorm}."); |
| 532 |
} |
| 533 |
TEUCHOS_TEST_FOR_EXCEPTION(info!=0, EpetraMultiVecFailure, |
| 534 |
"Belos::EpetraMultiVec::MvNorm: Call to Epetra_Multi" |
| 535 |
"Vector::Norm() returned a nonzero error code " |
| 536 |
<< info << "."); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
/////////////////////////////////////////////////////////////// |
| 541 |
// |
| 542 |
// Implementation of the Belos::EpetraOp class. |
| 543 |
// |
| 544 |
/////////////////////////////////////////////////////////////// |
| 545 |
|
| 546 |
EpetraOp::EpetraOp( const Teuchos::RCP<Epetra_Operator> &Op ) |
| 547 |
: Epetra_Op(Op) |
| 548 |
{ |
| 549 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 550 |
using std::endl; |
| 551 |
|
| 552 |
std::ostream& err = *(getErrStream (*Epetra_Op)); |
| 553 |
err << "Belos::EpetraOp constructor" << endl; |
| 554 |
err << "-- On input: Epetra_Op->UseTranspose() = " |
| 555 |
<< Epetra_Op->UseTranspose() << endl; |
| 556 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 557 |
} |
| 558 |
|
| 559 |
void |
| 560 |
EpetraOp::Apply (const MultiVec<double>& x, |
| 561 |
MultiVec<double>& y, |
| 562 |
ETrans trans) const |
| 563 |
{ |
| 564 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 565 |
using std::endl; |
| 566 |
|
| 567 |
std::ostream& err = *(getErrStream (*Epetra_Op)); |
| 568 |
err << "-- Belos::EpetraOp::Apply:" << endl |
| 569 |
<< "---- Implements Belos::Operator<double>::Apply()" << endl |
| 570 |
<< "---- trans input = " << etransToString (trans) << endl |
| 571 |
<< "---- On input: Epetra_Op->UseTranspose() = " |
| 572 |
<< Epetra_Op->UseTranspose() << endl; |
| 573 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 574 |
|
| 575 |
MultiVec<double> & temp_x = const_cast<MultiVec<double> &>(x); |
| 576 |
Epetra_MultiVector* vec_x = dynamic_cast<Epetra_MultiVector* >(&temp_x); |
| 577 |
Epetra_MultiVector* vec_y = dynamic_cast<Epetra_MultiVector* >(&y); |
| 578 |
|
| 579 |
TEUCHOS_TEST_FOR_EXCEPTION(vec_x==NULL || vec_y==NULL, EpetraOpFailure, |
| 580 |
"Belos::EpetraOp::Apply: x and/or y could not be " |
| 581 |
"dynamic cast to an Epetra_MultiVector."); |
| 582 |
|
| 583 |
// Temporarily set the transpose state of Op, if it's not the same |
| 584 |
// as trans, and restore it on exit of this scope. |
| 585 |
EpetraOperatorTransposeScopeGuard guard (*Epetra_Op, trans); |
| 586 |
|
| 587 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 588 |
err << "---- Before calling Apply: Epetra_Op->UseTranspose() = " |
| 589 |
<< Epetra_Op->UseTranspose() << endl; |
| 590 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 591 |
|
| 592 |
// Apply the operator to x and put the result in y. |
| 593 |
const int info = Epetra_Op->Apply (*vec_x, *vec_y); |
| 594 |
TEUCHOS_TEST_FOR_EXCEPTION(info!=0, EpetraOpFailure, |
| 595 |
"Belos::EpetraOp::Apply: The Epetra_Operator's Apply() " |
| 596 |
"method returned a nonzero error code of " << info << "."); |
| 597 |
} |
| 598 |
|
| 599 |
bool |
| 600 |
EpetraOp::HasApplyTranspose() const |
| 601 |
{ |
| 602 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 603 |
std::ostream& err = *(getErrStream (*Epetra_Op)); |
| 604 |
err << "-- Belos::EpetraOp::HasApplyTranspose" << std::endl; |
| 605 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 606 |
|
| 607 |
return implementsApplyTranspose (*Epetra_Op); |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
// /////////////////////////////////////////////////////////////////// |
| 612 |
// |
| 613 |
// Implementation of the Belos::EpetraPrecOp class. |
| 614 |
// |
| 615 |
// /////////////////////////////////////////////////////////////////// |
| 616 |
|
| 617 |
EpetraPrecOp::EpetraPrecOp (const Teuchos::RCP<Epetra_Operator> &Op) |
| 618 |
: Epetra_Op(Op) |
| 619 |
{ |
| 620 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 621 |
using std::endl; |
| 622 |
|
| 623 |
std::ostream& err = *(getErrStream (*Epetra_Op)); |
| 624 |
err << "-- Belos::EpetraPrecOp constructor" << endl |
| 625 |
<< "---- On input: Epetra_Op->UseTranspose() = " |
| 626 |
<< Epetra_Op->UseTranspose() << endl; |
| 627 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 628 |
} |
| 629 |
|
| 630 |
// The version of Apply() that takes an optional 'trans' argument and |
| 631 |
// returns void implements the Belos::Operator interface. |
| 632 |
void |
| 633 |
EpetraPrecOp::Apply (const MultiVec<double>& x, |
| 634 |
MultiVec<double>& y, |
| 635 |
ETrans trans) const |
| 636 |
{ |
| 637 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 638 |
using std::endl; |
| 639 |
|
| 640 |
std::ostream& err = *(getErrStream (*Epetra_Op)); |
| 641 |
err << "-- Belos::EpetraPrecOp::Apply:" << endl |
| 642 |
<< "---- Implements Belos::Operator<double>::Apply()" << endl |
| 643 |
<< "---- trans input = " << etransToString (trans) << endl |
| 644 |
<< "---- On input: Epetra_Op->UseTranspose() = " |
| 645 |
<< Epetra_Op->UseTranspose() << endl; |
| 646 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 647 |
|
| 648 |
MultiVec<double>& temp_x = const_cast<MultiVec<double> &>(x); |
| 649 |
Epetra_MultiVector* vec_x = dynamic_cast<Epetra_MultiVector* >(&temp_x); |
| 650 |
TEUCHOS_TEST_FOR_EXCEPTION(vec_x == NULL, EpetraOpFailure, |
| 651 |
"Belos::EpetraPrecOp::Apply: The MultiVec<double> input x " |
| 652 |
"cannot be dynamic cast to an Epetra_MultiVector."); |
| 653 |
Epetra_MultiVector* vec_y = dynamic_cast<Epetra_MultiVector* >(&y); |
| 654 |
TEUCHOS_TEST_FOR_EXCEPTION(vec_x == NULL, EpetraOpFailure, |
| 655 |
"Belos::EpetraPrecOp::Apply: The MultiVec<double> input y " |
| 656 |
"cannot be dynamic cast to an Epetra_MultiVector."); |
| 657 |
|
| 658 |
// Temporarily set the transpose state of Op, if it's not the same |
| 659 |
// as trans, and restore it on exit of this scope. |
| 660 |
EpetraOperatorTransposeScopeGuard guard (*Epetra_Op, trans); |
| 661 |
|
| 662 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 663 |
err << "---- Before calling ApplyInverse: Epetra_Op->UseTranspose() = " |
| 664 |
<< Epetra_Op->UseTranspose() << endl; |
| 665 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 666 |
|
| 667 |
// EpetraPrecOp's Apply() methods apply the inverse of the |
| 668 |
// underlying operator. This may not succeed for all |
| 669 |
// implementations of Epetra_Operator, so we have to check. |
| 670 |
const int info = Epetra_Op->ApplyInverse (*vec_x, *vec_y); |
| 671 |
TEUCHOS_TEST_FOR_EXCEPTION(info != 0, EpetraOpFailure, |
| 672 |
"Belos::EpetraPrecOp::Apply: Calling ApplyInverse() on the " |
| 673 |
"underlying Epetra_Operator object failed, returning a " |
| 674 |
"nonzero error code of " << info << ". This probably means" |
| 675 |
" that the underlying Epetra_Operator object doesn't know " |
| 676 |
"how to apply its inverse."); |
| 677 |
} |
| 678 |
|
| 679 |
// The version of Apply() that takes two arguments and returns int |
| 680 |
// implements the Epetra_Operator interface. |
| 681 |
int |
| 682 |
EpetraPrecOp::Apply (const Epetra_MultiVector &X, |
| 683 |
Epetra_MultiVector &Y) const |
| 684 |
{ |
| 685 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 686 |
using std::endl; |
| 687 |
|
| 688 |
std::ostream& err = *(getErrStream (*Epetra_Op)); |
| 689 |
err << "-- Belos::EpetraPrecOp::Apply:" << endl |
| 690 |
<< "---- Implements Epetra_Operator::Apply()" << endl |
| 691 |
<< "---- On input: Epetra_Op->UseTranspose() = " |
| 692 |
<< Epetra_Op->UseTranspose() << endl |
| 693 |
<< "---- Calling Epetra_Op->ApplyInverse (X, Y)" << endl; |
| 694 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 695 |
|
| 696 |
// This operation computes Y = A^{-1}*X. |
| 697 |
const int info = Epetra_Op->ApplyInverse( X, Y ); |
| 698 |
|
| 699 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 700 |
err << "---- Epetra_Op->ApplyInverse (X, Y) returned info = " << info << endl; |
| 701 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 702 |
|
| 703 |
return info; |
| 704 |
} |
| 705 |
|
| 706 |
// This implements Epetra_Operator::ApplyInverse(). |
| 707 |
int |
| 708 |
EpetraPrecOp::ApplyInverse (const Epetra_MultiVector &X, |
| 709 |
Epetra_MultiVector &Y) const |
| 710 |
{ |
| 711 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 712 |
using std::endl; |
| 713 |
|
| 714 |
std::ostream& err = *(getErrStream (*Epetra_Op)); |
| 715 |
err << "-- Belos::EpetraPrecOp::ApplyInverse:" << endl |
| 716 |
<< "---- Implements Epetra_Operator::ApplyInverse()" << endl |
| 717 |
<< "---- On input: Epetra_Op->UseTranspose() = " |
| 718 |
<< Epetra_Op->UseTranspose() << endl |
| 719 |
<< "---- Calling Epetra_Op->Apply (X, Y)" << endl; |
| 720 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 721 |
|
| 722 |
// This operation computes Y = A*X. |
| 723 |
const int info = Epetra_Op->Apply( X, Y ); |
| 724 |
|
| 725 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 726 |
err << "---- Epetra_Op->Apply (X, Y) returned info = " << info << endl; |
| 727 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 728 |
|
| 729 |
return info; |
| 730 |
} |
| 731 |
|
| 732 |
bool |
| 733 |
EpetraPrecOp::HasApplyTranspose() const |
| 734 |
{ |
| 735 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 736 |
std::ostream& err = *(getErrStream (*Epetra_Op)); |
| 737 |
err << "-- Belos::EpetraPrecOp::HasApplyTranspose" << std::endl; |
| 738 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 739 |
|
| 740 |
return implementsApplyTranspose (*Epetra_Op); |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
// /////////////////////////////////////////////////////////////////// |
| 745 |
// |
| 746 |
// Specialization of Belos::OperatorTraits for Epetra_Operator. |
| 747 |
// |
| 748 |
// /////////////////////////////////////////////////////////////////// |
| 749 |
|
| 750 |
void |
| 751 |
OperatorTraits<double, Epetra_MultiVector, Epetra_Operator>:: |
| 752 |
Apply (const Epetra_Operator& Op, |
| 753 |
const Epetra_MultiVector& x, |
| 754 |
Epetra_MultiVector& y, |
| 755 |
ETrans trans) |
| 756 |
{ |
| 757 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 758 |
using std::endl; |
| 759 |
|
| 760 |
std::ostream& err = *(getErrStream (Op)); |
| 761 |
err << "Belos::OperatorTraits<double, Epetra_MultiVector, Epetra_Operator>::Apply:" |
| 762 |
<< endl |
| 763 |
<< "-- trans input = " << etransToString (trans) << endl |
| 764 |
<< "-- On input: Op.UseTranspose() = " |
| 765 |
<< Op.UseTranspose() << endl; |
| 766 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 767 |
|
| 768 |
// Temporarily set the transpose state of Op, if it's not the same |
| 769 |
// as trans, and restore it on exit of this scope. |
| 770 |
EpetraOperatorTransposeScopeGuard guard (Op, trans); |
| 771 |
|
| 772 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 773 |
err << "-- Before calling Op.Apply: Op.UseTranspose() = " |
| 774 |
<< Op.UseTranspose() << endl; |
| 775 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 776 |
|
| 777 |
const int info = Op.Apply (x, y); |
| 778 |
TEUCHOS_TEST_FOR_EXCEPTION(info != 0, EpetraOpFailure, |
| 779 |
"Belos::OperatorTraits::Apply (Epetra specialization): " |
| 780 |
"Calling the Epetra_Operator object's Apply() method " |
| 781 |
"failed, returning a nonzero error code of " << info |
| 782 |
<< "."); |
| 783 |
} |
| 784 |
|
| 785 |
bool |
| 786 |
OperatorTraits<double, Epetra_MultiVector, Epetra_Operator>:: |
| 787 |
HasApplyTranspose (const Epetra_Operator& Op) |
| 788 |
{ |
| 789 |
#ifdef BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 790 |
std::ostream& err = *(getErrStream (Op)); |
| 791 |
err << "Belos::OperatorTraits<double, Epetra_MultiVector, Epetra_Operator>::" |
| 792 |
"HasApplyTranspose" << std::endl; |
| 793 |
#endif // BELOS_EPETRA_AGGRESSIVE_DEBUGGING |
| 794 |
|
| 795 |
return implementsApplyTranspose (Op); |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
} // end namespace Belos |