Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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"
56 #include "Teuchos_Describable.hpp"
57 #include "Tpetra_CrsMatrix.hpp"
58 
59 namespace Ifpack2 {
60 namespace Details {
61 
62 #ifndef DOXYGEN_SHOULD_SKIP_THIS
63 template<class TpetraOperatorType>
64 class ChebyshevKernel; // forward declaration
65 #endif // DOXYGEN_SHOULD_SKIP_THIS
66 
105 template<class ScalarType, class MV>
107 public:
109 
110 
112  typedef ScalarType ST;
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 
146 
157 
264 
298  void compute ();
299 
316  MT apply (const MV& B, MV& X);
317 
318  ST getLambdaMaxForApply() const;
319 
322 
329 
331  bool hasTransposeApply () const;
332 
334  void print (std::ostream& out);
335 
337 
339 
341  std::string description() const;
342 
344  void
346  const Teuchos::EVerbosityLevel verbLevel =
349 private:
351 
352 
360 
363 
375 
377  typedef Kokkos::View<size_t*, typename MV::node_type::device_type> offsets_type;
378 
384  offsets_type diagOffsets_;
385 
393  bool savedDiagOffsets_;
394 
396 
398 
402  Teuchos::RCP<MV> W_;
403 
409  ST computedLambdaMax_;
410 
416  ST computedLambdaMin_;
417 
419 
421 
424  ST lambdaMaxForApply_;
425 
432  MT boostFactor_;
435  ST lambdaMinForApply_;
438  ST eigRatioForApply_;
439 
441 
443 
448  Teuchos::RCP<const V> userInvDiag_;
449 
453  ST userLambdaMax_;
454 
458  ST userLambdaMin_;
459 
463  ST userEigRatio_;
464 
469  ST minDiagVal_;
470 
472  int numIters_;
473 
475  int eigMaxIters_;
476 
478  bool zeroStartingSolution_;
479 
486  bool assumeMatrixUnchanged_;
487 
489  bool textbookAlgorithm_;
490 
492  bool computeMaxResNorm_;
493 
498 
500  bool debug_;
501 
503 
505 
507  void checkConstructorInput () const;
508 
510  void checkInputMatrix () const;
511 
519  void reset ();
520 
526  Teuchos::RCP<MV> makeTempMultiVector (const MV& B);
527 
529  void
530  firstIterationWithZeroStartingSolution
531  (MV& W,
532  const ScalarType& alpha,
533  const V& D_inv,
534  const MV& B,
535  MV& X);
536 
538  static void
539  computeResidual (MV& R, const MV& B, const op_type& A, const MV& X,
540  const Teuchos::ETransp mode = Teuchos::NO_TRANS);
541 
547  static void solve (MV& Z, const V& D_inv, const MV& R);
548 
554  static void solve (MV& Z, const ST alpha, const V& D_inv, const MV& R);
555 
564  makeInverseDiagonal (const row_matrix_type& A, const bool useDiagOffsets=false) const;
565 
575  Teuchos::RCP<V> makeRangeMapVector (const Teuchos::RCP<V>& D) const;
576 
579  makeRangeMapVectorConst (const Teuchos::RCP<const V>& D) const;
580 
599  void
600  textbookApplyImpl (const op_type& A,
601  const MV& B,
602  MV& X,
603  const int numIters,
604  const ST lambdaMax,
605  const ST lambdaMin,
606  const ST eigRatio,
607  const V& D_inv) const;
608 
631  void
632  ifpackApplyImpl (const op_type& A,
633  const MV& B,
634  MV& X,
635  const int numIters,
636  const ST lambdaMax,
637  const ST lambdaMin,
638  const ST eigRatio,
639  const V& D_inv);
640 
652  void computeInitialGuessForPowerMethod (V& x, const bool nonnegativeRealParts = false) const;
653 
666  ST
667  powerMethodWithInitGuess (const op_type& A, const V& D_inv, const int numIters, V& x);
668 
678  ST
679  powerMethod (const op_type& A, const V& D_inv, const int numIters);
680 
682  static MT maxNormInf (const MV& X);
683 
684  // mfh 22 Jan 2013: This code builds correctly, but does not
685  // converge. I'm leaving it in place in case someone else wants to
686  // work on it.
687 #if 0
688  void
711  mlApplyImpl (const op_type& A,
712  const MV& B,
713  MV& X,
714  const int numIters,
715  const ST lambdaMax,
716  const ST lambdaMin,
717  const ST eigRatio,
718  const V& D_inv)
719  {
720  const ST zero = Teuchos::as<ST> (0);
721  const ST one = Teuchos::as<ST> (1);
722  const ST two = Teuchos::as<ST> (2);
723 
724  MV pAux (B.getMap (), B.getNumVectors ()); // Result of A*X
725  MV dk (B.getMap (), B.getNumVectors ()); // Solution update
726  MV R (B.getMap (), B.getNumVectors ()); // Not in original ML; need for B - pAux
727 
728  ST beta = Teuchos::as<ST> (1.1) * lambdaMax;
729  ST alpha = lambdaMax / eigRatio;
730 
731  ST delta = (beta - alpha) / two;
732  ST theta = (beta + alpha) / two;
733  ST s1 = theta / delta;
734  ST rhok = one / s1;
735 
736  // Diagonal: ML replaces entries containing 0 with 1. We
737  // shouldn't have any entries like that in typical test problems,
738  // so it's OK not to do that here.
739 
740  // The (scaled) matrix is the identity: set X = D_inv * B. (If A
741  // is the identity, then certainly D_inv is too. D_inv comes from
742  // A, so if D_inv * A is the identity, then we still need to apply
743  // the "preconditioner" D_inv to B as well, to get X.)
744  if (lambdaMin == one && lambdaMin == lambdaMax) {
745  solve (X, D_inv, B);
746  return;
747  }
748 
749  // The next bit of code is a direct translation of code from ML's
750  // ML_Cheby function, in the "normal point scaling" section, which
751  // is in lines 7365-7392 of ml_smoother.c.
752 
753  if (! zeroStartingSolution_) {
754  // dk = (1/theta) * D_inv * (B - (A*X))
755  A.apply (X, pAux); // pAux = A * X
756  R = B;
757  R.update (-one, pAux, one); // R = B - pAux
758  dk.elementWiseMultiply (one/theta, D_inv, R, zero); // dk = (1/theta)*D_inv*R
759  X.update (one, dk, one); // X = X + dk
760  } else {
761  dk.elementWiseMultiply (one/theta, D_inv, B, zero); // dk = (1/theta)*D_inv*B
762  X = dk;
763  }
764 
765  ST rhokp1, dtemp1, dtemp2;
766  for (int k = 0; k < numIters-1; ++k) {
767  A.apply (X, pAux);
768  rhokp1 = one / (two*s1 - rhok);
769  dtemp1 = rhokp1*rhok;
770  dtemp2 = two*rhokp1/delta;
771  rhok = rhokp1;
772 
773  R = B;
774  R.update (-one, pAux, one); // R = B - pAux
775  // dk = dtemp1 * dk + dtemp2 * D_inv * (B - pAux)
776  dk.elementWiseMultiply (dtemp2, D_inv, B, dtemp1);
777  X.update (one, dk, one); // X = X + dk
778  }
779  }
780 #endif // 0
781 
782 };
783 
784 } // namespace Details
785 } // namespace Ifpack2
786 
787 #endif // IFPACK2_DETAILS_CHEBYSHEV_DECL_HPP
void setParameters(Teuchos::ParameterList &plist)
Set (or reset) parameters.
Definition: Ifpack2_Details_Chebyshev_def.hpp:333
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:905
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
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:1523
void setMatrix(const Teuchos::RCP< const row_matrix_type > &A)
Set the matrix.
Definition: Ifpack2_Details_Chebyshev_def.hpp:697
Teuchos::RCP< const row_matrix_type > getMatrix() const
Get the matrix given to the constructor.
Definition: Ifpack2_Details_Chebyshev_def.hpp:1473
Chebyshev(Teuchos::RCP< const row_matrix_type > A)
Definition: Ifpack2_Details_Chebyshev_def.hpp:279
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
void print(std::ostream &out)
Print instance data to the given output stream.
Definition: Ifpack2_Details_Chebyshev_def.hpp:958
STS::magnitudeType MT
The type of the absolute value of a ScalarType.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:116
ScalarType ST
The type of entries in the matrix and vectors.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:112
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
Left-scaled Chebyshev iteration.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:106
static const EVerbosityLevel verbLevel_default
std::string description() const
A single-line description of the Chebyshev solver.
Definition: Ifpack2_Details_Chebyshev_def.hpp:1505
Teuchos::ScalarTraits< ScalarType > STS
Traits class for ST.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:114
bool hasTransposeApply() const
Whether it&#39;s possible to apply the transpose of this operator.
Definition: Ifpack2_Details_Chebyshev_def.hpp:1480
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:738
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