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

Generated on Mon Feb 5 2018 15:01:51 for Belos by doxygen 1.8.13