Ifpack2 Templated Preconditioning Package  Version 1.0
Ifpack2_Hypre_decl.hpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 */
42 
43 #ifndef IFPACK2_HYPRE_DECL_HPP
44 #define IFPACK2_HYPRE_DECL_HPP
45 
46 #include "Ifpack2_ConfigDefs.hpp"
47 #if defined(HAVE_IFPACK2_HYPRE) && defined(HAVE_IFPACK2_MPI)
48 
51 #include "Tpetra_MultiVector.hpp"
52 #include "Tpetra_Vector.hpp"
53 #include "Tpetra_CrsGraph.hpp"
54 #include "Tpetra_CrsMatrix.hpp"
55 #include "Tpetra_Map.hpp"
56 #include "Tpetra_CrsMatrix.hpp"
57 
58 #include "Teuchos_RefCountPtr.hpp"
59 #include "Teuchos_ArrayRCP.hpp"
60 
61 #include "Ifpack2_Hypre_FunctionParameters.hpp"
62 
63 #include <map>
64 
65 // Hypre forward declarations (to avoid downstream header pollution)
66 struct hypre_IJMatrix_struct;
67 typedef struct hypre_IJMatrix_struct *HYPRE_IJMatrix;
68 struct hypre_IJVector_struct;
69 typedef struct hypre_IJVector_struct *HYPRE_IJVector;
70 struct hypre_ParCSRMatrix_struct;
71 typedef struct hypre_ParCSRMatrix_struct* HYPRE_ParCSRMatrix;
72 struct hypre_ParVector_struct;
73 typedef struct hypre_ParVector_struct * HYPRE_ParVector;
74 struct hypre_Solver_struct;
75 typedef struct hypre_Solver_struct *HYPRE_Solver;
76 struct hypre_ParVector_struct;
77 typedef struct hypre_ParVector_struct hypre_ParVector;
78 //struct hypre_Vector;
79 
80 namespace Ifpack2 {
81 
82 
83 #ifndef HYPRE_ENUMS
84 #define HYPRE_ENUMS
85  enum Hypre_Solver{
87  BoomerAMG,
88  ParaSails,
89  Euclid,
90  AMS,
91  Hybrid,
92  PCG,
93  GMRES,
94  FlexGMRES,
95  LGMRES,
96  BiCGSTAB
97  };
98 
100  enum Hypre_Chooser{
101  Hypre_Is_Solver,
102  Hypre_Is_Preconditioner
103  };
104 #endif //HYPRE_ENUMS
105 
106 
108 
113 template<class MatrixType>
114 class Hypre:
115  virtual public Ifpack2::Preconditioner<typename MatrixType::scalar_type,
116  typename MatrixType::local_ordinal_type,
117  typename MatrixType::global_ordinal_type,
118  typename MatrixType::node_type>,
119  virtual public Ifpack2::Details::CanChangeMatrix<Tpetra::RowMatrix<typename MatrixType::scalar_type,
120  typename MatrixType::local_ordinal_type,
121  typename MatrixType::global_ordinal_type,
122  typename MatrixType::node_type> >
123 {
124 public:
125 public:
127 
128 
130  typedef MatrixType matrix_type;
131 
133  typedef typename MatrixType::scalar_type scalar_type;
134 
136  typedef typename MatrixType::local_ordinal_type local_ordinal_type;
137 
139  typedef typename MatrixType::global_ordinal_type global_ordinal_type;
140 
142  typedef typename MatrixType::node_type::device_type device_type;
143 
145  typedef typename MatrixType::node_type node_type;
146 
148  typedef typename Teuchos::ScalarTraits<scalar_type>::magnitudeType magnitude_type;
149 
154  typedef Tpetra::RowMatrix<scalar_type, local_ordinal_type,
155  global_ordinal_type, node_type> row_matrix_type;
156 
157  static_assert (std::is_same<MatrixType, row_matrix_type>::value,
158  "Ifpack2::Hypre: MatrixType must be a Tpetra::RowMatrix "
159  "specialization. Don't use Tpetra::CrsMatrix here.");
160 
161 
163  typedef Tpetra::CrsMatrix<scalar_type, local_ordinal_type,
164  global_ordinal_type, node_type> crs_matrix_type;
165 
167  typedef Tpetra::Map<local_ordinal_type, global_ordinal_type, node_type> map_type;
168 
174  typedef Tpetra::Vector<scalar_type, local_ordinal_type,
175  global_ordinal_type, node_type> vector_type;
176 
178  typedef Tpetra::MultiVector<scalar_type, local_ordinal_type,
179  global_ordinal_type, node_type> multivector_type;
180 
181  // Hypre Specs
182  // This will need to be either int or long long depending on how Hypre was built
183  // typedef global_ordinal_type global_ordinal_type;
184 
185  typedef global_ordinal_type (*HYPRE_PtrToParSolverFcn)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
186 
188  // \name Constructors and destructors
190 
212  explicit Hypre(const Teuchos::RCP<const row_matrix_type>& A);
213 
215  ~Hypre();
216 
217  // @}
218  // @{ Construction methods
220  void initialize();
221 
223  bool isInitialized() const{ return(IsInitialized_);}
224 
226 
228  void compute();
229 
231  bool isComputed() const{ return(IsComputed_);}
232 
234  /* This method is only available if the Teuchos package is enabled.
235  This method recognizes six parameter names: Solver,
236  Preconditioner, SolveOrPrecondition, SetPreconditioner, NumFunctions and Functions. These names are
237  case sensitive. Solver requires an enumerated parameter of type Hypre_Solver. Preconditioner is similar
238  except requires the type be a preconditioner. The options are listed below:
239  Solvers Preconditioners
240  BoomerAMG BoomerAMG
241  AMS ParaSails
242  Hybrid AMS
243  PCG (Default) Euclid (Default)
244  GMRES
245  FlexGMRES
246  LGMRES
247  BiCGSTAB
248  SolveOrPrecondition takes enumerated type Hypre_Chooser, Solver will solve the system, Preconditioner will apply the preconditioner.
249  SetPreconditioner takes a boolean, true means the solver will use the preconditioner.
250  NumFunctions takes an int that describes how many parameters will be passed into Functions. (This needs to be correct.)
251  Functions takes an array of Ref Counted Pointers to an object called FunctionParameter. This class is implemented in Ifpack_Hypre.h.
252  The object takes whether it is Solver or Preconditioner that we are setting a parameter for.
253  The function in Hypre that sets the parameter, and the parameters for that function. An example is below:
254 
255  RCP<FunctionParameter> functs[2];
256  functs[0] = rcp(new FunctionParameter(Solver, &HYPRE_PCGSetMaxIter, 1000)); // max iterations
257  functs[1] = rcp(new FunctionParameter(Solver, &HYPRE_PCGSetTol, 1e-7)); // conv. tolerance
258  list.set("NumFunctions", 2);
259  list.set<RCP<FunctionParameter>*>("Functions", functs);
260  NOTE: SetParameters() must be called to use ApplyInverse(), the solvers will not be created otherwise. An empty list is acceptable to use defaults.
261  */
262  void setParameters(const Teuchos::ParameterList& parameterlist);
263 
265 
273  int SetParameter(Hypre_Chooser chooser, global_ordinal_type (*pt2Func)(HYPRE_Solver, global_ordinal_type), global_ordinal_type parameter);
274 
276 
284  int SetParameter(Hypre_Chooser chooser, global_ordinal_type (*pt2Func)(HYPRE_Solver, double), double parameter);
285 
287 
296  int SetParameter(Hypre_Chooser chooser, global_ordinal_type (*pt2Func)(HYPRE_Solver, double, global_ordinal_type), double parameter1, global_ordinal_type parameter2);
297 
299 
308  int SetParameter(Hypre_Chooser chooser, global_ordinal_type (*pt2Func)(HYPRE_Solver, global_ordinal_type, global_ordinal_type), global_ordinal_type parameter1, global_ordinal_type parameter2);
309 
311 
319  int SetParameter(Hypre_Chooser chooser, global_ordinal_type (*pt2Func)(HYPRE_Solver, double*), double* parameter);
320 
322 
330  int SetParameter(Hypre_Chooser chooser, global_ordinal_type (*pt2Func)(HYPRE_Solver, global_ordinal_type*), global_ordinal_type* parameter);
331 
333 
342  int SetParameter(Hypre_Chooser chooser, global_ordinal_type (*pt2Func)(HYPRE_Solver, global_ordinal_type**), global_ordinal_type** parameter);
343 
345 
354  int SetParameter(Hypre_Chooser chooser, Hypre_Solver Solver);
355 
357 
365  int SetParameter(bool UsePreconditioner){ UsePreconditioner_ = UsePreconditioner; return 0;}
366 
368 
374  int SetParameter(Hypre_Chooser chooser) { SolveOrPrec_ = chooser; return 0;}
375 
377  int SetCoordinates(Teuchos::RCP<multivector_type> coords);
378 
380  int SetDiscreteGradient(Teuchos::RCP<const crs_matrix_type> G);
381 
383  int CallFunctions() const;
384 
386 
388 
411  virtual void
412  setMatrix (const Teuchos::RCP<const row_matrix_type>& A);
414 
443  void
444  apply (const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
445  Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
446  Teuchos::ETransp mode = Teuchos::NO_TRANS,
447  scalar_type alpha = Teuchos::ScalarTraits<scalar_type>::one(),
448  scalar_type beta = Teuchos::ScalarTraits<scalar_type>::zero()) const;
449 
451  Teuchos::RCP<const map_type> getDomainMap() const;
452 
454  Teuchos::RCP<const map_type> getRangeMap() const;
455 
457  bool hasTransposeApply() const;
458 
479  void
480  applyMat (const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
481  Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
482  Teuchos::ETransp mode = Teuchos::NO_TRANS) const;
483 
485 
487 
489  Teuchos::RCP<const Teuchos::Comm<int> > getComm() const;
490 
492  Teuchos::RCP<const row_matrix_type> getMatrix() const;
493 
498  Teuchos::RCP<const Tpetra::CrsMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type> >
499  getCrsMatrix() const;
500 
502  int getNumInitialize() const;
503 
505  int getNumCompute() const;
506 
508  int getNumApply() const;
509 
511  double getInitializeTime() const;
512 
514  double getComputeTime() const;
515 
517  double getApplyTime() const;
518 
520 
522 
524  std::string description() const;
525 
527  void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const;
528 
530 
531 private:
532  // @{ Private methods
533 
535  typedef Teuchos::ScalarTraits<typename MatrixType::scalar_type> STS;
536 
538  typedef Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type> MV;
539 
541  Hypre(const Hypre<MatrixType>&);
542 
544  Hypre<MatrixType>& operator= (const Hypre<MatrixType>&);
545 
547  int SetSolverType(Hypre_Solver solver);
548 
550  int SetPrecondType(Hypre_Solver precond);
551 
553  int CreateSolver();
554 
556  int CreatePrecond();
557 
559  int CopyTpetraToHypre();
560 
562  int AddFunToList(Teuchos::RCP<FunctionParameter> NewFun);
563 
565  int Hypre_BoomerAMGCreate(MPI_Comm comm, HYPRE_Solver *solver);
566 
568  int Hypre_ParaSailsCreate(MPI_Comm comm, HYPRE_Solver *solver);
569 
571  int Hypre_EuclidCreate(MPI_Comm comm, HYPRE_Solver *solver);
572 
574  int Hypre_AMSCreate(MPI_Comm comm, HYPRE_Solver *solver);
575 
577  int Hypre_ParCSRHybridCreate(MPI_Comm comm, HYPRE_Solver *solver);
578 
580  int Hypre_ParCSRPCGCreate(MPI_Comm comm, HYPRE_Solver *solver);
581 
583  int Hypre_ParCSRGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver);
584 
586  int Hypre_ParCSRFlexGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver);
587 
589  int Hypre_ParCSRLGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver);
590 
592  int Hypre_ParCSRBiCGSTABCreate(MPI_Comm comm, HYPRE_Solver *solver);
593 
595  Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type, typename MatrixType::global_ordinal_type, typename MatrixType::node_type> >
596  MakeContiguousColumnMap(Teuchos::RCP<const crs_matrix_type> &Matrix) const;
597 
599  void Destroy();
600 
601  // @}
602  // @{ Internal data
604  Teuchos::RCP<const MatrixType> A_;
606  Teuchos::ParameterList List_;
607 
609  bool IsInitialized_;
611  bool IsComputed_;
613  int NumInitialize_;
615  int NumCompute_;
620  mutable int NumApply_;
622  double InitializeTime_;
624  double ComputeTime_;
629  mutable double ApplyTime_;
631  double ComputeFlops_;
636  mutable double ApplyFlops_;
637 
638 
640  mutable HYPRE_IJMatrix HypreA_;
642  mutable HYPRE_ParCSRMatrix ParMatrix_;
643 
645  Teuchos::RCP<const crs_matrix_type> G_;
647  mutable HYPRE_IJMatrix HypreG_;
649  mutable HYPRE_ParCSRMatrix ParMatrixG_;
650 
652  mutable HYPRE_IJVector XHypre_;
654  mutable HYPRE_IJVector YHypre_;
655  mutable HYPRE_ParVector ParX_;
656  mutable HYPRE_ParVector ParY_;
657  mutable Teuchos::RCP<hypre_ParVector> XVec_;
658  mutable Teuchos::RCP<hypre_ParVector> YVec_;
659 
660  Teuchos::RCP<multivector_type> Coords_;
661  mutable HYPRE_IJVector xHypre_;
662  mutable HYPRE_IJVector yHypre_;
663  mutable HYPRE_IJVector zHypre_;
664  mutable HYPRE_ParVector xPar_;
665  mutable HYPRE_ParVector yPar_;
666  mutable HYPRE_ParVector zPar_;
667 
669  mutable HYPRE_Solver Solver_;
671  mutable HYPRE_Solver Preconditioner_;
672  // The following are pointers to functions to use the solver and preconditioner.
673  int (Hypre::*SolverCreatePtr_)(MPI_Comm, HYPRE_Solver*);
674  global_ordinal_type (*SolverDestroyPtr_)(HYPRE_Solver);
675  global_ordinal_type (*SolverSetupPtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
676  global_ordinal_type (*SolverSolvePtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
677  global_ordinal_type (*SolverPrecondPtr_)(HYPRE_Solver, HYPRE_PtrToParSolverFcn, HYPRE_PtrToParSolverFcn, HYPRE_Solver);
678  int (Hypre::*PrecondCreatePtr_)(MPI_Comm, HYPRE_Solver*);
679  global_ordinal_type (*PrecondDestroyPtr_)(HYPRE_Solver);
680  global_ordinal_type (*PrecondSetupPtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
681  global_ordinal_type (*PrecondSolvePtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
682 
683  bool IsSolverCreated_;
684  bool IsPrecondCreated_;
686  Hypre_Chooser SolveOrPrec_;
688  Teuchos::RCP<const map_type> GloballyContiguousRowMap_;
689  Teuchos::RCP<const map_type> GloballyContiguousColMap_;
690  Teuchos::RCP<const map_type> GloballyContiguousNodeRowMap_;
691  Teuchos::RCP<const map_type> GloballyContiguousNodeColMap_;
693  int NumFunsToCall_;
695  Hypre_Solver SolverType_;
697  Hypre_Solver PrecondType_;
699  bool UsePreconditioner_;
701  std::vector<Teuchos::RCP<FunctionParameter> > FunsToCall_;
703  bool Dump_;
705  mutable Teuchos::ArrayRCP<double> VectorCache_;
706 
707 };
708 
709 
710 }//end Ifpack2 namespace
711 
712 #endif // HAVE_IFPACK2_HYPRE && HAVE_IFPACK2_MPI
713 #endif /* IFPACK2_HYPRE_DECL_HPP */
Mix-in interface for preconditioners that can change their matrix after construction.
Definition: Ifpack2_Details_CanChangeMatrix.hpp:93
Interface for all Ifpack2 preconditioners.
Definition: Ifpack2_Preconditioner.hpp:107
Declaration of interface for preconditioners that can change their matrix after construction.
Uses AztecOO&#39;s GMRES.
Definition: Ifpack2_CondestType.hpp:53
Preconditioners and smoothers for Tpetra sparse matrices.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:73