Stratimikos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_AztecOOLinearOpWithSolve.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Stratimikos: Thyra-based strategies for linear solvers
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
43 #include "Thyra_LinearOpWithSolveHelpers.hpp"
46 #include "Teuchos_BLAS_types.hpp"
47 #include "Teuchos_TimeMonitor.hpp"
48 #include "Teuchos_Time.hpp"
50 
51 
52 namespace {
53 
54 
55 inline
56 Teuchos::ETransp convert( Thyra::EOpTransp trans_in )
57 {
58  Teuchos::ETransp trans_out;
59  switch(trans_in) {
60  case Thyra::NOTRANS:
61  trans_out = Teuchos::NO_TRANS;
62  break;
63  case Thyra::TRANS:
64  trans_out = Teuchos::TRANS;
65  break;
66  default:
67  TEUCHOS_TEST_FOR_EXCEPT(true); // Should never get here!
68  }
69  return trans_out;
70 }
71 
72 
73 // This class sets some solve instance specific state and then sets it back to
74 // the default state on destruction. But using the destructor to unset the
75 // state we can be sure that the state is rest correctly even if an exception
76 // is thrown.
77 class SetAztecSolveState {
78 public:
79  SetAztecSolveState(
80  const Teuchos::RCP<AztecOO> &aztecSolver,
81  const Teuchos::RCP<Teuchos::FancyOStream> &fancyOStream,
82  const Teuchos::EVerbosityLevel verbLevel,
83  const Thyra::SolveMeasureType &solveMeasureType
84  );
85  ~SetAztecSolveState();
86 private:
87  Teuchos::RCP<AztecOO> aztecSolver_;
89  Teuchos::EVerbosityLevel verbLevel_;
90  int outputFrequency_;
91  int convergenceTest_;
92  SetAztecSolveState(); // Not defined and not to be called!
93 };
94 
95 
96 SetAztecSolveState::SetAztecSolveState(
97  const Teuchos::RCP<AztecOO> &aztecSolver,
98  const Teuchos::RCP<Teuchos::FancyOStream> &fancyOStream,
99  const Teuchos::EVerbosityLevel verbLevel,
100  const Thyra::SolveMeasureType &solveMeasureType
101  )
102  :aztecSolver_(aztecSolver.assert_not_null())
103 {
104 
105  // Output state
106  verbLevel_ = verbLevel;
107  if ( Teuchos::VERB_NONE != verbLevel_ ) {
108  if (!is_null(fancyOStream)) {
109  // AztecOO puts in two tabs before it prints anything. Therefore,
110  // there is not much that we can do to improve the layout of the
111  // indentation so just leave it!
112  fancyOStream_= Teuchos::tab(
113  fancyOStream.create_weak(),
114  0, // Don't indent since AztecOO already puts in two tabs (not spaces!)
115  Teuchos::implicit_cast<std::string>("AZTECOO")
116  );
117  aztecSolver_->SetOutputStream(*fancyOStream_);
118  aztecSolver_->SetErrorStream(*fancyOStream_);
119  // Note, above we can not save the current output and error streams
120  // since AztecOO does not define functions to get them. In the
121  // future, AztecOO should define these functions if we are to avoid
122  // treading on each others print statements. However, since the
123  // AztecOO object is most likely owned by these Thyra wrappers, this
124  // should not be a problem.
125  }
126  }
127  else {
128  outputFrequency_ = aztecSolver_->GetAllAztecOptions()[AZ_output];
129  aztecSolver_->SetAztecOption(AZ_output,0);
130  }
131 
132  // Convergence test
133  convergenceTest_ = aztecSolver_->GetAztecOption(AZ_conv);
134  if (solveMeasureType.useDefault())
135  {
136  // Just use the default solve measure type already set!
137  }
138  else if (
139  solveMeasureType(
140  Thyra::SOLVE_MEASURE_NORM_RESIDUAL,
141  Thyra::SOLVE_MEASURE_NORM_RHS
142  )
143  )
144  {
145  aztecSolver_->SetAztecOption(AZ_conv,AZ_rhs);
146  }
147  else if (
148  solveMeasureType(
149  Thyra::SOLVE_MEASURE_NORM_RESIDUAL,
150  Thyra::SOLVE_MEASURE_NORM_INIT_RESIDUAL
151  )
152  )
153  {
154  aztecSolver_->SetAztecOption(AZ_conv,AZ_r0);
155  }
156  else {
157  TEUCHOS_TEST_FOR_EXCEPT("Invalid solve measure type, you should never get here!");
158  }
159 
160 }
161 
162 
163 SetAztecSolveState::~SetAztecSolveState()
164 {
165 
166  // Output state
167  if ( Teuchos::VERB_NONE != verbLevel_ ) {
168  if (!is_null(fancyOStream_)) {
169  aztecSolver_->SetOutputStream(std::cout);
170  aztecSolver_->SetErrorStream(std::cerr);
171  *fancyOStream_ << "\n";
172  }
173  }
174  else {
175  aztecSolver_->SetAztecOption(AZ_output,outputFrequency_);
176  }
177 
178  // Convergence test
179  aztecSolver_->SetAztecOption(AZ_conv,convergenceTest_);
180 
181 }
182 
183 
184 } // namespace
185 
186 
187 namespace Thyra {
188 
189 
190 // Constructors/initializers/accessors
191 
192 
194  const int fwdDefaultMaxIterations_in
195  ,const double fwdDefaultTol_in
196  ,const int adjDefaultMaxIterations_in
197  ,const double adjDefaultTol_in
198  ,const bool outputEveryRhs_in
199  )
200  :fwdDefaultMaxIterations_(fwdDefaultMaxIterations_in)
201  ,fwdDefaultTol_(fwdDefaultTol_in)
202  ,adjDefaultMaxIterations_(adjDefaultMaxIterations_in)
203  ,adjDefaultTol_(adjDefaultTol_in)
204  ,outputEveryRhs_(outputEveryRhs_in)
205  ,isExternalPrec_(false)
206  ,allowInexactFwdSolve_(false)
207  ,allowInexactAdjSolve_(false)
208  ,aztecSolverScalar_(0.0)
209 {}
210 
211 
213  const RCP<const LinearOpBase<double> > &fwdOp
214  ,const RCP<const LinearOpSourceBase<double> > &fwdOpSrc
215  ,const RCP<const PreconditionerBase<double> > &prec
216  ,const bool isExternalPrec_in
217  ,const RCP<const LinearOpSourceBase<double> > &approxFwdOpSrc
218  ,const RCP<AztecOO> &aztecFwdSolver
219  ,const bool allowInexactFwdSolve
220  ,const RCP<AztecOO> &aztecAdjSolver
221  ,const bool allowInexactAdjSolve
222  ,const double aztecSolverScalar
223  )
224 {
225 #ifdef TEUCHOS_DEBUG
226  TEUCHOS_TEST_FOR_EXCEPT(fwdOp.get()==NULL);
227  TEUCHOS_TEST_FOR_EXCEPT(fwdOpSrc.get()==NULL);
228  TEUCHOS_TEST_FOR_EXCEPT(aztecFwdSolver.get()==NULL);
229 #endif
230  fwdOp_ = fwdOp;
231  fwdOpSrc_ = fwdOpSrc;
232  isExternalPrec_ = isExternalPrec_in;
233  prec_ = prec;
234  approxFwdOpSrc_ = approxFwdOpSrc;
235  aztecFwdSolver_ = aztecFwdSolver;
236  allowInexactFwdSolve_ = allowInexactFwdSolve;
237  aztecAdjSolver_ = aztecAdjSolver;
238  allowInexactAdjSolve_ = allowInexactAdjSolve;
239  aztecSolverScalar_ = aztecSolverScalar;
240  const std::string fwdOpLabel = fwdOp_->getObjectLabel();
241  if (fwdOpLabel.length())
242  this->setObjectLabel( "lows("+fwdOpLabel+")" );
243 }
244 
245 
248 {
250  _fwdOpSrc = fwdOpSrc_;
252  return _fwdOpSrc;
253 }
254 
255 
258 {
260  _prec = prec_;
262  return _prec;
263 }
264 
265 
267 {
268  return isExternalPrec_;
269 }
270 
271 
274 {
276  _approxFwdOpSrc = approxFwdOpSrc_;
278  return _approxFwdOpSrc;
279 }
280 
281 
283  RCP<const LinearOpBase<double> > *fwdOp,
284  RCP<const LinearOpSourceBase<double> > *fwdOpSrc,
285  RCP<const PreconditionerBase<double> > *prec,
286  bool *isExternalPrec_inout,
287  RCP<const LinearOpSourceBase<double> > *approxFwdOpSrc,
288  RCP<AztecOO> *aztecFwdSolver,
289  bool *allowInexactFwdSolve,
290  RCP<AztecOO> *aztecAdjSolver,
291  bool *allowInexactAdjSolve,
292  double *aztecSolverScalar
293  )
294 {
295  if (fwdOp) *fwdOp = fwdOp_;
296  if (fwdOpSrc) *fwdOpSrc = fwdOpSrc_;
297  if (prec) *prec = prec_;
298  if (isExternalPrec_inout) *isExternalPrec_inout = isExternalPrec_;
299  if (approxFwdOpSrc) *approxFwdOpSrc = approxFwdOpSrc_;
300  if (aztecFwdSolver) *aztecFwdSolver = aztecFwdSolver_;
301  if (allowInexactFwdSolve) *allowInexactFwdSolve = allowInexactFwdSolve_;
302  if (aztecAdjSolver) *aztecAdjSolver = aztecAdjSolver_;
303  if (allowInexactAdjSolve) *allowInexactAdjSolve = allowInexactAdjSolve_;
304  if (aztecSolverScalar) *aztecSolverScalar = aztecSolverScalar_;
305 
309  isExternalPrec_ = false; // Just to make unique
312  allowInexactFwdSolve_ = false;
314  allowInexactAdjSolve_ = false;
315  aztecSolverScalar_ = 0.0;
316 }
317 
318 
319 // Overridden from LinearOpBase
320 
321 
324 {
325  return ( fwdOp_.get() ? fwdOp_->range() : Teuchos::null );
326 }
327 
328 
331 {
332  return ( fwdOp_.get() ? fwdOp_->domain() : Teuchos::null );
333 }
334 
335 
338 {
339  return Teuchos::null; // Not supported yet but could be
340 }
341 
342 
343 // Overridden from Teuchos::Describable
344 
345 
347 {
348  std::ostringstream oss;
350  if (fwdOp_.get()) {
351  oss << "{";
352  oss << "fwdOp="<<fwdOp_->description()<<"";
353  oss << "}";
354  }
355  return oss.str();
356 }
357 
358 
361  const Teuchos::EVerbosityLevel verbLevel
362  ) const
363 {
364  using Teuchos::OSTab;
365  using Teuchos::typeName;
366  using Teuchos::describe;
367  switch(verbLevel) {
369  case Teuchos::VERB_LOW:
370  out << this->description() << std::endl;
371  break;
373  case Teuchos::VERB_HIGH:
375  {
376  out
378  << "rangeDim=" << this->range()->dim()
379  << ",domainDim="<< this->domain()->dim() << "}\n";
380  OSTab tab(out);
381  if (!is_null(fwdOp_)) {
382  out << "fwdOp = " << describe(*fwdOp_,verbLevel);
383  }
384  if (!is_null(prec_)) {
385  out << "prec = " << describe(*prec_,verbLevel);
386  }
387  if (!is_null(aztecFwdSolver_)) {
388  if (aztecFwdSolver_->GetUserOperator())
389  out
390  << "Aztec Fwd Op = "
391  << typeName(*aztecFwdSolver_->GetUserOperator()) << "\n";
392  if (aztecFwdSolver_->GetUserMatrix())
393  out
394  << "Aztec Fwd Mat = "
395  << typeName(*aztecFwdSolver_->GetUserMatrix()) << "\n";
396  if (aztecFwdSolver_->GetPrecOperator())
397  out
398  << "Aztec Fwd Prec Op = "
399  << typeName(*aztecFwdSolver_->GetPrecOperator()) << "\n";
400  if (aztecFwdSolver_->GetPrecMatrix())
401  out
402  << "Aztec Fwd Prec Mat = "
403  << typeName(*aztecFwdSolver_->GetPrecMatrix()) << "\n";
404  }
405  if (!is_null(aztecAdjSolver_)) {
406  if (aztecAdjSolver_->GetUserOperator())
407  out
408  << "Aztec Adj Op = "
409  << typeName(*aztecAdjSolver_->GetUserOperator()) << "\n";
410  if (aztecAdjSolver_->GetUserMatrix())
411  out
412  << "Aztec Adj Mat = "
413  << typeName(*aztecAdjSolver_->GetUserMatrix()) << "\n";
414  if (aztecAdjSolver_->GetPrecOperator())
415  out
416  << "Aztec Adj Prec Op = "
417  << typeName(*aztecAdjSolver_->GetPrecOperator()) << "\n";
418  if (aztecAdjSolver_->GetPrecMatrix())
419  out
420  << "Aztec Adj Prec Mat = "
421  << typeName(*aztecAdjSolver_->GetPrecMatrix()) << "\n";
422  }
423  break;
424  }
425  default:
426  TEUCHOS_TEST_FOR_EXCEPT(true); // Should never get here!
427  }
428 }
429 
430 
431 // protected
432 
433 
434 // Overridden from LinearOpBase
435 
436 
437 bool AztecOOLinearOpWithSolve::opSupportedImpl(EOpTransp M_trans) const
438 {
439  return ::Thyra::opSupported(*fwdOp_,M_trans);
440 }
441 
442 
444  const EOpTransp M_trans,
445  const MultiVectorBase<double> &X,
446  const Ptr<MultiVectorBase<double> > &Y,
447  const double alpha,
448  const double beta
449  ) const
450 {
451  Thyra::apply( *fwdOp_, M_trans, X, Y, alpha, beta );
452 }
453 
454 
455 // Overridden from LinearOpWithSolveBase
456 
457 
458 bool
460 {
461  if (real_trans(M_trans)==NOTRANS) return true;
462  return (nonnull(aztecAdjSolver_));
463 }
464 
465 
466 bool
468  EOpTransp M_trans, const SolveMeasureType& solveMeasureType
469  ) const
470 {
471  if (real_trans(M_trans)==NOTRANS) {
472  if (solveMeasureType.useDefault())
473  {
474  return true;
475  }
476  else if (
477  solveMeasureType(
478  SOLVE_MEASURE_NORM_RESIDUAL,
479  SOLVE_MEASURE_NORM_RHS
480  )
481  &&
483  )
484  {
485  return true;
486  }
487  else if (
488  solveMeasureType(
489  SOLVE_MEASURE_NORM_RESIDUAL,
490  SOLVE_MEASURE_NORM_INIT_RESIDUAL
491  )
492  &&
494  )
495  {
496  return true;
497  }
498  }
499  else {
500  // TRANS
501  if (aztecAdjSolver_.get()==NULL)
502  {
503  return false;
504  }
505  else if (solveMeasureType.useDefault())
506  {
507  return true;
508  }
509  else if (
510  solveMeasureType(
511  SOLVE_MEASURE_NORM_RESIDUAL,
512  SOLVE_MEASURE_NORM_RHS
513  )
514  &&
516  )
517  {
518  return true;
519  }
520  else if (
521  solveMeasureType(
522  SOLVE_MEASURE_NORM_RESIDUAL,
523  SOLVE_MEASURE_NORM_INIT_RESIDUAL
524  )
525  &&
527  )
528  {
529  return true;
530  }
531  }
532  // If you get here then we don't support the solve measure type!
533  return false;
534 }
535 
536 
537 // Overridden from LinearOpWithSolveBase
538 
539 
540 SolveStatus<double>
542  const EOpTransp M_trans,
543  const MultiVectorBase<double> &B,
544  const Ptr<MultiVectorBase<double> > &X,
545  const Ptr<const SolveCriteria<double> > solveCriteria
546  ) const
547 {
548 
549  using Teuchos::rcp;
550  using Teuchos::rcpFromRef;
551  using Teuchos::rcpFromPtr;
552  using Teuchos::OSTab;
553  typedef SolveCriteria<double> SC;
554  typedef SolveStatus<double> SS;
555 
556  THYRA_FUNC_TIME_MONITOR("Stratimikos: AztecOOLOWS");
557  Teuchos::Time totalTimer(""), timer("");
558  totalTimer.start(true);
559 
560  RCP<Teuchos::FancyOStream> out = this->getOStream();
561  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
562  OSTab tab = this->getOSTab();
563  if (out.get() && static_cast<int>(verbLevel) > static_cast<int>(Teuchos::VERB_NONE))
564  *out << "\nSolving block system using AztecOO ...\n\n";
565 
566  //
567  // Validate input
568  //
569  TEUCHOS_ASSERT(this->solveSupportsImpl(M_trans));
570  SolveMeasureType solveMeasureType;
571  if (nonnull(solveCriteria)) {
572  solveMeasureType = solveCriteria->solveMeasureType;
573  assertSupportsSolveMeasureType(*this, M_trans, solveMeasureType);
574  }
575 
576  //
577  // Get the transpose argument
578  //
579  const EOpTransp aztecOpTransp = real_trans(M_trans);
580 
581  //
582  // Get the solver, operator, and preconditioner that we will use
583  //
585  aztecSolver = ( aztecOpTransp == NOTRANS ? aztecFwdSolver_ : aztecAdjSolver_ );
586  const Epetra_Operator
587  *aztecOp = aztecSolver->GetUserOperator();
588 
589  //
590  // Get the op(...) range and domain maps
591  //
592  const Epetra_Map
593  &opRangeMap = aztecOp->OperatorRangeMap(),
594  &opDomainMap = aztecOp->OperatorDomainMap();
595 
596  //
597  // Get the convergence criteria
598  //
599  double tol = ( aztecOpTransp==NOTRANS ? fwdDefaultTol() : adjDefaultTol() );
600  int maxIterations = ( aztecOpTransp==NOTRANS
601  ? fwdDefaultMaxIterations() : adjDefaultMaxIterations() );
602  bool isDefaultSolveCriteria = true;
603  if (nonnull(solveCriteria)) {
604  if ( solveCriteria->requestedTol != SC::unspecifiedTolerance() ) {
605  tol = solveCriteria->requestedTol;
606  isDefaultSolveCriteria = false;
607  }
608  if (nonnull(solveCriteria->extraParameters)) {
609  maxIterations = solveCriteria->extraParameters->get("Maximum Iterations",maxIterations);
610  }
611  }
612 
613  //
614  // Get Epetra_MultiVector views of B and X
615  //
616 
618  RCP<Epetra_MultiVector> epetra_X;
619 
620  const EpetraOperatorWrapper* opWrapper =
621  dynamic_cast<const EpetraOperatorWrapper*>(aztecOp);
622 
623  if (opWrapper == 0) {
624  epetra_B = get_Epetra_MultiVector(opRangeMap, rcpFromRef(B));
625  epetra_X = get_Epetra_MultiVector(opDomainMap, rcpFromPtr(X));
626  }
627 
628  //
629  // Use AztecOO to solve each RHS one at a time (which is all that I can do anyway)
630  //
631 
632  int totalIterations = 0;
633  SolveStatus<double> solveStatus;
634  solveStatus.solveStatus = SOLVE_STATUS_CONVERGED;
635  solveStatus.achievedTol = -1.0;
636 
637  /* Get the number of columns in the multivector. We use Thyra
638  * functions rather than Epetra functions to do this, as we
639  * might not yet have created an Epetra multivector. - KL */
640  //const int m = epetra_B->NumVectors();
641  const int m = B.domain()->dim();
642 
643  for( int j = 0; j < m; ++j ) {
644 
645  THYRA_FUNC_TIME_MONITOR_DIFF("Stratimikos: AztecOOLOWS:SingleSolve", SingleSolve);
646 
647  //
648  // Get Epetra_Vector views of B(:,j) and X(:,j)
649  // How this is done will depend on whether we have a true Epetra operator
650  // or we are wrapping a general Thyra operator in an Epetra operator.
651  //
652 
653  // We need to declare epetra_x_j as non-const because when we have a phony
654  // Epetra operator we'll have to copy a thyra vector into it.
655  RCP<Epetra_Vector> epetra_b_j;
656  RCP<Epetra_Vector> epetra_x_j;
657 
658  if (opWrapper == 0) {
659  epetra_b_j = rcpFromRef(*const_cast<Epetra_Vector*>((*epetra_B)(j)));
660  epetra_x_j = rcpFromRef(*(*epetra_X)(j));
661  }
662  else {
663  if (is_null(epetra_b_j)) {
664  epetra_b_j = rcp(new Epetra_Vector(opRangeMap));
665  epetra_x_j = rcp(new Epetra_Vector(opDomainMap));
666  }
667  opWrapper->copyThyraIntoEpetra(*B.col(j), *epetra_b_j);
668  opWrapper->copyThyraIntoEpetra(*X->col(j), *epetra_x_j);
669  }
670 
671  //
672  // Set the RHS and LHS
673  //
674 
675  aztecSolver->SetRHS(&*epetra_b_j);
676  aztecSolver->SetLHS(&*epetra_x_j);
677 
678  //
679  // Solve the linear system
680  //
681  timer.start(true);
682  {
683  SetAztecSolveState
684  setAztecSolveState(aztecSolver,out,verbLevel,solveMeasureType);
685  aztecSolver->Iterate( maxIterations, tol );
686  // NOTE: We ignore the returned status but get it below
687  }
688  timer.stop();
689 
690  //
691  // Scale the solution
692  // (Originally, this was at the end of the loop after all columns had been
693  // processed. It's moved here because we need to do it before copying the
694  // solution back into a Thyra vector. - KL
695  //
696  if (aztecSolverScalar_ != 1.0)
697  epetra_x_j->Scale(1.0/aztecSolverScalar_);
698 
699  //
700  // If necessary, convert the solution back to a non-epetra vector
701  //
702  if (opWrapper != 0) {
703  opWrapper->copyEpetraIntoThyra(*epetra_x_j, X->col(j).ptr());
704  }
705 
706  //
707  // Set the return solve status
708  //
709 
710  const int iterations = aztecSolver->NumIters();
711  const double achievedTol = aztecSolver->ScaledResidual();
712  const double *AZ_status = aztecSolver->GetAztecStatus();
713  std::ostringstream oss;
714  bool converged = false;
715  if (AZ_status[AZ_why]==AZ_normal) { oss << "Aztec returned AZ_normal."; converged = true; }
716  else if (AZ_status[AZ_why]==AZ_param) oss << "Aztec returned AZ_param.";
717  else if (AZ_status[AZ_why]==AZ_breakdown) oss << "Aztec returned AZ_breakdown.";
718  else if (AZ_status[AZ_why]==AZ_loss) oss << "Aztec returned AZ_loss.";
719  else if (AZ_status[AZ_why]==AZ_ill_cond) oss << "Aztec returned AZ_ill_cond.";
720  else if (AZ_status[AZ_why]==AZ_maxits) oss << "Aztec returned AZ_maxits.";
721  else oss << "Aztec returned an unknown status?";
722  oss << " Iterations = " << iterations << ".";
723  oss << " Achieved Tolerance = " << achievedTol << ".";
724  oss << " Total time = " << timer.totalElapsedTime() << " sec.";
725  if (out.get() && static_cast<int>(verbLevel) > static_cast<int>(Teuchos::VERB_NONE) && outputEveryRhs())
726  Teuchos::OSTab(out).o() << "j="<<j<<": " << oss.str() << "\n";
727 
728  solveStatus.achievedTol = TEUCHOS_MAX(solveStatus.achievedTol, achievedTol);
729  // Note, achieveTol may actually be greater than tol due to ill conditioning and roundoff!
730 
731  totalIterations += iterations;
732 
733  solveStatus.message = oss.str();
734  if ( isDefaultSolveCriteria ) {
735  switch(solveStatus.solveStatus) {
736  case SOLVE_STATUS_UNKNOWN:
737  // Leave overall unknown!
738  break;
739  case SOLVE_STATUS_CONVERGED:
740  solveStatus.solveStatus = ( converged ? SOLVE_STATUS_CONVERGED : SOLVE_STATUS_UNCONVERGED );
741  break;
742  case SOLVE_STATUS_UNCONVERGED:
743  // Leave overall unconverged!
744  break;
745  default:
746  TEUCHOS_TEST_FOR_EXCEPT(true); // Should never get here!
747  }
748  }
749  }
750 
751  aztecSolver->UnsetLHSRHS();
752 
753  //
754  // Release the Epetra_MultiVector views of X and B
755  //
756  epetra_X = Teuchos::null;
757  epetra_B = Teuchos::null;
758 
759  //
760  // Update the overall solve criteria
761  //
762  totalTimer.stop();
763  SolveStatus<double> overallSolveStatus;
764  if (isDefaultSolveCriteria) {
765  overallSolveStatus.solveStatus = SOLVE_STATUS_UNKNOWN;
766  overallSolveStatus.achievedTol = SS::unknownTolerance();
767  }
768  else {
769  overallSolveStatus.solveStatus = solveStatus.solveStatus;
770  overallSolveStatus.achievedTol = solveStatus.achievedTol;
771  }
772  std::ostringstream oss;
773  oss
774  << "AztecOO solver "
775  << ( overallSolveStatus.solveStatus==SOLVE_STATUS_CONVERGED ? "converged" : "unconverged" )
776  << " on m = "<<m<<" RHSs using " << totalIterations << " cumulative iterations"
777  << " for an average of " << (totalIterations/m) << " iterations/RHS and"
778  << " total CPU time of "<<totalTimer.totalElapsedTime()<<" sec.";
779  overallSolveStatus.message = oss.str();
780 
781  // Added these statistics following what was done for Belos
782  if (overallSolveStatus.extraParameters.is_null()) {
783  overallSolveStatus.extraParameters = Teuchos::parameterList ();
784  }
785  overallSolveStatus.extraParameters->set ("AztecOO/Iteration Count",
786  totalIterations);
787  // package independent version of the same
788  overallSolveStatus.extraParameters->set ("Iteration Count",
789  totalIterations);
790  overallSolveStatus.extraParameters->set ("AztecOO/Achieved Tolerance",
791  overallSolveStatus.achievedTol);
792 
793  //
794  // Report the overall time
795  //
796  if (out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
797  *out
798  << "\nTotal solve time = "<<totalTimer.totalElapsedTime()<<" sec\n";
799 
800  return overallSolveStatus;
801 
802 }
803 
804 
805 } // end namespace Thyra
RCP< Epetra_MultiVector > get_Epetra_MultiVector(const Epetra_Map &map, const RCP< MultiVectorBase< double > > &mv)
virtual bool opSupportedImpl(EOpTransp M_trans) const
std::string typeName(const T &t)
bool is_null(const boost::shared_ptr< T > &p)
void copyEpetraIntoThyra(const Epetra_MultiVector &x, const Ptr< VectorBase< double > > &thyraVec) const
RCP< const PreconditionerBase< double > > prec_
RCP< T > create_weak() const
basic_FancyOStream< CharT, Traits > & o() const
RCP< const LinearOpSourceBase< double > > extract_fwdOpSrc()
Extract the forward LinearOpBase&lt;double&gt; object so that it can be modified.
bool isExternalPrec() const
Determine if the preconditioner was external or not.
T * get() const
virtual bool solveSupportsSolveMeasureTypeImpl(EOpTransp M_trans, const SolveMeasureType &solveMeasureType) const
virtual void applyImpl(const EOpTransp M_trans, const MultiVectorBase< double > &X, const Ptr< MultiVectorBase< double > > &Y, const double alpha, const double beta) const
basic_OSTab< char > OSTab
virtual bool solveSupportsImpl(EOpTransp M_trans) const
RCP< const VectorSpaceBase< double > > domain() const
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
void start(bool reset=false)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
double stop()
Ptr< T > ptr() const
virtual std::string description() const
#define TEUCHOS_MAX(x, y)
TypeTo implicit_cast(const TypeFrom &t)
RCP< const LinearOpBase< double > > fwdOp_
RCP< const LinearOpSourceBase< double > > fwdOpSrc_
void copyThyraIntoEpetra(const VectorBase< double > &thyraVec, Epetra_MultiVector &x) const
bool nonnull(const boost::shared_ptr< T > &p)
void uninitialize(RCP< const LinearOpBase< double > > *fwdOp=NULL, RCP< const LinearOpSourceBase< double > > *fwdOpSrc=NULL, RCP< const PreconditionerBase< double > > *prec=NULL, bool *isExternalPrec=NULL, RCP< const LinearOpSourceBase< double > > *approxFwdOpSrc=NULL, RCP< AztecOO > *aztecFwdSolver=NULL, bool *allowInexactFwdSolve=NULL, RCP< AztecOO > *aztecAdjSolver=NULL, bool *allowInexactAdjSolve=NULL, double *aztecSolverScalar=NULL)
Uninitialize.
RCP< const PreconditionerBase< double > > extract_prec()
Extract the preconditioner.
double totalElapsedTime(bool readCurrentTime=false) const
#define TEUCHOS_ASSERT(assertion_test)
void initialize(const RCP< const LinearOpBase< double > > &fwdOp, const RCP< const LinearOpSourceBase< double > > &fwdOpSrc, const RCP< const PreconditionerBase< double > > &prec, const bool isExternalPrec, const RCP< const LinearOpSourceBase< double > > &approxFwdOpSrc, const RCP< AztecOO > &aztecFwdSolver, const bool allowInexactFwdSolve=false, const RCP< AztecOO > &aztecAdjSolver=Teuchos::null, const bool allowInexactAdjSolve=false, const double aztecSolverScalar=1.0)
Sets up this object.
RCP< const LinearOpBase< double > > clone() const
RCP< const LinearOpSourceBase< double > > approxFwdOpSrc_
RCP< const VectorSpaceBase< double > > range() const
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
SolveStatus< double > solveImpl(const EOpTransp M_trans, const MultiVectorBase< double > &B, const Ptr< MultiVectorBase< double > > &X, const Ptr< const SolveCriteria< double > > solveCriteria) const
RCP< const LinearOpSourceBase< double > > extract_approxFwdOpSrc()
Extract the approximate forward LinearOpBase&lt;double&gt; object used to build the preconditioner.
AztecOOLinearOpWithSolve(const int fwdDefaultMaxIterations=400, const double fwdDefaultTol=1e-6, const int adjDefaultMaxIterations=400, const double adjDefaultTol=1e-6, const bool outputEveryRhs=false)