Stratimikos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_AmesosLinearOpWithSolve.cpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Stratimikos: Thyra-based strategies for linear solvers
6 // Copyright (2006) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
46 #include "Thyra_MultiVectorStdOps.hpp"
47 #include "Epetra_MultiVector.h"
48 #include "Teuchos_TimeMonitor.hpp"
49 
50 
51 namespace Thyra {
52 
53 
54 // Constructors/initializers/accessors
55 
56 
58 {}
59 
60 
62  const Teuchos::RCP<const LinearOpBase<double> > &fwdOp,
63  const Teuchos::RCP<const LinearOpSourceBase<double> > &fwdOpSrc,
64  const Teuchos::RCP<Epetra_LinearProblem> &epetraLP,
65  const Teuchos::RCP<Amesos_BaseSolver> &amesosSolver,
66  const EOpTransp amesosSolverTransp,
67  const double amesosSolverScalar
68  )
69 {
70  this->initialize(fwdOp,fwdOpSrc,epetraLP,amesosSolver,
71  amesosSolverTransp,amesosSolverScalar);
72 }
73 
74 
76  const Teuchos::RCP<const LinearOpBase<double> > &fwdOp,
77  const Teuchos::RCP<const LinearOpSourceBase<double> > &fwdOpSrc,
78  const Teuchos::RCP<Epetra_LinearProblem> &epetraLP,
79  const Teuchos::RCP<Amesos_BaseSolver> &amesosSolver,
80  const EOpTransp amesosSolverTransp,
81  const double amesosSolverScalar
82  )
83 {
84 #ifdef TEUCHOS_DEBUG
85  TEUCHOS_TEST_FOR_EXCEPT(fwdOp.get()==NULL);
86  TEUCHOS_TEST_FOR_EXCEPT(fwdOpSrc.get()==NULL);
87  TEUCHOS_TEST_FOR_EXCEPT(epetraLP.get()==NULL);
88  TEUCHOS_TEST_FOR_EXCEPT(amesosSolver.get()==NULL);
89  TEUCHOS_TEST_FOR_EXCEPT(epetraLP->GetLHS()!=NULL);
90  TEUCHOS_TEST_FOR_EXCEPT(epetraLP->GetRHS()!=NULL);
91 #endif
92  fwdOp_ = fwdOp;
93  fwdOpSrc_ = fwdOpSrc;
94  epetraLP_ = epetraLP;
95  amesosSolver_ = amesosSolver;
96  amesosSolverTransp_ = amesosSolverTransp;
97  amesosSolverScalar_ = amesosSolverScalar;
98  const std::string fwdOpLabel = fwdOp_->getObjectLabel();
99  if(fwdOpLabel.length())
100  this->setObjectLabel( "lows("+fwdOpLabel+")" );
101 }
102 
103 
106 {
108  _fwdOpSrc = fwdOpSrc_;
110  return _fwdOpSrc;
111 }
112 
113 
115  Teuchos::RCP<const LinearOpBase<double> > *fwdOp,
116  Teuchos::RCP<const LinearOpSourceBase<double> > *fwdOpSrc,
118  Teuchos::RCP<Amesos_BaseSolver> *amesosSolver,
119  EOpTransp *amesosSolverTransp,
120  double *amesosSolverScalar
121  )
122 {
123 
124  if(fwdOp) *fwdOp = fwdOp_;
125  if(fwdOpSrc) *fwdOpSrc = fwdOpSrc_;
126  if(epetraLP) *epetraLP = epetraLP_;
127  if(amesosSolver) *amesosSolver = amesosSolver_;
128  if(amesosSolverTransp) *amesosSolverTransp = amesosSolverTransp_;
129  if(amesosSolverScalar) *amesosSolverScalar = amesosSolverScalar_;
130 
136  amesosSolverScalar_ = 0.0;
137 
138 }
139 
140 
141 // Overridden from LinearOpBase
142 
143 
146 {
147  return ( fwdOp_.get() ? fwdOp_->range() : Teuchos::null );
148 }
149 
150 
153 {
154  return ( fwdOp_.get() ? fwdOp_->domain() : Teuchos::null );
155 }
156 
157 
160 {
161  return Teuchos::null; // Not supported yet but could be
162 }
163 
164 
165 // Overridden from Teuchos::Describable
166 
167 
169 {
170  std::ostringstream oss;
172  if(!is_null(amesosSolver_)) {
173  oss
174  << "{fwdOp="<<fwdOp_->description()
175  << ",amesosSolver="<<typeName(*amesosSolver_)<<"}";
176  }
177  return oss.str();
178 }
179 
180 
183  const Teuchos::EVerbosityLevel verbLevel
184  ) const
185 {
186  using Teuchos::OSTab;
187  using Teuchos::typeName;
188  using Teuchos::describe;
189  switch(verbLevel) {
191  case Teuchos::VERB_LOW:
192  out << this->description() << std::endl;
193  break;
195  case Teuchos::VERB_HIGH:
197  {
198  out
200  << "rangeDim=" << this->range()->dim()
201  << ",domainDim="<< this->domain()->dim() << "}\n";
202  OSTab tab(out);
203  if(!is_null(fwdOp_)) {
204  out << "fwdOp = " << describe(*fwdOp_,verbLevel);
205  }
206  if(!is_null(amesosSolver_)) {
207  out << "amesosSolver=" << typeName(*amesosSolver_) << "\n";
208  }
209  break;
210  }
211  default:
212  TEUCHOS_TEST_FOR_EXCEPT(true); // Should never get here!
213  }
214 }
215 
216 
217 // protected
218 
219 
220 // Overridden from LinearOpBase
221 
222 
223 bool AmesosLinearOpWithSolve::opSupportedImpl(EOpTransp M_trans) const
224 {
225  return ::Thyra::opSupported(*fwdOp_,M_trans);
226 }
227 
228 
230  const EOpTransp M_trans,
231  const MultiVectorBase<double> &X,
232  const Ptr<MultiVectorBase<double> > &Y,
233  const double alpha,
234  const double beta
235  ) const
236 {
237  Thyra::apply( *fwdOp_, M_trans, X, Y, alpha, beta );
238 }
239 
240 
241 // Overridden from LinearOpWithSolveBase
242 
243 
244 bool AmesosLinearOpWithSolve::solveSupportsImpl(EOpTransp M_trans) const
245 {
246  if (Thyra::real_trans(M_trans) == Thyra::NOTRANS) {
247  // Assume every amesos solver supports a basic forward solve!
248  return true;
249  }
250  // Query the amesos solver to see if it supports the transpose operation.
251  // NOTE: Amesos_BaseSolver makes you change the state of the object to
252  // determine if the object supports an adjoint solver. This is a bad design
253  // but I have no control over that. This is why you see this hacked
254  // oldUseTranspose varible and logic. NOTE: This function meets the basic
255  // guarantee but if setUseTransplse(...) throws, then the state of
256  // UseTranspose() may be different.
257  const bool oldUseTranspose = amesosSolver_->UseTranspose();
258  const bool supportsAdjoint = (amesosSolver_->SetUseTranspose(true) == 0);
259  amesosSolver_->SetUseTranspose(oldUseTranspose);
260  return supportsAdjoint;
261 }
262 
263 
265  EOpTransp M_trans, const SolveMeasureType& solveMeasureType
266  ) const
267 {
268  return true; // I am a direct solver so I should be able to do it all!
269 }
270 
271 
272 SolveStatus<double>
274  const EOpTransp M_trans,
275  const MultiVectorBase<double> &B,
276  const Ptr<MultiVectorBase<double> > &X,
277  const Ptr<const SolveCriteria<double> > solveCriteria
278  ) const
279 {
280  using Teuchos::rcpFromPtr;
281  using Teuchos::rcpFromRef;
282  using Teuchos::OSTab;
283 
284  Teuchos::Time totalTimer("");
285  totalTimer.start(true);
286 
287  THYRA_FUNC_TIME_MONITOR("Stratimikos: AmesosLOWS");
288 
289  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
290  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
291  OSTab tab = this->getOSTab();
292  if(out.get() && static_cast<int>(verbLevel) > static_cast<int>(Teuchos::VERB_NONE))
293  *out << "\nSolving block system using Amesos solver "
294  << typeName(*amesosSolver_) << " ...\n\n";
295 
296  //
297  // Get the op(...) range and domain maps
298  //
299  const EOpTransp amesosOpTransp = real_trans(trans_trans(amesosSolverTransp_,M_trans));
300  const Epetra_Operator *amesosOp = epetraLP_->GetOperator();
301  const Epetra_Map
302  &opRangeMap = ( amesosOpTransp == NOTRANS
303  ? amesosOp->OperatorRangeMap() : amesosOp->OperatorDomainMap() ),
304  &opDomainMap = ( amesosOpTransp == NOTRANS
305  ? amesosOp->OperatorDomainMap() : amesosOp->OperatorRangeMap() );
306 
307  //
308  // Get Epetra_MultiVector views of B and X
309  //
311  epetra_B = get_Epetra_MultiVector(opRangeMap, rcpFromRef(B));
313  epetra_X = get_Epetra_MultiVector(opDomainMap, rcpFromPtr(X));
314 
315  //
316  // Set B and X in the linear problem
317  //
318  epetraLP_->SetLHS(&*epetra_X);
319  epetraLP_->SetRHS(const_cast<Epetra_MultiVector*>(&*epetra_B));
320  // Above should be okay but cross your fingers!
321 
322  //
323  // Solve the linear system
324  //
325  const bool oldUseTranspose = amesosSolver_->UseTranspose();
326  amesosSolver_->SetUseTranspose(amesosOpTransp==TRANS);
327  const int err = amesosSolver_->Solve();
328  TEUCHOS_TEST_FOR_EXCEPTION( 0!=err, CatastrophicSolveFailure,
329  "Error, the function Solve() on the amesos solver of type\n"
330  "\'"<<typeName(*amesosSolver_)<<"\' failed with error code "<<err<<"!"
331  );
332  amesosSolver_->SetUseTranspose(oldUseTranspose);
333 
334  //
335  // Unset B and X
336  //
337  epetraLP_->SetLHS(NULL);
338  epetraLP_->SetRHS(NULL);
339  epetra_X = Teuchos::null;
340  epetra_B = Teuchos::null;
341 
342  //
343  // Scale X if needed
344  //
345  if(amesosSolverScalar_!=1.0)
346  Thyra::scale(1.0/amesosSolverScalar_, X);
347 
348  //
349  // Set the solve status if requested
350  //
351  SolveStatus<double> solveStatus;
352  solveStatus.solveStatus = SOLVE_STATUS_CONVERGED;
353  solveStatus.achievedTol = SolveStatus<double>::unknownTolerance();
354  solveStatus.message =
355  std::string("Solver ")+typeName(*amesosSolver_)+std::string(" converged!");
356 
357  //
358  // Report the overall time
359  //
360  if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
361  *out
362  << "\nTotal solve time = "<<totalTimer.totalElapsedTime()<<" sec\n";
363 
364  return solveStatus;
365 
366 }
367 
368 
369 } // end namespace Thyra
Teuchos::RCP< Epetra_LinearProblem > epetraLP_
RCP< Epetra_MultiVector > get_Epetra_MultiVector(const Epetra_Map &map, const RCP< MultiVectorBase< double > > &mv)
Teuchos::RCP< const LinearOpSourceBase< double > > extract_fwdOpSrc()
Extract the LinearOpSourceBase&lt;double&gt; object so that it can be modified.
std::string typeName(const T &t)
bool is_null(const boost::shared_ptr< T > &p)
Teuchos::RCP< Amesos_BaseSolver > amesosSolver_
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)
T * get() const
basic_OSTab< char > OSTab
virtual bool solveSupportsImpl(EOpTransp M_trans) const
void start(bool reset=false)
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
Teuchos::RCP< const LinearOpSourceBase< double > > fwdOpSrc_
virtual std::string description() const
virtual bool solveSupportsSolveMeasureTypeImpl(EOpTransp M_trans, const SolveMeasureType &solveMeasureType) const
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.
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.
virtual void applyImpl(const EOpTransp M_trans, const MultiVectorBase< double > &X, const Ptr< MultiVectorBase< double > > &Y, const double alpha, const double beta) const
Teuchos::RCP< const LinearOpBase< double > > fwdOp_
Teuchos::RCP< const LinearOpBase< double > > clone() const
double totalElapsedTime(bool readCurrentTime=false) const
Teuchos::RCP< const VectorSpaceBase< double > > domain() const
AmesosLinearOpWithSolve()
Construct to uninitialized.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)