Ifpack2 Templated Preconditioning Package Version 1.0
Ifpack2_Details_Chebyshev_decl.hpp
Go to the documentation of this file.
1/*
2//@HEADER
3// ***********************************************************************
4//
5// Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
6// Copyright (2009) Sandia Corporation
7//
8// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9// license for use of this work by or on behalf of the U.S. Government.
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17//
18// 2. Redistributions in binary form must reproduce the above copyright
19// notice, this list of conditions and the following disclaimer in the
20// documentation and/or other materials provided with the distribution.
21//
22// 3. Neither the name of the Corporation nor the names of the
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39//
40// ***********************************************************************
41//@HEADER
42*/
43
44#ifndef IFPACK2_DETAILS_CHEBYSHEV_DECL_HPP
45#define IFPACK2_DETAILS_CHEBYSHEV_DECL_HPP
46
53
54#include "Ifpack2_ConfigDefs.hpp"
55#include "Teuchos_VerbosityLevel.hpp"
56#include "Teuchos_Describable.hpp"
57#include "Tpetra_CrsMatrix.hpp"
58
59namespace Ifpack2 {
60namespace Details {
61
62#ifndef DOXYGEN_SHOULD_SKIP_THIS
63template<class TpetraOperatorType>
64class ChebyshevKernel; // forward declaration
65#endif // DOXYGEN_SHOULD_SKIP_THIS
66
105template<class ScalarType, class MV>
106class Chebyshev : public Teuchos::Describable {
107public:
109
110
112 typedef ScalarType ST;
114 typedef Teuchos::ScalarTraits<ScalarType> STS;
116 typedef typename STS::magnitudeType MT;
118 typedef Tpetra::Operator<typename MV::scalar_type,
119 typename MV::local_ordinal_type,
120 typename MV::global_ordinal_type,
121 typename MV::node_type> op_type;
123 typedef Tpetra::RowMatrix<typename MV::scalar_type,
124 typename MV::local_ordinal_type,
125 typename MV::global_ordinal_type,
126 typename MV::node_type> row_matrix_type;
128 typedef Tpetra::Vector<typename MV::scalar_type,
129 typename MV::local_ordinal_type,
130 typename MV::global_ordinal_type,
131 typename MV::node_type> V;
133 typedef Tpetra::Map<typename MV::local_ordinal_type,
134 typename MV::global_ordinal_type,
135 typename MV::node_type> map_type;
137
145 Chebyshev (Teuchos::RCP<const row_matrix_type> A);
146
156 Chebyshev (Teuchos::RCP<const row_matrix_type> A, Teuchos::ParameterList& params);
157
263 void setParameters (Teuchos::ParameterList& plist);
264
265 void setZeroStartingSolution (bool zeroStartingSolution) { zeroStartingSolution_ = zeroStartingSolution; }
266
300 void compute ();
301
318 MT apply (const MV& B, MV& X);
319
320 ST getLambdaMaxForApply() const;
321
323 Teuchos::RCP<const row_matrix_type> getMatrix () const;
324
330 void setMatrix (const Teuchos::RCP<const row_matrix_type>& A);
331
333 bool hasTransposeApply () const;
334
336 void print (std::ostream& out);
337
339
341
343 std::string description() const;
344
346 void
347 describe (Teuchos::FancyOStream& out,
348 const Teuchos::EVerbosityLevel verbLevel =
349 Teuchos::Describable::verbLevel_default) const;
351private:
353
354
361 Teuchos::RCP<const row_matrix_type> A_;
362
364 Teuchos::RCP<ChebyshevKernel<op_type> > ck_;
365
376 Teuchos::RCP<const V> D_;
377
379 typedef Kokkos::View<size_t*, typename MV::node_type::device_type> offsets_type;
380
386 offsets_type diagOffsets_;
387
395 bool savedDiagOffsets_;
396
398
400
404 Teuchos::RCP<MV> W_;
405
411 ST computedLambdaMax_;
412
418 ST computedLambdaMin_;
419
421
423
426 ST lambdaMaxForApply_;
427
434 MT boostFactor_;
437 ST lambdaMinForApply_;
440 ST eigRatioForApply_;
441
443
445
450 Teuchos::RCP<const V> userInvDiag_;
451
455 ST userLambdaMax_;
456
460 ST userLambdaMin_;
461
465 ST userEigRatio_;
466
471 ST minDiagVal_;
472
474 int numIters_;
475
477 int eigMaxIters_;
478
480 MT eigRelTolerance_;
481
483 bool eigKeepVectors_;
484
486 std::string eigenAnalysisType_;
487
489 Teuchos::RCP<V> eigVector_;
490 Teuchos::RCP<V> eigVector2_;
491
493 int eigNormalizationFreq_;
494
496 bool zeroStartingSolution_;
497
504 bool assumeMatrixUnchanged_;
505
507 bool textbookAlgorithm_;
508
510 bool computeMaxResNorm_;
511
515 Teuchos::RCP<Teuchos::FancyOStream> out_;
516
518 bool debug_;
519
521
523
525 void checkConstructorInput () const;
526
528 void checkInputMatrix () const;
529
537 void reset ();
538
544 Teuchos::RCP<MV> makeTempMultiVector (const MV& B);
545
547 void
548 firstIterationWithZeroStartingSolution
549 (MV& W,
550 const ScalarType& alpha,
551 const V& D_inv,
552 const MV& B,
553 MV& X);
554
556 static void
557 computeResidual (MV& R, const MV& B, const op_type& A, const MV& X,
558 const Teuchos::ETransp mode = Teuchos::NO_TRANS);
559
565 static void solve (MV& Z, const V& D_inv, const MV& R);
566
572 static void solve (MV& Z, const ST alpha, const V& D_inv, const MV& R);
573
581 Teuchos::RCP<const V>
582 makeInverseDiagonal (const row_matrix_type& A, const bool useDiagOffsets=false) const;
583
593 Teuchos::RCP<V> makeRangeMapVector (const Teuchos::RCP<V>& D) const;
594
596 Teuchos::RCP<const V>
597 makeRangeMapVectorConst (const Teuchos::RCP<const V>& D) const;
598
617 void
618 textbookApplyImpl (const op_type& A,
619 const MV& B,
620 MV& X,
621 const int numIters,
622 const ST lambdaMax,
623 const ST lambdaMin,
624 const ST eigRatio,
625 const V& D_inv) const;
626
649 void
650 ifpackApplyImpl (const op_type& A,
651 const MV& B,
652 MV& X,
653 const int numIters,
654 const ST lambdaMax,
655 const ST lambdaMin,
656 const ST eigRatio,
657 const V& D_inv);
658
670 void computeInitialGuessForPowerMethod (V& x, const bool nonnegativeRealParts = false) const;
671
684 ST
685 powerMethodWithInitGuess (const op_type& A, const V& D_inv, const int numIters, V& x);
686
696 ST
697 powerMethod (const op_type& A, const V& D_inv, const int numIters);
698
711 ST
712 cgMethodWithInitGuess (const op_type& A, const V& D_inv, const int numIters, V& x);
713
723 ST
724 cgMethod (const op_type& A, const V& D_inv, const int numIters);
725
727 static MT maxNormInf (const MV& X);
728
729 // mfh 22 Jan 2013: This code builds correctly, but does not
730 // converge. I'm leaving it in place in case someone else wants to
731 // work on it.
732#if 0
755 void
756 mlApplyImpl (const op_type& A,
757 const MV& B,
758 MV& X,
759 const int numIters,
760 const ST lambdaMax,
761 const ST lambdaMin,
762 const ST eigRatio,
763 const V& D_inv)
764 {
765 const ST zero = Teuchos::as<ST> (0);
766 const ST one = Teuchos::as<ST> (1);
767 const ST two = Teuchos::as<ST> (2);
768
769 MV pAux (B.getMap (), B.getNumVectors ()); // Result of A*X
770 MV dk (B.getMap (), B.getNumVectors ()); // Solution update
771 MV R (B.getMap (), B.getNumVectors ()); // Not in original ML; need for B - pAux
772
773 ST beta = Teuchos::as<ST> (1.1) * lambdaMax;
774 ST alpha = lambdaMax / eigRatio;
775
776 ST delta = (beta - alpha) / two;
777 ST theta = (beta + alpha) / two;
778 ST s1 = theta / delta;
779 ST rhok = one / s1;
780
781 // Diagonal: ML replaces entries containing 0 with 1. We
782 // shouldn't have any entries like that in typical test problems,
783 // so it's OK not to do that here.
784
785 // The (scaled) matrix is the identity: set X = D_inv * B. (If A
786 // is the identity, then certainly D_inv is too. D_inv comes from
787 // A, so if D_inv * A is the identity, then we still need to apply
788 // the "preconditioner" D_inv to B as well, to get X.)
789 if (lambdaMin == one && lambdaMin == lambdaMax) {
790 solve (X, D_inv, B);
791 return;
792 }
793
794 // The next bit of code is a direct translation of code from ML's
795 // ML_Cheby function, in the "normal point scaling" section, which
796 // is in lines 7365-7392 of ml_smoother.c.
797
798 if (! zeroStartingSolution_) {
799 // dk = (1/theta) * D_inv * (B - (A*X))
800 A.apply (X, pAux); // pAux = A * X
801 R = B;
802 R.update (-one, pAux, one); // R = B - pAux
803 dk.elementWiseMultiply (one/theta, D_inv, R, zero); // dk = (1/theta)*D_inv*R
804 X.update (one, dk, one); // X = X + dk
805 } else {
806 dk.elementWiseMultiply (one/theta, D_inv, B, zero); // dk = (1/theta)*D_inv*B
807 X = dk;
808 }
809
810 ST rhokp1, dtemp1, dtemp2;
811 for (int k = 0; k < numIters-1; ++k) {
812 A.apply (X, pAux);
813 rhokp1 = one / (two*s1 - rhok);
814 dtemp1 = rhokp1*rhok;
815 dtemp2 = two*rhokp1/delta;
816 rhok = rhokp1;
817
818 R = B;
819 R.update (-one, pAux, one); // R = B - pAux
820 // dk = dtemp1 * dk + dtemp2 * D_inv * (B - pAux)
821 dk.elementWiseMultiply (dtemp2, D_inv, B, dtemp1);
822 X.update (one, dk, one); // X = X + dk
823 }
824 }
825#endif // 0
827};
828
829} // namespace Details
830} // namespace Ifpack2
831
832#endif // IFPACK2_DETAILS_CHEBYSHEV_DECL_HPP
Left-scaled Chebyshev iteration.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:106
Tpetra::Vector< typename MV::scalar_type, typename MV::local_ordinal_type, typename MV::global_ordinal_type, typename MV::node_type > V
Specialization of Tpetra::Vector.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:131
Teuchos::ScalarTraits< ScalarType > STS
Traits class for ST.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:114
void compute()
(Re)compute the left scaling D_inv, and estimate min and max eigenvalues of D_inv * A.
Definition: Ifpack2_Details_Chebyshev_def.hpp:814
void setParameters(Teuchos::ParameterList &plist)
Set (or reset) parameters.
Definition: Ifpack2_Details_Chebyshev_def.hpp:384
void setMatrix(const Teuchos::RCP< const row_matrix_type > &A)
Set the matrix.
Definition: Ifpack2_Details_Chebyshev_def.hpp:773
std::string description() const
A single-line description of the Chebyshev solver.
Definition: Ifpack2_Details_Chebyshev_def.hpp:1731
Chebyshev(Teuchos::RCP< const row_matrix_type > A)
Definition: Ifpack2_Details_Chebyshev_def.hpp:322
Tpetra::Map< typename MV::local_ordinal_type, typename MV::global_ordinal_type, typename MV::node_type > map_type
Specialization of Tpetra::Map.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:135
Tpetra::RowMatrix< typename MV::scalar_type, typename MV::local_ordinal_type, typename MV::global_ordinal_type, typename MV::node_type > row_matrix_type
Specialization of Tpetra::RowMatrix.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:126
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_Details_Chebyshev_def.hpp:1751
void print(std::ostream &out)
Print instance data to the given output stream.
Definition: Ifpack2_Details_Chebyshev_def.hpp:1038
Tpetra::Operator< typename MV::scalar_type, typename MV::local_ordinal_type, typename MV::global_ordinal_type, typename MV::node_type > op_type
Specialization of Tpetra::Operator.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:121
ScalarType ST
The type of entries in the matrix and vectors.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:112
STS::magnitudeType MT
The type of the absolute value of a ScalarType.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:116
MT apply(const MV &B, MV &X)
Solve Ax=b for x with Chebyshev iteration with left diagonal scaling.
Definition: Ifpack2_Details_Chebyshev_def.hpp:985
bool hasTransposeApply() const
Whether it's possible to apply the transpose of this operator.
Definition: Ifpack2_Details_Chebyshev_def.hpp:1707
Teuchos::RCP< const row_matrix_type > getMatrix() const
Get the matrix given to the constructor.
Definition: Ifpack2_Details_Chebyshev_def.hpp:1700
Ifpack2 implementation details.
Preconditioners and smoothers for Tpetra sparse matrices.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:73