Belos Version of the Day
Loading...
Searching...
No Matches
BelosLSQRSolMgr.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Belos: Block Linear Solvers Package
4//
5// Copyright 2004-2016 NTESS and the Belos contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef BELOS_LSQR_SOLMGR_HPP
11#define BELOS_LSQR_SOLMGR_HPP
12
15
16#include "BelosConfigDefs.hpp"
17#include "BelosTypes.hpp"
18
23
25#include "BelosLSQRIter.hpp"
31#include "Teuchos_as.hpp"
32
33#ifdef BELOS_TEUCHOS_TIME_MONITOR
34#include "Teuchos_TimeMonitor.hpp"
35#endif
36
37namespace Belos {
38
39
41
42
50public:
54};
55
63public:
66 {}
67};
68
182
183
184// Partial specialization for complex ScalarType.
185// This contains a trivial implementation.
186// See discussion in the class documentation above.
187template<class ScalarType, class MV, class OP, class DM = DefaultDenseMatrix<int,ScalarType>,
188 const bool scalarTypeIsComplex = Teuchos::ScalarTraits<ScalarType>::isComplex>
190 public Details::RealSolverManager<ScalarType, MV, OP, DM,
191 Teuchos::ScalarTraits<ScalarType>::isComplex>
192{
193 static const bool isComplex = Teuchos::ScalarTraits<ScalarType>::isComplex;
195
196public:
198 base_type ()
199 {}
201 const Teuchos::RCP<Teuchos::ParameterList> &pl) :
202 base_type ()
203 {}
204 virtual ~LSQRSolMgr () {}
205
207 Teuchos::RCP<SolverManager<ScalarType, MV, OP, DM> > clone () const override {
209 }
210};
211
212
213// Partial specialization for real ScalarType.
214// This contains the actual working implementation of LSQR.
215// See discussion in the class documentation above.
216template<class ScalarType, class MV, class OP, class DM>
217class LSQRSolMgr<ScalarType, MV, OP, DM, false> :
218 public Details::RealSolverManager<ScalarType, MV, OP, DM, false> {
219private:
222 typedef Teuchos::ScalarTraits<ScalarType> STS;
223 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
224 typedef Teuchos::ScalarTraits<MagnitudeType> STM;
225
226public:
227
229
230
237 LSQRSolMgr ();
238
267 const Teuchos::RCP<Teuchos::ParameterList>& pl);
268
270 virtual ~LSQRSolMgr () {}
271
273 Teuchos::RCP<SolverManager<ScalarType, MV, OP, DM> > clone () const override {
274 return Teuchos::rcp(new LSQRSolMgr<ScalarType,MV,OP,DM>);
275 }
277
279
283 return *problem_;
284 }
285
288 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters() const override;
289
292 Teuchos::RCP<const Teuchos::ParameterList> getCurrentParameters() const override {
293 return params_;
294 }
295
301 Teuchos::Array<Teuchos::RCP<Teuchos::Time> > getTimers () const {
302 return Teuchos::tuple (timerSolve_);
303 }
304
306 int getNumIters () const override {
307 return numIters_;
308 }
309
314 MagnitudeType getMatCondNum () const {
315 return matCondNum_;
316 }
317
322 MagnitudeType getMatNorm () const {
323 return matNorm_;
324 }
325
334 MagnitudeType getResNorm () const {
335 return resNorm_;
336 }
337
339 MagnitudeType getMatResNorm () const {
340 return matResNorm_;
341 }
342
351 bool isLOADetected () const override { return false; }
352
354
356
357
359 void setProblem (const Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> >& problem) override {
360 problem_ = problem;
361 // Force the status tests to be rebuilt on the next solve() so that a
362 // status test installed via setDebugStatusTest() is wired into sTest_.
363 isSet_ = false;
364 }
365
367 void setParameters (const Teuchos::RCP<Teuchos::ParameterList>& params) override;
368
371 debugStatusTest_ = debugStatusTest;
372 // Force the status-test tree to be rebuilt on the next solve() so the debug
373 // test gets OR-combined into sTest_. sTest_ is cached behind an is_null()
374 // guard in setParameters(), so it must be reset to null to trigger a
375 // rebuild.
376 sTest_ = Teuchos::null;
377 isSet_ = false;
378 }
379
381
383
384
388 void reset (const ResetType type) override {
389 if ((type & Belos::Problem) && ! problem_.is_null ()) {
390 problem_->setProblem ();
391 }
392 }
393
395
397
416 ReturnType solve() override;
417
419
421
423 std::string description () const override;
424
426
427private:
428
430 Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > problem_;
432 Teuchos::RCP<OutputManager<ScalarType> > printer_;
434 Teuchos::RCP<std::ostream> outputStream_;
435
437 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > sTest_;
438 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP,DM> > maxIterTest_;
439 Teuchos::RCP<LSQRStatusTest<ScalarType,MV,OP,DM> > convTest_;
440 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP,DM> > outputTest_;
441 Teuchos::RCP<StatusTest<ScalarType, MV, OP, DM> > debugStatusTest_;
442
444 Teuchos::RCP<Teuchos::ParameterList> params_;
445
451 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
452
453 // Current solver input parameters
454 MagnitudeType lambda_;
455 MagnitudeType relRhsErr_;
456 MagnitudeType relMatErr_;
457 MagnitudeType condMax_;
458 int maxIters_, termIterMax_;
459 int verbosity_, outputStyle_, outputFreq_;
460
461 // Terminal solver state values
462 int numIters_;
463 MagnitudeType matCondNum_;
464 MagnitudeType matNorm_;
465 MagnitudeType resNorm_;
466 MagnitudeType matResNorm_;
467
468 // Timers.
469 std::string label_;
470 Teuchos::RCP<Teuchos::Time> timerSolve_;
471
472 // Internal state variables.
473 bool isSet_;
474 bool loaDetected_;
475};
476
477template<class ScalarType, class MV, class OP, class DM>
479 lambda_ (STM::zero ()),
480 relRhsErr_ (Teuchos::as<MagnitudeType> (10) * STM::squareroot (STM::eps ())),
481 relMatErr_ (Teuchos::as<MagnitudeType> (10) * STM::squareroot (STM::eps ())),
482 condMax_ (STM::one () / STM::eps ()),
483 maxIters_ (1000),
484 termIterMax_ (1),
485 verbosity_ (Belos::Errors),
486 outputStyle_ (Belos::General),
487 outputFreq_ (-1),
488 numIters_ (0),
489 matCondNum_ (STM::zero ()),
490 matNorm_ (STM::zero ()),
491 resNorm_ (STM::zero ()),
492 matResNorm_ (STM::zero ()),
493 isSet_ (false),
494 loaDetected_ (false)
495{}
496
497template<class ScalarType, class MV, class OP, class DM>
500 const Teuchos::RCP<Teuchos::ParameterList>& pl) :
501 problem_ (problem),
502 lambda_ (STM::zero ()),
503 relRhsErr_ (Teuchos::as<MagnitudeType> (10) * STM::squareroot (STM::eps ())),
504 relMatErr_ (Teuchos::as<MagnitudeType> (10) * STM::squareroot (STM::eps ())),
505 condMax_ (STM::one () / STM::eps ()),
506 maxIters_ (1000),
507 termIterMax_ (1),
508 verbosity_ (Belos::Errors),
509 outputStyle_ (Belos::General),
510 outputFreq_ (-1),
511 numIters_ (0),
512 matCondNum_ (STM::zero ()),
513 matNorm_ (STM::zero ()),
514 resNorm_ (STM::zero ()),
515 matResNorm_ (STM::zero ()),
516 isSet_ (false),
517 loaDetected_ (false)
518{
519 // The linear problem to solve is allowed to be null here. The user
520 // must then set a nonnull linear problem (by calling setProblem())
521 // before calling solve().
522 //
523 // Similarly, users are allowed to set a null parameter list here,
524 // but they must first set a nonnull parameter list (by calling
525 // setParameters()) before calling solve().
526 if (! pl.is_null ()) {
527 setParameters (pl);
528 }
529}
530
531
532template<class ScalarType, class MV, class OP, class DM>
533Teuchos::RCP<const Teuchos::ParameterList>
535{
536 using Teuchos::ParameterList;
537 using Teuchos::parameterList;
538 using Teuchos::RCP;
539 using Teuchos::rcp;
540 using Teuchos::rcpFromRef;
541
542 // Set all the valid parameters and their default values.
543 if (validParams_.is_null ()) {
544 // We use Teuchos::as just in case MagnitudeType doesn't have a
545 // constructor that takes an int. Otherwise, we could just write
546 // "MagnitudeType(10)".
547 const MagnitudeType ten = Teuchos::as<MagnitudeType> (10);
548 const MagnitudeType sqrtEps = STM::squareroot (STM::eps());
549
550 const MagnitudeType lambda = STM::zero();
552 const MagnitudeType relRhsErr = ten * sqrtEps;
553 const MagnitudeType relMatErr = ten * sqrtEps;
554 const MagnitudeType condMax = STM::one() / STM::eps();
555 const int maxIters = 1000;
556 const int termIterMax = 1;
557 const int verbosity = Belos::Errors;
558 const int outputStyle = Belos::General;
559 const int outputFreq = -1;
560 const std::string label ("Belos");
561
562 RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
563 pl->set ("Output Stream", outputStream, "Teuchos::RCP<std::ostream> "
564 "(reference-counted pointer to the output stream) receiving "
565 "all solver output");
566 pl->set ("Lambda", lambda, "Damping parameter");
567 pl->set ("Rel RHS Err", relRhsErr, "Estimates the error in the data "
568 "defining the right-hand side");
569 pl->set ("Rel Mat Err", relMatErr, "Estimates the error in the data "
570 "defining the matrix.");
571 pl->set ("Condition Limit", condMax, "Bounds the estimated condition "
572 "number of Abar.");
573 pl->set ("Maximum Iterations", maxIters, "Maximum number of iterations");
574 pl->set ("Term Iter Max", termIterMax, "The number of consecutive "
575 "iterations must that satisfy all convergence criteria in order "
576 "for LSQR to stop iterating");
577 pl->set ("Verbosity", verbosity, "Type(s) of solver information written to "
578 "the output stream");
579 pl->set ("Output Style", outputStyle, "Style of solver output");
580 pl->set ("Output Frequency", outputFreq, "Frequency at which information "
581 "is written to the output stream (-1 means \"not at all\")");
582 pl->set ("Timer Label", label, "String to use as a prefix for the timer "
583 "labels");
584 pl->set ("Block Size", 1, "Block size parameter (currently, LSQR requires "
585 "this must always be 1)");
586 validParams_ = pl;
587 }
588 return validParams_;
589}
590
591
592template<class ScalarType, class MV, class OP, class DM>
593void
595setParameters (const Teuchos::RCP<Teuchos::ParameterList>& params)
596{
597 using Teuchos::isParameterType;
598 using Teuchos::getParameter;
599 using Teuchos::null;
600 using Teuchos::ParameterList;
601 using Teuchos::parameterList;
602 using Teuchos::RCP;
603 using Teuchos::rcp;
604 using Teuchos::rcp_dynamic_cast;
605 using Teuchos::rcpFromRef;
606 using Teuchos::Time;
607 using Teuchos::TimeMonitor;
608 using Teuchos::Exceptions::InvalidParameter;
609 using Teuchos::Exceptions::InvalidParameterName;
610 using Teuchos::Exceptions::InvalidParameterType;
611
613 (params.is_null (), std::invalid_argument,
614 "Belos::LSQRSolMgr::setParameters: The input ParameterList is null.");
615 RCP<const ParameterList> defaultParams = getValidParameters ();
616
617 // FIXME (mfh 29 Apr 2015) Our users would like to supply one
618 // ParameterList that works for both GMRES and LSQR. Thus, we want
619 // LSQR (the less-used solver) to ignore parameters it doesn't
620 // recognize). For now, therefore, it should not validate, since
621 // validation cannot distinguish between misspellings and
622 // unrecognized parameters. (Perhaps Belos should have a central
623 // facility for all parameters recognized by some solver in Belos,
624 // so we could use that for spell checking.)
625 //
626 //params->validateParameters (*defaultParams);
627
628 // mfh 29 Apr 2015: The convention in Belos is that the input
629 // ParameterList is a "delta" from the current state. Thus, we
630 // don't fill in the input ParameterList with defaults, and we only
631 // change the current state if the corresponding parameter was
632 // explicitly set in the input ParameterList. We set up the solver
633 // with the default state on construction.
634
635 // Get the damping (regularization) parameter lambda.
636 if (params->isParameter ("Lambda")) {
637 lambda_ = params->get<MagnitudeType> ("Lambda");
638 } else if (params->isParameter ("lambda")) {
639 lambda_ = params->get<MagnitudeType> ("lambda");
640 }
641
642 // Get the maximum number of iterations.
643 if (params->isParameter ("Maximum Iterations")) {
644 maxIters_ = params->get<int> ("Maximum Iterations");
645 }
647 (maxIters_ < 0, std::invalid_argument, "Belos::LSQRSolMgr::setParameters: "
648 "\"Maximum Iterations\" = " << maxIters_ << " < 0.");
649
650 // (Re)set the timer label.
651 {
652 const std::string newLabel =
653 params->isParameter ("Timer Label") ?
654 params->get<std::string> ("Timer Label") :
655 label_;
656
657 // Update parameter in our list and solver timer
658 if (newLabel != label_) {
659 label_ = newLabel;
660 }
661
662#ifdef BELOS_TEUCHOS_TIME_MONITOR
663 const std::string newSolveLabel = (newLabel != "") ?
664 (newLabel + ": Belos::LSQRSolMgr total solve time") :
665 std::string ("Belos::LSQRSolMgr total solve time");
666 if (timerSolve_.is_null ()) {
667 // Ask TimeMonitor for a new timer.
668 timerSolve_ = TimeMonitor::getNewCounter (newSolveLabel);
669 } else {
670 // We've already created a timer, but we may have changed its
671 // label. If we did change its name, then we have to forget
672 // about the old timer and create a new one with a different
673 // name. This is because Teuchos::Time doesn't give you a way
674 // to change a timer's name, once you've created it. We assume
675 // that if the user changed the timer's label, then the user
676 // wants to reset the timer's results.
677 const std::string oldSolveLabel = timerSolve_->name ();
678
680 // Tell TimeMonitor to forget about the old timer.
681 // TimeMonitor lets you clear timers by name.
682 TimeMonitor::clearCounter (oldSolveLabel);
683 timerSolve_ = TimeMonitor::getNewCounter (newSolveLabel);
684 }
685 }
686#endif // BELOS_TEUCHOS_TIME_MONITOR
687 }
688
689 // Check for a change in verbosity level
690 if (params->isParameter ("Verbosity")) {
691 int newVerbosity = 0;
692 // ParameterList gets confused sometimes about enums. This
693 // ensures that no matter how "Verbosity" was stored -- either an
694 // an int, or as a Belos::MsgType enum, we will be able to extract
695 // it. If it was stored as some other type, we let the exception
696 // through.
697 try {
698 newVerbosity = params->get<Belos::MsgType> ("Verbosity");
699 } catch (Teuchos::Exceptions::InvalidParameterType&) {
700 newVerbosity = params->get<int> ("Verbosity");
701 }
702 if (newVerbosity != verbosity_) {
703 verbosity_ = newVerbosity;
704 }
705 }
706
707 // (Re)set the output style.
708 if (params->isParameter ("Output Style")) {
709 outputStyle_ = params->get<int> ("Output Style");
710 }
711
712 // Get the output stream for the output manager.
713 //
714 // In case the output stream can't be read back in, we default to
715 // stdout (std::cout), just to ensure reasonable behavior.
716 if (params->isParameter ("Output Stream")) {
717 outputStream_ = params->get<RCP<std::ostream> > ("Output Stream");
718 }
719 // We assume that a null output stream indicates that the user
720 // doesn't want to print anything, so we replace it with a "black
721 // hole" stream that prints nothing sent to it. (We can't use a
722 // null output stream, since the output manager always sends
723 // things it wants to print to the output stream.)
724 if (outputStream_.is_null ()) {
725 outputStream_ = rcp (new Teuchos::oblackholestream ());
726 }
727
728 // Get the frequency of solver output. (For example, -1 means
729 // "never," and 1 means "every iteration.")
730 if (params->isParameter ("Output Frequency")) {
731 outputFreq_ = params->get<int> ("Output Frequency");
732 }
733
734 // Create output manager if we need to, using the verbosity level
735 // and output stream that we fetched above. Status tests (i.e.,
736 // stopping criteria) need this.
737 if (printer_.is_null ()) {
738 printer_ = rcp (new OutputManager<ScalarType> (verbosity_, outputStream_));
739 } else {
740 printer_->setVerbosity (verbosity_);
741 printer_->setOStream (outputStream_);
742 }
743
744 // Check for condition number limit, number of consecutive passed
745 // iterations, relative RHS error, and relative matrix error.
746 // Create the LSQR convergence test if necessary.
747 {
748 if (params->isParameter ("Condition Limit")) {
749 condMax_ = params->get<MagnitudeType> ("Condition Limit");
750 }
751 if (params->isParameter ("Term Iter Max")) {
752 termIterMax_ = params->get<int> ("Term Iter Max");
753 }
754 if (params->isParameter ("Rel RHS Err")) {
755 relRhsErr_ = params->get<MagnitudeType> ("Rel RHS Err");
756 }
757 else if (params->isParameter ("Convergence Tolerance")) {
758 // NOTE (mfh 29 Apr 2015) We accept this parameter as an alias
759 // for "Rel RHS Err".
760 relRhsErr_ = params->get<MagnitudeType> ("Convergence Tolerance");
761 }
762
763 if (params->isParameter ("Rel Mat Err")) {
764 relMatErr_ = params->get<MagnitudeType> ("Rel Mat Err");
765 }
766
767 // Create the LSQR convergence test if it doesn't exist yet.
768 // Otherwise, update its parameters.
769 if (convTest_.is_null ()) {
770 convTest_ =
771 rcp (new LSQRStatusTest<ScalarType,MV,OP,DM> (condMax_, termIterMax_,
772 relRhsErr_, relMatErr_));
773 } else {
774 convTest_->setCondLim (condMax_);
775 convTest_->setTermIterMax (termIterMax_);
776 convTest_->setRelRhsErr (relRhsErr_);
777 convTest_->setRelMatErr (relMatErr_);
778 }
779 }
780
781 // Create the status test for maximum number of iterations if
782 // necessary. Otherwise, update it with the new maximum iteration
783 // count.
784 if (maxIterTest_.is_null()) {
785 maxIterTest_ = rcp (new StatusTestMaxIters<ScalarType,MV,OP,DM> (maxIters_));
786 } else {
787 maxIterTest_->setMaxIters (maxIters_);
788 }
789
790 // The stopping criterion is an OR combination of the test for
791 // maximum number of iterations, and the LSQR convergence test.
792 // ("OR combination" means that both tests will always be evaluated,
793 // as opposed to a SEQ combination.)
795 // If sTest_ is not null, then maxIterTest_ and convTest_ were
796 // already constructed on entry to this routine, and sTest_ has
797 // their pointers. Thus, maxIterTest_ and convTest_ have gotten any
798 // parameter changes, so we don't need to do anything to sTest_.
799 if (sTest_.is_null()) {
800 sTest_ = rcp (new combo_type (combo_type::OR, maxIterTest_, convTest_));
801 }
802
803 // Add a debug status test if one was provided (e.g. a wall-clock time
804 // limit). OR-combining it into the top-level test lets it stop the solve;
805 // the dispatch in solve() treats such a stop as an unconverged
806 // (recoverable) termination.
807 if (Teuchos::nonnull(debugStatusTest_)) {
808 sTest_ = rcp (new combo_type (combo_type::OR, sTest_, debugStatusTest_));
809 }
810
811 if (outputTest_.is_null ()) {
812 // Create the status test output class.
813 // This class manages and formats the output from the status test.
815 outputTest_ = stoFactory.create (printer_, sTest_, outputFreq_,
817 // Set the solver string for the output test.
818 const std::string solverDesc = " LSQR ";
819 outputTest_->setSolverDesc (solverDesc);
820 } else {
821 // FIXME (mfh 18 Sep 2011) How do we change the output style of
822 // outputTest_, without destroying and recreating it?
823 outputTest_->setOutputManager (printer_);
824 outputTest_->setChild (sTest_);
825 outputTest_->setOutputFrequency (outputFreq_);
826 // Since outputTest_ can only be created here, I'm assuming that
827 // the fourth constructor argument ("printStates") was set
828 // correctly on constrution; I don't need to reset it (and I can't
829 // set it anyway, given StatusTestOutput's interface).
830 }
831
832 // At this point, params is a valid ParameterList. Now we can
833 // "commit" it to our instance's ParameterList.
834 params_ = params;
835
836 // Inform the solver manager that the current parameters were set.
837 isSet_ = true;
838}
839
840
841template<class ScalarType, class MV, class OP, class DM>
844{
845 using Teuchos::RCP;
846 using Teuchos::rcp;
847
849
850 // Set the current parameters if they were not set before. NOTE:
851 // This may occur if the user generated the solver manager with the
852 // default constructor, but did not set any parameters using
853 // setParameters().
854 if (! isSet_) {
855 this->setParameters (Teuchos::parameterList (* (getValidParameters ())));
856 }
857
859 (problem_.is_null (), LSQRSolMgrLinearProblemFailure,
860 "Belos::LSQRSolMgr::solve: The linear problem to solve is null.");
862 (! problem_->isProblemSet (), LSQRSolMgrLinearProblemFailure,
863 "Belos::LSQRSolMgr::solve: The linear problem is not ready, "
864 "as its setProblem() method has not been called.");
866 (MVT::GetNumberVecs (*(problem_->getRHS ())) != 1,
867 LSQRSolMgrBlockSizeFailure, "Belos::LSQRSolMgr::solve: "
868 "The current implementation of LSQR only knows how to solve problems "
869 "with one right-hand side, but the linear problem to solve has "
870 << MVT::GetNumberVecs (* (problem_->getRHS ()))
871 << " right-hand sides.");
872
873 // We've validated the LinearProblem instance above. If any of the
874 // StatusTests needed to be initialized using information from the
875 // LinearProblem, now would be the time to do so. (This is true of
876 // GMRES, where the residual convergence test(s) to instantiate
877 // depend on knowing whether there is a left preconditioner. This
878 // is why GMRES has an "isSTSet_" Boolean member datum, which tells
879 // you whether the status tests have been instantiated and are ready
880 // for use.
881
882 // test isFlexible might go here.
883
884 // Next the right-hand sides to solve are identified. Among other things,
885 // this enables getCurrLHSVec() to get the current initial guess vector,
886 // and getCurrRHSVec() to get the current right-hand side (in Iter).
887 std::vector<int> currRHSIdx (1, 0);
888 problem_->setLSIndex (currRHSIdx);
889
890 // Reset the status test.
891 outputTest_->reset ();
892
893 // Don't assume convergence unless we've verified that the
894 // convergence test passed.
895 bool isConverged = false;
896
897 // FIXME: Currently we are setting the initial guess to zero, since
898 // the solver doesn't yet know how to handle a nonzero initial
899 // guess. This could be fixed by rewriting the solver to work with
900 // the residual and a delta.
901 //
902 // In a least squares problem with a nonzero initial guess, the
903 // minimzation problem involves the distance (in a norm depending on
904 // the preconditioner) between the solution and the the initial
905 // guess.
906
908 // Solve the linear problem using LSQR
910
911 // Parameter list for the LSQR iteration.
912 Teuchos::ParameterList plist;
913
914 // Use the solver manager's "Lambda" parameter to set the
915 // iteration's "Lambda" parameter. We know that the solver
916 // manager's parameter list (params_) does indeed contain the
917 // "Lambda" parameter, because solve() always ensures that
918 // setParameters() has been called.
919 plist.set ("Lambda", lambda_);
920
923 rcp (new iter_type (problem_, printer_, outputTest_, plist));
924#ifdef BELOS_TEUCHOS_TIME_MONITOR
925 Teuchos::TimeMonitor slvtimer (*timerSolve_);
926#endif
927
928 // Reset the number of iterations.
929 lsqr_iter->resetNumIters ();
930 // Reset the number of calls that the status test output knows about.
931 outputTest_->resetNumCalls ();
932 // Set the new state and initialize the solver.
934 lsqr_iter->initializeLSQR (newstate);
935 // tell lsqr_iter to iterate
936 try {
937 lsqr_iter->iterate ();
938
939 // First check for convergence. If we didn't converge, then check
940 // whether we reached the maximum number of iterations. If
941 // neither of those happened, there must have been a bug.
942 if (convTest_->getStatus () == Belos::Passed) {
943 isConverged = true;
944 } else if (maxIterTest_->getStatus () == Belos::Passed) {
946 isConverged = false;
947 } else if (Teuchos::nonnull(debugStatusTest_) &&
948 debugStatusTest_->getStatus() == Belos::Passed) {
949 // A debug status test (e.g. a wall-clock time limit) stopped the
950 // iteration. Treat as an unconverged termination rather than an
951 // inconsistent state.
953 isConverged = false;
954 } else {
957 std::logic_error, "Belos::LSQRSolMgr::solve: "
958 "LSQRIteration::iterate returned without either the convergence test "
959 "or the maximum iteration count test passing. "
960 "Please report this bug to the Belos developers.");
961 }
962 } catch (const std::exception& e) {
964 printer_->stream(Belos::Errors)
965 << "Error! Caught std::exception in LSQRIter::iterate at iteration "
966 << lsqr_iter->getNumIters () << std::endl << e.what () << std::endl;
967 throw;
968 }
969
970 // identify current linear system as solved LinearProblem
971 problem_->setCurrLS();
972 // print final summary
973 sTest_->print (printer_->stream (Belos::FinalSummary));
974
975 // Print timing information, if the corresponding compile-time and
976 // run-time options are enabled.
977#ifdef BELOS_TEUCHOS_TIME_MONITOR
978 // Calling summarize() can be expensive, so don't call unless the
979 // user wants to print out timing details. summarize() will do all
980 // the work even if it's passed a "black hole" output stream.
981 if (verbosity_ & TimingDetails)
982 Teuchos::TimeMonitor::summarize (printer_->stream (Belos::TimingDetails));
983#endif // BELOS_TEUCHOS_TIME_MONITOR
984
985 // A posteriori solve information
986 numIters_ = maxIterTest_->getNumIters();
987 matCondNum_ = convTest_->getMatCondNum();
988 matNorm_ = convTest_->getMatNorm();
989 resNorm_ = convTest_->getResidNorm();
990 matResNorm_ = convTest_->getLSResidNorm();
991
992 if (!isConverged) {
993 return retType;
994 } else {
995 return Belos::Converged;
996 }
997}
998
999// LSQRSolMgr requires the solver manager to return an eponymous std::string.
1000template<class ScalarType, class MV, class OP, class DM>
1002{
1003 std::ostringstream oss;
1004 oss << "LSQRSolMgr<...," << STS::name () << ">";
1005 oss << "{";
1006 oss << "Lambda: " << lambda_;
1007 oss << ", condition number limit: " << condMax_;
1008 oss << ", relative RHS Error: " << relRhsErr_;
1009 oss << ", relative Matrix Error: " << relMatErr_;
1010 oss << ", maximum number of iterations: " << maxIters_;
1011 oss << ", termIterMax: " << termIterMax_;
1012 oss << "}";
1013 return oss.str ();
1014}
1015
1016} // end Belos namespace
1017
1018#ifdef HAVE_BELOS_TPETRA
1020
1021#define BELOS_TPETRA_LSQRSOLMGR_NOEXTERN_CALL(SC, LO, GO, NT) \
1022 BELOS_TPETRA_CALL(Belos::LSQRSolMgr, SC, LO, GO, NT)
1023
1024#define BELOS_TPETRA_LSQRSOLMGR_EXTERN_CALL(SC, LO, GO, NT) \
1025 BELOS_TPETRA_EXTERN_CALL(Belos::LSQRSolMgr, SC, LO, GO, NT)
1026
1027TPETRA_INSTANTIATE_SLGN_NO_ORDINAL_SCALAR(BELOS_TPETRA_LSQRSOLMGR_EXTERN_CALL)
1028#endif
1029
1030
1031#endif /* BELOS_LSQR_SOLMGR_HPP */
Belos header file which uses auto-configuration information to include necessary C++ headers.
Full specialization of Belos::DenseMatTraits for Kokkos::DualView with arbitrary scalarType....
Belos concrete class that iterates LSQR.
IterationState contains the data that defines the state of the LSQR solver at any given time.
Belos::StatusTest class defining LSQR convergence.
Class which describes the linear problem to be solved by the iterative solver.
Class which manages the output and verbosity of the Belos solvers.
Pure virtual base class which describes the basic interface for a solver manager.
Belos::StatusTest for logically combining several status tests.
Belos::StatusTest class for specifying a maximum number of iterations.
A factory class for generating StatusTestOutput objects.
Full specialization of Belos::DenseMatTraits for Teuchos::SerialDenseMatrix with ordinal type int and...
Collection of types and exceptions used within the Belos solvers.
Parent class to all Belos exceptions.
Base class for Belos::SolverManager subclasses which normally can only compile for real ScalarType.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
bool isLOADetected() const override
Whether a loss of accuracy was detected during the last solve.
MagnitudeType getMatNorm() const
Estimated matrix Frobenius norm from the last solve.
virtual ~LSQRSolMgr()
Destructor (declared virtual for memory safety of base classes).
int getNumIters() const override
Iteration count from the last solve.
MagnitudeType getMatResNorm() const
Estimate of (residual vector ) from the last solve.
MagnitudeType getResNorm() const
Estimated residual norm from the last solve.
MagnitudeType getMatCondNum() const
Estimated matrix condition number from the last solve.
Teuchos::RCP< SolverManager< ScalarType, MV, OP, DM > > clone() const override
clone for Inverted Injection (DII)
void setDebugStatusTest(const Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > &debugStatusTest) override
Set a debug status test, OR-combined into the top-level status test.
void reset(const ResetType type) override
reset the solver manager as specified by the ResetType, informs the solver manager that the solver sh...
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem) override
Set the linear problem that needs to be solved.
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const override
Get current linear problem being solved for in this object.
LSQRSolMgrBlockSizeFailure is thrown when the linear problem has more than one RHS.
LSQRSolMgrBlockSizeFailure(const std::string &what_arg)
LSQR method (for linear systems and linear least-squares problems).
LSQRSolMgr(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem, const Teuchos::RCP< Teuchos::ParameterList > &pl)
Teuchos::RCP< SolverManager< ScalarType, MV, OP, DM > > clone() const override
clone for Inverted Injection (DII)
Belos::LSQRSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
LSQRSolMgrLinearProblemFailure(const std::string &what_arg)
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).
MsgType
Available message types recognized by the linear solvers.
@ FinalSummary
@ TimingDetails
ReturnType
Whether the Belos solve converged for all linear systems.
@ Unconverged
@ MaxItersReached
@ NonspecificException
@ InconsistentState
@ Undetermined
ResetType
How to reset the solver.

Generated for Belos by doxygen 1.9.8