Ifpack2 Templated Preconditioning Package Version 1.0
Loading...
Searching...
No Matches
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)
66struct hypre_IJMatrix_struct;
67typedef struct hypre_IJMatrix_struct *HYPRE_IJMatrix;
68struct hypre_IJVector_struct;
69typedef struct hypre_IJVector_struct *HYPRE_IJVector;
70struct hypre_ParCSRMatrix_struct;
71typedef struct hypre_ParCSRMatrix_struct* HYPRE_ParCSRMatrix;
72struct hypre_ParVector_struct;
73typedef struct hypre_ParVector_struct * HYPRE_ParVector;
74struct hypre_Solver_struct;
75typedef struct hypre_Solver_struct *HYPRE_Solver;
76struct hypre_ParVector_struct;
77typedef struct hypre_ParVector_struct hypre_ParVector;
78//struct hypre_Vector;
79
80namespace Ifpack2 {
81
82
83#ifndef HYPRE_ENUMS
84#define HYPRE_ENUMS
86 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
112
113template<class MatrixType>
114class 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{
124public:
125public:
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, double), global_ordinal_type parameter1, double parameter2);
309
311
320 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);
321
323
331 int SetParameter(Hypre_Chooser chooser, global_ordinal_type (*pt2Func)(HYPRE_Solver, double*), double* parameter);
332
334
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, global_ordinal_type (*pt2Func)(HYPRE_Solver, global_ordinal_type**), global_ordinal_type** parameter);
355
357
365
366 int SetParameter(Hypre_Chooser chooser, Hypre_Solver Solver);
367
369
376
377 int SetParameter(bool UsePreconditioner){ UsePreconditioner_ = UsePreconditioner; return 0;}
378
380
386 int SetParameter(Hypre_Chooser chooser) { SolveOrPrec_ = chooser; return 0;}
387
389 int SetCoordinates(Teuchos::RCP<multivector_type> coords);
390
392 int SetDiscreteGradient(Teuchos::RCP<const crs_matrix_type> G);
393
395 int CallFunctions() const;
396
398
400
423 virtual void
424 setMatrix (const Teuchos::RCP<const row_matrix_type>& A);
426
455 void
456 apply (const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
457 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
458 Teuchos::ETransp mode = Teuchos::NO_TRANS,
459 scalar_type alpha = Teuchos::ScalarTraits<scalar_type>::one(),
460 scalar_type beta = Teuchos::ScalarTraits<scalar_type>::zero()) const;
461
463 Teuchos::RCP<const map_type> getDomainMap() const;
464
466 Teuchos::RCP<const map_type> getRangeMap() const;
467
469 bool hasTransposeApply() const;
470
491 void
492 applyMat (const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
493 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
494 Teuchos::ETransp mode = Teuchos::NO_TRANS) const;
495
497
499
501 Teuchos::RCP<const Teuchos::Comm<int> > getComm() const;
502
504 Teuchos::RCP<const row_matrix_type> getMatrix() const;
505
510 Teuchos::RCP<const Tpetra::CrsMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type> >
511 getCrsMatrix() const;
512
514 int getNumInitialize() const;
515
517 int getNumCompute() const;
518
520 int getNumApply() const;
521
523 double getInitializeTime() const;
524
526 double getComputeTime() const;
527
529 double getApplyTime() const;
530
532
534
536 std::string description() const;
537
539 void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const;
540
542
543private:
544 // @{ Private methods
545
547 typedef Teuchos::ScalarTraits<typename MatrixType::scalar_type> STS;
548
550 typedef Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type> MV;
551
553 Hypre(const Hypre<MatrixType>&);
554
556 Hypre<MatrixType>& operator= (const Hypre<MatrixType>&);
557
559 int SetSolverType(Hypre_Solver solver);
560
562 int SetPrecondType(Hypre_Solver precond);
563
565 int CreateSolver();
566
568 int CreatePrecond();
569
571 int CopyTpetraToHypre();
572
574 int AddFunToList(Teuchos::RCP<FunctionParameter> NewFun);
575
577 int Hypre_BoomerAMGCreate(MPI_Comm comm, HYPRE_Solver *solver);
578
580 int Hypre_ParaSailsCreate(MPI_Comm comm, HYPRE_Solver *solver);
581
583 int Hypre_EuclidCreate(MPI_Comm comm, HYPRE_Solver *solver);
584
586 int Hypre_AMSCreate(MPI_Comm comm, HYPRE_Solver *solver);
587
589 int Hypre_ParCSRHybridCreate(MPI_Comm comm, HYPRE_Solver *solver);
590
592 int Hypre_ParCSRPCGCreate(MPI_Comm comm, HYPRE_Solver *solver);
593
595 int Hypre_ParCSRGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver);
596
598 int Hypre_ParCSRFlexGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver);
599
601 int Hypre_ParCSRLGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver);
602
604 int Hypre_ParCSRBiCGSTABCreate(MPI_Comm comm, HYPRE_Solver *solver);
605
607 Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type, typename MatrixType::global_ordinal_type, typename MatrixType::node_type> >
608 MakeContiguousColumnMap(Teuchos::RCP<const crs_matrix_type> &Matrix) const;
609
611 void Destroy();
612
613 // @}
614 // @{ Internal data
616 Teuchos::RCP<const MatrixType> A_;
618 Teuchos::ParameterList List_;
619
621 bool IsInitialized_;
623 bool IsComputed_;
625 int NumInitialize_;
627 int NumCompute_;
632 mutable int NumApply_;
634 double InitializeTime_;
636 double ComputeTime_;
641 mutable double ApplyTime_;
643 double ComputeFlops_;
648 mutable double ApplyFlops_;
649
650
652 mutable HYPRE_IJMatrix HypreA_;
654 mutable HYPRE_ParCSRMatrix ParMatrix_;
655
657 Teuchos::RCP<const crs_matrix_type> G_;
659 mutable HYPRE_IJMatrix HypreG_;
661 mutable HYPRE_ParCSRMatrix ParMatrixG_;
662
664 mutable HYPRE_IJVector XHypre_;
666 mutable HYPRE_IJVector YHypre_;
667 mutable HYPRE_ParVector ParX_;
668 mutable HYPRE_ParVector ParY_;
669 mutable Teuchos::RCP<hypre_ParVector> XVec_;
670 mutable Teuchos::RCP<hypre_ParVector> YVec_;
671
672 Teuchos::RCP<multivector_type> Coords_;
673 mutable HYPRE_IJVector xHypre_;
674 mutable HYPRE_IJVector yHypre_;
675 mutable HYPRE_IJVector zHypre_;
676 mutable HYPRE_ParVector xPar_;
677 mutable HYPRE_ParVector yPar_;
678 mutable HYPRE_ParVector zPar_;
679
681 mutable HYPRE_Solver Solver_;
683 mutable HYPRE_Solver Preconditioner_;
684 // The following are pointers to functions to use the solver and preconditioner.
685 int (Hypre::*SolverCreatePtr_)(MPI_Comm, HYPRE_Solver*);
686 global_ordinal_type (*SolverDestroyPtr_)(HYPRE_Solver);
687 global_ordinal_type (*SolverSetupPtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
688 global_ordinal_type (*SolverSolvePtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
689 global_ordinal_type (*SolverPrecondPtr_)(HYPRE_Solver, HYPRE_PtrToParSolverFcn, HYPRE_PtrToParSolverFcn, HYPRE_Solver);
690 int (Hypre::*PrecondCreatePtr_)(MPI_Comm, HYPRE_Solver*);
691 global_ordinal_type (*PrecondDestroyPtr_)(HYPRE_Solver);
692 global_ordinal_type (*PrecondSetupPtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
693 global_ordinal_type (*PrecondSolvePtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
694
695 bool IsSolverCreated_;
696 bool IsPrecondCreated_;
698 Hypre_Chooser SolveOrPrec_;
700 Teuchos::RCP<const map_type> GloballyContiguousRowMap_;
701 Teuchos::RCP<const map_type> GloballyContiguousColMap_;
702 Teuchos::RCP<const map_type> GloballyContiguousNodeRowMap_;
703 Teuchos::RCP<const map_type> GloballyContiguousNodeColMap_;
705 int NumFunsToCall_;
707 Hypre_Solver SolverType_;
709 Hypre_Solver PrecondType_;
711 bool UsePreconditioner_;
713 std::vector<Teuchos::RCP<FunctionParameter> > FunsToCall_;
715 bool Dump_;
717 mutable Teuchos::ArrayRCP<double> VectorCache_;
718
719};
720
721
722}//end Ifpack2 namespace
723
724#endif // HAVE_IFPACK2_HYPRE && HAVE_IFPACK2_MPI
725#endif /* IFPACK2_HYPRE_DECL_HPP */
Declaration of interface for preconditioners that can change their matrix after construction.
Preconditioners and smoothers for Tpetra sparse matrices.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:74
@ GMRES
Uses AztecOO's GMRES.
Definition Ifpack2_CondestType.hpp:53