Belos Package Browser (Single Doxygen Collection)  Development
BelosFixedPointSolMgr.hpp
Go to the documentation of this file.
1 //@HEADER
2 // ************************************************************************
3 //
4 // Belos: Block Linear Solvers Package
5 // Copyright 2004 Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
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 Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 //@HEADER
41 
42 #ifndef BELOS_FIXEDPOINT_SOLMGR_HPP
43 #define BELOS_FIXEDPOINT_SOLMGR_HPP
44 
49 #include "BelosConfigDefs.hpp"
50 #include "BelosTypes.hpp"
51 
52 #include "BelosLinearProblem.hpp"
53 #include "BelosSolverManager.hpp"
54 
55 #include "BelosCGIter.hpp"
56 #include "BelosFixedPointIter.hpp"
59 #include "BelosStatusTestCombo.hpp"
61 #include "BelosOutputManager.hpp"
62 #ifdef BELOS_TEUCHOS_TIME_MONITOR
63 # include "Teuchos_TimeMonitor.hpp"
64 #endif
65 #include <algorithm>
66 
75 namespace Belos {
76 
78 
79 
87  FixedPointSolMgrLinearProblemFailure(const std::string& what_arg) : BelosError(what_arg)
88  {}};
89 
90  template<class ScalarType, class MV, class OP>
91  class FixedPointSolMgr : public SolverManager<ScalarType,MV,OP> {
92 
93  private:
96  typedef Teuchos::ScalarTraits<ScalarType> SCT;
97  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
98  typedef Teuchos::ScalarTraits<MagnitudeType> MT;
99 
100  public:
101 
103 
104 
111 
129  FixedPointSolMgr( const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > &problem,
130  const Teuchos::RCP<Teuchos::ParameterList> &pl );
131 
133  virtual ~FixedPointSolMgr() {};
134 
136  Teuchos::RCP<SolverManager<ScalarType, MV, OP> > clone () const override {
137  return Teuchos::rcp(new FixedPointSolMgr<ScalarType,MV,OP>);
138  }
140 
142 
143 
144  const LinearProblem<ScalarType,MV,OP>& getProblem() const override {
145  return *problem_;
146  }
147 
150  Teuchos::RCP<const Teuchos::ParameterList> getValidParameters() const override;
151 
154  Teuchos::RCP<const Teuchos::ParameterList> getCurrentParameters() const override { return params_; }
155 
161  Teuchos::Array<Teuchos::RCP<Teuchos::Time> > getTimers() const {
162  return Teuchos::tuple(timerSolve_);
163  }
164 
170  MagnitudeType achievedTol() const override {
171  return achievedTol_;
172  }
173 
175  int getNumIters() const override {
176  return numIters_;
177  }
178 
181  bool isLOADetected() const override { return false; }
183 
185 
186 
188  void setProblem( const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > &problem ) override { problem_ = problem; }
189 
191  void setParameters( const Teuchos::RCP<Teuchos::ParameterList> &params ) override;
192 
194  void replaceUserConvStatusTest( const Teuchos::RCP<StatusTestResNorm<ScalarType,MV,OP> > &userConvStatusTest )
195  {
196 
197  convTest_ = userConvStatusTest;
198 
199  typedef Belos::StatusTestCombo<ScalarType,MV,OP> StatusTestCombo_t;
200  sTest_ = Teuchos::rcp( new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
201 
204 
205  std::string solverDesc = " Fixed Point ";
206  outputTest_->setSolverDesc( solverDesc );
207  }
208 
210 
212 
213 
217  void reset( const ResetType type ) override { if ((type & Belos::Problem) && !Teuchos::is_null(problem_)) problem_->setProblem(); }
219 
221 
222 
240  ReturnType solve() override;
242 
245 
247  std::string description() const override;
249 
250  private:
251 
253  Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
254 
256  Teuchos::RCP<OutputManager<ScalarType> > printer_;
258  Teuchos::RCP<std::ostream> outputStream_;
259 
264  Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
265 
267  Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
268 
270  Teuchos::RCP<StatusTestResNorm<ScalarType,MV,OP> > convTest_;
271 
273  Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
274 
276  Teuchos::RCP<Teuchos::ParameterList> params_;
277 
278  //
279  // Default solver parameters.
280  //
281  static constexpr int maxIters_default_ = 1000;
282  static constexpr bool showMaxResNormOnly_default_ = false;
283  static constexpr int blockSize_default_ = 1;
284  static constexpr int verbosity_default_ = Belos::Errors;
285  static constexpr int outputStyle_default_ = Belos::General;
286  static constexpr int outputFreq_default_ = -1;
287  static constexpr const char * label_default_ = "Belos";
288 // https://stackoverflow.com/questions/24398102/constexpr-and-initialization-of-a-static-const-void-pointer-with-reinterpret-cas
289 #if defined(_WIN32) && defined(__clang__)
290  static constexpr std::ostream * outputStream_default_ =
291  __builtin_constant_p(reinterpret_cast<const std::ostream*>(&std::cout));
292 #else
293  static constexpr std::ostream * outputStream_default_ = &std::cout;
294 #endif
295 
296  //
297  // Current solver parameters and other values.
298  //
299 
302 
309 
312 
315 
318 
320  std::string label_;
321 
323  Teuchos::RCP<Teuchos::Time> timerSolve_;
324 
326  bool isSet_;
327  };
328 
329 
330 // Empty Constructor
331 template<class ScalarType, class MV, class OP>
333  outputStream_(Teuchos::rcp(outputStream_default_,false)),
334  convtol_(DefaultSolverParameters::convTol),
335  achievedTol_(Teuchos::ScalarTraits<MagnitudeType>::zero()),
336  maxIters_(maxIters_default_),
337  numIters_(0),
338  blockSize_(blockSize_default_),
339  verbosity_(verbosity_default_),
340  outputStyle_(outputStyle_default_),
341  outputFreq_(outputFreq_default_),
342  showMaxResNormOnly_(showMaxResNormOnly_default_),
343  label_(label_default_),
344  isSet_(false)
345 {}
346 
347 
348 // Basic Constructor
349 template<class ScalarType, class MV, class OP>
351 FixedPointSolMgr(const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > &problem,
352  const Teuchos::RCP<Teuchos::ParameterList> &pl) :
353  problem_(problem),
354  outputStream_(Teuchos::rcp(outputStream_default_,false)),
355  convtol_(DefaultSolverParameters::convTol),
356  achievedTol_(Teuchos::ScalarTraits<MagnitudeType>::zero()),
357  maxIters_(maxIters_default_),
358  numIters_(0),
359  blockSize_(blockSize_default_),
360  verbosity_(verbosity_default_),
361  outputStyle_(outputStyle_default_),
362  outputFreq_(outputFreq_default_),
363  showMaxResNormOnly_(showMaxResNormOnly_default_),
364  label_(label_default_),
365  isSet_(false)
366 {
367  TEUCHOS_TEST_FOR_EXCEPTION(problem_.is_null(), std::invalid_argument,
368  "FixedPointSolMgr's constructor requires a nonnull LinearProblem instance.");
369 
370  // If the user passed in a nonnull parameter list, set parameters.
371  // Otherwise, the next solve() call will use default parameters,
372  // unless the user calls setParameters() first.
373  if (! pl.is_null()) {
374  setParameters (pl);
375  }
376 }
377 
378 template<class ScalarType, class MV, class OP>
379 void
381 setParameters (const Teuchos::RCP<Teuchos::ParameterList> &params)
382 {
383  // Create the internal parameter list if one doesn't already exist.
384  if (params_ == Teuchos::null) {
385  params_ = Teuchos::rcp( new Teuchos::ParameterList(*getValidParameters()) );
386  }
387  else {
388  params->validateParameters(*getValidParameters());
389  }
390 
391  // Check for maximum number of iterations
392  if (params->isParameter("Maximum Iterations")) {
393  maxIters_ = params->get("Maximum Iterations",maxIters_default_);
394 
395  // Update parameter in our list and in status test.
396  params_->set("Maximum Iterations", maxIters_);
397  if (maxIterTest_!=Teuchos::null)
398  maxIterTest_->setMaxIters( maxIters_ );
399  }
400 
401  // Check for blocksize
402  if (params->isParameter("Block Size")) {
403  blockSize_ = params->get("Block Size",blockSize_default_);
404  TEUCHOS_TEST_FOR_EXCEPTION(blockSize_ <= 0, std::invalid_argument,
405  "Belos::FixedPointSolMgr: \"Block Size\" must be strictly positive.");
406 
407  // Update parameter in our list.
408  params_->set("Block Size", blockSize_);
409  }
410 
411  // Check to see if the timer label changed.
412  if (params->isParameter("Timer Label")) {
413  std::string tempLabel = params->get("Timer Label", label_default_);
414 
415  // Update parameter in our list and solver timer
416  if (tempLabel != label_) {
417  label_ = tempLabel;
418  params_->set("Timer Label", label_);
419  std::string solveLabel = label_ + ": FixedPointSolMgr total solve time";
420 #ifdef BELOS_TEUCHOS_TIME_MONITOR
421  timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
422 #endif
423  }
424  }
425 
426  // Check for a change in verbosity level
427  if (params->isParameter("Verbosity")) {
428  if (Teuchos::isParameterType<int>(*params,"Verbosity")) {
429  verbosity_ = params->get("Verbosity", verbosity_default_);
430  } else {
431  verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,"Verbosity");
432  }
433 
434  // Update parameter in our list.
435  params_->set("Verbosity", verbosity_);
436  if (printer_ != Teuchos::null)
437  printer_->setVerbosity(verbosity_);
438  }
439 
440  // Check for a change in output style
441  if (params->isParameter("Output Style")) {
442  if (Teuchos::isParameterType<int>(*params,"Output Style")) {
443  outputStyle_ = params->get("Output Style", outputStyle_default_);
444  } else {
445  outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,"Output Style");
446  }
447 
448  // Update parameter in our list.
449  params_->set("Output Style", outputStyle_);
450  outputTest_ = Teuchos::null;
451  }
452 
453  // output stream
454  if (params->isParameter("Output Stream")) {
455  outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,"Output Stream");
456 
457  // Update parameter in our list.
458  params_->set("Output Stream", outputStream_);
459  if (printer_ != Teuchos::null)
460  printer_->setOStream( outputStream_ );
461  }
462 
463  // frequency level
464  if (verbosity_ & Belos::StatusTestDetails) {
465  if (params->isParameter("Output Frequency")) {
466  outputFreq_ = params->get("Output Frequency", outputFreq_default_);
467  }
468 
469  // Update parameter in out list and output status test.
470  params_->set("Output Frequency", outputFreq_);
471  if (outputTest_ != Teuchos::null)
472  outputTest_->setOutputFrequency( outputFreq_ );
473  }
474 
475  // Create output manager if we need to.
476  if (printer_ == Teuchos::null) {
477  printer_ = Teuchos::rcp( new OutputManager<ScalarType>(verbosity_, outputStream_) );
478  }
479 
480  // Convergence
481  typedef Belos::StatusTestCombo<ScalarType,MV,OP> StatusTestCombo_t;
482  typedef Belos::StatusTestGenResNorm<ScalarType,MV,OP> StatusTestResNorm_t;
483 
484  // Check for convergence tolerance
485  if (params->isParameter("Convergence Tolerance")) {
486  if (params->isType<MagnitudeType> ("Convergence Tolerance")) {
487  convtol_ = params->get ("Convergence Tolerance",
488  static_cast<MagnitudeType> (DefaultSolverParameters::convTol));
489  }
490  else {
491  convtol_ = params->get ("Convergence Tolerance", DefaultSolverParameters::convTol);
492  }
493 
494  // Update parameter in our list and residual tests.
495  params_->set("Convergence Tolerance", convtol_);
496  if (convTest_ != Teuchos::null)
497  convTest_->setTolerance( convtol_ );
498  }
499 
500  if (params->isParameter("Show Maximum Residual Norm Only")) {
501  showMaxResNormOnly_ = Teuchos::getParameter<bool>(*params,"Show Maximum Residual Norm Only");
502 
503  // Update parameter in our list and residual tests
504  params_->set("Show Maximum Residual Norm Only", showMaxResNormOnly_);
505  if (convTest_ != Teuchos::null)
506  convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
507  }
508 
509  // Create status tests if we need to.
510 
511  // Basic test checks maximum iterations and native residual.
512  if (maxIterTest_ == Teuchos::null)
513  maxIterTest_ = Teuchos::rcp( new StatusTestMaxIters<ScalarType,MV,OP>( maxIters_ ) );
514 
515  // Implicit residual test, using the native residual to determine if convergence was achieved.
516  if (convTest_ == Teuchos::null)
517  convTest_ = Teuchos::rcp( new StatusTestResNorm_t( convtol_, 1 ) );
518 
519  if (sTest_ == Teuchos::null)
520  sTest_ = Teuchos::rcp( new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
521 
522  if (outputTest_ == Teuchos::null) {
523 
524  // Create the status test output class.
525  // This class manages and formats the output from the status test.
526  StatusTestOutputFactory<ScalarType,MV,OP> stoFactory( outputStyle_ );
527  outputTest_ = stoFactory.create( printer_, sTest_, outputFreq_, Passed+Failed+Undefined );
528 
529  // Set the solver string for the output test
530  std::string solverDesc = " Fixed Point ";
531  outputTest_->setSolverDesc( solverDesc );
532 
533  }
534 
535  // Create the timer if we need to.
536  if (timerSolve_ == Teuchos::null) {
537  std::string solveLabel = label_ + ": FixedPointSolMgr total solve time";
538 #ifdef BELOS_TEUCHOS_TIME_MONITOR
539  timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
540 #endif
541  }
542 
543  // Inform the solver manager that the current parameters were set.
544  isSet_ = true;
545 }
546 
547 
548 template<class ScalarType, class MV, class OP>
549 Teuchos::RCP<const Teuchos::ParameterList>
551 {
552  static Teuchos::RCP<const Teuchos::ParameterList> validPL;
553 
554  // Set all the valid parameters and their default values.
555  if(is_null(validPL)) {
556  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
557 
558  // The static_cast is to resolve an issue with older clang versions which
559  // would cause the constexpr to link fail. With c++17 the problem is resolved.
560  pl->set("Convergence Tolerance", static_cast<MagnitudeType>(DefaultSolverParameters::convTol),
561  "The relative residual tolerance that needs to be achieved by the\n"
562  "iterative solver in order for the linear system to be declared converged.");
563  pl->set("Maximum Iterations", static_cast<int>(maxIters_default_),
564  "The maximum number of block iterations allowed for each\n"
565  "set of RHS solved.");
566  pl->set("Block Size", static_cast<int>(blockSize_default_),
567  "The number of vectors in each block.");
568  pl->set("Verbosity", static_cast<int>(verbosity_default_),
569  "What type(s) of solver information should be outputted\n"
570  "to the output stream.");
571  pl->set("Output Style", static_cast<int>(outputStyle_default_),
572  "What style is used for the solver information outputted\n"
573  "to the output stream.");
574  pl->set("Output Frequency", static_cast<int>(outputFreq_default_),
575  "How often convergence information should be outputted\n"
576  "to the output stream.");
577  pl->set("Output Stream", Teuchos::rcp(outputStream_default_,false),
578  "A reference-counted pointer to the output stream where all\n"
579  "solver output is sent.");
580  pl->set("Show Maximum Residual Norm Only", static_cast<bool>(showMaxResNormOnly_default_),
581  "When convergence information is printed, only show the maximum\n"
582  "relative residual norm when the block size is greater than one.");
583  pl->set("Timer Label", static_cast<const char *>(label_default_),
584  "The string to use as a prefix for the timer labels.");
585  validPL = pl;
586  }
587  return validPL;
588 }
589 
590 
591 // solve()
592 template<class ScalarType, class MV, class OP>
594  using Teuchos::RCP;
595  using Teuchos::rcp;
596  using Teuchos::rcp_const_cast;
597  using Teuchos::rcp_dynamic_cast;
598 
599  // Set the current parameters if they were not set before. NOTE:
600  // This may occur if the user generated the solver manager with the
601  // default constructor and then didn't set any parameters using
602  // setParameters().
603  if (!isSet_) {
604  setParameters(Teuchos::parameterList(*getValidParameters()));
605  }
606 
607  TEUCHOS_TEST_FOR_EXCEPTION( !problem_->isProblemSet(),
609  "Belos::FixedPointSolMgr::solve(): Linear problem is not ready, setProblem() "
610  "has not been called.");
611 
612  // Create indices for the linear systems to be solved.
613  int startPtr = 0;
614  int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
615  int numCurrRHS = ( numRHS2Solve < blockSize_) ? numRHS2Solve : blockSize_;
616 
617  std::vector<int> currIdx, currIdx2;
618  currIdx.resize( blockSize_ );
619  currIdx2.resize( blockSize_ );
620  for (int i=0; i<numCurrRHS; ++i)
621  { currIdx[i] = startPtr+i; currIdx2[i]=i; }
622  for (int i=numCurrRHS; i<blockSize_; ++i)
623  { currIdx[i] = -1; currIdx2[i] = i; }
624 
625  // Inform the linear problem of the current linear system to solve.
626  problem_->setLSIndex( currIdx );
627 
629  // Set up the parameter list for the Iteration subclass.
630  Teuchos::ParameterList plist;
631  plist.set("Block Size",blockSize_);
632 
633  // Reset the output status test (controls all the other status tests).
634  outputTest_->reset();
635 
636  // Assume convergence is achieved, then let any failed convergence
637  // set this to false. "Innocent until proven guilty."
638  bool isConverged = true;
639 
641  // Set up the FixedPoint Iteration subclass.
642 
643  RCP<FixedPointIteration<ScalarType,MV,OP> > block_fp_iter;
644  block_fp_iter = rcp (new FixedPointIter<ScalarType,MV,OP> (problem_, printer_, outputTest_, plist));
645 
646  // Enter solve() iterations
647  {
648 #ifdef BELOS_TEUCHOS_TIME_MONITOR
649  Teuchos::TimeMonitor slvtimer(*timerSolve_);
650 #endif
651 
652  while ( numRHS2Solve > 0 ) {
653  //
654  // Reset the active / converged vectors from this block
655  std::vector<int> convRHSIdx;
656  std::vector<int> currRHSIdx( currIdx );
657  currRHSIdx.resize(numCurrRHS);
658 
659  // Reset the number of iterations.
660  block_fp_iter->resetNumIters();
661 
662  // Reset the number of calls that the status test output knows about.
663  outputTest_->resetNumCalls();
664 
665  // Get the current residual for this block of linear systems.
666  RCP<MV> R_0 = MVT::CloneViewNonConst( *(rcp_const_cast<MV>(problem_->getInitResVec())), currIdx );
667 
668  // Set the new state and initialize the solver.
670  newstate.R = R_0;
671  block_fp_iter->initializeFixedPoint(newstate);
672 
673  while(1) {
674 
675  // tell block_fp_iter to iterate
676  try {
677  block_fp_iter->iterate();
678  //
679  // Check whether any of the linear systems converged.
680  //
681  if (convTest_->getStatus() == Passed) {
682  // At least one of the linear system(s) converged.
683  //
684  // Get the column indices of the linear systems that converged.
685  std::vector<int> convIdx = convTest_->convIndices();
686 
687  // If the number of converged linear systems equals the
688  // number of linear systems currently being solved, then
689  // we are done with this block.
690  if (convIdx.size() == currRHSIdx.size())
691  break; // break from while(1){block_fp_iter->iterate()}
692 
693  // Inform the linear problem that we are finished with
694  // this current linear system.
695  problem_->setCurrLS();
696 
697  // Reset currRHSIdx to contain the right-hand sides that
698  // are left to converge for this block.
699  int have = 0;
700  std::vector<int> unconvIdx(currRHSIdx.size());
701  for (unsigned int i=0; i<currRHSIdx.size(); ++i) {
702  bool found = false;
703  for (unsigned int j=0; j<convIdx.size(); ++j) {
704  if (currRHSIdx[i] == convIdx[j]) {
705  found = true;
706  break;
707  }
708  }
709  if (!found) {
710  currIdx2[have] = currIdx2[i];
711  currRHSIdx[have++] = currRHSIdx[i];
712  }
713  else {
714  }
715  }
716  currRHSIdx.resize(have);
717  currIdx2.resize(have);
718 
719  // Set the remaining indices after deflation.
720  problem_->setLSIndex( currRHSIdx );
721 
722  // Get the current residual vector.
723  std::vector<MagnitudeType> norms;
724  R_0 = MVT::CloneCopy( *(block_fp_iter->getNativeResiduals(&norms)),currIdx2 );
725  for (int i=0; i<have; ++i) { currIdx2[i] = i; }
726 
727  // Set the new blocksize for the solver.
728  block_fp_iter->setBlockSize( have );
729 
730  // Set the new state and initialize the solver.
732  defstate.R = R_0;
733  block_fp_iter->initializeFixedPoint(defstate);
734  }
735  //
736  // None of the linear systems converged. Check whether the
737  // maximum iteration count was reached.
738  //
739  else if (maxIterTest_->getStatus() == Passed) {
740  isConverged = false; // None of the linear systems converged.
741  break; // break from while(1){block_fp_iter->iterate()}
742  }
743  //
744  // iterate() returned, but none of our status tests Passed.
745  // This indicates a bug.
746  //
747  else {
748  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
749  "Belos::FixedPointSolMgr::solve(): Neither the convergence test nor "
750  "the maximum iteration count test passed. Please report this bug "
751  "to the Belos developers.");
752  }
753  }
754  catch (const std::exception &e) {
755  std::ostream& err = printer_->stream (Errors);
756  err << "Error! Caught std::exception in FixedPointIteration::iterate() at "
757  << "iteration " << block_fp_iter->getNumIters() << std::endl
758  << e.what() << std::endl;
759  throw;
760  }
761  }
762 
763  // Inform the linear problem that we are finished with this
764  // block linear system.
765  problem_->setCurrLS();
766 
767  // Update indices for the linear systems to be solved.
768  startPtr += numCurrRHS;
769  numRHS2Solve -= numCurrRHS;
770  if ( numRHS2Solve > 0 ) {
771  numCurrRHS = ( numRHS2Solve < blockSize_) ? numRHS2Solve : blockSize_;
772 
773 
774  currIdx.resize( blockSize_ );
775  currIdx2.resize( blockSize_ );
776  for (int i=0; i<numCurrRHS; ++i)
777  { currIdx[i] = startPtr+i; currIdx2[i] = i; }
778  for (int i=numCurrRHS; i<blockSize_; ++i)
779  { currIdx[i] = -1; currIdx2[i] = i; }
780 
781  // Set the next indices.
782  problem_->setLSIndex( currIdx );
783 
784  // Set the new blocksize for the solver.
785  block_fp_iter->setBlockSize( blockSize_ );
786  }
787  else {
788  currIdx.resize( numRHS2Solve );
789  }
790 
791  }// while ( numRHS2Solve > 0 )
792 
793  }
794 
795  // print final summary
796  sTest_->print( printer_->stream(FinalSummary) );
797 
798  // print timing information
799 #ifdef BELOS_TEUCHOS_TIME_MONITOR
800  // Calling summarize() requires communication in general, so don't
801  // call it unless the user wants to print out timing details.
802  // summarize() will do all the work even if it's passed a "black
803  // hole" output stream.
804  if (verbosity_ & TimingDetails) {
805  Teuchos::TimeMonitor::summarize( printer_->stream(TimingDetails) );
806  }
807 #endif
808 
809  // Save the iteration count for this solve.
810  numIters_ = maxIterTest_->getNumIters();
811 
812  // Save the convergence test value ("achieved tolerance") for this solve.
813  {
814  // testValues is nonnull and not persistent.
815  const std::vector<MagnitudeType>* pTestValues = convTest_->getTestValue();
816 
817  TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
818  "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() "
819  "method returned NULL. Please report this bug to the Belos developers.");
820 
821  TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
822  "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() "
823  "method returned a vector of length zero. Please report this bug to the "
824  "Belos developers.");
825 
826  // FIXME (mfh 12 Dec 2011) Does pTestValues really contain the
827  // achieved tolerances for all vectors in the current solve(), or
828  // just for the vectors from the last deflation?
829  achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
830  }
831 
832  if (!isConverged) {
833  return Unconverged; // return from FixedPointSolMgr::solve()
834  }
835  return Converged; // return from FixedPointSolMgr::solve()
836 }
837 
838 // This method requires the solver manager to return a std::string that describes itself.
839 template<class ScalarType, class MV, class OP>
841 {
842  std::ostringstream oss;
843  oss << "Belos::FixedPointSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<">";
844  return oss.str();
845 }
846 
847 } // end Belos namespace
848 
849 #endif /* BELOS_FIXEDPOINT_SOLMGR_HPP */
Collection of types and exceptions used within the Belos solvers.
Belos&#39;s basic output manager for sending information of select verbosity levels to the appropriate ou...
The Belos::FixedPointSolMgr provides a powerful and fully-featured solver manager over the FixedPoint...
Class which manages the output and verbosity of the Belos solvers.
Teuchos::ScalarTraits< ScalarType >::magnitudeType MagnitudeType
Teuchos::RCP< Teuchos::ParameterList > params_
Current parameter list.
Belos concrete class for performing fixed point iteration iteration.
OperatorTraits< ScalarType, MV, OP > OPT
FixedPointSolMgr()
Empty constructor for FixedPointSolMgr. This constructor takes no arguments and sets the default valu...
static constexpr int outputStyle_default_
static constexpr int maxIters_default_
Belos concrete class for performing the conjugate-gradient (CG) iteration.
static constexpr std::ostream * outputStream_default_
Teuchos::ScalarTraits< MagnitudeType > MT
A factory class for generating StatusTestOutput objects.
static constexpr int outputFreq_default_
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
An abstract class of StatusTest for stopping criteria using residual norms.
static constexpr int verbosity_default_
An implementation of StatusTestResNorm using a family of residual norms.
static constexpr bool showMaxResNormOnly_default_
static const double convTol
Default convergence tolerance.
Definition: BelosTypes.hpp:293
Belos::StatusTest class for specifying a maximum number of iterations.
MultiVecTraits< ScalarType, MV > MVT
static constexpr int blockSize_default_
A factory class for generating StatusTestOutput objects.
Traits class which defines basic operations on multivectors.
Belos::StatusTest for logically combining several status tests.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > &params) override
Set the parameters the solver manager should use to solve the linear problem.
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
Teuchos::RCP< std::ostream > outputStream_
Output stream to which the output manager prints.
A Belos::StatusTest class for specifying a maximum number of iterations.
ResetType
How to reset the solver.
Definition: BelosTypes.hpp:206
Pure virtual base class which describes the basic interface for a solver manager. ...
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve...
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
virtual ~FixedPointSolMgr()
Destructor.
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
int numIters_
Number of iterations taken by the last solve() invocation.
std::string description() const override
Method to return description of the block CG solver manager.
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver&#39;s iterate() routine unti...
bool isSet_
Whether or not the parameters have been set (via setParameters()).
int maxIters_
Maximum iteration count (read from parameter list).
Teuchos::RCP< StatusTestMaxIters< ScalarType, MV, OP > > maxIterTest_
Maximum iteration count stopping criterion.
ReturnType
Whether the Belos solve converged for all linear systems.
Definition: BelosTypes.hpp:155
Teuchos::RCP< Teuchos::Time > timerSolve_
Solve timer.
void replaceUserConvStatusTest(const Teuchos::RCP< StatusTestResNorm< ScalarType, MV, OP > > &userConvStatusTest)
Set user-defined convergence status test.
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
Teuchos::RCP< OutputManager< ScalarType > > printer_
Output manager, that handles printing of different kinds of messages.
Teuchos::RCP< const MV > R
The current residual.
static constexpr const char * label_default_
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
FixedPointSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i...
int setTolerance(MagnitudeType tolerance)
Set the value of the tolerance.
Teuchos::RCP< StatusTestOutput< ScalarType, MV, OP > > create(const Teuchos::RCP< OutputManager< ScalarType > > &printer, Teuchos::RCP< StatusTest< ScalarType, MV, OP > > test, int mod, int printStates)
Create the StatusTestOutput object specified by the outputStyle.
Structure to contain pointers to FixedPointIteration state variables.
FixedPointSolMgrLinearProblemFailure(const std::string &what_arg)
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
Teuchos::RCP< StatusTestOutput< ScalarType, MV, OP > > outputTest_
Output "status test" that controls all the other status tests.
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
A class for extending the status testing capabilities of Belos via logical combinations.
int getNumIters() const override
Get the iteration count for the most recent call to solve().
Teuchos::RCP< StatusTestResNorm< ScalarType, MV, OP > > convTest_
Convergence stopping criterion.
Class which defines basic traits for the operator type.
MagnitudeType achievedTol_
Tolerance achieved by the last solve() invocation.
Parent class to all Belos exceptions.
Definition: BelosTypes.hpp:60
Default parameters common to most Belos solvers.
Definition: BelosTypes.hpp:283
Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > problem_
The linear problem to solve.
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
Teuchos::ScalarTraits< ScalarType > SCT
Belos header file which uses auto-configuration information to include necessary C++ headers...
This class implements the preconditioned fixed point iteration.
std::string label_
Prefix label for all the timers.
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
Teuchos::RCP< StatusTest< ScalarType, MV, OP > > sTest_
Aggregate stopping criterion.
MagnitudeType convtol_
Convergence tolerance (read from parameter list).