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 
51 #ifndef HYPRE_ENUMS
52 #define HYPRE_ENUMS
53  enum Hypre_Solver{
55  BoomerAMG,
56  ParaSails,
57  Euclid,
58  AMS,
59  Hybrid,
60  PCG,
61  GMRES,
62  FlexGMRES,
63  LGMRES,
64  BiCGSTAB
65  };
66 
68  enum Hypre_Chooser{
69  Hypre_Is_Solver,
70  Hypre_Is_Preconditioner
71  };
72 #endif //HYPRE_ENUMS
73 
74 
75 class NotImplemented : public Teuchos::ExceptionBase {
76 public:
77  NotImplemented(const std::string& what_arg) : Teuchos::ExceptionBase(what_arg) {}
78 };
79 
81 
86 template<class MatrixType>
87 class Hypre:
88  virtual public Ifpack2::Preconditioner<typename MatrixType::scalar_type,
89  typename MatrixType::local_ordinal_type,
90  typename MatrixType::global_ordinal_type,
91  typename MatrixType::node_type>,
92  virtual public Ifpack2::Details::CanChangeMatrix<Tpetra::RowMatrix<typename MatrixType::scalar_type,
93  typename MatrixType::local_ordinal_type,
94  typename MatrixType::global_ordinal_type,
95  typename MatrixType::node_type> >
96 {
97 public:
98 
100  typedef typename MatrixType::scalar_type scalar_type;
101 
103  typedef typename MatrixType::local_ordinal_type local_ordinal_type;
104 
106  typedef typename MatrixType::global_ordinal_type global_ordinal_type;
107 
109  typedef typename MatrixType::node_type::device_type device_type;
110 
112  typedef typename MatrixType::node_type node_type;
113 
115  typedef typename Teuchos::ScalarTraits<scalar_type>::magnitudeType magnitude_type;
116 
121  typedef Tpetra::RowMatrix<scalar_type, local_ordinal_type,
122  global_ordinal_type, node_type> row_matrix_type;
123 
124  static_assert (std::is_same<MatrixType, row_matrix_type>::value,
125  "Ifpack2::Hypre: MatrixType must be a Tpetra::RowMatrix "
126  "specialization. Don't use Tpetra::CrsMatrix here.");
127 
128 
130  typedef Tpetra::CrsMatrix<scalar_type, local_ordinal_type,
131  global_ordinal_type, node_type> crs_matrix_type;
132 
134  typedef Tpetra::Map<local_ordinal_type, global_ordinal_type, node_type> map_type;
135 
141  typedef Tpetra::Vector<scalar_type, local_ordinal_type,
142  global_ordinal_type, node_type> vector_type;
143 
145  typedef Tpetra::MultiVector<scalar_type, local_ordinal_type,
146  global_ordinal_type, node_type> multivector_type;
147 
148 
149  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>"); }
150 
151  // @}
152  // @{ Construction methods
154  void initialize() {}
155 
157  bool isInitialized() const{ return false;}
158 
160 
162  void compute() {}
163 
165  bool isComputed() const{ return false;}
166 
167  void setParameters(const Teuchos::ParameterList& parameterlist) {}
168 
169 
171 
172 
195  virtual void
196  setMatrix (const Teuchos::RCP<const row_matrix_type>& A) {}
198 
227  void
228  apply (const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
229  Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
231  scalar_type alpha = Teuchos::ScalarTraits<scalar_type>::one(),
232  scalar_type beta = Teuchos::ScalarTraits<scalar_type>::zero()) const {}
233 
235  Teuchos::RCP<const map_type> getDomainMap() const { return Teuchos::null; }
236 
238  Teuchos::RCP<const map_type> getRangeMap() const { return Teuchos::null; }
239 
241  bool hasTransposeApply() const { return false; }
242 
263  void
264  applyMat (const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
265  Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
266  Teuchos::ETransp mode = Teuchos::NO_TRANS) const {}
267 
268  Teuchos::RCP<const row_matrix_type> getMatrix() const { return Teuchos::null; }
269 
271 
273  int getNumInitialize() const { return 0; }
274 
276  int getNumCompute() const { return 0; }
277 
279  int getNumApply() const { return 0; }
280 
282  double getInitializeTime() const { return 0.0; }
283 
285  double getComputeTime() const { return 0.0; }
286 
288  double getApplyTime() const { return 0.0; }
289 
290 
291 };
292 
293 template<class LocalOrdinal, class Node>
294 class Hypre<Tpetra::RowMatrix<HYPRE_Real, LocalOrdinal, HYPRE_Int, Node> >:
295  virtual public Ifpack2::Preconditioner<HYPRE_Real,
296  LocalOrdinal,
297  HYPRE_Int,
298  Node>,
299  virtual public Ifpack2::Details::CanChangeMatrix<Tpetra::RowMatrix<HYPRE_Real,
300  LocalOrdinal,
301  HYPRE_Int,
302  Node> >
303 {
304 public:
306 
307 
309  typedef Tpetra::RowMatrix<HYPRE_Real, LocalOrdinal, HYPRE_Int, Node> MatrixType;
310 
312  typedef typename MatrixType::scalar_type scalar_type;
313 
315  typedef typename MatrixType::local_ordinal_type local_ordinal_type;
316 
318  typedef typename MatrixType::global_ordinal_type global_ordinal_type;
319 
321  typedef typename MatrixType::node_type::device_type device_type;
322 
324  typedef typename MatrixType::node_type node_type;
325 
327  typedef typename Teuchos::ScalarTraits<scalar_type>::magnitudeType magnitude_type;
328 
333  typedef Tpetra::RowMatrix<scalar_type, local_ordinal_type,
334  global_ordinal_type, node_type> row_matrix_type;
335 
336  static_assert (std::is_same<MatrixType, row_matrix_type>::value,
337  "Ifpack2::Hypre: MatrixType must be a Tpetra::RowMatrix "
338  "specialization. Don't use Tpetra::CrsMatrix here.");
339 
340 
342  typedef Tpetra::CrsMatrix<scalar_type, local_ordinal_type,
343  global_ordinal_type, node_type> crs_matrix_type;
344 
346  typedef Tpetra::Map<local_ordinal_type, global_ordinal_type, node_type> map_type;
347 
353  typedef Tpetra::Vector<scalar_type, local_ordinal_type,
354  global_ordinal_type, node_type> vector_type;
355 
357  typedef Tpetra::MultiVector<scalar_type, local_ordinal_type,
358  global_ordinal_type, node_type> multivector_type;
359 
360  // Hypre Specs
361  // This will need to be either int or long long depending on how Hypre was built
362  // typedef global_ordinal_type global_ordinal_type;
363 
364  typedef global_ordinal_type (*HYPRE_PtrToParSolverFcn)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
365 
367  // \name Constructors and destructors
369 
391  explicit Hypre(const Teuchos::RCP<const row_matrix_type>& A);
392 
394  ~Hypre();
395 
396  // @}
397  // @{ Construction methods
399  void initialize();
400 
402  bool isInitialized() const{ return(IsInitialized_);}
403 
405 
407  void compute();
408 
410  bool isComputed() const{ return(IsComputed_);}
411 
413  /* This method is only available if the Teuchos package is enabled.
414  This method recognizes six parameter names: Solver,
415  Preconditioner, SolveOrPrecondition, SetPreconditioner, NumFunctions and Functions. These names are
416  case sensitive. Solver requires an enumerated parameter of type Hypre_Solver. Preconditioner is similar
417  except requires the type be a preconditioner. The options are listed below:
418  Solvers Preconditioners
419  BoomerAMG BoomerAMG
420  AMS ParaSails
421  Hybrid AMS
422  PCG (Default) Euclid (Default)
423  GMRES
424  FlexGMRES
425  LGMRES
426  BiCGSTAB
427  SolveOrPrecondition takes enumerated type Hypre_Chooser, Solver will solve the system, Preconditioner will apply the preconditioner.
428  SetPreconditioner takes a boolean, true means the solver will use the preconditioner.
429  NumFunctions takes an int that describes how many parameters will be passed into Functions. (This needs to be correct.)
430  Functions takes an array of Ref Counted Pointers to an object called FunctionParameter. This class is implemented in Ifpack_Hypre.h.
431  The object takes whether it is Solver or Preconditioner that we are setting a parameter for.
432  The function in Hypre that sets the parameter, and the parameters for that function. An example is below:
433 
434  RCP<FunctionParameter> functs[2];
435  functs[0] = rcp(new FunctionParameter(Solver, &HYPRE_PCGSetMaxIter, 1000)); // max iterations
436  functs[1] = rcp(new FunctionParameter(Solver, &HYPRE_PCGSetTol, 1e-7)); // conv. tolerance
437  list.set("NumFunctions", 2);
438  list.set<RCP<FunctionParameter>*>("Functions", functs);
439  NOTE: SetParameters() must be called to use ApplyInverse(), the solvers will not be created otherwise. An empty list is acceptable to use defaults.
440  */
441  void setParameters(const Teuchos::ParameterList& parameterlist);
442 
444 
452  int SetParameter(Hypre_Chooser chooser, HYPRE_Int (*pt2Func)(HYPRE_Solver, HYPRE_Int), HYPRE_Int parameter);
453 
455 
463  int SetParameter(Hypre_Chooser chooser, HYPRE_Int (*pt2Func)(HYPRE_Solver, HYPRE_Real), HYPRE_Real parameter);
464 
466 
475  int SetParameter(Hypre_Chooser chooser, HYPRE_Int (*pt2Func)(HYPRE_Solver, HYPRE_Real, HYPRE_Int), HYPRE_Real parameter1, HYPRE_Int parameter2);
476 
478 
487  int SetParameter(Hypre_Chooser chooser, HYPRE_Int (*pt2Func)(HYPRE_Solver, HYPRE_Int, HYPRE_Real), HYPRE_Int parameter1, HYPRE_Real parameter2);
488 
490 
499  int SetParameter(Hypre_Chooser chooser, HYPRE_Int (*pt2Func)(HYPRE_Solver, HYPRE_Int, HYPRE_Int), HYPRE_Int parameter1, HYPRE_Int parameter2);
500 
502 
510  int SetParameter(Hypre_Chooser chooser, HYPRE_Int (*pt2Func)(HYPRE_Solver, HYPRE_Real*), HYPRE_Real* parameter);
511 
513 
521  int SetParameter(Hypre_Chooser chooser, HYPRE_Int (*pt2Func)(HYPRE_Solver, HYPRE_Int*), HYPRE_Int* parameter);
522 
524 
533  int SetParameter(Hypre_Chooser chooser, HYPRE_Int (*pt2Func)(HYPRE_Solver, HYPRE_Int**), HYPRE_Int** parameter);
534 
536 
545  int SetParameter(Hypre_Chooser chooser, Hypre_Solver Solver);
546 
548 
556  int SetParameter(bool UsePreconditioner){ UsePreconditioner_ = UsePreconditioner; return 0;}
557 
559 
565  int SetParameter(Hypre_Chooser chooser) { SolveOrPrec_ = chooser; return 0;}
566 
568  int SetCoordinates(Teuchos::RCP<multivector_type> coords);
569 
571  int SetDiscreteGradient(Teuchos::RCP<const crs_matrix_type> G);
572 
574  int CallFunctions() const;
575 
577 
579 
602  virtual void
603  setMatrix (const Teuchos::RCP<const row_matrix_type>& A);
605 
634  void
635  apply (const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
636  Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
638  scalar_type alpha = Teuchos::ScalarTraits<scalar_type>::one(),
639  scalar_type beta = Teuchos::ScalarTraits<scalar_type>::zero()) const;
640 
642  Teuchos::RCP<const map_type> getDomainMap() const;
643 
645  Teuchos::RCP<const map_type> getRangeMap() const;
646 
648  bool hasTransposeApply() const;
649 
670  void
671  applyMat (const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
672  Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
673  Teuchos::ETransp mode = Teuchos::NO_TRANS) const;
674 
676 
678 
680  Teuchos::RCP<const Teuchos::Comm<int> > getComm() const;
681 
683  Teuchos::RCP<const row_matrix_type> getMatrix() const;
684 
690  getCrsMatrix() const;
691 
693  int getNumInitialize() const;
694 
696  int getNumCompute() const;
697 
699  int getNumApply() const;
700 
702  double getInitializeTime() const;
703 
705  double getComputeTime() const;
706 
708  double getApplyTime() const;
709 
711 
713 
715  std::string description() const;
716 
719 
721 
722 private:
723  // @{ Private methods
724 
727 
729  typedef Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type> MV;
730 
732  Hypre(const Hypre<MatrixType>&);
733 
735  Hypre<MatrixType>& operator= (const Hypre<MatrixType>&);
736 
738  int SetSolverType(Hypre_Solver solver);
739 
741  int SetPrecondType(Hypre_Solver precond);
742 
744  int CreateSolver();
745 
747  int CreatePrecond();
748 
750  int CopyTpetraToHypre();
751 
753  int AddFunToList(Teuchos::RCP<FunctionParameter> NewFun);
754 
756  HYPRE_Int Hypre_BoomerAMGCreate(MPI_Comm comm, HYPRE_Solver *solver);
757 
759  HYPRE_Int Hypre_ParaSailsCreate(MPI_Comm comm, HYPRE_Solver *solver);
760 
762  HYPRE_Int Hypre_EuclidCreate(MPI_Comm comm, HYPRE_Solver *solver);
763 
765  HYPRE_Int Hypre_AMSCreate(MPI_Comm comm, HYPRE_Solver *solver);
766 
768  HYPRE_Int Hypre_ParCSRHybridCreate(MPI_Comm comm, HYPRE_Solver *solver);
769 
771  HYPRE_Int Hypre_ParCSRPCGCreate(MPI_Comm comm, HYPRE_Solver *solver);
772 
774  HYPRE_Int Hypre_ParCSRGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver);
775 
777  HYPRE_Int Hypre_ParCSRFlexGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver);
778 
780  HYPRE_Int Hypre_ParCSRLGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver);
781 
783  HYPRE_Int Hypre_ParCSRBiCGSTABCreate(MPI_Comm comm, HYPRE_Solver *solver);
784 
787  MakeContiguousColumnMap(Teuchos::RCP<const crs_matrix_type> &Matrix) const;
788 
790  void Destroy();
791 
792  // @}
793  // @{ Internal data
798 
800  bool IsInitialized_;
802  bool IsComputed_;
804  int NumInitialize_;
806  int NumCompute_;
811  mutable int NumApply_;
813  double InitializeTime_;
815  double ComputeTime_;
820  mutable double ApplyTime_;
822  double ComputeFlops_;
827  mutable double ApplyFlops_;
828 
829 
831  mutable HYPRE_IJMatrix HypreA_;
833  mutable HYPRE_ParCSRMatrix ParMatrix_;
834 
838  mutable HYPRE_IJMatrix HypreG_;
840  mutable HYPRE_ParCSRMatrix ParMatrixG_;
841 
843  mutable HYPRE_IJVector XHypre_;
845  mutable HYPRE_IJVector YHypre_;
846  mutable HYPRE_ParVector ParX_;
847  mutable HYPRE_ParVector ParY_;
848  mutable Teuchos::RCP<hypre_ParVector> XVec_;
849  mutable Teuchos::RCP<hypre_ParVector> YVec_;
850 
852  mutable HYPRE_IJVector xHypre_;
853  mutable HYPRE_IJVector yHypre_;
854  mutable HYPRE_IJVector zHypre_;
855  mutable HYPRE_ParVector xPar_;
856  mutable HYPRE_ParVector yPar_;
857  mutable HYPRE_ParVector zPar_;
858 
860  mutable HYPRE_Solver Solver_;
862  mutable HYPRE_Solver Preconditioner_;
863  // The following are pointers to functions to use the solver and preconditioner.
864  HYPRE_Int (Hypre::*SolverCreatePtr_)(MPI_Comm, HYPRE_Solver*);
865  HYPRE_Int (*SolverDestroyPtr_)(HYPRE_Solver);
866  HYPRE_Int (*SolverSetupPtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
867  HYPRE_Int (*SolverSolvePtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
868  HYPRE_Int (*SolverPrecondPtr_)(HYPRE_Solver, HYPRE_PtrToParSolverFcn, HYPRE_PtrToParSolverFcn, HYPRE_Solver);
869  HYPRE_Int (Hypre::*PrecondCreatePtr_)(MPI_Comm, HYPRE_Solver*);
870  HYPRE_Int (*PrecondDestroyPtr_)(HYPRE_Solver);
871  HYPRE_Int (*PrecondSetupPtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
872  HYPRE_Int (*PrecondSolvePtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
873 
874  bool IsSolverCreated_;
875  bool IsPrecondCreated_;
877  Hypre_Chooser SolveOrPrec_;
879  Teuchos::RCP<const map_type> GloballyContiguousRowMap_;
880  Teuchos::RCP<const map_type> GloballyContiguousColMap_;
881  Teuchos::RCP<const map_type> GloballyContiguousNodeRowMap_;
882  Teuchos::RCP<const map_type> GloballyContiguousNodeColMap_;
884  int NumFunsToCall_;
886  Hypre_Solver SolverType_;
888  Hypre_Solver PrecondType_;
890  bool UsePreconditioner_;
892  std::vector<Teuchos::RCP<FunctionParameter> > FunsToCall_;
894  bool Dump_;
896  mutable Teuchos::ArrayRCP<scalar_type> VectorCache_;
897 
898 };
899 
900 
901 }//end Ifpack2 namespace
902 
903 #endif // HAVE_IFPACK2_HYPRE && HAVE_IFPACK2_MPI
904 #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