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, int), int parameter1, int parameter2);
230 
232 
240  int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, double*), double* parameter);
241 
243 
251  int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, int*), int* parameter);
252 
254 
263  int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, int**), int** parameter);
264 
266 
275  int SetParameter(Hypre_Chooser chooser, Hypre_Solver Solver);
276 
278 
286  int SetParameter(bool UsePreconditioner){ UsePreconditioner_ = UsePreconditioner; return 0;}
287 
289 
295  int SetParameter(Hypre_Chooser chooser) { SolveOrPrec_ = chooser; return 0;}
296 
298  int SetCoordinates(Teuchos::RCP<Epetra_MultiVector> coords);
299 
301  int SetDiscreteGradient(Teuchos::RCP<const Epetra_CrsMatrix> G);
302 
304  int CallFunctions() const;
305 
307 
316  int SetUseTranspose(bool UseTranspose_in) {UseTranspose_ = UseTranspose_in; return(0);};
317 
318  // @}
319 
320  // @{ Mathematical functions.
321  // Applies the matrix to X, returns the result in Y.
322  int Apply(const Epetra_MultiVector& X,
323  Epetra_MultiVector& Y) const{ return(Multiply(false,X,Y));}
324 
326 
337  int Multiply(bool Trans, const Epetra_MultiVector& X, Epetra_MultiVector& Y) const;
338 
340 
353  int ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const;
354 
356  double Condest(const Ifpack_CondestType CT = Ifpack_Cheap,
357  const int MaxIters = 1550,
358  const double Tol = 1e-9,
359  Epetra_RowMatrix* Matrix_in = 0);
360 
362  double Condest() const{ return(Condest_);}
363 
364  // @}
365  // @{ Query methods
366 
368  const char* Label() const {return(Label_);}
369 
371  int SetLabel(const char* Label_in)
372  {
373  strcpy(Label_,Label_in);
374  return(0);
375  }
376 
378  const Epetra_Map& OperatorDomainMap() const{ return *GloballyContiguousRowMap_;}
379 
381  const Epetra_Map& OperatorRangeMap() const{ return *GloballyContiguousRowMap_;}
382 
384  double NormInf() const {return(0.0);};
385 
387  bool HasNormInf() const {return(false);};
388 
390  bool UseTranspose() const {return(UseTranspose_);};
391 
393  const Epetra_Comm & Comm() const{return(A_->Comm());};
394 
396  const Epetra_RowMatrix& Matrix() const{ return(*A_);}
397 
399 #if 0
400  const HYPRE_IJMatrix& HypreMatrix()
401  {
402  if(IsInitialized() == false)
403  Initialize();
404  return(HypreA_);
405  }
406 #endif
407 
409  virtual std::ostream& Print(std::ostream& os) const;
410 
412  virtual int NumInitialize() const{ return(NumInitialize_);}
413 
415  virtual int NumCompute() const{ return(NumCompute_);}
416 
418  virtual int NumApplyInverse() const{ return(NumApplyInverse_);}
419 
421  virtual double InitializeTime() const{ return(InitializeTime_);}
422 
424  virtual double ComputeTime() const{ return(ComputeTime_);}
425 
427  virtual double ApplyInverseTime() const{ return(ApplyInverseTime_);}
428 
430  virtual double InitializeFlops() const{ return(0.0);}
431 
433  virtual double ComputeFlops() const{ return(ComputeFlops_);}
434 
436  virtual double ApplyInverseFlops() const{ return(ApplyInverseFlops_);}
437 
438 private:
439 
440  // @}
441  // @{ Private methods
442 
444  Ifpack_Hypre(const Ifpack_Hypre& RHS) : Time_(RHS.Comm()){}
445 
447  Ifpack_Hypre& operator=(const Ifpack_Hypre& /*RHS*/){ return(*this);}
448 
450  void Destroy();
451 
453  MPI_Comm GetMpiComm() const
454  { return (dynamic_cast<const Epetra_MpiComm*>(&A_->Comm()))->GetMpiComm();}
455 
457 
467  int Solve(bool Trans, const Epetra_MultiVector& X, Epetra_MultiVector& Y) const;
468 
469 
471  int NumGlobalRows() const {return(A_->NumGlobalRows());};
472 
474  int NumGlobalCols() const {return(A_->NumGlobalCols());};
475 
477  int NumMyRows() const {return(A_->NumMyRows());};
478 
480  int NumMyCols() const {return(A_->NumMyCols());};
481 
483  int SetSolverType(Hypre_Solver solver);
484 
486  int SetPrecondType(Hypre_Solver precond);
487 
489  int CreateSolver();
490 
492  int CreatePrecond();
493 
495  int CopyEpetraToHypre();
496 
498  int AddFunToList(Teuchos::RCP<FunctionParameter> NewFun);
499 
501  int Hypre_BoomerAMGCreate(MPI_Comm comm, HYPRE_Solver *solver);
502 
504  int Hypre_ParaSailsCreate(MPI_Comm comm, HYPRE_Solver *solver);
505 
507  int Hypre_EuclidCreate(MPI_Comm comm, HYPRE_Solver *solver);
508 
510  int Hypre_AMSCreate(MPI_Comm comm, HYPRE_Solver *solver);
511 
513  int Hypre_ParCSRHybridCreate(MPI_Comm comm, HYPRE_Solver *solver);
514 
516  int Hypre_ParCSRPCGCreate(MPI_Comm comm, HYPRE_Solver *solver);
517 
519  int Hypre_ParCSRGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver);
520 
522  int Hypre_ParCSRFlexGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver);
523 
525  int Hypre_ParCSRLGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver);
526 
528  int Hypre_ParCSRBiCGSTABCreate(MPI_Comm comm, HYPRE_Solver *solver);
529 
531  Teuchos::RCP<const Epetra_Map> MakeContiguousColumnMap(Teuchos::RCP<const Epetra_RowMatrix> &Matrix) const;
532  // @}
533  // @{ Internal data
534 
540  bool UseTranspose_;
542  double Condest_;
544  bool IsInitialized_;
546  bool IsComputed_;
548  char Label_[160];
550  int NumInitialize_;
552  int NumCompute_;
554  mutable int NumApplyInverse_;
556  double InitializeTime_;
558  double ComputeTime_;
560  mutable double ApplyInverseTime_;
562  double ComputeFlops_;
564  mutable double ApplyInverseFlops_;
566  mutable Epetra_Time Time_;
567 
569  mutable HYPRE_IJMatrix HypreA_;
571  mutable HYPRE_ParCSRMatrix ParMatrix_;
572 
576  mutable HYPRE_IJMatrix HypreG_;
578  mutable HYPRE_ParCSRMatrix ParMatrixG_;
579 
581  mutable HYPRE_IJVector XHypre_;
583  mutable HYPRE_IJVector YHypre_;
584  mutable HYPRE_ParVector ParX_;
585  mutable HYPRE_ParVector ParY_;
586  mutable Teuchos::RCP<hypre_ParVector> XVec_;
587  mutable Teuchos::RCP<hypre_ParVector> YVec_;
588 
590  mutable HYPRE_IJVector xHypre_;
591  mutable HYPRE_IJVector yHypre_;
592  mutable HYPRE_IJVector zHypre_;
593  mutable HYPRE_ParVector xPar_;
594  mutable HYPRE_ParVector yPar_;
595  mutable HYPRE_ParVector zPar_;
596 
598  mutable HYPRE_Solver Solver_;
600  mutable HYPRE_Solver Preconditioner_;
601  // The following are pointers to functions to use the solver and preconditioner.
602  int (Ifpack_Hypre::*SolverCreatePtr_)(MPI_Comm, HYPRE_Solver*);
603  int (*SolverDestroyPtr_)(HYPRE_Solver);
604  int (*SolverSetupPtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
605  int (*SolverSolvePtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
606  int (*SolverPrecondPtr_)(HYPRE_Solver, HYPRE_PtrToParSolverFcn, HYPRE_PtrToParSolverFcn, HYPRE_Solver);
607  int (Ifpack_Hypre::*PrecondCreatePtr_)(MPI_Comm, HYPRE_Solver*);
608  int (*PrecondDestroyPtr_)(HYPRE_Solver);
609  int (*PrecondSetupPtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
610  int (*PrecondSolvePtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
611 
612  bool IsSolverCreated_;
613  bool IsPrecondCreated_;
615  Hypre_Chooser SolveOrPrec_;
617  Teuchos::RCP<const Epetra_Map> GloballyContiguousRowMap_;
618  Teuchos::RCP<const Epetra_Map> GloballyContiguousColMap_;
619  Teuchos::RCP<const Epetra_Map> GloballyContiguousNodeRowMap_;
620  Teuchos::RCP<const Epetra_Map> GloballyContiguousNodeColMap_;
622  int NumFunsToCall_;
624  Hypre_Solver SolverType_;
626  Hypre_Solver PrecondType_;
628  bool UsePreconditioner_;
630  std::vector<Teuchos::RCP<FunctionParameter> > FunsToCall_;
632  bool Dump_;
634  mutable Teuchos::ArrayRCP<double> VectorCache_;
635 
636 };
637 
638 #endif // HAVE_HYPRE
639 #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