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 
61 namespace Details {
100 template<class ScalarType, class MV>
102 public:
104 
105 
107  typedef ScalarType ST;
111  typedef typename STS::magnitudeType MT;
113  typedef Tpetra::Operator<typename MV::scalar_type,
114  typename MV::local_ordinal_type,
115  typename MV::global_ordinal_type,
116  typename MV::node_type> op_type;
118  typedef Tpetra::RowMatrix<typename MV::scalar_type,
119  typename MV::local_ordinal_type,
120  typename MV::global_ordinal_type,
121  typename MV::node_type> row_matrix_type;
123  typedef Tpetra::Vector<typename MV::scalar_type,
124  typename MV::local_ordinal_type,
125  typename MV::global_ordinal_type,
126  typename MV::node_type> V;
128  typedef Tpetra::Map<typename MV::local_ordinal_type,
129  typename MV::global_ordinal_type,
130  typename MV::node_type> map_type;
132 
141 
152 
259 
293  void compute ();
294 
311  MT apply (const MV& B, MV& X);
312 
313  ST getLambdaMaxForApply() const;
314 
317 
324 
326  bool hasTransposeApply () const;
327 
329  void print (std::ostream& out);
330 
332 
334 
336  std::string description() const;
337 
339  void
341  const Teuchos::EVerbosityLevel verbLevel =
344 private:
346 
347 
355 
367 
369  typedef Kokkos::View<size_t*, typename MV::node_type::device_type> offsets_type;
370 
376  offsets_type diagOffsets_;
377 
385  bool savedDiagOffsets_;
386 
388 
390 
394  Teuchos::RCP<MV> V_;
395 
399  Teuchos::RCP<MV> W_;
400 
406  ST computedLambdaMax_;
407 
413  ST computedLambdaMin_;
414 
416 
418 
421  ST lambdaMaxForApply_;
422 
429  MT boostFactor_;
432  ST lambdaMinForApply_;
435  ST eigRatioForApply_;
436 
438 
440 
445  Teuchos::RCP<const V> userInvDiag_;
446 
450  ST userLambdaMax_;
451 
455  ST userLambdaMin_;
456 
460  ST userEigRatio_;
461 
466  ST minDiagVal_;
467 
469  int numIters_;
470 
472  int eigMaxIters_;
473 
475  bool zeroStartingSolution_;
476 
483  bool assumeMatrixUnchanged_;
484 
486  bool textbookAlgorithm_;
487 
489  bool computeMaxResNorm_;
490 
495 
497  bool debug_;
498 
500 
502 
504  void checkConstructorInput () const;
505 
507  void checkInputMatrix () const;
508 
516  void reset ();
517 
530  void
531  makeTempMultiVectors (Teuchos::RCP<MV>& V1,
532  Teuchos::RCP<MV>& W,
533  const MV& X);
534 
536  static void
537  computeResidual (MV& R, const MV& B, const op_type& A, const MV& X,
538  const Teuchos::ETransp mode = Teuchos::NO_TRANS);
539 
545  static void solve (MV& Z, const V& D_inv, const MV& R);
546 
552  static void solve (MV& Z, const ST alpha, const V& D_inv, const MV& R);
553 
562  makeInverseDiagonal (const row_matrix_type& A, const bool useDiagOffsets=false) const;
563 
573  Teuchos::RCP<V> makeRangeMapVector (const Teuchos::RCP<V>& D) const;
574 
577  makeRangeMapVectorConst (const Teuchos::RCP<const V>& D) const;
578 
597  void
598  textbookApplyImpl (const op_type& A,
599  const MV& B,
600  MV& X,
601  const int numIters,
602  const ST lambdaMax,
603  const ST lambdaMin,
604  const ST eigRatio,
605  const V& D_inv) const;
606 
629  void
630  ifpackApplyImpl (const op_type& A,
631  const MV& B,
632  MV& X,
633  const int numIters,
634  const ST lambdaMax,
635  const ST lambdaMin,
636  const ST eigRatio,
637  const V& D_inv);
638 
650  void computeInitialGuessForPowerMethod (V& x, const bool nonnegativeRealParts = false) const;
651 
664  ST
665  powerMethodWithInitGuess (const op_type& A, const V& D_inv, const int numIters, V& x);
666 
676  ST
677  powerMethod (const op_type& A, const V& D_inv, const int numIters);
678 
680  static MT maxNormInf (const MV& X);
681 
682  // mfh 22 Jan 2013: This code builds correctly, but does not
683  // converge. I'm leaving it in place in case someone else wants to
684  // work on it.
685 #if 0
686  void
709  mlApplyImpl (const op_type& A,
710  const MV& B,
711  MV& X,
712  const int numIters,
713  const ST lambdaMax,
714  const ST lambdaMin,
715  const ST eigRatio,
716  const V& D_inv)
717  {
718  const ST zero = Teuchos::as<ST> (0);
719  const ST one = Teuchos::as<ST> (1);
720  const ST two = Teuchos::as<ST> (2);
721 
722  MV pAux (B.getMap (), B.getNumVectors ()); // Result of A*X
723  MV dk (B.getMap (), B.getNumVectors ()); // Solution update
724  MV R (B.getMap (), B.getNumVectors ()); // Not in original ML; need for B - pAux
725 
726  ST beta = Teuchos::as<ST> (1.1) * lambdaMax;
727  ST alpha = lambdaMax / eigRatio;
728 
729  ST delta = (beta - alpha) / two;
730  ST theta = (beta + alpha) / two;
731  ST s1 = theta / delta;
732  ST rhok = one / s1;
733 
734  // Diagonal: ML replaces entries containing 0 with 1. We
735  // shouldn't have any entries like that in typical test problems,
736  // so it's OK not to do that here.
737 
738  // The (scaled) matrix is the identity: set X = D_inv * B. (If A
739  // is the identity, then certainly D_inv is too. D_inv comes from
740  // A, so if D_inv * A is the identity, then we still need to apply
741  // the "preconditioner" D_inv to B as well, to get X.)
742  if (lambdaMin == one && lambdaMin == lambdaMax) {
743  solve (X, D_inv, B);
744  return;
745  }
746 
747  // The next bit of code is a direct translation of code from ML's
748  // ML_Cheby function, in the "normal point scaling" section, which
749  // is in lines 7365-7392 of ml_smoother.c.
750 
751  if (! zeroStartingSolution_) {
752  // dk = (1/theta) * D_inv * (B - (A*X))
753  A.apply (X, pAux); // pAux = A * X
754  R = B;
755  R.update (-one, pAux, one); // R = B - pAux
756  dk.elementWiseMultiply (one/theta, D_inv, R, zero); // dk = (1/theta)*D_inv*R
757  X.update (one, dk, one); // X = X + dk
758  } else {
759  dk.elementWiseMultiply (one/theta, D_inv, B, zero); // dk = (1/theta)*D_inv*B
760  X = dk;
761  }
762 
763  ST rhokp1, dtemp1, dtemp2;
764  for (int k = 0; k < numIters-1; ++k) {
765  A.apply (X, pAux);
766  rhokp1 = one / (two*s1 - rhok);
767  dtemp1 = rhokp1*rhok;
768  dtemp2 = two*rhokp1/delta;
769  rhok = rhokp1;
770 
771  R = B;
772  R.update (-one, pAux, one); // R = B - pAux
773  // dk = dtemp1 * dk + dtemp2 * D_inv * (B - pAux)
774  dk.elementWiseMultiply (dtemp2, D_inv, B, dtemp1);
775  X.update (one, dk, one); // X = X + dk
776  }
777  }
778 #endif // 0
779 
780 };
781 
782 } // namespace Details
783 } // namespace Ifpack2
784 
785 #endif // IFPACK2_DETAILS_CHEBYSHEV_DECL_HPP
void setParameters(Teuchos::ParameterList &plist)
Set (or reset) parameters.
Definition: Ifpack2_Details_Chebyshev_def.hpp:329
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:956
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:116
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:1574
void setMatrix(const Teuchos::RCP< const row_matrix_type > &A)
Set the matrix.
Definition: Ifpack2_Details_Chebyshev_def.hpp:745
Teuchos::RCP< const row_matrix_type > getMatrix() const
Get the matrix given to the constructor.
Definition: Ifpack2_Details_Chebyshev_def.hpp:1520
Chebyshev(Teuchos::RCP< const row_matrix_type > A)
Definition: Ifpack2_Details_Chebyshev_def.hpp:276
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:130
void print(std::ostream &out)
Print instance data to the given output stream.
Definition: Ifpack2_Details_Chebyshev_def.hpp:1009
STS::magnitudeType MT
The type of the absolute value of a ScalarType.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:111
ScalarType ST
The type of entries in the matrix and vectors.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:107
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:121
Left-scaled Chebyshev iteration.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:101
static const EVerbosityLevel verbLevel_default
std::string description() const
A single-line description of the Chebyshev solver.
Definition: Ifpack2_Details_Chebyshev_def.hpp:1556
Teuchos::ScalarTraits< ScalarType > STS
Traits class for ST.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:109
bool hasTransposeApply() const
Whether it&#39;s possible to apply the transpose of this operator.
Definition: Ifpack2_Details_Chebyshev_def.hpp:1527
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:786
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:126