Belos  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BelosPseudoBlockCGSolMgr.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_PSEUDO_BLOCK_CG_SOLMGR_HPP
43 #define BELOS_PSEUDO_BLOCK_CG_SOLMGR_HPP
44 
49 #include "BelosConfigDefs.hpp"
50 #include "BelosTypes.hpp"
51 
52 #include "BelosLinearProblem.hpp"
53 #include "BelosSolverManager.hpp"
54 
58 #include "BelosStatusTestCombo.hpp"
60 #include "BelosOutputManager.hpp"
61 #include "Teuchos_BLAS.hpp"
62 #ifdef BELOS_TEUCHOS_TIME_MONITOR
63 #include "Teuchos_TimeMonitor.hpp"
64 #endif
65 
82 namespace Belos {
83 
85 
86 
94  PseudoBlockCGSolMgrLinearProblemFailure(const std::string& what_arg) : BelosError(what_arg)
95  {}};
96 
104  PseudoBlockCGSolMgrOrthoFailure(const std::string& what_arg) : BelosError(what_arg)
105  {}};
106 
107  template<class ScalarType, class MV, class OP>
108  class PseudoBlockCGSolMgr : public SolverManager<ScalarType,MV,OP> {
109 
110  private:
114  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
116 
117  public:
118 
120 
121 
128 
140 
142  virtual ~PseudoBlockCGSolMgr() {};
144 
146 
147 
149  return *problem_;
150  }
151 
155 
159 
166  return Teuchos::tuple(timerSolve_);
167  }
168 
170  int getNumIters() const {
171  return numIters_;
172  }
173 
177  bool isLOADetected() const { return false; }
178 
180 
182 
183 
185  void setProblem( const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > &problem ) { problem_ = problem; }
186 
189 
191 
193 
194 
198  void reset( const ResetType type ) { if ((type & Belos::Problem) && !Teuchos::is_null(problem_)) problem_->setProblem(); }
200 
202 
203 
221  ReturnType solve();
222 
224 
227 
229  std::string description() const;
230 
232 
233  private:
234 
235  // Linear problem.
237 
238  // Output manager.
240  Teuchos::RCP<std::ostream> outputStream_;
241 
242  // Status test.
247 
248  // Orthogonalization manager.
250 
251  // Current parameter list.
253 
259  mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
260 
261  // Default solver values.
262  static const MagnitudeType convtol_default_;
263  static const int maxIters_default_;
264  static const bool assertPositiveDefiniteness_default_;
265  static const bool showMaxResNormOnly_default_;
266  static const int verbosity_default_;
267  static const int outputStyle_default_;
268  static const int outputFreq_default_;
269  static const int defQuorum_default_;
270  static const std::string resScale_default_;
271  static const std::string label_default_;
272  static const Teuchos::RCP<std::ostream> outputStream_default_;
273 
274  // Current solver values.
275  MagnitudeType convtol_;
276  int maxIters_, numIters_;
277  int verbosity_, outputStyle_, outputFreq_, defQuorum_;
278  bool assertPositiveDefiniteness_, showMaxResNormOnly_;
279  std::string resScale_;
280 
281  // Timers.
282  std::string label_;
283  Teuchos::RCP<Teuchos::Time> timerSolve_;
284 
285  // Internal state variables.
286  bool isSet_;
287  };
288 
289 
290 // Default solver values.
291 template<class ScalarType, class MV, class OP>
292 const typename PseudoBlockCGSolMgr<ScalarType,MV,OP>::MagnitudeType PseudoBlockCGSolMgr<ScalarType,MV,OP>::convtol_default_ = 1e-8;
293 
294 template<class ScalarType, class MV, class OP>
295 const int PseudoBlockCGSolMgr<ScalarType,MV,OP>::maxIters_default_ = 1000;
296 
297 template<class ScalarType, class MV, class OP>
298 const bool PseudoBlockCGSolMgr<ScalarType,MV,OP>::assertPositiveDefiniteness_default_ = true;
299 
300 template<class ScalarType, class MV, class OP>
301 const bool PseudoBlockCGSolMgr<ScalarType,MV,OP>::showMaxResNormOnly_default_ = false;
302 
303 template<class ScalarType, class MV, class OP>
304 const int PseudoBlockCGSolMgr<ScalarType,MV,OP>::verbosity_default_ = Belos::Errors;
305 
306 template<class ScalarType, class MV, class OP>
307 const int PseudoBlockCGSolMgr<ScalarType,MV,OP>::outputStyle_default_ = Belos::General;
308 
309 template<class ScalarType, class MV, class OP>
310 const int PseudoBlockCGSolMgr<ScalarType,MV,OP>::outputFreq_default_ = -1;
311 
312 template<class ScalarType, class MV, class OP>
313 const int PseudoBlockCGSolMgr<ScalarType,MV,OP>::defQuorum_default_ = 1;
314 
315 template<class ScalarType, class MV, class OP>
316 const std::string PseudoBlockCGSolMgr<ScalarType,MV,OP>::resScale_default_ = "Norm of Initial Residual";
317 
318 template<class ScalarType, class MV, class OP>
319 const std::string PseudoBlockCGSolMgr<ScalarType,MV,OP>::label_default_ = "Belos";
320 
321 template<class ScalarType, class MV, class OP>
322 const Teuchos::RCP<std::ostream> PseudoBlockCGSolMgr<ScalarType,MV,OP>::outputStream_default_ = Teuchos::rcp(&std::cout,false);
323 
324 
325 // Empty Constructor
326 template<class ScalarType, class MV, class OP>
328  outputStream_(outputStream_default_),
329  convtol_(convtol_default_),
330  maxIters_(maxIters_default_),
331  verbosity_(verbosity_default_),
332  outputStyle_(outputStyle_default_),
333  outputFreq_(outputFreq_default_),
334  defQuorum_(defQuorum_default_),
335  assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
336  showMaxResNormOnly_(showMaxResNormOnly_default_),
337  resScale_(resScale_default_),
338  label_(label_default_),
339  isSet_(false)
340 {}
341 
342 // Basic Constructor
343 template<class ScalarType, class MV, class OP>
347  problem_(problem),
348  outputStream_(outputStream_default_),
349  convtol_(convtol_default_),
350  maxIters_(maxIters_default_),
351  verbosity_(verbosity_default_),
352  outputStyle_(outputStyle_default_),
353  outputFreq_(outputFreq_default_),
354  defQuorum_(defQuorum_default_),
355  assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
356  showMaxResNormOnly_(showMaxResNormOnly_default_),
357  resScale_(resScale_default_),
358  label_(label_default_),
359  isSet_(false)
360 {
361  TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null, std::invalid_argument, "Problem not given to solver manager.");
362 
363  // If the parameter list pointer is null, then set the current parameters to the default parameter list.
364  if (!is_null(pl)) {
365  // Set the parameters using the list that was passed in.
366  setParameters( pl );
367  }
368 }
369 
370 template<class ScalarType, class MV, class OP>
372 {
374  using Teuchos::parameterList;
375  using Teuchos::RCP;
376 
377  RCP<const ParameterList> defaultParams = getValidParameters();
378 
379  // Create the internal parameter list if one doesn't already exist.
380  if (params_.is_null()) {
381  params_ = parameterList (*defaultParams);
382  } else {
383  params->validateParameters (*defaultParams);
384  }
385 
386  // Check for maximum number of iterations
387  if (params->isParameter("Maximum Iterations")) {
388  maxIters_ = params->get("Maximum Iterations",maxIters_default_);
389 
390  // Update parameter in our list and in status test.
391  params_->set("Maximum Iterations", maxIters_);
392  if (maxIterTest_!=Teuchos::null)
393  maxIterTest_->setMaxIters( maxIters_ );
394  }
395 
396  // Check if positive definiteness assertions are to be performed
397  if (params->isParameter("Assert Positive Definiteness")) {
398  assertPositiveDefiniteness_ = params->get("Assert Positive Definiteness",assertPositiveDefiniteness_default_);
399 
400  // Update parameter in our list.
401  params_->set("Assert Positive Definiteness", assertPositiveDefiniteness_);
402  }
403 
404  // Check to see if the timer label changed.
405  if (params->isParameter("Timer Label")) {
406  std::string tempLabel = params->get("Timer Label", label_default_);
407 
408  // Update parameter in our list and solver timer
409  if (tempLabel != label_) {
410  label_ = tempLabel;
411  params_->set("Timer Label", label_);
412  std::string solveLabel = label_ + ": PseudoBlockCGSolMgr total solve time";
413 #ifdef BELOS_TEUCHOS_TIME_MONITOR
414  timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
415 #endif
416  if (ortho_ != Teuchos::null) {
417  ortho_->setLabel( label_ );
418  }
419  }
420  }
421 
422  // Check for a change in verbosity level
423  if (params->isParameter("Verbosity")) {
424  if (Teuchos::isParameterType<int>(*params,"Verbosity")) {
425  verbosity_ = params->get("Verbosity", verbosity_default_);
426  } else {
427  verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,"Verbosity");
428  }
429 
430  // Update parameter in our list.
431  params_->set("Verbosity", verbosity_);
432  if (printer_ != Teuchos::null)
433  printer_->setVerbosity(verbosity_);
434  }
435 
436  // Check for a change in output style
437  if (params->isParameter("Output Style")) {
438  if (Teuchos::isParameterType<int>(*params,"Output Style")) {
439  outputStyle_ = params->get("Output Style", outputStyle_default_);
440  } else {
441  outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,"Output Style");
442  }
443 
444  // Reconstruct the convergence test if the explicit residual test is not being used.
445  params_->set("Output Style", outputStyle_);
446  outputTest_ = Teuchos::null;
447  }
448 
449  // output stream
450  if (params->isParameter("Output Stream")) {
451  outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,"Output Stream");
452 
453  // Update parameter in our list.
454  params_->set("Output Stream", outputStream_);
455  if (printer_ != Teuchos::null)
456  printer_->setOStream( outputStream_ );
457  }
458 
459  // frequency level
460  if (verbosity_ & Belos::StatusTestDetails) {
461  if (params->isParameter("Output Frequency")) {
462  outputFreq_ = params->get("Output Frequency", outputFreq_default_);
463  }
464 
465  // Update parameter in out list and output status test.
466  params_->set("Output Frequency", outputFreq_);
467  if (outputTest_ != Teuchos::null)
468  outputTest_->setOutputFrequency( outputFreq_ );
469  }
470 
471  // Create output manager if we need to.
472  if (printer_ == Teuchos::null) {
473  printer_ = Teuchos::rcp( new OutputManager<ScalarType>(verbosity_, outputStream_) );
474  }
475 
476  // Convergence
477  typedef Belos::StatusTestCombo<ScalarType,MV,OP> StatusTestCombo_t;
478  typedef Belos::StatusTestGenResNorm<ScalarType,MV,OP> StatusTestResNorm_t;
479 
480  // Check for convergence tolerance
481  if (params->isParameter("Convergence Tolerance")) {
482  convtol_ = params->get("Convergence Tolerance",convtol_default_);
483 
484  // Update parameter in our list and residual tests.
485  params_->set("Convergence Tolerance", convtol_);
486  if (convTest_ != Teuchos::null)
487  convTest_->setTolerance( convtol_ );
488  }
489 
490  if (params->isParameter("Show Maximum Residual Norm Only")) {
491  showMaxResNormOnly_ = Teuchos::getParameter<bool>(*params,"Show Maximum Residual Norm Only");
492 
493  // Update parameter in our list and residual tests
494  params_->set("Show Maximum Residual Norm Only", showMaxResNormOnly_);
495  if (convTest_ != Teuchos::null)
496  convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
497  }
498 
499  // Check for a change in scaling, if so we need to build new residual tests.
500  bool newResTest = false;
501  {
502  // "Residual Scaling" is the old parameter name; "Implicit
503  // Residual Scaling" is the new name. We support both options for
504  // backwards compatibility.
505  std::string tempResScale = resScale_;
506  bool implicitResidualScalingName = false;
507  if (params->isParameter ("Residual Scaling")) {
508  tempResScale = params->get<std::string> ("Residual Scaling");
509  }
510  else if (params->isParameter ("Implicit Residual Scaling")) {
511  tempResScale = params->get<std::string> ("Implicit Residual Scaling");
512  implicitResidualScalingName = true;
513  }
514 
515  // Only update the scaling if it's different.
516  if (resScale_ != tempResScale) {
517  Belos::ScaleType resScaleType = convertStringToScaleType( tempResScale );
518  resScale_ = tempResScale;
519 
520  // Update parameter in our list and residual tests, using the
521  // given parameter name.
522  if (implicitResidualScalingName) {
523  params_->set ("Implicit Residual Scaling", resScale_);
524  }
525  else {
526  params_->set ("Residual Scaling", resScale_);
527  }
528 
529  if (! convTest_.is_null()) {
530  try {
531  convTest_->defineScaleForm( resScaleType, Belos::TwoNorm );
532  }
533  catch (std::exception& e) {
534  // Make sure the convergence test gets constructed again.
535  newResTest = true;
536  }
537  }
538  }
539  }
540 
541  // Get the deflation quorum, or number of converged systems before deflation is allowed
542  if (params->isParameter("Deflation Quorum")) {
543  defQuorum_ = params->get("Deflation Quorum", defQuorum_);
544  params_->set("Deflation Quorum", defQuorum_);
545  if (convTest_ != Teuchos::null)
546  convTest_->setQuorum( defQuorum_ );
547  }
548 
549  // Create status tests if we need to.
550 
551  // Basic test checks maximum iterations and native residual.
552  if (maxIterTest_ == Teuchos::null)
553  maxIterTest_ = Teuchos::rcp( new StatusTestMaxIters<ScalarType,MV,OP>( maxIters_ ) );
554 
555  // Implicit residual test, using the native residual to determine if convergence was achieved.
556  if (convTest_ == Teuchos::null || newResTest) {
557  convTest_ = Teuchos::rcp( new StatusTestResNorm_t( convtol_, defQuorum_, showMaxResNormOnly_ ) );
558  convTest_->defineScaleForm( convertStringToScaleType( resScale_ ), Belos::TwoNorm );
559  }
560 
561  if (sTest_ == Teuchos::null || newResTest)
562  sTest_ = Teuchos::rcp( new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
563 
564  if (outputTest_ == Teuchos::null || newResTest) {
565 
566  // Create the status test output class.
567  // This class manages and formats the output from the status test.
568  StatusTestOutputFactory<ScalarType,MV,OP> stoFactory( outputStyle_ );
569  outputTest_ = stoFactory.create( printer_, sTest_, outputFreq_, Passed+Failed+Undefined );
570 
571  // Set the solver string for the output test
572  std::string solverDesc = " Pseudo Block CG ";
573  outputTest_->setSolverDesc( solverDesc );
574 
575  }
576 
577  // Create the timer if we need to.
578  if (timerSolve_ == Teuchos::null) {
579  std::string solveLabel = label_ + ": PseudoBlockCGSolMgr total solve time";
580 #ifdef BELOS_TEUCHOS_TIME_MONITOR
581  timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
582 #endif
583  }
584 
585  // Inform the solver manager that the current parameters were set.
586  isSet_ = true;
587 }
588 
589 
590 template<class ScalarType, class MV, class OP>
593 {
595  using Teuchos::parameterList;
596  using Teuchos::RCP;
597 
598  if (validParams_.is_null()) {
599  // Set all the valid parameters and their default values.
600  RCP<ParameterList> pl = parameterList ();
601  pl->set("Convergence Tolerance", convtol_default_,
602  "The relative residual tolerance that needs to be achieved by the\n"
603  "iterative solver in order for the linera system to be declared converged.");
604  pl->set("Maximum Iterations", maxIters_default_,
605  "The maximum number of block iterations allowed for each\n"
606  "set of RHS solved.");
607  pl->set("Assert Positive Definiteness", assertPositiveDefiniteness_default_,
608  "Whether or not to assert that the linear operator\n"
609  "and the preconditioner are indeed positive definite.");
610  pl->set("Verbosity", verbosity_default_,
611  "What type(s) of solver information should be outputted\n"
612  "to the output stream.");
613  pl->set("Output Style", outputStyle_default_,
614  "What style is used for the solver information outputted\n"
615  "to the output stream.");
616  pl->set("Output Frequency", outputFreq_default_,
617  "How often convergence information should be outputted\n"
618  "to the output stream.");
619  pl->set("Deflation Quorum", defQuorum_default_,
620  "The number of linear systems that need to converge before\n"
621  "they are deflated. This number should be <= block size.");
622  pl->set("Output Stream", outputStream_default_,
623  "A reference-counted pointer to the output stream where all\n"
624  "solver output is sent.");
625  pl->set("Show Maximum Residual Norm Only", showMaxResNormOnly_default_,
626  "When convergence information is printed, only show the maximum\n"
627  "relative residual norm when the block size is greater than one.");
628  pl->set("Implicit Residual Scaling", resScale_default_,
629  "The type of scaling used in the residual convergence test.");
630  // We leave the old name as a valid parameter for backwards
631  // compatibility (so that validateParametersAndSetDefaults()
632  // doesn't raise an exception if it encounters "Residual
633  // Scaling"). The new name was added for compatibility with other
634  // solvers, none of which use "Residual Scaling".
635  pl->set("Residual Scaling", resScale_default_,
636  "The type of scaling used in the residual convergence test. This "
637  "name is deprecated; the new name is \"Implicit Residual Scaling\".");
638  pl->set("Timer Label", label_default_,
639  "The string to use as a prefix for the timer labels.");
640  // defaultParams_->set("Restart Timers", restartTimers_);
641  validParams_ = pl;
642  }
643  return validParams_;
644 }
645 
646 
647 // solve()
648 template<class ScalarType, class MV, class OP>
650 
651  // Set the current parameters if they were not set before.
652  // NOTE: This may occur if the user generated the solver manager with the default constructor and
653  // then didn't set any parameters using setParameters().
654  if (!isSet_) { setParameters( params_ ); }
655 
657 
659  "Belos::PseudoBlockCGSolMgr::solve(): Linear problem is not ready, setProblem() has not been called.");
660 
661  // Create indices for the linear systems to be solved.
662  int startPtr = 0;
663  int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
664  int numCurrRHS = numRHS2Solve;
665 
666  std::vector<int> currIdx( numRHS2Solve ), currIdx2( numRHS2Solve );
667  for (int i=0; i<numRHS2Solve; ++i) {
668  currIdx[i] = startPtr+i;
669  currIdx2[i]=i;
670  }
671 
672  // Inform the linear problem of the current linear system to solve.
673  problem_->setLSIndex( currIdx );
674 
676  // Parameter list
678 
679  plist.set("Assert Positive Definiteness",assertPositiveDefiniteness_);
680 
681  // Reset the status test.
682  outputTest_->reset();
683 
684  // Assume convergence is achieved, then let any failed convergence set this to false.
685  bool isConverged = true;
686 
688  // Pseudo-Block CG solver
689 
691  = Teuchos::rcp( new PseudoBlockCGIter<ScalarType,MV,OP>(problem_,printer_,outputTest_,plist) );
692 
693  // Enter solve() iterations
694  {
695 #ifdef BELOS_TEUCHOS_TIME_MONITOR
696  Teuchos::TimeMonitor slvtimer(*timerSolve_);
697 #endif
698 
699  while ( numRHS2Solve > 0 ) {
700 
701  // Reset the active / converged vectors from this block
702  std::vector<int> convRHSIdx;
703  std::vector<int> currRHSIdx( currIdx );
704  currRHSIdx.resize(numCurrRHS);
705 
706  // Reset the number of iterations.
707  block_cg_iter->resetNumIters();
708 
709  // Reset the number of calls that the status test output knows about.
710  outputTest_->resetNumCalls();
711 
712  // Get the current residual for this block of linear systems.
713  Teuchos::RCP<MV> R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())), currIdx );
714 
715  // Get a new state struct and initialize the solver.
717  newState.R = R_0;
718  block_cg_iter->initializeCG(newState);
719 
720  while(1) {
721 
722  // tell block_gmres_iter to iterate
723  try {
724  block_cg_iter->iterate();
725 
727  //
728  // check convergence first
729  //
731  if ( convTest_->getStatus() == Passed ) {
732 
733  // Figure out which linear systems converged.
734  std::vector<int> convIdx = Teuchos::rcp_dynamic_cast<StatusTestGenResNorm<ScalarType,MV,OP> >(convTest_)->convIndices();
735 
736  // If the number of converged linear systems is equal to the
737  // number of current linear systems, then we are done with this block.
738  if (convIdx.size() == currRHSIdx.size())
739  break; // break from while(1){block_cg_iter->iterate()}
740 
741  // Inform the linear problem that we are finished with this current linear system.
742  problem_->setCurrLS();
743 
744  // Reset currRHSIdx to have the right-hand sides that are left to converge for this block.
745  int have = 0;
746  std::vector<int> unconvIdx(currRHSIdx.size());
747  for (unsigned int i=0; i<currRHSIdx.size(); ++i) {
748  bool found = false;
749  for (unsigned int j=0; j<convIdx.size(); ++j) {
750  if (currRHSIdx[i] == convIdx[j]) {
751  found = true;
752  break;
753  }
754  }
755  if (!found) {
756  currIdx2[have] = currIdx2[i];
757  currRHSIdx[have++] = currRHSIdx[i];
758  }
759  }
760  currRHSIdx.resize(have);
761  currIdx2.resize(have);
762 
763  // Set the remaining indices after deflation.
764  problem_->setLSIndex( currRHSIdx );
765 
766  // Get the current residual vector.
767  std::vector<MagnitudeType> norms;
768  R_0 = MVT::CloneCopy( *(block_cg_iter->getNativeResiduals(&norms)),currIdx2 );
769  for (int i=0; i<have; ++i) { currIdx2[i] = i; }
770 
771  // Set the new state and initialize the solver.
773  defstate.R = R_0;
774  block_cg_iter->initializeCG(defstate);
775  }
776 
778  //
779  // check for maximum iterations
780  //
782  else if ( maxIterTest_->getStatus() == Passed ) {
783  // we don't have convergence
784  isConverged = false;
785  break; // break from while(1){block_cg_iter->iterate()}
786  }
787 
789  //
790  // we returned from iterate(), but none of our status tests Passed.
791  // something is wrong, and it is probably our fault.
792  //
794 
795  else {
796  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
797  "Belos::PseudoBlockCGSolMgr::solve(): Invalid return from PseudoBlockCGIter::iterate().");
798  }
799  }
800  catch (const std::exception &e) {
801  printer_->stream(Errors) << "Error! Caught std::exception in PseudoBlockCGIter::iterate() at iteration "
802  << block_cg_iter->getNumIters() << std::endl
803  << e.what() << std::endl;
804  throw;
805  }
806  }
807 
808  // Inform the linear problem that we are finished with this block linear system.
809  problem_->setCurrLS();
810 
811  // Update indices for the linear systems to be solved.
812  startPtr += numCurrRHS;
813  numRHS2Solve -= numCurrRHS;
814 
815  if ( numRHS2Solve > 0 ) {
816 
817  numCurrRHS = numRHS2Solve;
818  currIdx.resize( numCurrRHS );
819  currIdx2.resize( numCurrRHS );
820  for (int i=0; i<numCurrRHS; ++i)
821  { currIdx[i] = startPtr+i; currIdx2[i] = i; }
822 
823  // Set the next indices.
824  problem_->setLSIndex( currIdx );
825  }
826  else {
827  currIdx.resize( numRHS2Solve );
828  }
829 
830  }// while ( numRHS2Solve > 0 )
831 
832  }
833 
834  // print final summary
835  sTest_->print( printer_->stream(FinalSummary) );
836 
837  // print timing information
838 #ifdef BELOS_TEUCHOS_TIME_MONITOR
839  // Calling summarize() can be expensive, so don't call unless the
840  // user wants to print out timing details. summarize() will do all
841  // the work even if it's passed a "black hole" output stream.
842  if (verbosity_ & TimingDetails)
843  Teuchos::TimeMonitor::summarize( printer_->stream(TimingDetails) );
844 #endif
845 
846  // get iteration information for this solve
847  numIters_ = maxIterTest_->getNumIters();
848 
849  if (!isConverged ) {
850  return Unconverged; // return from PseudoBlockCGSolMgr::solve()
851  }
852  return Converged; // return from PseudoBlockCGSolMgr::solve()
853 }
854 
855 // This method requires the solver manager to return a std::string that describes itself.
856 template<class ScalarType, class MV, class OP>
858 {
859  std::ostringstream oss;
860  oss << "Belos::PseudoBlockCGSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<">";
861  oss << "{";
862  oss << "}";
863  return oss.str();
864 }
865 
866 } // end Belos namespace
867 
868 #endif /* BELOS_PSEUDO_BLOCK_CG_SOLMGR_HPP */
ScaleType convertStringToScaleType(const std::string &scaleType)
Convert the given string to its ScaleType enum value.
Definition: BelosTypes.cpp:93
Teuchos::RCP< const MV > R
The current residual.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem)
Set the linear problem that needs to be solved.
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...
Class which manages the output and verbosity of the Belos solvers.
bool is_null(const boost::shared_ptr< T > &p)
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Get a parameter list containing the valid parameters for this object.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > &params)
Set the parameters the solver manager should use to solve the linear problem.
ScaleType
The type of scaling to use on the residual norm value.
Definition: BelosTypes.hpp:119
void reset(const ResetType type)
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
Structure to contain pointers to CGIteration state variables.
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
static RCP< Time > getNewCounter(const std::string &name)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
A factory class for generating StatusTestOutput objects.
bool isLOADetected() const
Return whether a loss of accuracy was detected by this solver during the most current solve...
PseudoBlockCGSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i...
An implementation of StatusTestResNorm using a family of residual norms.
Belos::StatusTest class for specifying a maximum number of iterations.
static std::string name()
A factory class for generating StatusTestOutput objects.
PseudoBlockCGSolMgr()
Empty constructor for BlockCGSolMgr. This constructor takes no arguments and sets the default values ...
std::string description() const
Method to return description of the block CG solver manager.
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="")
Traits class which defines basic operations on multivectors.
The Belos::PseudoBlockCGSolMgr provides a powerful and fully-featured solver manager over the pseudo-...
Belos::StatusTest for logically combining several status tests.
bool isParameter(const std::string &name) const
This class implements the pseudo-block CG iteration, where the basic CG algorithm is performed on all...
A Belos::StatusTest class for specifying a maximum number of iterations.
ResetType
How to reset the solver.
Definition: BelosTypes.hpp:200
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Pure virtual base class which describes the basic interface for a solver manager. ...
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
PseudoBlockCGSolMgrOrthoFailure(const std::string &what_arg)
int getNumIters() const
Get the iteration count for the most recent call to solve().
ReturnType
Whether the Belos solve converged for all linear systems.
Definition: BelosTypes.hpp:149
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
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.
PseudoBlockCGSolMgrLinearProblemFailure(const std::string &what_arg)
ReturnType solve()
This method performs possibly repeated calls to the underlying linear solver&#39;s iterate() routine unti...
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Return a reference to the linear problem being solved by this solver manager.
A class for extending the status testing capabilities of Belos via logical combinations.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
PseudoBlockCGSolMgrOrthoFailure is thrown when the orthogonalization manager is unable to generate or...
Class which defines basic traits for the operator type.
Belos concrete class for performing the pseudo-block CG iteration.
Parent class to all Belos exceptions.
Definition: BelosTypes.hpp:60
Belos header file which uses auto-configuration information to include necessary C++ headers...
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const
Get a parameter list containing the current parameters for this object.
virtual ~PseudoBlockCGSolMgr()
Destructor.