Stratimikos  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Thyra_AmesosLinearOpWithSolve.cpp
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"
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 
24  amesosSolverTransp_(Thyra::NOTRANS),
25  amesosSolverScalar_(1.0)
26 {}
27 
28 
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 
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 
74 {
76  _fwdOpSrc = fwdOpSrc_;
77  fwdOpSrc_ = Teuchos::null;
78  return _fwdOpSrc;
79 }
80 
81 
83  Teuchos::RCP<const LinearOpBase<double> > *fwdOp,
84  Teuchos::RCP<const LinearOpSourceBase<double> > *fwdOpSrc,
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 
114 {
115  return ( fwdOp_.get() ? fwdOp_->range() : Teuchos::null );
116 }
117 
118 
121 {
122  return ( fwdOp_.get() ? fwdOp_->domain() : Teuchos::null );
123 }
124 
125 
128 {
129  return Teuchos::null; // Not supported yet but could be
130 }
131 
132 
133 // Overridden from Teuchos::Describable
134 
135 
137 {
138  std::ostringstream oss;
140  if(!is_null(amesosSolver_)) {
141  oss
142  << "{fwdOp="<<fwdOp_->description()
143  << ",amesosSolver="<<typeName(*amesosSolver_)<<"}";
144  }
145  return oss.str();
146 }
147 
148 
151  const Teuchos::EVerbosityLevel verbLevel
152  ) const
153 {
154  using Teuchos::OSTab;
155  using Teuchos::typeName;
156  using Teuchos::describe;
157  switch(verbLevel) {
159  case Teuchos::VERB_LOW:
160  out << this->description() << std::endl;
161  break;
163  case Teuchos::VERB_HIGH:
165  {
166  out
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 
192 {
193  return ::Thyra::opSupported(*fwdOp_,M_trans);
194 }
195 
196 
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 
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 
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 
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  //
279  epetra_B = get_Epetra_MultiVector(opRangeMap, rcpFromRef(B));
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();
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;
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
RCP< Epetra_MultiVector > get_Epetra_MultiVector(const Epetra_Map &map, const RCP< MultiVectorBase< double > > &mv)
void SetLHS(Epetra_MultiVector *X)
Teuchos::RCP< const LinearOpSourceBase< double > > extract_fwdOpSrc()
Extract the LinearOpSourceBase&lt;double&gt; object so that it can be modified.
bool is_null(const boost::shared_ptr< T > &p)
EOpTransp
virtual int Solve()=0
Epetra_MultiVector * GetLHS() const
basic_OSTab< char > OSTab
Epetra_MultiVector * GetRHS() const
std::string message
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
virtual bool opSupportedImpl(EOpTransp M_trans) const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
EOpTransp real_trans(EOpTransp transp)
T * get() const
virtual const Epetra_Map & OperatorDomainMap() const =0
virtual bool solveSupportsImpl(EOpTransp M_trans) const
virtual const Epetra_Map & OperatorRangeMap() const =0
void start(bool reset=false)
virtual bool UseTranspose() const =0
SolveStatus< double > solveImpl(const EOpTransp M_trans, const MultiVectorBase< double > &B, const Ptr< MultiVectorBase< double > > &X, const Ptr< const SolveCriteria< double > > solveCriteria) const
Teuchos::RCP< const VectorSpaceBase< double > > range() const
EOpTransp trans_trans(EOpTransp trans1, EOpTransp trans2)
static ScalarMag unknownTolerance()
virtual std::string description() const
virtual void setObjectLabel(const std::string &objectLabel)
virtual bool solveSupportsSolveMeasureTypeImpl(EOpTransp M_trans, const SolveMeasureType &solveMeasureType) const
virtual int SetUseTranspose(bool UseTranspose)=0
void initialize(const Teuchos::RCP< const LinearOpBase< double > > &fwdOp, const Teuchos::RCP< const LinearOpSourceBase< double > > &fwdOpSrc, const Teuchos::RCP< Epetra_LinearProblem > &epetraLP, const Teuchos::RCP< Amesos_BaseSolver > &amesosSolver, const EOpTransp amesosSolverTransp, const double amesosSolverScalar)
First initialization.
ESolveStatus solveStatus
void uninitialize(Teuchos::RCP< const LinearOpBase< double > > *fwdOp=NULL, Teuchos::RCP< const LinearOpSourceBase< double > > *fwdOpSrc=NULL, Teuchos::RCP< Epetra_LinearProblem > *epetraLP=NULL, Teuchos::RCP< Amesos_BaseSolver > *amesosSolver=NULL, EOpTransp *amesosSolverTransp=NULL, double *amesosSolverScalar=NULL)
Uninitialize.
void SetRHS(Epetra_MultiVector *B)
virtual void applyImpl(const EOpTransp M_trans, const MultiVectorBase< double > &X, const Ptr< MultiVectorBase< double > > &Y, const double alpha, const double beta) const
ScalarMag achievedTol
Teuchos::RCP< const LinearOpBase< double > > clone() const
double totalElapsedTime(bool readCurrentTime=false) const
Epetra_Operator * GetOperator() const
Teuchos::RCP< const VectorSpaceBase< double > > domain() const
AmesosLinearOpWithSolve()
Construct to uninitialized.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
std::string typeName(const T &t)

Generated on Fri Dec 20 2024 09:21:59 for Stratimikos by doxygen 1.8.5