Ifpack Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Ifpack_Hypre.h
Go to the documentation of this file.
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack: Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2002) 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 IFPACK_HYPRE_H
44 #define IFPACK_HYPRE_H
45 
46 #include "Ifpack_ConfigDefs.h"
47 #ifdef HAVE_HYPRE
48 
49 #include "Ifpack_Preconditioner.h"
50 #include "Ifpack_Condest.h"
51 #include "Ifpack_ScalingType.h"
52 #include "Epetra_CompObject.h"
53 #include "Epetra_MultiVector.h"
54 #include "Epetra_Vector.h"
55 #include "Epetra_CrsGraph.h"
56 #include "Epetra_CrsMatrix.h"
57 #include "Epetra_BlockMap.h"
58 #include "Epetra_Map.h"
59 #include "Epetra_Object.h"
60 #include "Epetra_Comm.h"
61 #include "Epetra_CrsMatrix.h"
62 #include "Epetra_Time.h"
63 #include "Teuchos_RefCountPtr.hpp"
64 #include "Teuchos_ArrayRCP.hpp"
65 #include "Epetra_MpiComm.h"
66 
67 
68 #include <map>
69 
70 // Hypre forward declarations (to avoid downstream header pollution)
71 
72 struct hypre_IJMatrix_struct;
73 typedef struct hypre_IJMatrix_struct *HYPRE_IJMatrix;
74 struct hypre_IJVector_struct;
75 typedef struct hypre_IJVector_struct *HYPRE_IJVector;
76 struct hypre_ParCSRMatrix_struct;
77 typedef struct hypre_ParCSRMatrix_struct* HYPRE_ParCSRMatrix;
78 struct hypre_ParVector_struct;
79 typedef struct hypre_ParVector_struct * HYPRE_ParVector;
80 struct hypre_Solver_struct;
81 typedef struct hypre_Solver_struct *HYPRE_Solver;
82 struct hypre_ParVector_struct;
83 typedef struct hypre_ParVector_struct hypre_ParVector;
84 //struct hypre_Vector;
85 
86 // Will only work if Hypre is built with HYPRE_BIGINT=OFF
87 typedef int HYPRE_Int;
88 
89 typedef HYPRE_Int (*HYPRE_PtrToParSolverFcn)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
90 
91 #ifndef HYPRE_ENUMS
92 #define HYPRE_ENUMS
93 enum Hypre_Solver{
95  BoomerAMG,
96  ParaSails,
97  Euclid,
98  AMS,
99  Hybrid,
100  PCG,
101  GMRES,
102  FlexGMRES,
103  LGMRES,
104  BiCGSTAB
105 };
106 
108 enum Hypre_Chooser{
109  Solver,
111 };
112 #endif //HYPRE_ENUMS
113 
114 class FunctionParameter;
115 
116 namespace Teuchos {
117  class ParameterList;
118 }
119 
121 
126 class Ifpack_Hypre: public Ifpack_Preconditioner {
127 
128 public:
129  // @{ Constructors and destructors.
131  Ifpack_Hypre(Epetra_RowMatrix* A);
132 
134  ~Ifpack_Hypre(){ Destroy();}
135 
136  // @}
137  // @{ Construction methods
138 
140  int Initialize();
141 
143  bool IsInitialized() const{ return(IsInitialized_);}
144 
146 
148  int Compute();
149 
151  bool IsComputed() const{ return(IsComputed_);}
152 
153 
155  /* This method is only available if the Teuchos package is enabled.
156  This method recognizes six parameter names: Solver,
157  Preconditioner, SolveOrPrecondition, SetPreconditioner, NumFunctions and Functions. These names are
158  case sensitive. Solver requires an enumerated parameter of type Hypre_Solver. Preconditioner is similar
159  except requires the type be a preconditioner. The options are listed below:
160  Solvers Preconditioners
161  BoomerAMG BoomerAMG
162  AMS ParaSails
163  Hybrid AMS
164  PCG (Default) Euclid (Default)
165  GMRES
166  FlexGMRES
167  LGMRES
168  BiCGSTAB
169  SolveOrPrecondition takes enumerated type Hypre_Chooser, Solver will solve the system, Preconditioner will apply the preconditioner.
170  SetPreconditioner takes a boolean, true means the solver will use the preconditioner.
171  NumFunctions takes an int that describes how many parameters will be passed into Functions. (This needs to be correct.)
172  Functions takes an array of Ref Counted Pointers to an object called FunctionParameter. This class is implemented in Ifpack_Hypre.h.
173  The object takes whether it is Solver or Preconditioner that we are setting a parameter for.
174  The function in Hypre that sets the parameter, and the parameters for that function. An example is below:
175 
176  RCP<FunctionParameter> functs[2];
177  functs[0] = rcp(new FunctionParameter(Solver, &HYPRE_PCGSetMaxIter, 1000)); // max iterations
178  functs[1] = rcp(new FunctionParameter(Solver, &HYPRE_PCGSetTol, 1e-7)); // conv. tolerance
179  list.set("NumFunctions", 2);
180  list.set<RCP<FunctionParameter>*>("Functions", functs);
181  NOTE: SetParameters() must be called to use ApplyInverse(), the solvers will not be created otherwise. An empty list is acceptable to use defaults.
182  */
183  int SetParameters(Teuchos::ParameterList& parameterlist);
184 
186 
194  int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, int), int parameter);
195 
197 
205  int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, double), double parameter);
206 
208 
217  int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, double, int), double parameter1, int parameter2);
218 
220 
229  int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, int, double), int parameter1, double parameter2);
230 
232 
241  int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, int, int), int parameter1, int parameter2);
242 
244 
252  int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, double*), double* parameter);
253 
255 
263  int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, int*), int* parameter);
264 
266 
275  int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, int**), int** parameter);
276 
278 
287  int SetParameter(Hypre_Chooser chooser, Hypre_Solver Solver);
288 
290 
298  int SetParameter(bool UsePreconditioner){ UsePreconditioner_ = UsePreconditioner; return 0;}
299 
301 
307  int SetParameter(Hypre_Chooser chooser) { SolveOrPrec_ = chooser; return 0;}
308 
310  int SetCoordinates(Teuchos::RCP<Epetra_MultiVector> coords);
311 
313  int SetDiscreteGradient(Teuchos::RCP<const Epetra_CrsMatrix> G);
314 
316  int CallFunctions() const;
317 
319 
328  int SetUseTranspose(bool UseTranspose_in) {UseTranspose_ = UseTranspose_in; return(0);};
329 
330  // @}
331 
332  // @{ Mathematical functions.
333  // Applies the matrix to X, returns the result in Y.
334  int Apply(const Epetra_MultiVector& X,
335  Epetra_MultiVector& Y) const{ return(Multiply(false,X,Y));}
336 
338 
349  int Multiply(bool Trans, const Epetra_MultiVector& X, Epetra_MultiVector& Y) const;
350 
352 
365  int ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const;
366 
368  double Condest(const Ifpack_CondestType CT = Ifpack_Cheap,
369  const int MaxIters = 1550,
370  const double Tol = 1e-9,
371  Epetra_RowMatrix* Matrix_in = 0);
372 
374  double Condest() const{ return(Condest_);}
375 
376  // @}
377  // @{ Query methods
378 
380  const char* Label() const {return(Label_);}
381 
383  int SetLabel(const char* Label_in)
384  {
385  strcpy(Label_,Label_in);
386  return(0);
387  }
388 
390  const Epetra_Map& OperatorDomainMap() const{ return *GloballyContiguousRowMap_;}
391 
393  const Epetra_Map& OperatorRangeMap() const{ return *GloballyContiguousRowMap_;}
394 
396  double NormInf() const {return(0.0);};
397 
399  bool HasNormInf() const {return(false);};
400 
402  bool UseTranspose() const {return(UseTranspose_);};
403 
405  const Epetra_Comm & Comm() const{return(A_->Comm());};
406 
408  const Epetra_RowMatrix& Matrix() const{ return(*A_);}
409 
411 #if 0
412  const HYPRE_IJMatrix& HypreMatrix()
413  {
414  if(IsInitialized() == false)
415  Initialize();
416  return(HypreA_);
417  }
418 #endif
419 
421  virtual std::ostream& Print(std::ostream& os) const;
422 
424  virtual int NumInitialize() const{ return(NumInitialize_);}
425 
427  virtual int NumCompute() const{ return(NumCompute_);}
428 
430  virtual int NumApplyInverse() const{ return(NumApplyInverse_);}
431 
433  virtual double InitializeTime() const{ return(InitializeTime_);}
434 
436  virtual double ComputeTime() const{ return(ComputeTime_);}
437 
439  virtual double ApplyInverseTime() const{ return(ApplyInverseTime_);}
440 
442  virtual double InitializeFlops() const{ return(0.0);}
443 
445  virtual double ComputeFlops() const{ return(ComputeFlops_);}
446 
448  virtual double ApplyInverseFlops() const{ return(ApplyInverseFlops_);}
449 
450 private:
451 
452  // @}
453  // @{ Private methods
454 
456  Ifpack_Hypre(const Ifpack_Hypre& RHS) : Time_(RHS.Comm()){}
457 
459  Ifpack_Hypre& operator=(const Ifpack_Hypre& /*RHS*/){ return(*this);}
460 
462  void Destroy();
463 
465  MPI_Comm GetMpiComm() const
466  { return (dynamic_cast<const Epetra_MpiComm*>(&A_->Comm()))->GetMpiComm();}
467 
469 
479  int Solve(bool Trans, const Epetra_MultiVector& X, Epetra_MultiVector& Y) const;
480 
481 
483  int NumGlobalRows() const {return(A_->NumGlobalRows());};
484 
486  int NumGlobalCols() const {return(A_->NumGlobalCols());};
487 
489  int NumMyRows() const {return(A_->NumMyRows());};
490 
492  int NumMyCols() const {return(A_->NumMyCols());};
493 
495  int SetSolverType(Hypre_Solver solver);
496 
498  int SetPrecondType(Hypre_Solver precond);
499 
501  int CreateSolver();
502 
504  int CreatePrecond();
505 
507  int CopyEpetraToHypre();
508 
510  int AddFunToList(Teuchos::RCP<FunctionParameter> NewFun);
511 
513  int Hypre_BoomerAMGCreate(MPI_Comm comm, HYPRE_Solver *solver);
514 
516  int Hypre_ParaSailsCreate(MPI_Comm comm, HYPRE_Solver *solver);
517 
519  int Hypre_EuclidCreate(MPI_Comm comm, HYPRE_Solver *solver);
520 
522  int Hypre_AMSCreate(MPI_Comm comm, HYPRE_Solver *solver);
523 
525  int Hypre_ParCSRHybridCreate(MPI_Comm comm, HYPRE_Solver *solver);
526 
528  int Hypre_ParCSRPCGCreate(MPI_Comm comm, HYPRE_Solver *solver);
529 
531  int Hypre_ParCSRGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver);
532 
534  int Hypre_ParCSRFlexGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver);
535 
537  int Hypre_ParCSRLGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver);
538 
540  int Hypre_ParCSRBiCGSTABCreate(MPI_Comm comm, HYPRE_Solver *solver);
541 
543  Teuchos::RCP<const Epetra_Map> MakeContiguousColumnMap(Teuchos::RCP<const Epetra_RowMatrix> &Matrix) const;
544  // @}
545  // @{ Internal data
546 
552  bool UseTranspose_;
554  double Condest_;
556  bool IsInitialized_;
558  bool IsComputed_;
560  char Label_[160];
562  int NumInitialize_;
564  int NumCompute_;
566  mutable int NumApplyInverse_;
568  double InitializeTime_;
570  double ComputeTime_;
572  mutable double ApplyInverseTime_;
574  double ComputeFlops_;
576  mutable double ApplyInverseFlops_;
578  mutable Epetra_Time Time_;
579 
581  mutable HYPRE_IJMatrix HypreA_;
583  mutable HYPRE_ParCSRMatrix ParMatrix_;
584 
588  mutable HYPRE_IJMatrix HypreG_;
590  mutable HYPRE_ParCSRMatrix ParMatrixG_;
591 
593  mutable HYPRE_IJVector XHypre_;
595  mutable HYPRE_IJVector YHypre_;
596  mutable HYPRE_ParVector ParX_;
597  mutable HYPRE_ParVector ParY_;
598  mutable Teuchos::RCP<hypre_ParVector> XVec_;
599  mutable Teuchos::RCP<hypre_ParVector> YVec_;
600 
602  mutable HYPRE_IJVector xHypre_;
603  mutable HYPRE_IJVector yHypre_;
604  mutable HYPRE_IJVector zHypre_;
605  mutable HYPRE_ParVector xPar_;
606  mutable HYPRE_ParVector yPar_;
607  mutable HYPRE_ParVector zPar_;
608 
610  mutable HYPRE_Solver Solver_;
612  mutable HYPRE_Solver Preconditioner_;
613  // The following are pointers to functions to use the solver and preconditioner.
614  int (Ifpack_Hypre::*SolverCreatePtr_)(MPI_Comm, HYPRE_Solver*);
615  int (*SolverDestroyPtr_)(HYPRE_Solver);
616  int (*SolverSetupPtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
617  int (*SolverSolvePtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
618  int (*SolverPrecondPtr_)(HYPRE_Solver, HYPRE_PtrToParSolverFcn, HYPRE_PtrToParSolverFcn, HYPRE_Solver);
619  int (Ifpack_Hypre::*PrecondCreatePtr_)(MPI_Comm, HYPRE_Solver*);
620  int (*PrecondDestroyPtr_)(HYPRE_Solver);
621  int (*PrecondSetupPtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
622  int (*PrecondSolvePtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
623 
624  bool IsSolverCreated_;
625  bool IsPrecondCreated_;
627  Hypre_Chooser SolveOrPrec_;
629  Teuchos::RCP<const Epetra_Map> GloballyContiguousRowMap_;
630  Teuchos::RCP<const Epetra_Map> GloballyContiguousColMap_;
631  Teuchos::RCP<const Epetra_Map> GloballyContiguousNodeRowMap_;
632  Teuchos::RCP<const Epetra_Map> GloballyContiguousNodeColMap_;
634  int NumFunsToCall_;
636  Hypre_Solver SolverType_;
638  Hypre_Solver PrecondType_;
640  bool UsePreconditioner_;
642  std::vector<Teuchos::RCP<FunctionParameter> > FunsToCall_;
644  bool Dump_;
646  mutable Teuchos::ArrayRCP<double> VectorCache_;
647 
648 };
649 
650 #endif // HAVE_HYPRE
651 #endif /* IFPACK_HYPRE_H */
virtual int NumInitialize() const =0
Returns the number of calls to Initialize().
virtual int SetUseTranspose(bool UseTranspose)=0
virtual double ComputeTime() const =0
Returns the time spent in Compute().
virtual double ComputeFlops() const =0
Returns the number of flops in the computation phase.
virtual double ApplyInverseTime() const =0
Returns the time spent in ApplyInverse().
virtual double ApplyInverseFlops() const =0
Returns the number of flops in the application of the preconditioner.
virtual const Epetra_RowMatrix & Matrix() const =0
Returns a pointer to the matrix to be preconditioned.
virtual bool IsInitialized() const =0
Returns true if the preconditioner has been successfully initialized, false otherwise.
virtual std::ostream & Print(std::ostream &os) const =0
Prints basic information on iostream. This function is used by operator&lt;&lt;.
virtual const Epetra_Map & OperatorDomainMap() const =0
virtual const char * Label() const =0
virtual int Initialize()=0
Computes all it is necessary to initialize the preconditioner.
static bool Solver
Definition: performance.cpp:70
virtual double InitializeTime() const =0
Returns the time spent in Initialize().
Ifpack_CondestType
Ifpack_CondestType: enum to define the type of condition number estimate.
virtual int Apply(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const =0
virtual const Epetra_Map & OperatorRangeMap() const =0
virtual const Epetra_Comm & Comm() const =0
virtual int SetParameters(Teuchos::ParameterList &List)=0
Sets all parameters for the preconditioner.
virtual double Condest() const =0
Returns the computed condition number estimate, or -1.0 if not computed.
virtual bool UseTranspose() const =0
Ifpack_ScalingType enumerable type.
Ifpack_Preconditioner: basic class for preconditioning in Ifpack.
Hypre_Chooser
virtual int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const =0
Applies the preconditioner to vector X, returns the result in Y.
virtual double InitializeFlops() const =0
Returns the number of flops in the initialization phase.
void BiCGSTAB(Epetra_CrsMatrix &A, Epetra_Vector &x, Epetra_Vector &b, Ifpack_CrsRiluk *M, int Maxiter, double Tolerance, double *residual, bool verbose)
cheap estimate
virtual bool HasNormInf() const =0
virtual int NumCompute() const =0
Returns the number of calls to Compute().
virtual double NormInf() const =0
Hypre_Solver
virtual bool IsComputed() const =0
Returns true if the preconditioner has been successfully computed, false otherwise.
virtual int Compute()=0
Computes all it is necessary to apply the preconditioner.
int Solve(int, TYPE *, TYPE *, TYPE *)
virtual int NumApplyInverse() const =0
Returns the number of calls to ApplyInverse().
#define RHS(a)
Definition: MatGenFD.c:60