70 #ifndef IFPACK2_DETAILS_CHEBYSHEV_DEF_HPP 71 #define IFPACK2_DETAILS_CHEBYSHEV_DEF_HPP 95 const char computeBeforeApplyReminder[] =
96 "This means one of the following:\n" 97 " - you have not yet called compute() on this instance, or \n" 98 " - you didn't call compute() after calling setParameters().\n\n" 99 "After creating an Ifpack2::Chebyshev instance,\n" 100 "you must _always_ call compute() at least once before calling apply().\n" 101 "After calling compute() once, you do not need to call it again,\n" 102 "unless the matrix has changed or you have changed parameters\n" 103 "(by calling setParameters()).";
106 template<
class ScalarType,
class MV,
class MAT>
108 Chebyshev<ScalarType, MV, MAT>::
109 checkConstructorInput ()
const 111 TEUCHOS_TEST_FOR_EXCEPTION(STS::isComplex, std::logic_error,
112 "Ifpack2::Details::Chebyshev: This class' implementation of Chebyshev " 113 "iteration only works for real-valued, symmetric positive definite " 114 "matrices. However, you instantiated this class for ScalarType=" 115 << Teuchos::TypeNameTraits<ScalarType>::name () <<
", which is a complex-" 116 "valued type. While this may be algorithmically correct if all of the " 117 "complex numbers in the matrix have zero imaginary part, we forbid using " 118 "complex ScalarType altogether in order to remind you of the limitations " 119 "of our implementation (and of the algorithm itself).");
120 TEUCHOS_TEST_FOR_EXCEPTION(A_.is_null (), std::invalid_argument,
121 "Ifpack2::Chebyshev: Input matrix to constructor is null.");
122 TEUCHOS_TEST_FOR_EXCEPTION(
123 A_->getGlobalNumRows() != A_->getGlobalNumCols(),
124 std::invalid_argument,
125 "Ifpack2::Chebyshev: The input matrix A must be square. " 126 "A has " << A_->getGlobalNumRows() <<
" rows and " 127 << A_->getGlobalNumCols() <<
" columns.");
131 #ifdef HAVE_TEUCHOS_DEBUG 132 Teuchos::RCP<const map_type> domainMap = A_->getDomainMap ();
133 Teuchos::RCP<const map_type> rangeMap = A_->getRangeMap ();
138 TEUCHOS_TEST_FOR_EXCEPTION(
139 ! domainMap->isSameAs (*rangeMap), std::invalid_argument,
140 "Ifpack2::Chebyshev: The domain Map and range Map of the matrix must be " 141 "the same (in the sense of isSameAs())." << std::endl <<
"We only check " 142 "for this if Trilinos was built with the CMake configuration option " 143 "Teuchos_ENABLE_DEBUG set to ON.");
144 #endif // HAVE_TEUCHOS_DEBUG 147 template<
class ScalarType,
class MV,
class MAT>
151 savedDiagOffsets_ (false),
152 computedLambdaMax_ (STS::nan ()),
153 computedLambdaMin_ (STS::nan ()),
154 lambdaMaxForApply_ (STS::nan ()),
155 lambdaMinForApply_ (STS::nan ()),
156 eigRatioForApply_ (STS::nan ()),
157 userLambdaMax_ (STS::nan ()),
158 userLambdaMin_ (STS::nan ()),
159 userEigRatio_ (
Teuchos::as<ST> (30)),
160 minDiagVal_ (STS::eps ()),
163 zeroStartingSolution_ (true),
164 assumeMatrixUnchanged_ (false),
165 textbookAlgorithm_ (false),
166 computeMaxResNorm_ (false)
168 checkConstructorInput ();
171 template<
class ScalarType,
class MV,
class MAT>
173 Chebyshev (Teuchos::RCP<const MAT> A, Teuchos::ParameterList& params) :
175 savedDiagOffsets_ (false),
176 computedLambdaMax_ (STS::nan ()),
177 computedLambdaMin_ (STS::nan ()),
178 lambdaMaxForApply_ (STS::nan ()),
179 lambdaMinForApply_ (STS::nan ()),
180 eigRatioForApply_ (STS::nan ()),
181 userLambdaMax_ (STS::nan ()),
182 userLambdaMin_ (STS::nan ()),
183 userEigRatio_ (
Teuchos::as<ST> (30)),
184 minDiagVal_ (STS::eps ()),
187 zeroStartingSolution_ (true),
188 assumeMatrixUnchanged_ (false),
189 textbookAlgorithm_ (false),
190 computeMaxResNorm_ (false)
192 checkConstructorInput ();
196 template<
class ScalarType,
class MV,
class MAT>
202 using Teuchos::rcp_const_cast;
203 using Teuchos::rcpFromRef;
215 const ST defaultLambdaMax = STS::nan ();
216 const ST defaultLambdaMin = STS::nan ();
223 const ST defaultEigRatio = Teuchos::as<ST> (30);
224 const ST defaultMinDiagVal = STS::eps ();
225 const int defaultNumIters = 1;
226 const int defaultEigMaxIters = 10;
227 const bool defaultZeroStartingSolution =
true;
228 const bool defaultAssumeMatrixUnchanged =
false;
229 const bool defaultTextbookAlgorithm =
false;
230 const bool defaultComputeMaxResNorm =
false;
236 RCP<const V> userInvDiag;
237 ST lambdaMax = defaultLambdaMax;
238 ST lambdaMin = defaultLambdaMin;
239 ST eigRatio = defaultEigRatio;
240 ST minDiagVal = defaultMinDiagVal;
241 int numIters = defaultNumIters;
242 int eigMaxIters = defaultEigMaxIters;
243 bool zeroStartingSolution = defaultZeroStartingSolution;
244 bool assumeMatrixUnchanged = defaultAssumeMatrixUnchanged;
245 bool textbookAlgorithm = defaultTextbookAlgorithm;
246 bool computeMaxResNorm = defaultComputeMaxResNorm;
259 if (plist.isParameter (
"chebyshev: operator inv diagonal")) {
261 const V* rawUserInvDiag = plist.get<
const V*> (
"chebyshev: operator inv diagonal");
263 userInvDiag = rcp (rawUserInvDiag,
false);
264 }
catch (Teuchos::Exceptions::InvalidParameterType&) {
266 if (userInvDiag.is_null ()) {
268 V* rawUserInvDiag = plist.get<V*> (
"chebyshev: operator inv diagonal");
270 userInvDiag = rcp (const_cast<const V*> (rawUserInvDiag),
false);
271 }
catch (Teuchos::Exceptions::InvalidParameterType&) {
274 if (userInvDiag.is_null ()) {
276 userInvDiag = plist.get<RCP<const V> > (
"chebyshev: operator inv diagonal");
277 }
catch (Teuchos::Exceptions::InvalidParameterType&) {
280 if (userInvDiag.is_null ()) {
282 RCP<V> userInvDiagNonconst = plist.get<RCP<V> > (
"chebyshev: operator inv diagonal");
283 userInvDiag = rcp_const_cast<
const V> (userInvDiagNonconst);
284 }
catch (Teuchos::Exceptions::InvalidParameterType&) {
287 if (userInvDiag.is_null ()) {
290 userInvDiag = rcp (
new V (plist.get<
const V> (
"chebyshev: operator inv diagonal")));
291 }
catch (Teuchos::Exceptions::InvalidParameterType&) {
294 if (userInvDiag.is_null ()) {
296 V userInvDiagNonconst = plist.get<V> (
"chebyshev: operator inv diagonal");
297 userInvDiag = rcp (
new V (userInvDiagNonconst));
298 }
catch (Teuchos::Exceptions::InvalidParameterType&) {
303 if (! userInvDiag.is_null ()) {
304 userInvDiag = makeRangeMapVector (*userInvDiag);
311 if (plist.isParameter (
"chebyshev: max eigenvalue")) {
312 lambdaMax = plist.get<ST> (
"chebyshev: max eigenvalue");
313 TEUCHOS_TEST_FOR_EXCEPTION(
314 STS::isnaninf (lambdaMax), std::invalid_argument,
315 "Ifpack2::Chebyshev::setParameters: \"chebyshev: max eigenvalue\" " 316 "parameter is NaN or Inf. This parameter is optional, but if you " 317 "choose to supply it, it must have a finite value.");
319 if (plist.isParameter (
"chebyshev: min eigenvalue")) {
320 lambdaMin = plist.get<ST> (
"chebyshev: min eigenvalue");
321 TEUCHOS_TEST_FOR_EXCEPTION(
322 STS::isnaninf (lambdaMin), std::invalid_argument,
323 "Ifpack2::Chebyshev::setParameters: \"chebyshev: min eigenvalue\" " 324 "parameter is NaN or Inf. This parameter is optional, but if you " 325 "choose to supply it, it must have a finite value.");
329 if (plist.isParameter (
"smoother: Chebyshev alpha")) {
330 eigRatio = plist.get<ST> (
"smoother: Chebyshev alpha");
333 eigRatio = plist.get (
"chebyshev: ratio eigenvalue", eigRatio);
334 TEUCHOS_TEST_FOR_EXCEPTION(
335 STS::isnaninf (eigRatio), std::invalid_argument,
336 "Ifpack2::Chebyshev::setParameters: \"chebyshev: ratio eigenvalue\" " 337 "parameter (also called \"smoother: Chebyshev alpha\") is NaN or Inf. " 338 "This parameter is optional, but if you choose to supply it, it must have " 345 TEUCHOS_TEST_FOR_EXCEPTION(
346 STS::real (eigRatio) < STS::real (STS::one ()),
347 std::invalid_argument,
348 "Ifpack2::Chebyshev::setParameters: \"chebyshev: ratio eigenvalue\"" 349 "parameter (also called \"smoother: Chebyshev alpha\") must be >= 1, " 350 "but you supplied the value " << eigRatio <<
".");
353 minDiagVal = plist.get (
"chebyshev: min diagonal value", minDiagVal);
354 TEUCHOS_TEST_FOR_EXCEPTION(
355 STS::isnaninf (minDiagVal), std::invalid_argument,
356 "Ifpack2::Chebyshev::setParameters: \"chebyshev: min diagonal value\" " 357 "parameter is NaN or Inf. This parameter is optional, but if you choose " 358 "to supply it, it must have a finite value.");
361 if (plist.isParameter (
"smoother: sweeps")) {
362 numIters = plist.get<
int> (
"smoother: sweeps");
364 if (plist.isParameter (
"relaxation: sweeps")) {
365 numIters = plist.get<
int> (
"relaxation: sweeps");
367 numIters = plist.get (
"chebyshev: degree", numIters);
368 TEUCHOS_TEST_FOR_EXCEPTION(
369 numIters < 0, std::invalid_argument,
370 "Ifpack2::Chebyshev::setParameters: \"chebyshev: degree\" parameter (also " 371 "called \"smoother: sweeps\" or \"relaxation: sweeps\") must be a " 372 "nonnegative integer. You gave a value of " << numIters <<
".");
375 if (plist.isParameter (
"eigen-analysis: iterations")) {
376 eigMaxIters = plist.get<
int> (
"eigen-analysis: iterations");
378 eigMaxIters = plist.get (
"chebyshev: eigenvalue max iterations", eigMaxIters);
379 TEUCHOS_TEST_FOR_EXCEPTION(
380 eigMaxIters < 0, std::invalid_argument,
381 "Ifpack2::Chebyshev::setParameters: \"chebyshev: eigenvalue max iterations" 382 "\" parameter (also called \"eigen-analysis: iterations\") must be a " 383 "nonnegative integer. You gave a value of " << eigMaxIters <<
".");
385 zeroStartingSolution = plist.get (
"chebyshev: zero starting solution",
386 zeroStartingSolution);
387 assumeMatrixUnchanged = plist.get (
"chebyshev: assume matrix does not change",
388 assumeMatrixUnchanged);
392 if (plist.isParameter (
"chebyshev: textbook algorithm")) {
393 textbookAlgorithm = plist.get<
bool> (
"chebyshev: textbook algorithm");
395 if (plist.isParameter (
"chebyshev: compute max residual norm")) {
396 computeMaxResNorm = plist.get<
bool> (
"chebyshev: compute max residual norm");
402 TEUCHOS_TEST_FOR_EXCEPTION(
403 plist.isParameter (
"chebyshev: use block mode") &&
404 ! plist.get<
bool> (
"chebyshev: use block mode"),
405 std::invalid_argument,
406 "Ifpack2::Chebyshev requires that if you supply the Ifpack parameter " 407 "\"chebyshev: use block mode\", it must be set to false. Ifpack2's " 408 "Chebyshev does not implement Ifpack's block mode.");
409 TEUCHOS_TEST_FOR_EXCEPTION(
410 plist.isParameter (
"chebyshev: solve normal equations") &&
411 ! plist.get<
bool> (
"chebyshev: solve normal equations"),
412 std::invalid_argument,
413 "Ifpack2::Chebyshev does not and will never implement the Ifpack " 414 "parameter \"chebyshev: solve normal equations\". If you want to solve " 415 "the normal equations, construct a Tpetra::Operator that implements " 416 "A^* A, and use Chebyshev to solve A^* A x = A^* b.");
424 std::string eigenAnalysisType (
"power-method");
425 if (plist.isParameter (
"eigen-analysis: type")) {
426 eigenAnalysisType = plist.get<std::string> (
"eigen-analysis: type");
427 TEUCHOS_TEST_FOR_EXCEPTION(
428 eigenAnalysisType !=
"power-method" &&
429 eigenAnalysisType !=
"power method",
430 std::invalid_argument,
431 "Ifpack2::Chebyshev: This class supports the ML parameter \"eigen-" 432 "analysis: type\" for backwards compatibility. However, Ifpack2 " 433 "currently only supports the \"power-method\" option for this " 434 "parameter. This imitates Ifpack, which only implements the power " 435 "method for eigenanalysis.");
439 userInvDiag_ = userInvDiag;
440 userLambdaMax_ = lambdaMax;
441 userLambdaMin_ = lambdaMin;
442 userEigRatio_ = eigRatio;
443 minDiagVal_ = minDiagVal;
444 numIters_ = numIters;
445 eigMaxIters_ = eigMaxIters;
446 zeroStartingSolution_ = zeroStartingSolution;
447 assumeMatrixUnchanged_ = assumeMatrixUnchanged;
448 textbookAlgorithm_ = textbookAlgorithm;
449 computeMaxResNorm_ = computeMaxResNorm;
452 template<
class ScalarType,
class MV,
class MAT>
462 typedef Tpetra::CrsMatrix<
typename MV::scalar_type,
463 typename MV::local_ordinal_type,
464 typename MV::global_ordinal_type,
465 typename MV::node_type> crs_matrix_type;
479 if (userInvDiag_.is_null ()) {
480 Teuchos::RCP<const crs_matrix_type> A_crsMat =
481 Teuchos::rcp_dynamic_cast<
const crs_matrix_type> (A_);
484 if (! A_crsMat.is_null () && A_crsMat->isStaticGraph ()) {
486 A_crsMat->getLocalDiagOffsets (diagOffsets_);
487 savedDiagOffsets_ =
true;
488 D_ = makeInverseDiagonal (*A_,
true);
491 D_ = makeInverseDiagonal (*A_);
494 else if (! assumeMatrixUnchanged_) {
495 if (! A_crsMat.is_null () && A_crsMat->isStaticGraph ()) {
498 if (! savedDiagOffsets_) {
499 A_crsMat->getLocalDiagOffsets (diagOffsets_);
500 savedDiagOffsets_ =
true;
503 D_ = makeInverseDiagonal (*A_,
true);
506 D_ = makeInverseDiagonal (*A_);
515 const bool computedEigenvalueEstimates =
516 STS::isnaninf (computedLambdaMax_) || STS::isnaninf (computedLambdaMin_);
525 if (! assumeMatrixUnchanged_ ||
526 (! computedEigenvalueEstimates && STS::isnaninf (userLambdaMax_))) {
527 const ST computedLambdaMax = powerMethod (*A_, *D_, eigMaxIters_);
528 TEUCHOS_TEST_FOR_EXCEPTION(
529 STS::isnaninf (computedLambdaMax),
531 "Ifpack2::Chebyshev::compute: Estimation of the max eigenvalue " 532 "of D^{-1} A failed, by producing Inf or NaN. This probably means that " 533 "the matrix contains Inf or NaN values, or that it is badly scaled.");
534 TEUCHOS_TEST_FOR_EXCEPTION(
535 STS::isnaninf (userEigRatio_),
537 "Ifpack2::Chebyshev::compute: userEigRatio_ is Inf or NaN." 538 << endl <<
"This should be impossible." << endl <<
539 "Please report this bug to the Ifpack2 developers.");
545 const ST computedLambdaMin = computedLambdaMax / userEigRatio_;
548 computedLambdaMax_ = computedLambdaMax;
549 computedLambdaMin_ = computedLambdaMin;
551 TEUCHOS_TEST_FOR_EXCEPTION(
552 STS::isnaninf (userLambdaMax_) && STS::isnaninf (computedLambdaMax_),
554 "Ifpack2::Chebyshev::compute: " << endl <<
555 "Both userLambdaMax_ and computedLambdaMax_ are Inf or NaN." 556 << endl <<
"This should be impossible." << endl <<
557 "Please report this bug to the Ifpack2 developers.");
565 if (STS::isnaninf (userLambdaMax_)) {
566 lambdaMaxForApply_ = computedLambdaMax_;
568 lambdaMaxForApply_ = userLambdaMax_;
581 lambdaMinForApply_ = lambdaMaxForApply_ / userEigRatio_;
582 eigRatioForApply_ = userEigRatio_;
584 if (! textbookAlgorithm_) {
587 const ST one = Teuchos::as<ST> (1);
588 if (STS::magnitude (lambdaMaxForApply_ - one) < Teuchos::as<MT> (1.0e-6)) {
589 lambdaMinForApply_ = one;
590 lambdaMaxForApply_ = lambdaMinForApply_;
591 eigRatioForApply_ = one;
596 template<
class ScalarType,
class MV,
class MAT>
600 return lambdaMaxForApply_;
603 template<
class ScalarType,
class MV,
class MAT>
604 typename Chebyshev<ScalarType, MV, MAT>::MT
606 apply (
const MV& B, MV& X) {
607 TEUCHOS_TEST_FOR_EXCEPTION(
608 STS::isnaninf (lambdaMaxForApply_), std::runtime_error,
609 "Ifpack2::Chebyshev::apply: There is no estimate for the max eigenvalue." 610 << std::endl << computeBeforeApplyReminder);
611 TEUCHOS_TEST_FOR_EXCEPTION(
612 STS::isnaninf (lambdaMinForApply_), std::runtime_error,
613 "Ifpack2::Chebyshev::apply: There is no estimate for the min eigenvalue." 614 << std::endl << computeBeforeApplyReminder);
615 TEUCHOS_TEST_FOR_EXCEPTION(
616 STS::isnaninf (eigRatioForApply_), std::runtime_error,
617 "Ifpack2::Chebyshev::apply: There is no estimate for the ratio of the max " 618 "eigenvalue to the min eigenvalue." 619 << std::endl << computeBeforeApplyReminder);
620 TEUCHOS_TEST_FOR_EXCEPTION(
621 D_.is_null (), std::runtime_error,
622 "Ifpack2::Chebyshev::apply: " 623 "The vector of inverse diagonal entries of the matrix has not yet been " 624 "computed." << std::endl << computeBeforeApplyReminder);
626 if (textbookAlgorithm_) {
627 textbookApplyImpl (*A_, B, X, numIters_, lambdaMaxForApply_,
628 lambdaMinForApply_, eigRatioForApply_, *D_);
630 ifpackApplyImpl (*A_, B, X, numIters_, lambdaMaxForApply_,
631 lambdaMinForApply_, eigRatioForApply_, *D_);
634 if (computeMaxResNorm_ && B.getNumVectors () > 0) {
635 MV R (B.getMap (), B.getNumVectors ());
636 computeResidual (R, B, *A_, X);
637 Teuchos::Array<MT> norms (B.getNumVectors ());
639 return *std::max_element (norms.begin (), norms.end ());
641 return Teuchos::ScalarTraits<MT>::zero ();
645 template<
class ScalarType,
class MV,
class MAT>
649 this->
describe (* (Teuchos::getFancyOStream (Teuchos::rcpFromRef (out))),
650 Teuchos::VERB_MEDIUM);
653 template<
class ScalarType,
class MV,
class MAT>
657 const Teuchos::ETransp mode)
660 A.
apply (X, R, mode, -STS::one(), STS::one());
663 template<
class ScalarType,
class MV,
class MAT>
666 solve (MV& Z,
const V& D_inv,
const MV& R) {
667 Z.elementWiseMultiply (STS::one(), D_inv, R, STS::zero());
670 template<
class ScalarType,
class MV,
class MAT>
673 solve (MV& Z,
const ST alpha,
const V& D_inv,
const MV& R) {
674 Z.elementWiseMultiply (alpha, D_inv, R, STS::zero());
677 template<
class ScalarType,
class MV,
class MAT>
678 Teuchos::RCP<typename Chebyshev<ScalarType, MV, MAT>::V>
682 using Teuchos::rcpFromRef;
683 using Teuchos::rcp_dynamic_cast;
685 RCP<V> D_rowMap (
new V (A.getGraph ()->getRowMap ()));
686 if (useDiagOffsets) {
690 typedef Tpetra::CrsMatrix<
typename MV::scalar_type,
691 typename MV::local_ordinal_type,
692 typename MV::global_ordinal_type,
693 typename MV::node_type> crs_matrix_type;
694 RCP<const crs_matrix_type> A_crsMat =
695 rcp_dynamic_cast<
const crs_matrix_type> (rcpFromRef (A));
696 if (! A_crsMat.is_null ()) {
697 TEUCHOS_TEST_FOR_EXCEPTION(
698 ! savedDiagOffsets_, std::logic_error,
699 "Ifpack2::Details::Chebyshev::makeInverseDiagonal: " 700 "It is not allowed to call this method with useDiagOffsets=true, " 701 "if you have not previously saved offsets of diagonal entries. " 702 "This situation should never arise if this class is used properly. " 703 "Please report this bug to the Ifpack2 developers.");
704 A_crsMat->getLocalDiagCopy (*D_rowMap, diagOffsets_ ());
708 A.getLocalDiagCopy (*D_rowMap);
710 RCP<V> D_rangeMap = makeRangeMapVector (*D_rowMap);
714 typedef Kokkos::MultiVector<ST, typename MAT::node_type> KMV;
715 KMV& localDiag = D_rangeMap->getLocalMVNonConst ();
716 typedef Kokkos::DefaultArithmetic<KMV> KMVT;
717 KMVT::ReciprocalThreshold (localDiag, minDiagVal_);
721 template<
class ScalarType,
class MV,
class MAT>
722 Teuchos::RCP<typename Chebyshev<ScalarType, MV, MAT>::V>
727 typedef Tpetra::Export<
typename MV::local_ordinal_type,
728 typename MV::global_ordinal_type,
729 typename MV::node_type> export_type;
730 RCP<const map_type> sourceMap = D.getMap ();
731 RCP<const map_type> rangeMap = A_->getRangeMap ();
732 RCP<const map_type> rowMap = A_->getRowMap ();
735 if (rangeMap.getRawPtr () == sourceMap.getRawPtr ()) {
741 D_out = rcp (
new V (D));
744 RCP<const export_type> exporter;
748 if (sourceMap.getRawPtr () == rowMap.getRawPtr () ||
749 sourceMap->isSameAs (*rowMap)) {
751 exporter = A_->getGraph ()->getExporter ();
754 exporter = rcp (
new export_type (sourceMap, rangeMap));
757 D_out = rcp (
new V (D));
758 if (exporter.is_null ()) {
763 D_out->doExport (D, *exporter, Tpetra::ADD);
771 template<
class ScalarType,
class MV,
class MAT>
781 const V& D_inv)
const 784 const ST myLambdaMin = lambdaMax / eigRatio;
786 const ST zero = Teuchos::as<ST> (0);
787 const ST one = Teuchos::as<ST> (1);
788 const ST two = Teuchos::as<ST> (2);
789 const ST d = (lambdaMax + myLambdaMin) / two;
790 const ST c = (lambdaMax - myLambdaMin) / two;
792 if (zeroStartingSolution_ && numIters > 0) {
796 MV R (B.getMap (), B.getNumVectors (),
false);
797 MV P (B.getMap (), B.getNumVectors (),
false);
798 MV Z (B.getMap (), B.getNumVectors (),
false);
800 for (
int i = 0; i < numIters; ++i) {
801 computeResidual (R, B, A, X);
810 beta = alpha * (c/two) * (c/two);
811 alpha = one / (d - beta);
812 P.update (one, Z, beta);
814 X.update (alpha, P, one);
821 template<
class ScalarType,
class MV,
class MAT>
822 typename Chebyshev<ScalarType, MV, MAT>::MT
824 std::vector<MT> norms (X.getNumVectors ());
826 return *std::max_element (norms.begin (), norms.end ());
829 template<
class ScalarType,
class MV,
class MAT>
841 #ifdef HAVE_TEUCHOS_DEBUG 842 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG 845 cerr <<
"\\|B\\|_{\\infty} = " << maxNormInf (B) << endl;
846 cerr <<
"\\|X\\|_{\\infty} = " << maxNormInf (X) << endl;
847 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG 848 #endif // HAVE_TEUCHOS_DEBUG 853 const ST one = Teuchos::as<ST> (1);
854 const ST two = Teuchos::as<ST> (2);
857 if (lambdaMin == one && lambdaMax == lambdaMin) {
863 const ST alpha = lambdaMax / eigRatio;
864 const ST beta = Teuchos::as<ST> (1.1) * lambdaMax;
865 const ST delta = two / (beta - alpha);
866 const ST theta = (beta + alpha) / two;
867 const ST s1 = theta * delta;
869 #ifdef HAVE_TEUCHOS_DEBUG 870 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG 871 cerr <<
"alpha = " << alpha << endl
872 <<
"beta = " << beta << endl
873 <<
"delta = " << delta << endl
874 <<
"theta = " << theta << endl
875 <<
"s1 = " << s1 << endl;
876 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG 877 #endif // HAVE_TEUCHOS_DEBUG 880 Teuchos::RCP<MV> V_ptr, W_ptr;
881 makeTempMultiVectors (V_ptr, W_ptr, B);
890 #ifdef HAVE_TEUCHOS_DEBUG 891 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG 892 cerr <<
"Iteration " << 1 <<
":" << endl
893 <<
"- \\|D\\|_{\\infty} = " << D_->normInf () << endl;
894 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG 895 #endif // HAVE_TEUCHOS_DEBUG 898 if (! zeroStartingSolution_) {
899 computeResidual (V1, B, A, X);
901 #ifdef HAVE_TEUCHOS_DEBUG 902 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG 903 cerr <<
"- \\|B - A*X\\|_{\\infty} = " << maxNormInf (V1) << endl;
904 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG 905 #endif // HAVE_TEUCHOS_DEBUG 907 solve (W, one/theta, D_inv, V1);
909 #ifdef HAVE_TEUCHOS_DEBUG 910 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG 911 cerr <<
"- \\|W\\|_{\\infty} = " << maxNormInf (W) << endl;
912 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG 913 #endif // HAVE_TEUCHOS_DEBUG 915 X.update (one, W, one);
917 solve (W, one/theta, D_inv, B);
919 #ifdef HAVE_TEUCHOS_DEBUG 920 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG 921 cerr <<
"- \\|W\\|_{\\infty} = " << maxNormInf (W) << endl;
922 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG 923 #endif // HAVE_TEUCHOS_DEBUG 927 #ifdef HAVE_TEUCHOS_DEBUG 928 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG 929 cerr <<
"- \\|X\\|_{\\infty} = " << maxNormInf (X) << endl;
930 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG 931 #endif // HAVE_TEUCHOS_DEBUG 935 ST rhokp1, dtemp1, dtemp2;
936 for (
int deg = 1; deg < numIters; ++deg) {
938 #ifdef HAVE_TEUCHOS_DEBUG 939 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG 940 cerr <<
"Iteration " << deg+1 <<
":" << endl;
941 cerr <<
"- \\|D\\|_{\\infty} = " << D_->normInf () << endl;
942 cerr <<
"- \\|B\\|_{\\infty} = " << maxNormInf (B) << endl;
943 cerr <<
"- \\|A\\|_{\\text{frob}} = " << A_->getFrobeniusNorm () << endl;
944 cerr <<
"- rhok = " << rhok << endl;
945 V1.putScalar (STS::zero ());
946 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG 947 #endif // HAVE_TEUCHOS_DEBUG 949 computeResidual (V1, B, A, X);
951 #ifdef HAVE_TEUCHOS_DEBUG 952 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG 953 cerr <<
"- \\|B - A*X\\|_{\\infty} = " << maxNormInf (V1) << endl;
954 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG 955 #endif // HAVE_TEUCHOS_DEBUG 957 rhokp1 = one / (two * s1 - rhok);
958 dtemp1 = rhokp1 * rhok;
959 dtemp2 = two * rhokp1 * delta;
962 #ifdef HAVE_TEUCHOS_DEBUG 963 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG 964 cerr <<
"- dtemp1 = " << dtemp1 << endl
965 <<
"- dtemp2 = " << dtemp2 << endl;
966 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG 967 #endif // HAVE_TEUCHOS_DEBUG 970 W.elementWiseMultiply (dtemp2, D_inv, V1, one);
971 X.update (one, W, one);
973 #ifdef HAVE_TEUCHOS_DEBUG 974 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG 975 cerr <<
"- \\|W\\|_{\\infty} = " << maxNormInf (W) << endl;
976 cerr <<
"- \\|X\\|_{\\infty} = " << maxNormInf (X) << endl;
977 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG 978 #endif // HAVE_TEUCHOS_DEBUG 982 template<
class ScalarType,
class MV,
class MAT>
983 typename Chebyshev<ScalarType, MV, MAT>::ST
985 powerMethod (
const MAT& A,
const V& D_inv,
const int numIters) {
986 const ST zero = Teuchos::as<ST> (0);
987 const ST one = Teuchos::as<ST> (1);
989 ST RQ_top, RQ_bottom, norm;
991 V x (A.getDomainMap ());
992 V y (A.getRangeMap ());
995 TEUCHOS_TEST_FOR_EXCEPTION(norm == zero, std::runtime_error,
996 "Ifpack2::Chebyshev::powerMethod: " 997 "Tpetra::Vector's randomize() method filled the vector " 998 "with zeros. This is not impossible, but is unlikely. " 999 "It's far more likely that there is a bug in Tpetra.");
1001 x.scale (one / norm);
1002 for (
int iter = 0; iter < numIters; ++iter) {
1004 solve (y, D_inv, y);
1006 RQ_bottom = x.dot (x);
1007 lambdaMax = RQ_top / RQ_bottom;
1012 x.update (one / norm, y, zero);
1017 template<
class ScalarType,
class MV,
class MAT>
1018 Teuchos::RCP<const MAT>
1024 template<
class ScalarType,
class MV,
class MAT>
1032 template<
class ScalarType,
class MV,
class MAT>
1036 Teuchos::RCP<MV>& W,
1039 if (V_.is_null ()) {
1040 V_ = Teuchos::rcp (
new MV (X.getMap (), X.getNumVectors (),
false));
1043 if (W_.is_null ()) {
1044 W_ = Teuchos::rcp (
new MV (X.getMap (), X.getNumVectors (),
true));
1050 template<
class ScalarType,
class MV,
class MAT>
1054 std::ostringstream oss;
1055 oss <<
"Ifpack2::Details::Chebyshev : " 1056 <<
"degree = " << numIters_
1057 <<
", lambdaMax = " << lambdaMaxForApply_
1058 <<
", alpha = " << eigRatioForApply_
1059 <<
", lambdaMin = " << lambdaMinForApply_;
1064 template<
class ScalarType,
class MV,
class MAT>
1067 describe (Teuchos::FancyOStream& out,
1068 const Teuchos::EVerbosityLevel verbLevel)
const 1070 using Teuchos::TypeNameTraits;
1073 const Teuchos::EVerbosityLevel vl =
1074 (verbLevel == Teuchos::VERB_DEFAULT) ? Teuchos::VERB_LOW : verbLevel;
1075 if (vl > Teuchos::VERB_NONE) {
1076 if (vl == Teuchos::VERB_LOW) {
1077 out <<
"Ifpack2::Details::Chebyshev" << endl;
1083 out <<
"Ifpack2::Details::Chebyshev {" << endl;
1084 Teuchos::OSTab tab1 (Teuchos::rcpFromRef (out));
1085 out <<
"Template parameters: {" << endl;
1087 Teuchos::OSTab tab2 (Teuchos::rcpFromRef (out));
1088 out <<
"ScalarType: \"" << TypeNameTraits<ScalarType>::name () <<
"\"" << endl
1089 <<
"MV: \"" << TypeNameTraits<MV>::name () <<
"\"" << endl
1090 <<
"MAT: \"" << TypeNameTraits<MAT>::name () <<
"\"" << endl;
1094 out <<
"}" << endl <<
"Computed parameters: {" << endl;
1096 Teuchos::OSTab tab2 (Teuchos::rcpFromRef (out));
1100 if (D_.is_null ()) {
1101 out <<
"unset" << endl;
1102 }
else if (vl <= Teuchos::VERB_HIGH) {
1103 out <<
"set" << endl;
1107 Teuchos::OSTab tab3 (Teuchos::rcpFromRef (out));
1108 D_->describe (out, vl);
1114 out <<
"V_: " << (V_.is_null () ?
"unset" :
"set") << endl
1115 <<
"W_: " << (W_.is_null () ?
"unset" :
"set") << endl
1116 <<
"computedLambdaMax_: " << computedLambdaMax_ << endl
1117 <<
"computedLambdaMin_: " << computedLambdaMin_ << endl
1118 <<
"lambdaMaxForApply_: " << lambdaMaxForApply_ << endl
1119 <<
"lambdaMinForApply_: " << lambdaMinForApply_ << endl
1120 <<
"eigRatioForApply_: " << eigRatioForApply_ << endl;
1122 out <<
"}" << endl <<
"User parameters: {" << endl;
1124 Teuchos::OSTab tab2 (Teuchos::rcpFromRef (out));
1125 out <<
"userInvDiag_: ";
1126 if (userInvDiag_.is_null ()) {
1127 out <<
"unset" << endl;
1128 }
else if (vl <= Teuchos::VERB_HIGH) {
1129 out <<
"set" << endl;
1133 Teuchos::OSTab tab3 (Teuchos::rcpFromRef (out));
1134 userInvDiag_->describe (out, vl);
1138 out <<
"userLambdaMax_: " << userLambdaMax_ << endl
1139 <<
"userLambdaMin_: " << userLambdaMin_ << endl
1140 <<
"userEigRatio_: " << userEigRatio_ << endl
1141 <<
"numIters_: " << numIters_ << endl
1142 <<
"eigMaxIters_: " << eigMaxIters_ << endl
1143 <<
"zeroStartingSolution_: " << zeroStartingSolution_ << endl
1144 <<
"assumeMatrixUnchanged_: " << assumeMatrixUnchanged_ << endl
1145 <<
"textbookAlgorithm_: " << textbookAlgorithm_ << endl
1146 <<
"computeMaxResNorm_: " << computeMaxResNorm_ << endl;
1156 #endif // IFPACK2_DETAILS_CHEBYSHEV_DEF_HPP MT apply(const MV &B, MV &X)
Solve Ax=b for x with Chebyshev iteration with left diagonal scaling.
Definition: Ifpack2_Chebyshev_def.hpp:199
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print a description of the Chebyshev solver to out.
Definition: Ifpack2_Chebyshev_def.hpp:364
Chebyshev(Teuchos::RCP< const MAT > A)
Definition: Ifpack2_Details_Chebyshev_def.hpp:149
Ifpack2 implementation details.
void print(std::ostream &out)
Print instance data to the given output stream.
Definition: Ifpack2_Details_Chebyshev_def.hpp:648
Left-scaled Chebyshev iteration.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:128
void setParameters(Teuchos::ParameterList &plist)
Set (or reset) parameters.
Definition: Ifpack2_Chebyshev_def.hpp:75
Declaration of Chebyshev implementation.
Definition: Ifpack2_BlockRelaxation_decl.hpp:64
void compute()
(Re)compute the left scaling D_inv, and estimate min and max eigenvalues of D_inv * A...
Definition: Ifpack2_Chebyshev_def.hpp:270
bool hasTransposeApply() const
Whether it's possible to apply the transpose of this operator.
Definition: Ifpack2_Chebyshev_def.hpp:115
std::string description() const
A single-line description of the Chebyshev solver.
Definition: Ifpack2_Chebyshev_def.hpp:339
Ifpack2::Container: a pure virtual class for creating and solving local linear problems.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:67
Teuchos::RCP< const MAT > getMatrix() const
Get the matrix given to the constructor.
Definition: Ifpack2_Chebyshev_def.hpp:93