IFPACK  Development
 All Classes Files Functions Variables Enumerations Friends Pages
Ifpack_Hypre.cpp
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 #include "Ifpack_Hypre.h"
43 #if defined(HAVE_HYPRE) && defined(HAVE_MPI)
44 
45 #include "Ifpack_Utils.h"
46 #include "Epetra_MpiComm.h"
47 #include "Epetra_IntVector.h"
48 #include "Epetra_Import.h"
49 #include "Teuchos_ParameterList.hpp"
50 #include "Teuchos_RCP.hpp"
51 
52 using Teuchos::RCP;
53 using Teuchos::rcp;
54 
55 Ifpack_Hypre::Ifpack_Hypre(Epetra_RowMatrix* A):
56  A_(rcp(A,false)),
57  UseTranspose_(false),
58  IsInitialized_(false),
59  IsComputed_(false),
60  Label_(),
61  NumInitialize_(0),
62  NumCompute_(0),
63  NumApplyInverse_(0),
64  InitializeTime_(0.0),
65  ComputeTime_(0.0),
66  ApplyInverseTime_(0.0),
67  ComputeFlops_(0.0),
68  ApplyInverseFlops_(0.0),
69  Time_(A_->Comm()),
70  SolveOrPrec_(Solver),
71  NumFunsToCall_(0),
72  SolverType_(PCG),
73  PrecondType_(Euclid),
74  UsePreconditioner_(false),
75  NiceRowMap_(true)
76 {
77  IsSolverSetup_ = new bool[1];
78  IsPrecondSetup_ = new bool[1];
79  IsSolverSetup_[0] = false;
80  IsPrecondSetup_[0] = false;
81  MPI_Comm comm = GetMpiComm();
82  int ilower = A_->RowMatrixRowMap().MinMyGID();
83  int iupper = A_->RowMatrixRowMap().MaxMyGID();
84  // Need to check if the RowMap is the way Hypre expects (if not more difficult)
85  std::vector<int> ilowers; ilowers.resize(Comm().NumProc());
86  std::vector<int> iuppers; iuppers.resize(Comm().NumProc());
87  int myLower[1]; myLower[0] = ilower;
88  int myUpper[1]; myUpper[0] = iupper;
89  Comm().GatherAll(myLower, &ilowers[0], 1);
90  Comm().GatherAll(myUpper, &iuppers[0], 1);
91  for(int i = 0; i < Comm().NumProc()-1; i++){
92  NiceRowMap_ = (NiceRowMap_ && iuppers[i]+1 == ilowers[i+1]);
93  }
94  if(!NiceRowMap_){
95  ilower = (A_->NumGlobalRows() / Comm().NumProc())*Comm().MyPID();
96  iupper = (A_->NumGlobalRows() / Comm().NumProc())*(Comm().MyPID()+1)-1;
97  if(Comm().MyPID() == Comm().NumProc()-1){
98  iupper = A_-> NumGlobalRows()-1;
99  }
100  }
101 
102  // Next create vectors that will be used when ApplyInverse() is called
103  IFPACK_CHK_ERRV(HYPRE_IJVectorCreate(comm, ilower, iupper, &XHypre_));
104  IFPACK_CHK_ERRV(HYPRE_IJVectorSetObjectType(XHypre_, HYPRE_PARCSR));
105  IFPACK_CHK_ERRV(HYPRE_IJVectorInitialize(XHypre_));
106  IFPACK_CHK_ERRV(HYPRE_IJVectorAssemble(XHypre_));
107  IFPACK_CHK_ERRV(HYPRE_IJVectorGetObject(XHypre_, (void**) &ParX_));
108 
109  IFPACK_CHK_ERRV(HYPRE_IJVectorCreate(comm, ilower, iupper, &YHypre_));
110  IFPACK_CHK_ERRV(HYPRE_IJVectorSetObjectType(YHypre_, HYPRE_PARCSR));
111  IFPACK_CHK_ERRV(HYPRE_IJVectorInitialize(YHypre_));
112  IFPACK_CHK_ERRV(HYPRE_IJVectorAssemble(YHypre_));
113  IFPACK_CHK_ERRV(HYPRE_IJVectorGetObject(YHypre_, (void**) &ParY_));
114 
115  XVec_ = (hypre_ParVector *) hypre_IJVectorObject(((hypre_IJVector *) XHypre_));
116  XLocal_ = hypre_ParVectorLocalVector(XVec_);
117 
118  YVec_ = (hypre_ParVector *) hypre_IJVectorObject(((hypre_IJVector *) YHypre_));
119  YLocal_ = hypre_ParVectorLocalVector(YVec_);
120  std::vector<int> rows; rows.resize(iupper - ilower +1);
121  for(int i = ilower; i <= iupper; i++){
122  rows[i-ilower] = i;
123  }
124  MySimpleMap_ = rcp(new Epetra_Map(-1, iupper-ilower+1, &rows[0], 0, Comm()));
125 } //Constructor
126 
127 //==============================================================================
128 void Ifpack_Hypre::Destroy(){
129  if(IsInitialized()){
130  IFPACK_CHK_ERRV(HYPRE_IJMatrixDestroy(HypreA_));
131  }
132  IFPACK_CHK_ERRV(HYPRE_IJVectorDestroy(XHypre_));
133  IFPACK_CHK_ERRV(HYPRE_IJVectorDestroy(YHypre_));
134  if(IsSolverSetup_[0]){
135  IFPACK_CHK_ERRV(SolverDestroyPtr_(Solver_));
136  }
137  if(IsPrecondSetup_[0]){
138  IFPACK_CHK_ERRV(PrecondDestroyPtr_(Preconditioner_));
139  }
140  delete[] IsSolverSetup_;
141  delete[] IsPrecondSetup_;
142 } //Destroy()
143 
144 //==============================================================================
145 int Ifpack_Hypre::Initialize(){
146  Time_.ResetStartTime();
147  MPI_Comm comm = GetMpiComm();
148  int ilower = MySimpleMap_->MinMyGID();
149  int iupper = MySimpleMap_->MaxMyGID();
150  IFPACK_CHK_ERR(HYPRE_IJMatrixCreate(comm, ilower, iupper, ilower, iupper, &HypreA_));
151  IFPACK_CHK_ERR(HYPRE_IJMatrixSetObjectType(HypreA_, HYPRE_PARCSR));
152  IFPACK_CHK_ERR(HYPRE_IJMatrixInitialize(HypreA_));
153  for(int i = 0; i < A_->NumMyRows(); i++){
154  int numElements;
155  IFPACK_CHK_ERR(A_->NumMyRowEntries(i,numElements));
156  std::vector<int> indices; indices.resize(numElements);
157  std::vector<double> values; values.resize(numElements);
158  int numEntries;
159  IFPACK_CHK_ERR(A_->ExtractMyRowCopy(i, numElements, numEntries, &values[0], &indices[0]));
160  for(int j = 0; j < numEntries; j++){
161  indices[j] = A_->RowMatrixColMap().GID(indices[j]);
162  }
163  int GlobalRow[1];
164  GlobalRow[0] = A_->RowMatrixRowMap().GID(i);
165  IFPACK_CHK_ERR(HYPRE_IJMatrixSetValues(HypreA_, 1, &numEntries, GlobalRow, &indices[0], &values[0]));
166  }
167  IFPACK_CHK_ERR(HYPRE_IJMatrixAssemble(HypreA_));
168  IFPACK_CHK_ERR(HYPRE_IJMatrixGetObject(HypreA_, (void**)&ParMatrix_));
169  IsInitialized_=true;
170  NumInitialize_ = NumInitialize_ + 1;
171  InitializeTime_ = InitializeTime_ + Time_.ElapsedTime();
172  return 0;
173 } //Initialize()
174 
175 //==============================================================================
176 int Ifpack_Hypre::SetParameters(Teuchos::ParameterList& list){
177  List_ = list;
178  Hypre_Solver solType = list.get("Solver", PCG);
179  SolverType_ = solType;
180  Hypre_Solver precType = list.get("Preconditioner", Euclid);
181  PrecondType_ = precType;
182  Hypre_Chooser chooser = list.get("SolveOrPrecondition", Solver);
183  SolveOrPrec_ = chooser;
184  bool SetPrecond = list.get("SetPreconditioner", false);
185  IFPACK_CHK_ERR(SetParameter(SetPrecond));
186  int NumFunctions = list.get("NumFunctions", 0);
187  FunsToCall_.clear();
188  NumFunsToCall_ = 0;
189  if(NumFunctions > 0){
190  RCP<FunctionParameter>* params = list.get<RCP<FunctionParameter>*>("Functions");
191  for(int i = 0; i < NumFunctions; i++){
192  IFPACK_CHK_ERR(AddFunToList(params[i]));
193  }
194  }
195  return 0;
196 } //SetParameters()
197 
198 //==============================================================================
199 int Ifpack_Hypre::AddFunToList(RCP<FunctionParameter> NewFun){
200  NumFunsToCall_ = NumFunsToCall_+1;
201  FunsToCall_.resize(NumFunsToCall_);
202  FunsToCall_[NumFunsToCall_-1] = NewFun;
203  return 0;
204 } //AddFunToList()
205 
206 //==============================================================================
207 int Ifpack_Hypre::SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, int), int parameter){
208  RCP<FunctionParameter> temp = rcp(new FunctionParameter(chooser, pt2Func, parameter));
209  IFPACK_CHK_ERR(AddFunToList(temp));
210  return 0;
211 } //SetParameter() - int function pointer
212 
213 //==============================================================================
214 int Ifpack_Hypre::SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, double), double parameter){
215  RCP<FunctionParameter> temp = rcp(new FunctionParameter(chooser, pt2Func, parameter));
216  IFPACK_CHK_ERR(AddFunToList(temp));
217  return 0;
218 } //SetParameter() - double function pointer
219 
220 //==============================================================================
221 int Ifpack_Hypre::SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, double, int), double parameter1, int parameter2){
222  RCP<FunctionParameter> temp = rcp(new FunctionParameter(chooser, pt2Func, parameter1, parameter2));
223  IFPACK_CHK_ERR(AddFunToList(temp));
224  return 0;
225 } //SetParameter() - double,int function pointer
226 
227 //==============================================================================
228 int Ifpack_Hypre::SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, int, int), int parameter1, int parameter2){
229  RCP<FunctionParameter> temp = rcp(new FunctionParameter(chooser, pt2Func, parameter1, parameter2));
230  IFPACK_CHK_ERR(AddFunToList(temp));
231  return 0;
232 } //SetParameter() int,int function pointer
233 
234 //==============================================================================
235 int Ifpack_Hypre::SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, double*), double* parameter){
236  RCP<FunctionParameter> temp = rcp(new FunctionParameter(chooser, pt2Func, parameter));
237  IFPACK_CHK_ERR(AddFunToList(temp));
238  return 0;
239 } //SetParameter() - double* function pointer
240 
241 //==============================================================================
242 int Ifpack_Hypre::SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, int*), int* parameter){
243  RCP<FunctionParameter> temp = rcp(new FunctionParameter(chooser, pt2Func, parameter));
244  IFPACK_CHK_ERR(AddFunToList(temp));
245  return 0;
246 } //SetParameter() - int* function pointer
247 
248 //==============================================================================
249 int Ifpack_Hypre::SetParameter(Hypre_Chooser chooser, Hypre_Solver solver){
250  if(chooser == Solver){
251  SolverType_ = solver;
252  } else {
253  PrecondType_ = solver;
254  }
255  return 0;
256 } //SetParameter() - set type of solver
257 
258 //==============================================================================
259 int Ifpack_Hypre::Compute(){
260  if(IsInitialized() == false){
261  IFPACK_CHK_ERR(Initialize());
262  }
263  Time_.ResetStartTime();
264  IFPACK_CHK_ERR(SetSolverType(SolverType_));
265  IFPACK_CHK_ERR(SetPrecondType(PrecondType_));
266  CallFunctions();
267  if(UsePreconditioner_){
268  if(SolverPrecondPtr_ != NULL){
269  IFPACK_CHK_ERR(SolverPrecondPtr_(Solver_, PrecondSolvePtr_, PrecondSetupPtr_, Preconditioner_));
270  }
271  }
272  if(SolveOrPrec_ == Solver){
273  IFPACK_CHK_ERR(SolverSetupPtr_(Solver_, ParMatrix_, ParX_, ParY_));
274  IsSolverSetup_[0] = true;
275  } else {
276  IFPACK_CHK_ERR(PrecondSetupPtr_(Preconditioner_, ParMatrix_, ParX_, ParY_));
277  IsPrecondSetup_[0] = true;
278  }
279  IsComputed_ = true;
280  NumCompute_ = NumCompute_ + 1;
281  ComputeTime_ = ComputeTime_ + Time_.ElapsedTime();
282  return 0;
283 } //Compute()
284 
285 //==============================================================================
286 int Ifpack_Hypre::CallFunctions() const{
287  for(int i = 0; i < NumFunsToCall_; i++){
288  IFPACK_CHK_ERR(FunsToCall_[i]->CallFunction(Solver_, Preconditioner_));
289  }
290  return 0;
291 } //CallFunctions()
292 
293 //==============================================================================
294 int Ifpack_Hypre::ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const{
295  if(IsComputed() == false){
296  IFPACK_CHK_ERR(-1);
297  }
298  Time_.ResetStartTime();
299  bool SameVectors = false;
300  int NumVectors = X.NumVectors();
301  if (NumVectors != Y.NumVectors()) IFPACK_CHK_ERR(-1); // X and Y must have same number of vectors
302  if(X.Pointers() == Y.Pointers()){
303  SameVectors = true;
304  }
305  for(int VecNum = 0; VecNum < NumVectors; VecNum++) {
306  //Get values for current vector in multivector.
307  double * XValues;
308  IFPACK_CHK_ERR((*X(VecNum)).ExtractView(&XValues));
309  double * YValues;
310  if(!SameVectors){
311  IFPACK_CHK_ERR((*Y(VecNum)).ExtractView(&YValues));
312  } else {
313  YValues = new double[X.MyLength()];
314  }
315  // Temporarily make a pointer to data in Hypre for end
316  double *XTemp = XLocal_->data;
317  // Replace data in Hypre vectors with epetra values
318  XLocal_->data = XValues;
319  double *YTemp = YLocal_->data;
320  YLocal_->data = YValues;
321 
322  IFPACK_CHK_ERR(HYPRE_ParVectorSetConstantValues(ParY_, 0.0));
323  if(SolveOrPrec_ == Solver){
324  // Use the solver methods
325  IFPACK_CHK_ERR(SolverSolvePtr_(Solver_, ParMatrix_, ParX_, ParY_));
326  } else {
327  // Apply the preconditioner
328  IFPACK_CHK_ERR(PrecondSolvePtr_(Preconditioner_, ParMatrix_, ParX_, ParY_));
329  }
330  if(SameVectors){
331  int NumEntries = Y.MyLength();
332  std::vector<double> new_values; new_values.resize(NumEntries);
333  std::vector<int> new_indices; new_indices.resize(NumEntries);
334  for(int i = 0; i < NumEntries; i++){
335  new_values[i] = YValues[i];
336  new_indices[i] = i;
337  }
338  IFPACK_CHK_ERR((*Y(VecNum)).ReplaceMyValues(NumEntries, &new_values[0], &new_indices[0]));
339  delete[] YValues;
340  }
341  XLocal_->data = XTemp;
342  YLocal_->data = YTemp;
343  }
344  NumApplyInverse_ = NumApplyInverse_ + 1;
345  ApplyInverseTime_ = ApplyInverseTime_ + Time_.ElapsedTime();
346  return 0;
347 } //ApplyInverse()
348 
349 //==============================================================================
350 int Ifpack_Hypre::Multiply(bool TransA, const Epetra_MultiVector& X, Epetra_MultiVector& Y) const{
351  if(IsInitialized() == false){
352  IFPACK_CHK_ERR(-1);
353  }
354  bool SameVectors = false;
355  int NumVectors = X.NumVectors();
356  if (NumVectors != Y.NumVectors()) IFPACK_CHK_ERR(-1); // X and Y must have same number of vectors
357  if(X.Pointers() == Y.Pointers()){
358  SameVectors = true;
359  }
360  for(int VecNum = 0; VecNum < NumVectors; VecNum++) {
361  //Get values for current vector in multivector.
362  double * XValues;
363  double * YValues;
364  IFPACK_CHK_ERR((*X(VecNum)).ExtractView(&XValues));
365  double *XTemp = XLocal_->data;
366  double *YTemp = YLocal_->data;
367  if(!SameVectors){
368  IFPACK_CHK_ERR((*Y(VecNum)).ExtractView(&YValues));
369  } else {
370  YValues = new double[X.MyLength()];
371  }
372  YLocal_->data = YValues;
373  IFPACK_CHK_ERR(HYPRE_ParVectorSetConstantValues(ParY_,0.0));
374  // Temporarily make a pointer to data in Hypre for end
375  // Replace data in Hypre vectors with epetra values
376  XLocal_->data = XValues;
377  // Do actual computation.
378  if(TransA) {
379  // Use transpose of A in multiply
380  IFPACK_CHK_ERR(HYPRE_ParCSRMatrixMatvecT(1.0, ParMatrix_, ParX_, 1.0, ParY_));
381  } else {
382  IFPACK_CHK_ERR(HYPRE_ParCSRMatrixMatvec(1.0, ParMatrix_, ParX_, 1.0, ParY_));
383  }
384  if(SameVectors){
385  int NumEntries = Y.MyLength();
386  std::vector<double> new_values; new_values.resize(NumEntries);
387  std::vector<int> new_indices; new_indices.resize(NumEntries);
388  for(int i = 0; i < NumEntries; i++){
389  new_values[i] = YValues[i];
390  new_indices[i] = i;
391  }
392  IFPACK_CHK_ERR((*Y(VecNum)).ReplaceMyValues(NumEntries, &new_values[0], &new_indices[0]));
393  delete[] YValues;
394  }
395  XLocal_->data = XTemp;
396  YLocal_->data = YTemp;
397  }
398  return 0;
399 } //Multiply()
400 
401 //==============================================================================
402 ostream& Ifpack_Hypre::Print(ostream& os) const{
403  if (!Comm().MyPID()) {
404  os << endl;
405  os << "================================================================================" << endl;
406  os << "Ifpack_Hypre: " << Label () << endl << endl;
407  os << "Using " << Comm().NumProc() << " processors." << endl;
408  os << "Global number of rows = " << A_->NumGlobalRows() << endl;
409  os << "Global number of nonzeros = " << A_->NumGlobalNonzeros() << endl;
410  os << "Condition number estimate = " << Condest() << endl;
411  os << endl;
412  os << "Phase # calls Total Time (s) Total MFlops MFlops/s" << endl;
413  os << "----- ------- -------------- ------------ --------" << endl;
414  os << "Initialize() " << std::setw(5) << NumInitialize_
415  << " " << std::setw(15) << InitializeTime_
416  << " 0.0 0.0" << endl;
417  os << "Compute() " << std::setw(5) << NumCompute_
418  << " " << std::setw(15) << ComputeTime_
419  << " " << std::setw(15) << 1.0e-6 * ComputeFlops_;
420  if (ComputeTime_ != 0.0)
421  os << " " << std::setw(15) << 1.0e-6 * ComputeFlops_ / ComputeTime_ << endl;
422  else
423  os << " " << std::setw(15) << 0.0 << endl;
424  os << "ApplyInverse() " << std::setw(5) << NumApplyInverse_
425  << " " << std::setw(15) << ApplyInverseTime_
426  << " " << std::setw(15) << 1.0e-6 * ApplyInverseFlops_;
427  if (ApplyInverseTime_ != 0.0)
428  os << " " << std::setw(15) << 1.0e-6 * ApplyInverseFlops_ / ApplyInverseTime_ << endl;
429  else
430  os << " " << std::setw(15) << 0.0 << endl;
431  os << "================================================================================" << endl;
432  os << endl;
433  }
434  return os;
435 } //Print()
436 
437 //==============================================================================
438 double Ifpack_Hypre::Condest(const Ifpack_CondestType CT,
439  const int MaxIters,
440  const double Tol,
441  Epetra_RowMatrix* Matrix_in){
442  if (!IsComputed()) // cannot compute right now
443  return(-1.0);
444  Condest_ = Ifpack_Condest(*this, CT, MaxIters, Tol, Matrix_in);
445  return(Condest_);
446 } //Condest()
447 
448 //==============================================================================
449 int Ifpack_Hypre::SetSolverType(Hypre_Solver Solver){
450  switch(Solver) {
451  case BoomerAMG:
452  if(IsSolverSetup_[0]){
453  SolverDestroyPtr_(Solver_);
454  IsSolverSetup_[0] = false;
455  }
456  SolverCreatePtr_ = &Ifpack_Hypre::Hypre_BoomerAMGCreate;
457  SolverDestroyPtr_ = &HYPRE_BoomerAMGDestroy;
458  SolverSetupPtr_ = &HYPRE_BoomerAMGSetup;
459  SolverPrecondPtr_ = NULL;
460  SolverSolvePtr_ = &HYPRE_BoomerAMGSolve;
461  break;
462  case AMS:
463  if(IsSolverSetup_[0]){
464  SolverDestroyPtr_(Solver_);
465  IsSolverSetup_[0] = false;
466  }
467  SolverCreatePtr_ = &Ifpack_Hypre::Hypre_AMSCreate;
468  SolverDestroyPtr_ = &HYPRE_AMSDestroy;
469  SolverSetupPtr_ = &HYPRE_AMSSetup;
470  SolverSolvePtr_ = &HYPRE_AMSSolve;
471  SolverPrecondPtr_ = NULL;
472  break;
473  case Hybrid:
474  if(IsSolverSetup_[0]){
475  SolverDestroyPtr_(Solver_);
476  IsSolverSetup_[0] = false;
477  }
478  SolverCreatePtr_ = &Ifpack_Hypre::Hypre_ParCSRHybridCreate;
479  SolverDestroyPtr_ = &HYPRE_ParCSRHybridDestroy;
480  SolverSetupPtr_ = &HYPRE_ParCSRHybridSetup;
481  SolverSolvePtr_ = &HYPRE_ParCSRHybridSolve;
482  SolverPrecondPtr_ = &HYPRE_ParCSRHybridSetPrecond;
483  break;
484  case PCG:
485  if(IsSolverSetup_[0]){
486  SolverDestroyPtr_(Solver_);
487  IsSolverSetup_[0] = false;
488  }
489  SolverCreatePtr_ = &Ifpack_Hypre::Hypre_ParCSRPCGCreate;
490  SolverDestroyPtr_ = &HYPRE_ParCSRPCGDestroy;
491  SolverSetupPtr_ = &HYPRE_ParCSRPCGSetup;
492  SolverSolvePtr_ = &HYPRE_ParCSRPCGSolve;
493  SolverPrecondPtr_ = &HYPRE_ParCSRPCGSetPrecond;
494  break;
495  case GMRES:
496  if(IsSolverSetup_[0]){
497  SolverDestroyPtr_(Solver_);
498  IsSolverSetup_[0] = false;
499  }
500  SolverCreatePtr_ = &Ifpack_Hypre::Hypre_ParCSRGMRESCreate;
501  SolverDestroyPtr_ = &HYPRE_ParCSRGMRESDestroy;
502  SolverSetupPtr_ = &HYPRE_ParCSRGMRESSetup;
503  SolverPrecondPtr_ = &HYPRE_ParCSRGMRESSetPrecond;
504  break;
505  case FlexGMRES:
506  if(IsSolverSetup_[0]){
507  SolverDestroyPtr_(Solver_);
508  IsSolverSetup_[0] = false;
509  }
510  SolverCreatePtr_ = &Ifpack_Hypre::Hypre_ParCSRFlexGMRESCreate;
511  SolverDestroyPtr_ = &HYPRE_ParCSRFlexGMRESDestroy;
512  SolverSetupPtr_ = &HYPRE_ParCSRFlexGMRESSetup;
513  SolverSolvePtr_ = &HYPRE_ParCSRFlexGMRESSolve;
514  SolverPrecondPtr_ = &HYPRE_ParCSRFlexGMRESSetPrecond;
515  break;
516  case LGMRES:
517  if(IsSolverSetup_[0]){
518  SolverDestroyPtr_(Solver_);
519  IsSolverSetup_[0] = false;
520  }
521  SolverCreatePtr_ = &Ifpack_Hypre::Hypre_ParCSRLGMRESCreate;
522  SolverDestroyPtr_ = &HYPRE_ParCSRLGMRESDestroy;
523  SolverSetupPtr_ = &HYPRE_ParCSRLGMRESSetup;
524  SolverSolvePtr_ = &HYPRE_ParCSRLGMRESSolve;
525  SolverPrecondPtr_ = &HYPRE_ParCSRLGMRESSetPrecond;
526  break;
527  case BiCGSTAB:
528  if(IsSolverSetup_[0]){
529  SolverDestroyPtr_(Solver_);
530  IsSolverSetup_[0] = false;
531  }
532  SolverCreatePtr_ = &Ifpack_Hypre::Hypre_ParCSRBiCGSTABCreate;
533  SolverDestroyPtr_ = &HYPRE_ParCSRBiCGSTABDestroy;
534  SolverSetupPtr_ = &HYPRE_ParCSRBiCGSTABSetup;
535  SolverSolvePtr_ = &HYPRE_ParCSRBiCGSTABSolve;
536  SolverPrecondPtr_ = &HYPRE_ParCSRBiCGSTABSetPrecond;
537  break;
538  default:
539  return -1;
540  }
541  CreateSolver();
542  return 0;
543 } //SetSolverType()
544 
545 //==============================================================================
546 int Ifpack_Hypre::SetPrecondType(Hypre_Solver Precond){
547  switch(Precond) {
548  case BoomerAMG:
549  if(IsPrecondSetup_[0]){
550  PrecondDestroyPtr_(Preconditioner_);
551  IsPrecondSetup_[0] = false;
552  }
553  PrecondCreatePtr_ = &Ifpack_Hypre::Hypre_BoomerAMGCreate;
554  PrecondDestroyPtr_ = &HYPRE_BoomerAMGDestroy;
555  PrecondSetupPtr_ = &HYPRE_BoomerAMGSetup;
556  PrecondSolvePtr_ = &HYPRE_BoomerAMGSolve;
557  break;
558  case ParaSails:
559  if(IsPrecondSetup_[0]){
560  PrecondDestroyPtr_(Preconditioner_);
561  IsPrecondSetup_[0] = false;
562  }
563  PrecondCreatePtr_ = &Ifpack_Hypre::Hypre_ParaSailsCreate;
564  PrecondDestroyPtr_ = &HYPRE_ParaSailsDestroy;
565  PrecondSetupPtr_ = &HYPRE_ParaSailsSetup;
566  PrecondSolvePtr_ = &HYPRE_ParaSailsSolve;
567  break;
568  case Euclid:
569  if(IsPrecondSetup_[0]){
570  PrecondDestroyPtr_(Preconditioner_);
571  IsPrecondSetup_[0] = false;
572  }
573  PrecondCreatePtr_ = &Ifpack_Hypre::Hypre_EuclidCreate;
574  PrecondDestroyPtr_ = &HYPRE_EuclidDestroy;
575  PrecondSetupPtr_ = &HYPRE_EuclidSetup;
576  PrecondSolvePtr_ = &HYPRE_EuclidSolve;
577  break;
578  case AMS:
579  if(IsPrecondSetup_[0]){
580  PrecondDestroyPtr_(Preconditioner_);
581  IsPrecondSetup_[0] = false;
582  }
583  PrecondCreatePtr_ = &Ifpack_Hypre::Hypre_AMSCreate;
584  PrecondDestroyPtr_ = &HYPRE_AMSDestroy;
585  PrecondSetupPtr_ = &HYPRE_AMSSetup;
586  PrecondSolvePtr_ = &HYPRE_AMSSolve;
587  break;
588  default:
589  return -1;
590  }
591  CreatePrecond();
592  return 0;
593 
594 } //SetPrecondType()
595 
596 //==============================================================================
597 int Ifpack_Hypre::CreateSolver(){
598  MPI_Comm comm;
599  HYPRE_ParCSRMatrixGetComm(ParMatrix_, &comm);
600  return (this->*SolverCreatePtr_)(comm, &Solver_);
601 } //CreateSolver()
602 
603 //==============================================================================
604 int Ifpack_Hypre::CreatePrecond(){
605  MPI_Comm comm;
606  HYPRE_ParCSRMatrixGetComm(ParMatrix_, &comm);
607  return (this->*PrecondCreatePtr_)(comm, &Preconditioner_);
608 } //CreatePrecond()
609 
610 #endif // HAVE_HYPRE && HAVE_MPI