| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Stratimikos: Thyra-based strategies for linear solvers |
| 4 |
// |
| 5 |
// Copyright 2006 NTESS and the Stratimikos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#include "Thyra_AmesosLinearOpWithSolve.hpp" |
| 11 |
#include "Thyra_EpetraThyraWrappers.hpp" |
| 12 |
#include "Thyra_MultiVectorStdOps.hpp" |
| 13 |
#include "Epetra_MultiVector.h" |
| 14 |
#include "Teuchos_TimeMonitor.hpp" |
| 15 |
|
| 16 |
|
| 17 |
namespace Thyra { |
| 18 |
|
| 19 |
|
| 20 |
// Constructors/initializers/accessors |
| 21 |
|
| 22 |
|
| 23 |
AmesosLinearOpWithSolve::AmesosLinearOpWithSolve(): |
| 24 |
amesosSolverTransp_(Thyra::NOTRANS), |
| 25 |
amesosSolverScalar_(1.0) |
| 26 |
{} |
| 27 |
|
| 28 |
|
| 29 |
AmesosLinearOpWithSolve::AmesosLinearOpWithSolve( |
| 30 |
const Teuchos::RCP<const LinearOpBase<double> > &fwdOp, |
| 31 |
const Teuchos::RCP<const LinearOpSourceBase<double> > &fwdOpSrc, |
| 32 |
const Teuchos::RCP<Epetra_LinearProblem> &epetraLP, |
| 33 |
const Teuchos::RCP<Amesos_BaseSolver> &amesosSolver, |
| 34 |
const EOpTransp amesosSolverTransp, |
| 35 |
const double amesosSolverScalar |
| 36 |
) |
| 37 |
{ |
| 38 |
this->initialize(fwdOp,fwdOpSrc,epetraLP,amesosSolver, |
| 39 |
amesosSolverTransp,amesosSolverScalar); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
void AmesosLinearOpWithSolve::initialize( |
| 44 |
const Teuchos::RCP<const LinearOpBase<double> > &fwdOp, |
| 45 |
const Teuchos::RCP<const LinearOpSourceBase<double> > &fwdOpSrc, |
| 46 |
const Teuchos::RCP<Epetra_LinearProblem> &epetraLP, |
| 47 |
const Teuchos::RCP<Amesos_BaseSolver> &amesosSolver, |
| 48 |
const EOpTransp amesosSolverTransp, |
| 49 |
const double amesosSolverScalar |
| 50 |
) |
| 51 |
{ |
| 52 |
#ifdef TEUCHOS_DEBUG |
| 53 |
TEUCHOS_TEST_FOR_EXCEPT(fwdOp.get()==NULL); |
| 54 |
TEUCHOS_TEST_FOR_EXCEPT(fwdOpSrc.get()==NULL); |
| 55 |
TEUCHOS_TEST_FOR_EXCEPT(epetraLP.get()==NULL); |
| 56 |
TEUCHOS_TEST_FOR_EXCEPT(amesosSolver.get()==NULL); |
| 57 |
TEUCHOS_TEST_FOR_EXCEPT(epetraLP->GetLHS()!=NULL); |
| 58 |
TEUCHOS_TEST_FOR_EXCEPT(epetraLP->GetRHS()!=NULL); |
| 59 |
#endif |
| 60 |
fwdOp_ = fwdOp; |
| 61 |
fwdOpSrc_ = fwdOpSrc; |
| 62 |
epetraLP_ = epetraLP; |
| 63 |
amesosSolver_ = amesosSolver; |
| 64 |
amesosSolverTransp_ = amesosSolverTransp; |
| 65 |
amesosSolverScalar_ = amesosSolverScalar; |
| 66 |
const std::string fwdOpLabel = fwdOp_->getObjectLabel(); |
| 67 |
if(fwdOpLabel.length()) |
| 68 |
this->setObjectLabel( "lows("+fwdOpLabel+")" ); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
Teuchos::RCP<const LinearOpSourceBase<double> > |
| 73 |
AmesosLinearOpWithSolve::extract_fwdOpSrc() |
| 74 |
{ |
| 75 |
Teuchos::RCP<const LinearOpSourceBase<double> > |
| 76 |
_fwdOpSrc = fwdOpSrc_; |
| 77 |
fwdOpSrc_ = Teuchos::null; |
| 78 |
return _fwdOpSrc; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
void AmesosLinearOpWithSolve::uninitialize( |
| 83 |
Teuchos::RCP<const LinearOpBase<double> > *fwdOp, |
| 84 |
Teuchos::RCP<const LinearOpSourceBase<double> > *fwdOpSrc, |
| 85 |
Teuchos::RCP<Epetra_LinearProblem> *epetraLP, |
| 86 |
Teuchos::RCP<Amesos_BaseSolver> *amesosSolver, |
| 87 |
EOpTransp *amesosSolverTransp, |
| 88 |
double *amesosSolverScalar |
| 89 |
) |
| 90 |
{ |
| 91 |
|
| 92 |
if(fwdOp) *fwdOp = fwdOp_; |
| 93 |
if(fwdOpSrc) *fwdOpSrc = fwdOpSrc_; |
| 94 |
if(epetraLP) *epetraLP = epetraLP_; |
| 95 |
if(amesosSolver) *amesosSolver = amesosSolver_; |
| 96 |
if(amesosSolverTransp) *amesosSolverTransp = amesosSolverTransp_; |
| 97 |
if(amesosSolverScalar) *amesosSolverScalar = amesosSolverScalar_; |
| 98 |
|
| 99 |
fwdOp_ = Teuchos::null; |
| 100 |
fwdOpSrc_ = Teuchos::null; |
| 101 |
epetraLP_ = Teuchos::null; |
| 102 |
amesosSolver_ = Teuchos::null; |
| 103 |
amesosSolverTransp_ = NOTRANS; |
| 104 |
amesosSolverScalar_ = 0.0; |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
// Overridden from LinearOpBase |
| 110 |
|
| 111 |
|
| 112 |
Teuchos::RCP< const VectorSpaceBase<double> > |
| 113 |
AmesosLinearOpWithSolve::range() const |
| 114 |
{ |
| 115 |
return ( fwdOp_.get() ? fwdOp_->range() : Teuchos::null ); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
Teuchos::RCP< const VectorSpaceBase<double> > |
| 120 |
AmesosLinearOpWithSolve::domain() const |
| 121 |
{ |
| 122 |
return ( fwdOp_.get() ? fwdOp_->domain() : Teuchos::null ); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
Teuchos::RCP<const LinearOpBase<double> > |
| 127 |
AmesosLinearOpWithSolve::clone() const |
| 128 |
{ |
| 129 |
return Teuchos::null; // Not supported yet but could be |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
// Overridden from Teuchos::Describable |
| 134 |
|
| 135 |
|
| 136 |
std::string AmesosLinearOpWithSolve::description() const |
| 137 |
{ |
| 138 |
std::ostringstream oss; |
| 139 |
oss << Teuchos::Describable::description(); |
| 140 |
if(!is_null(amesosSolver_)) { |
| 141 |
oss |
| 142 |
<< "{fwdOp="<<fwdOp_->description() |
| 143 |
<< ",amesosSolver="<<typeName(*amesosSolver_)<<"}"; |
| 144 |
} |
| 145 |
return oss.str(); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
void AmesosLinearOpWithSolve::describe( |
| 150 |
Teuchos::FancyOStream &out, |
| 151 |
const Teuchos::EVerbosityLevel verbLevel |
| 152 |
) const |
| 153 |
{ |
| 154 |
using Teuchos::OSTab; |
| 155 |
using Teuchos::typeName; |
| 156 |
using Teuchos::describe; |
| 157 |
switch(verbLevel) { |
| 158 |
case Teuchos::VERB_DEFAULT: |
| 159 |
case Teuchos::VERB_LOW: |
| 160 |
out << this->description() << std::endl; |
| 161 |
break; |
| 162 |
case Teuchos::VERB_MEDIUM: |
| 163 |
case Teuchos::VERB_HIGH: |
| 164 |
case Teuchos::VERB_EXTREME: |
| 165 |
{ |
| 166 |
out |
| 167 |
<< Teuchos::Describable::description() << "{" |
| 168 |
<< "rangeDim=" << this->range()->dim() |
| 169 |
<< ",domainDim="<< this->domain()->dim() << "}\n"; |
| 170 |
OSTab tab(out); |
| 171 |
if(!is_null(fwdOp_)) { |
| 172 |
out << "fwdOp = " << describe(*fwdOp_,verbLevel); |
| 173 |
} |
| 174 |
if(!is_null(amesosSolver_)) { |
| 175 |
out << "amesosSolver=" << typeName(*amesosSolver_) << "\n"; |
| 176 |
} |
| 177 |
break; |
| 178 |
} |
| 179 |
default: |
| 180 |
TEUCHOS_TEST_FOR_EXCEPT(true); // Should never get here! |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
// protected |
| 186 |
|
| 187 |
|
| 188 |
// Overridden from LinearOpBase |
| 189 |
|
| 190 |
|
| 191 |
bool AmesosLinearOpWithSolve::opSupportedImpl(EOpTransp M_trans) const |
| 192 |
{ |
| 193 |
return ::Thyra::opSupported(*fwdOp_,M_trans); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
void AmesosLinearOpWithSolve::applyImpl( |
| 198 |
const EOpTransp M_trans, |
| 199 |
const MultiVectorBase<double> &X, |
| 200 |
const Ptr<MultiVectorBase<double> > &Y, |
| 201 |
const double alpha, |
| 202 |
const double beta |
| 203 |
) const |
| 204 |
{ |
| 205 |
Thyra::apply( *fwdOp_, M_trans, X, Y, alpha, beta ); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
// Overridden from LinearOpWithSolveBase |
| 210 |
|
| 211 |
|
| 212 |
bool AmesosLinearOpWithSolve::solveSupportsImpl(EOpTransp M_trans) const |
| 213 |
{ |
| 214 |
if (Thyra::real_trans(M_trans) == Thyra::NOTRANS) { |
| 215 |
// Assume every amesos solver supports a basic forward solve! |
| 216 |
return true; |
| 217 |
} |
| 218 |
// Query the amesos solver to see if it supports the transpose operation. |
| 219 |
// NOTE: Amesos_BaseSolver makes you change the state of the object to |
| 220 |
// determine if the object supports an adjoint solver. This is a bad design |
| 221 |
// but I have no control over that. This is why you see this hacked |
| 222 |
// oldUseTranspose variable and logic. NOTE: This function meets the basic |
| 223 |
// guarantee but if setUseTransplse(...) throws, then the state of |
| 224 |
// UseTranspose() may be different. |
| 225 |
const bool oldUseTranspose = amesosSolver_->UseTranspose(); |
| 226 |
const bool supportsAdjoint = (amesosSolver_->SetUseTranspose(true) == 0); |
| 227 |
amesosSolver_->SetUseTranspose(oldUseTranspose); |
| 228 |
return supportsAdjoint; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
bool AmesosLinearOpWithSolve::solveSupportsSolveMeasureTypeImpl( |
| 233 |
EOpTransp /* M_trans */, const SolveMeasureType& /* solveMeasureType */ |
| 234 |
) const |
| 235 |
{ |
| 236 |
return true; // I am a direct solver so I should be able to do it all! |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
SolveStatus<double> |
| 241 |
AmesosLinearOpWithSolve::solveImpl( |
| 242 |
const EOpTransp M_trans, |
| 243 |
const MultiVectorBase<double> &B, |
| 244 |
const Ptr<MultiVectorBase<double> > &X, |
| 245 |
const Ptr<const SolveCriteria<double> > /* solveCriteria */ |
| 246 |
) const |
| 247 |
{ |
| 248 |
using Teuchos::rcpFromPtr; |
| 249 |
using Teuchos::rcpFromRef; |
| 250 |
using Teuchos::OSTab; |
| 251 |
|
| 252 |
Teuchos::Time totalTimer(""); |
| 253 |
totalTimer.start(true); |
| 254 |
|
| 255 |
THYRA_FUNC_TIME_MONITOR("Stratimikos: AmesosLOWS"); |
| 256 |
|
| 257 |
Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream(); |
| 258 |
Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel(); |
| 259 |
OSTab tab = this->getOSTab(); |
| 260 |
if(out.get() && static_cast<int>(verbLevel) > static_cast<int>(Teuchos::VERB_NONE)) |
| 261 |
*out << "\nSolving block system using Amesos solver " |
| 262 |
<< typeName(*amesosSolver_) << " ...\n\n"; |
| 263 |
|
| 264 |
// |
| 265 |
// Get the op(...) range and domain maps |
| 266 |
// |
| 267 |
const EOpTransp amesosOpTransp = real_trans(trans_trans(amesosSolverTransp_,M_trans)); |
| 268 |
const Epetra_Operator *amesosOp = epetraLP_->GetOperator(); |
| 269 |
const Epetra_Map |
| 270 |
&opRangeMap = ( amesosOpTransp == NOTRANS |
| 271 |
? amesosOp->OperatorRangeMap() : amesosOp->OperatorDomainMap() ), |
| 272 |
&opDomainMap = ( amesosOpTransp == NOTRANS |
| 273 |
? amesosOp->OperatorDomainMap() : amesosOp->OperatorRangeMap() ); |
| 274 |
|
| 275 |
// |
| 276 |
// Get Epetra_MultiVector views of B and X |
| 277 |
// |
| 278 |
Teuchos::RCP<const Epetra_MultiVector> |
| 279 |
epetra_B = get_Epetra_MultiVector(opRangeMap, rcpFromRef(B)); |
| 280 |
Teuchos::RCP<Epetra_MultiVector> |
| 281 |
epetra_X = get_Epetra_MultiVector(opDomainMap, rcpFromPtr(X)); |
| 282 |
|
| 283 |
// |
| 284 |
// Set B and X in the linear problem |
| 285 |
// |
| 286 |
epetraLP_->SetLHS(&*epetra_X); |
| 287 |
epetraLP_->SetRHS(const_cast<Epetra_MultiVector*>(&*epetra_B)); |
| 288 |
// Above should be okay but cross your fingers! |
| 289 |
|
| 290 |
// |
| 291 |
// Solve the linear system |
| 292 |
// |
| 293 |
const bool oldUseTranspose = amesosSolver_->UseTranspose(); |
| 294 |
amesosSolver_->SetUseTranspose(amesosOpTransp==TRANS); |
| 295 |
const int err = amesosSolver_->Solve(); |
| 296 |
TEUCHOS_TEST_FOR_EXCEPTION( 0!=err, CatastrophicSolveFailure, |
| 297 |
"Error, the function Solve() on the amesos solver of type\n" |
| 298 |
"\'"<<typeName(*amesosSolver_)<<"\' failed with error code "<<err<<"!" |
| 299 |
); |
| 300 |
amesosSolver_->SetUseTranspose(oldUseTranspose); |
| 301 |
|
| 302 |
// |
| 303 |
// Unset B and X |
| 304 |
// |
| 305 |
epetraLP_->SetLHS(NULL); |
| 306 |
epetraLP_->SetRHS(NULL); |
| 307 |
epetra_X = Teuchos::null; |
| 308 |
epetra_B = Teuchos::null; |
| 309 |
|
| 310 |
// |
| 311 |
// Scale X if needed |
| 312 |
// |
| 313 |
if(amesosSolverScalar_!=1.0) |
| 314 |
Thyra::scale(1.0/amesosSolverScalar_, X); |
| 315 |
|
| 316 |
// |
| 317 |
// Set the solve status if requested |
| 318 |
// |
| 319 |
SolveStatus<double> solveStatus; |
| 320 |
solveStatus.solveStatus = SOLVE_STATUS_CONVERGED; |
| 321 |
solveStatus.achievedTol = SolveStatus<double>::unknownTolerance(); |
| 322 |
solveStatus.message = |
| 323 |
std::string("Solver ")+typeName(*amesosSolver_)+std::string(" converged!"); |
| 324 |
|
| 325 |
// |
| 326 |
// Report the overall time |
| 327 |
// |
| 328 |
if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW)) |
| 329 |
*out |
| 330 |
<< "\nTotal solve time = "<<totalTimer.totalElapsedTime()<<" sec\n"; |
| 331 |
|
| 332 |
return solveStatus; |
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
} // end namespace Thyra |