Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_Hypre_decl.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
4 //
5 // Copyright 2009 NTESS and the Ifpack2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef IFPACK2_HYPRE_DECL_HPP
11 #define IFPACK2_HYPRE_DECL_HPP
12 
13 #include "Ifpack2_ConfigDefs.hpp"
14 #if defined(HAVE_IFPACK2_HYPRE) && defined(HAVE_IFPACK2_MPI)
15 
18 #include "Tpetra_MultiVector.hpp"
19 #include "Tpetra_Vector.hpp"
20 #include "Tpetra_CrsGraph.hpp"
21 #include "Tpetra_CrsMatrix.hpp"
22 #include "Tpetra_Map.hpp"
23 #include "Tpetra_CrsMatrix.hpp"
24 
25 #include "Teuchos_RefCountPtr.hpp"
26 #include "Teuchos_ArrayRCP.hpp"
27 #include "Teuchos_Exceptions.hpp"
28 
29 #include "Ifpack2_Hypre_FunctionParameters.hpp"
30 
31 #include <map>
32 
33 // Hypre forward declarations (to avoid downstream header pollution)
34 struct hypre_IJMatrix_struct;
35 typedef struct hypre_IJMatrix_struct* HYPRE_IJMatrix;
36 struct hypre_IJVector_struct;
37 typedef struct hypre_IJVector_struct* HYPRE_IJVector;
38 struct hypre_ParCSRMatrix_struct;
39 typedef struct hypre_ParCSRMatrix_struct* HYPRE_ParCSRMatrix;
40 struct hypre_ParVector_struct;
41 typedef struct hypre_ParVector_struct* HYPRE_ParVector;
42 struct hypre_Solver_struct;
43 typedef struct hypre_Solver_struct* HYPRE_Solver;
44 struct hypre_ParVector_struct;
45 typedef struct hypre_ParVector_struct hypre_ParVector;
46 // struct hypre_Vector;
47 
48 namespace Ifpack2 {
49 
50 #ifndef HYPRE_ENUMS
51 #define HYPRE_ENUMS
52 enum Hypre_Solver {
54  BoomerAMG,
55  ParaSails,
56  Euclid,
57  AMS,
58  Hybrid,
59  PCG,
60  GMRES,
61  FlexGMRES,
62  LGMRES,
63  BiCGSTAB
64 };
65 
67 enum Hypre_Chooser {
68  Hypre_Is_Solver,
69  Hypre_Is_Preconditioner
70 };
71 #endif // HYPRE_ENUMS
72 
73 class NotImplemented : public Teuchos::ExceptionBase {
74  public:
75  NotImplemented(const std::string& what_arg)
76  : Teuchos::ExceptionBase(what_arg) {}
77 };
78 
80 
85 template <class MatrixType>
86 class Hypre : virtual public Ifpack2::Preconditioner<typename MatrixType::scalar_type,
87  typename MatrixType::local_ordinal_type,
88  typename MatrixType::global_ordinal_type,
89  typename MatrixType::node_type>,
90  virtual public Ifpack2::Details::CanChangeMatrix<Tpetra::RowMatrix<typename MatrixType::scalar_type,
91  typename MatrixType::local_ordinal_type,
92  typename MatrixType::global_ordinal_type,
93  typename MatrixType::node_type> > {
94  public:
96  typedef typename MatrixType::scalar_type scalar_type;
97 
99  typedef typename MatrixType::local_ordinal_type local_ordinal_type;
100 
102  typedef typename MatrixType::global_ordinal_type global_ordinal_type;
103 
105  typedef typename MatrixType::node_type::device_type device_type;
106 
108  typedef typename MatrixType::node_type node_type;
109 
111  typedef typename Teuchos::ScalarTraits<scalar_type>::magnitudeType magnitude_type;
112 
117  typedef Tpetra::RowMatrix<scalar_type, local_ordinal_type,
118  global_ordinal_type, node_type>
119  row_matrix_type;
120 
121  static_assert(std::is_same<MatrixType, row_matrix_type>::value,
122  "Ifpack2::Hypre: MatrixType must be a Tpetra::RowMatrix "
123  "specialization. Don't use Tpetra::CrsMatrix here.");
124 
126  typedef Tpetra::CrsMatrix<scalar_type, local_ordinal_type,
127  global_ordinal_type, node_type>
128  crs_matrix_type;
129 
131  typedef Tpetra::Map<local_ordinal_type, global_ordinal_type, node_type> map_type;
132 
138  typedef Tpetra::Vector<scalar_type, local_ordinal_type,
139  global_ordinal_type, node_type>
140  vector_type;
141 
143  typedef Tpetra::MultiVector<scalar_type, local_ordinal_type,
144  global_ordinal_type, node_type>
145  multivector_type;
146 
147  explicit Hypre(const Teuchos::RCP<const row_matrix_type>& A) { throw NotImplemented("Ifpack2::Hypre only works when instantiated on <HYPRE_REAL, LocalOrdinal, HYPRE_Int, Node>"); }
148 
149  // @}
150  // @{ Construction methods
152  void initialize() {}
153 
155  bool isInitialized() const { return false; }
156 
158 
160  void compute() {}
161 
163  bool isComputed() const { return false; }
164 
165  void setParameters(const Teuchos::ParameterList& parameterlist) {}
166 
168 
169 
192  virtual void
193  setMatrix(const Teuchos::RCP<const row_matrix_type>& A) {}
195 
224  void
225  apply(const Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& X,
226  Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& Y,
228  scalar_type alpha = Teuchos::ScalarTraits<scalar_type>::one(),
229  scalar_type beta = Teuchos::ScalarTraits<scalar_type>::zero()) const {}
230 
232  Teuchos::RCP<const map_type> getDomainMap() const { return Teuchos::null; }
233 
235  Teuchos::RCP<const map_type> getRangeMap() const { return Teuchos::null; }
236 
238  bool hasTransposeApply() const { return false; }
239 
260  void
261  applyMat(const Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& X,
262  Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& Y,
263  Teuchos::ETransp mode = Teuchos::NO_TRANS) const {}
264 
265  Teuchos::RCP<const row_matrix_type> getMatrix() const { return Teuchos::null; }
266 
268 
270  int getNumInitialize() const { return 0; }
271 
273  int getNumCompute() const { return 0; }
274 
276  int getNumApply() const { return 0; }
277 
279  double getInitializeTime() const { return 0.0; }
280 
282  double getComputeTime() const { return 0.0; }
283 
285  double getApplyTime() const { return 0.0; }
286 };
287 
288 template <class LocalOrdinal, class Node>
289 class Hypre<Tpetra::RowMatrix<HYPRE_Real, LocalOrdinal, HYPRE_Int, Node> > : virtual public Ifpack2::Preconditioner<HYPRE_Real,
290  LocalOrdinal,
291  HYPRE_Int,
292  Node>,
293  virtual public Ifpack2::Details::CanChangeMatrix<Tpetra::RowMatrix<HYPRE_Real,
294  LocalOrdinal,
295  HYPRE_Int,
296  Node> > {
297  public:
299 
300 
302  typedef Tpetra::RowMatrix<HYPRE_Real, LocalOrdinal, HYPRE_Int, Node> MatrixType;
303 
305  typedef typename MatrixType::scalar_type scalar_type;
306 
308  typedef typename MatrixType::local_ordinal_type local_ordinal_type;
309 
311  typedef typename MatrixType::global_ordinal_type global_ordinal_type;
312 
314  typedef typename MatrixType::node_type::device_type device_type;
315 
317  typedef typename MatrixType::node_type node_type;
318 
320  typedef typename Teuchos::ScalarTraits<scalar_type>::magnitudeType magnitude_type;
321 
326  typedef Tpetra::RowMatrix<scalar_type, local_ordinal_type,
327  global_ordinal_type, node_type>
328  row_matrix_type;
329 
330  static_assert(std::is_same<MatrixType, row_matrix_type>::value,
331  "Ifpack2::Hypre: MatrixType must be a Tpetra::RowMatrix "
332  "specialization. Don't use Tpetra::CrsMatrix here.");
333 
335  typedef Tpetra::CrsMatrix<scalar_type, local_ordinal_type,
336  global_ordinal_type, node_type>
337  crs_matrix_type;
338 
340  typedef Tpetra::Map<local_ordinal_type, global_ordinal_type, node_type> map_type;
341 
347  typedef Tpetra::Vector<scalar_type, local_ordinal_type,
348  global_ordinal_type, node_type>
349  vector_type;
350 
352  typedef Tpetra::MultiVector<scalar_type, local_ordinal_type,
353  global_ordinal_type, node_type>
354  multivector_type;
355 
356  // Hypre Specs
357  // This will need to be either int or long long depending on how Hypre was built
358  // typedef global_ordinal_type global_ordinal_type;
359 
360  typedef global_ordinal_type (*HYPRE_PtrToParSolverFcn)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
361 
363  // \name Constructors and destructors
365 
387  explicit Hypre(const Teuchos::RCP<const row_matrix_type>& A);
388 
390  ~Hypre();
391 
392  // @}
393  // @{ Construction methods
395  void initialize();
396 
398  bool isInitialized() const { return (IsInitialized_); }
399 
401 
403  void compute();
404 
406  bool isComputed() const { return (IsComputed_); }
407 
409  /* This method is only available if the Teuchos package is enabled.
410  This method recognizes six parameter names: Solver,
411  Preconditioner, SolveOrPrecondition, SetPreconditioner, NumFunctions and Functions. These names are
412  case sensitive. Solver requires an enumerated parameter of type Hypre_Solver. Preconditioner is similar
413  except requires the type be a preconditioner. The options are listed below:
414  Solvers Preconditioners
415  BoomerAMG BoomerAMG
416  AMS ParaSails
417  Hybrid AMS
418  PCG (Default) Euclid (Default)
419  GMRES
420  FlexGMRES
421  LGMRES
422  BiCGSTAB
423  SolveOrPrecondition takes enumerated type Hypre_Chooser, Solver will solve the system, Preconditioner will apply the preconditioner.
424  SetPreconditioner takes a boolean, true means the solver will use the preconditioner.
425  NumFunctions takes an int that describes how many parameters will be passed into Functions. (This needs to be correct.)
426  Functions takes an array of Ref Counted Pointers to an object called FunctionParameter. This class is implemented in Ifpack_Hypre.h.
427  The object takes whether it is Solver or Preconditioner that we are setting a parameter for.
428  The function in Hypre that sets the parameter, and the parameters for that function. An example is below:
429 
430  RCP<FunctionParameter> functs[2];
431  functs[0] = rcp(new FunctionParameter(Solver, &HYPRE_PCGSetMaxIter, 1000)); // max iterations
432  functs[1] = rcp(new FunctionParameter(Solver, &HYPRE_PCGSetTol, 1e-7)); // conv. tolerance
433  list.set("NumFunctions", 2);
434  list.set<RCP<FunctionParameter>*>("Functions", functs);
435  NOTE: SetParameters() must be called to use ApplyInverse(), the solvers will not be created otherwise. An empty list is acceptable to use defaults.
436  */
437  void setParameters(const Teuchos::ParameterList& parameterlist);
438 
440 
448  int SetParameter(Hypre_Chooser chooser, HYPRE_Int (*pt2Func)(HYPRE_Solver, HYPRE_Int), HYPRE_Int parameter);
449 
451 
459  int SetParameter(Hypre_Chooser chooser, HYPRE_Int (*pt2Func)(HYPRE_Solver, HYPRE_Real), HYPRE_Real parameter);
460 
462 
471  int SetParameter(Hypre_Chooser chooser, HYPRE_Int (*pt2Func)(HYPRE_Solver, HYPRE_Real, HYPRE_Int), HYPRE_Real parameter1, HYPRE_Int parameter2);
472 
474 
483  int SetParameter(Hypre_Chooser chooser, HYPRE_Int (*pt2Func)(HYPRE_Solver, HYPRE_Int, HYPRE_Real), HYPRE_Int parameter1, HYPRE_Real parameter2);
484 
486 
495  int SetParameter(Hypre_Chooser chooser, HYPRE_Int (*pt2Func)(HYPRE_Solver, HYPRE_Int, HYPRE_Int), HYPRE_Int parameter1, HYPRE_Int parameter2);
496 
498 
506  int SetParameter(Hypre_Chooser chooser, HYPRE_Int (*pt2Func)(HYPRE_Solver, HYPRE_Real*), HYPRE_Real* parameter);
507 
509 
517  int SetParameter(Hypre_Chooser chooser, HYPRE_Int (*pt2Func)(HYPRE_Solver, HYPRE_Int*), HYPRE_Int* parameter);
518 
520 
529  int SetParameter(Hypre_Chooser chooser, HYPRE_Int (*pt2Func)(HYPRE_Solver, HYPRE_Int**), HYPRE_Int** parameter);
530 
532 
541  int SetParameter(Hypre_Chooser chooser, Hypre_Solver Solver);
542 
544 
552  int SetParameter(bool UsePreconditioner) {
553  UsePreconditioner_ = UsePreconditioner;
554  return 0;
555  }
556 
558 
564  int SetParameter(Hypre_Chooser chooser) {
565  SolveOrPrec_ = chooser;
566  return 0;
567  }
568 
570  int SetCoordinates(Teuchos::RCP<multivector_type> coords);
571 
573  int SetDiscreteGradient(Teuchos::RCP<const crs_matrix_type> G);
574 
576  int CallFunctions() const;
577 
579 
581 
604  virtual void
605  setMatrix(const Teuchos::RCP<const row_matrix_type>& A);
607 
636  void
637  apply(const Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& X,
638  Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& Y,
640  scalar_type alpha = Teuchos::ScalarTraits<scalar_type>::one(),
641  scalar_type beta = Teuchos::ScalarTraits<scalar_type>::zero()) const;
642 
644  Teuchos::RCP<const map_type> getDomainMap() const;
645 
647  Teuchos::RCP<const map_type> getRangeMap() const;
648 
650  bool hasTransposeApply() const;
651 
672  void
673  applyMat(const Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& X,
674  Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& Y,
675  Teuchos::ETransp mode = Teuchos::NO_TRANS) const;
676 
678 
680 
682  Teuchos::RCP<const Teuchos::Comm<int> > getComm() const;
683 
685  Teuchos::RCP<const row_matrix_type> getMatrix() const;
686 
692  getCrsMatrix() const;
693 
695  int getNumInitialize() const;
696 
698  int getNumCompute() const;
699 
701  int getNumApply() const;
702 
704  double getInitializeTime() const;
705 
707  double getComputeTime() const;
708 
710  double getApplyTime() const;
711 
713 
715 
717  std::string description() const;
718 
721 
723 
724  private:
725  // @{ Private methods
726 
729 
731  typedef Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type> MV;
732 
734  Hypre(const Hypre<MatrixType>&);
735 
737  Hypre<MatrixType>& operator=(const Hypre<MatrixType>&);
738 
740  int SetSolverType(Hypre_Solver solver);
741 
743  int SetPrecondType(Hypre_Solver precond);
744 
746  int CreateSolver();
747 
749  int CreatePrecond();
750 
752  int CopyTpetraToHypre();
753 
755  int AddFunToList(Teuchos::RCP<FunctionParameter> NewFun);
756 
758  HYPRE_Int Hypre_BoomerAMGCreate(MPI_Comm comm, HYPRE_Solver* solver);
759 
761  HYPRE_Int Hypre_ParaSailsCreate(MPI_Comm comm, HYPRE_Solver* solver);
762 
764  HYPRE_Int Hypre_EuclidCreate(MPI_Comm comm, HYPRE_Solver* solver);
765 
767  HYPRE_Int Hypre_AMSCreate(MPI_Comm comm, HYPRE_Solver* solver);
768 
770  HYPRE_Int Hypre_ParCSRHybridCreate(MPI_Comm comm, HYPRE_Solver* solver);
771 
773  HYPRE_Int Hypre_ParCSRPCGCreate(MPI_Comm comm, HYPRE_Solver* solver);
774 
776  HYPRE_Int Hypre_ParCSRGMRESCreate(MPI_Comm comm, HYPRE_Solver* solver);
777 
779  HYPRE_Int Hypre_ParCSRFlexGMRESCreate(MPI_Comm comm, HYPRE_Solver* solver);
780 
782  HYPRE_Int Hypre_ParCSRLGMRESCreate(MPI_Comm comm, HYPRE_Solver* solver);
783 
785  HYPRE_Int Hypre_ParCSRBiCGSTABCreate(MPI_Comm comm, HYPRE_Solver* solver);
786 
789  MakeContiguousColumnMap(Teuchos::RCP<const crs_matrix_type>& Matrix) const;
790 
792  void Destroy();
793 
794  // @}
795  // @{ Internal data
800 
802  bool IsInitialized_;
804  bool IsComputed_;
806  int NumInitialize_;
808  int NumCompute_;
813  mutable int NumApply_;
815  double InitializeTime_;
817  double ComputeTime_;
822  mutable double ApplyTime_;
824  double ComputeFlops_;
829  mutable double ApplyFlops_;
830 
832  mutable HYPRE_IJMatrix HypreA_;
834  mutable HYPRE_ParCSRMatrix ParMatrix_;
835 
839  mutable HYPRE_IJMatrix HypreG_;
841  mutable HYPRE_ParCSRMatrix ParMatrixG_;
842 
844  mutable HYPRE_IJVector XHypre_;
846  mutable HYPRE_IJVector YHypre_;
847  mutable HYPRE_ParVector ParX_;
848  mutable HYPRE_ParVector ParY_;
849  mutable Teuchos::RCP<hypre_ParVector> XVec_;
850  mutable Teuchos::RCP<hypre_ParVector> YVec_;
851 
853  mutable HYPRE_IJVector xHypre_;
854  mutable HYPRE_IJVector yHypre_;
855  mutable HYPRE_IJVector zHypre_;
856  mutable HYPRE_ParVector xPar_;
857  mutable HYPRE_ParVector yPar_;
858  mutable HYPRE_ParVector zPar_;
859 
861  mutable HYPRE_Solver Solver_;
863  mutable HYPRE_Solver Preconditioner_;
864  // The following are pointers to functions to use the solver and preconditioner.
865  HYPRE_Int (Hypre::*SolverCreatePtr_)(MPI_Comm, HYPRE_Solver*);
866  HYPRE_Int (*SolverDestroyPtr_)(HYPRE_Solver);
867  HYPRE_Int (*SolverSetupPtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
868  HYPRE_Int (*SolverSolvePtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
869  HYPRE_Int (*SolverPrecondPtr_)(HYPRE_Solver, HYPRE_PtrToParSolverFcn, HYPRE_PtrToParSolverFcn, HYPRE_Solver);
870  HYPRE_Int (Hypre::*PrecondCreatePtr_)(MPI_Comm, HYPRE_Solver*);
871  HYPRE_Int (*PrecondDestroyPtr_)(HYPRE_Solver);
872  HYPRE_Int (*PrecondSetupPtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
873  HYPRE_Int (*PrecondSolvePtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
874 
875  bool IsSolverCreated_;
876  bool IsPrecondCreated_;
878  Hypre_Chooser SolveOrPrec_;
880  Teuchos::RCP<const map_type> GloballyContiguousRowMap_;
881  Teuchos::RCP<const map_type> GloballyContiguousColMap_;
882  Teuchos::RCP<const map_type> GloballyContiguousNodeRowMap_;
883  Teuchos::RCP<const map_type> GloballyContiguousNodeColMap_;
885  int NumFunsToCall_;
887  Hypre_Solver SolverType_;
889  Hypre_Solver PrecondType_;
891  bool UsePreconditioner_;
893  std::vector<Teuchos::RCP<FunctionParameter> > FunsToCall_;
895  bool Dump_;
897  mutable Teuchos::ArrayRCP<scalar_type> VectorCache_;
898 };
899 
900 } // namespace Ifpack2
901 
902 #endif // HAVE_IFPACK2_HYPRE && HAVE_IFPACK2_MPI
903 #endif /* IFPACK2_HYPRE_DECL_HPP */
Mix-in interface for preconditioners that can change their matrix after construction.
Definition: Ifpack2_Details_CanChangeMatrix.hpp:60
Interface for all Ifpack2 preconditioners.
Definition: Ifpack2_Preconditioner.hpp:74
Declaration of interface for preconditioners that can change their matrix after construction.
static const EVerbosityLevel verbLevel_default
Uses AztecOO&#39;s GMRES.
Definition: Ifpack2_CondestType.hpp:20