Actual source code: hyppilut.c

  1: #define PETSCKSP_DLL

  3: /*

  5: */

 7:  #include private/pcimpl.h
  9: #include "HYPRE.h"
 10: #include "IJ_mv.h"
 11: #include "parcsr_ls.h"

 14: EXTERN PetscErrorCode MatHYPRE_IJMatrixCreate(Mat,HYPRE_IJMatrix*);
 15: EXTERN PetscErrorCode MatHYPRE_IJMatrixCopy(Mat,HYPRE_IJMatrix);
 16: EXTERN PetscErrorCode VecHYPRE_IJVectorCreate(Vec,HYPRE_IJVector*);

 18: /* 
 19:    Private context (data structure) for the  preconditioner.  
 20: */
 21: typedef struct {
 22:   HYPRE_Solver       hsolver;
 23:   HYPRE_IJMatrix     ij;
 24:   HYPRE_IJVector     b,x;

 26:   PetscErrorCode     (*destroy)(HYPRE_Solver);
 27:   PetscErrorCode     (*solve)(HYPRE_Solver,HYPRE_ParCSRMatrix,HYPRE_ParVector,HYPRE_ParVector);
 28:   PetscErrorCode     (*setup)(HYPRE_Solver,HYPRE_ParCSRMatrix,HYPRE_ParVector,HYPRE_ParVector);
 29: 
 30:   MPI_Comm           comm_hypre;

 32:   /* options for pilut and BoomerAMG*/
 33:   int                maxiter;
 34:   double             tol;
 35:   PetscTruth         applyrichardson;

 37:   /* options for pilut */
 38:   int                factorrowsize;

 40:   /* options for parasails */
 41:   int                nlevels;
 42:   double             threshhold;
 43:   double             filter;
 44:   int                sym;
 45:   double             loadbal;
 46:   int                logging;
 47:   int                ruse;
 48:   int                symt;

 50:   /* options for euclid */
 51:   PetscTruth         bjilu;
 52:   int                levels;

 54:   /* options for euclid and BoomerAMG */
 55:   PetscTruth         printstatistics;

 57:   /* options for BoomerAMG */
 58:   int                maxlevels;
 59:   double             strongthreshold;
 60:   double             maxrowsum;
 61:   int                gridsweeps[4];
 62:   int                coarsentype;
 63:   int                measuretype;
 64:   int                relaxtype[4];
 65:   double             relaxweight;
 66:   double             outerrelaxweight;
 67:   int                relaxorder;
 68:   int                **gridrelaxpoints;
 69:   double             truncfactor;
 70: } PC_HYPRE;


 75: static PetscErrorCode PCSetUp_HYPRE(PC pc)
 76: {
 77:   PC_HYPRE           *jac = (PC_HYPRE*)pc->data;
 78:   PetscErrorCode     ierr;
 79:   HYPRE_ParCSRMatrix hmat;
 80:   HYPRE_ParVector    bv,xv;
 81:   int                hierr;

 84:   if (!jac->ij) { /* create the matrix the first time through */
 85:     MatHYPRE_IJMatrixCreate(pc->pmat,&jac->ij);
 86:   }
 87:   if (!jac->b) {
 88:     Vec vec;
 89:     MatGetVecs(pc->pmat,&vec,0);
 90:     VecHYPRE_IJVectorCreate(vec,&jac->b);
 91:     VecHYPRE_IJVectorCreate(vec,&jac->x);
 92:     VecDestroy(vec);
 93:   }
 94:   MatHYPRE_IJMatrixCopy(pc->pmat,jac->ij);
 95:   HYPRE_IJMatrixGetObject(jac->ij,(void**)&hmat);
 96:   HYPRE_IJVectorGetObject(jac->b,(void**)&bv);
 97:   HYPRE_IJVectorGetObject(jac->x,(void**)&xv);
 98:   h(*jac->setup)(jac->hsolver,hmat,bv,xv);
 99:   if (hierr) SETERRQ1(PETSC_ERR_LIB,"Error in HYPRE setup, error code %d",hierr);
100:   return(0);
101: }

103: /*
104:     Replaces the address where the HYPRE vector points to its data with the address of
105:   PETSc's data. Saves the old address so it can be reset when we are finished with it.
106:   Allows use to get the data into a HYPRE vector without the cost of memcopies 
107: */
108: #define HYPREReplacePointer(b,newvalue,savedvalue) {\
109:    hypre_ParVector *par_vector   = (hypre_ParVector *)hypre_IJVectorObject(((hypre_IJVector*)b));\
110:    hypre_Vector    *local_vector = hypre_ParVectorLocalVector(par_vector);\
111:    savedvalue         = local_vector->data;\
112:    local_vector->data = newvalue;}

116: static PetscErrorCode PCApply_HYPRE(PC pc,Vec b,Vec x)
117: {
118:   PC_HYPRE           *jac = (PC_HYPRE*)pc->data;
119:   PetscErrorCode     ierr;
120:   HYPRE_ParCSRMatrix hmat;
121:   PetscScalar        *bv,*xv;
122:   HYPRE_ParVector    jbv,jxv;
123:   PetscScalar        *sbv,*sxv;
124:   int                hierr;

127:   if (!jac->applyrichardson) {VecSet(x,0.0);}
128:   VecGetArray(b,&bv);
129:   VecGetArray(x,&xv);
130:   HYPREReplacePointer(jac->b,bv,sbv);
131:   HYPREReplacePointer(jac->x,xv,sxv);

133:   HYPRE_IJMatrixGetObject(jac->ij,(void**)&hmat);
134:   HYPRE_IJVectorGetObject(jac->b,(void**)&jbv);
135:   HYPRE_IJVectorGetObject(jac->x,(void**)&jxv);
136:   h(*jac->solve)(jac->hsolver,hmat,jbv,jxv);
137:   /* error code of 1 in boomerAMG merely means convergence not achieved */
138:   if (hierr && (hierr != 1 || jac->solve != HYPRE_BoomerAMGSolve)) SETERRQ1(PETSC_ERR_LIB,"Error in HYPRE solver, error code %d",hierr);
139: 
140:   HYPREReplacePointer(jac->b,sbv,bv);
141:   HYPREReplacePointer(jac->x,sxv,xv);
142:   VecRestoreArray(x,&xv);
143:   VecRestoreArray(b,&bv);
144:   return(0);
145: }

149: static PetscErrorCode PCDestroy_HYPRE(PC pc)
150: {
151:   PC_HYPRE       *jac = (PC_HYPRE*)pc->data;

155:   HYPRE_IJMatrixDestroy(jac->ij);
156:   HYPRE_IJVectorDestroy(jac->b);
157:   HYPRE_IJVectorDestroy(jac->x);
158:   (*jac->destroy)(jac->hsolver);
159:   MPI_Comm_free(&(jac->comm_hypre));
160:   PetscFree(jac);
161:   return(0);
162: }

164: /* --------------------------------------------------------------------------------------------*/
167: static PetscErrorCode PCSetFromOptions_HYPRE_Pilut(PC pc)
168: {
169:   PC_HYPRE       *jac = (PC_HYPRE*)pc->data;
171:   PetscTruth     flag;

174:   PetscOptionsHead("HYPRE Pilut Options");
175:   PetscOptionsInt("-pc_hypre_pilut_maxiter","Number of iterations","None",jac->maxiter,&jac->maxiter,&flag);
176:   if (flag) {
177:     HYPRE_ParCSRPilutSetMaxIter(jac->hsolver,jac->maxiter);
178:   }
179:   PetscOptionsReal("-pc_hypre_pilut_tol","Drop tolerance","None",jac->tol,&jac->tol,&flag);
180:   if (flag) {
181:     HYPRE_ParCSRPilutSetDropTolerance(jac->hsolver,jac->tol);
182:   }
183:   PetscOptionsInt("-pc_hypre_pilut_factorrowsize","FactorRowSize","None",jac->factorrowsize,&jac->factorrowsize,&flag);
184:   if (flag) {
185:     HYPRE_ParCSRPilutSetFactorRowSize(jac->hsolver,jac->factorrowsize);
186:   }
187:   PetscOptionsTail();
188:   return(0);
189: }

193: static PetscErrorCode PCView_HYPRE_Pilut(PC pc,PetscViewer viewer)
194: {
195:   PC_HYPRE       *jac = (PC_HYPRE*)pc->data;
197:   PetscTruth     iascii;

200:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
201:   if (iascii) {
202:     PetscViewerASCIIPrintf(viewer,"  HYPRE Pilut preconditioning\n");
203:     if (jac->maxiter != PETSC_DEFAULT) {
204:       PetscViewerASCIIPrintf(viewer,"  HYPRE Pilut: maximum number of iterations %d\n",jac->maxiter);
205:     } else {
206:       PetscViewerASCIIPrintf(viewer,"  HYPRE Pilut: default maximum number of iterations \n");
207:     }
208:     if (jac->tol != PETSC_DEFAULT) {
209:       PetscViewerASCIIPrintf(viewer,"  HYPRE Pilut: drop tolerance %G\n",jac->tol);
210:     } else {
211:       PetscViewerASCIIPrintf(viewer,"  HYPRE Pilut: default drop tolerance \n");
212:     }
213:     if (jac->factorrowsize != PETSC_DEFAULT) {
214:       PetscViewerASCIIPrintf(viewer,"  HYPRE Pilut: factor row size %d\n",jac->factorrowsize);
215:     } else {
216:       PetscViewerASCIIPrintf(viewer,"  HYPRE Pilut: default factor row size \n");
217:     }
218:   }
219:   return(0);
220: }

222: /* --------------------------------------------------------------------------------------------*/
225: static PetscErrorCode PCSetFromOptions_HYPRE_Euclid(PC pc)
226: {
227:   PC_HYPRE       *jac = (PC_HYPRE*)pc->data;
229:   PetscTruth     flag;
230:   char           *args[2];

233:   PetscOptionsHead("HYPRE Euclid Options");
234:   PetscOptionsInt("-pc_hypre_euclid_levels","Number of levels of fill ILU(k)","None",jac->levels,&jac->levels,&flag);
235:   if (flag) {
236:     char levels[16];
237:     if (jac->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Number of levels %d must be nonegative",jac->levels);
238:     sprintf(levels,"%d",jac->levels);
239:     args[0] = (char*)"-level"; args[1] = levels;
240:     HYPRE_EuclidSetParams(jac->hsolver,2,args);
241:   }
242:   PetscOptionsTruth("-pc_hypre_euclid_bj","Use block Jacobi ILU(k)","None",jac->bjilu,&jac->bjilu,PETSC_NULL);
243:   if (jac->bjilu) {
244:     args[0] =(char*) "-bj"; args[1] = (char*)"1";
245:     HYPRE_EuclidSetParams(jac->hsolver,2,args);
246:   }
247: 
248:   PetscOptionsTruth("-pc_hypre_euclid_print_statistics","Print statistics","None",jac->printstatistics,&jac->printstatistics,PETSC_NULL);
249:   if (jac->printstatistics) {
250:     args[0] = (char*)"-eu_stats"; args[1] = (char*)"1";
251:     HYPRE_EuclidSetParams(jac->hsolver,2,args);
252:     args[0] = (char*)"-eu_mem"; args[1] = (char*)"1";
253:     HYPRE_EuclidSetParams(jac->hsolver,2,args);
254:   }
255:   PetscOptionsTail();
256:   return(0);
257: }

261: static PetscErrorCode PCView_HYPRE_Euclid(PC pc,PetscViewer viewer)
262: {
263:   PC_HYPRE       *jac = (PC_HYPRE*)pc->data;
265:   PetscTruth     iascii;

268:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
269:   if (iascii) {
270:     PetscViewerASCIIPrintf(viewer,"  HYPRE Euclid preconditioning\n");
271:     PetscViewerASCIIPrintf(viewer,"  HYPRE Euclid: number of levels %d\n",jac->levels);
272:     if (jac->bjilu) {
273:       PetscViewerASCIIPrintf(viewer,"  HYPRE Euclid: Using block Jacobi ILU instead of parallel ILU\n");
274:     }
275:   }
276:   return(0);
277: }

279: /* --------------------------------------------------------------------------------------------*/

281: static const char *HYPREBoomerAMGCoarsenType[] = {"CLJP","Ruge-Stueben","","modifiedRuge-Stueben","","","Falgout"};
282: static const char *HYPREBoomerAMGMeasureType[] = {"local","global"};
283: static const char *HYPREBoomerAMGRelaxType[]   = {"Jacobi","sequential-Gauss-Seidel","","SOR/Jacobi","backward-SOR/Jacobi","","symmetric-SOR/Jacobi",
284:                                                   "","","Gaussian-elimination"};
287: static PetscErrorCode PCSetFromOptions_HYPRE_BoomerAMG(PC pc)
288: {
289:   PC_HYPRE       *jac = (PC_HYPRE*)pc->data;
291:   int            n,indx;
292:   PetscTruth     flg, tmp_truth;
293:   double         tmpdbl, twodbl[2];

296:   PetscOptionsHead("HYPRE BoomerAMG Options");
297:   PetscOptionsInt("-pc_hypre_boomeramg_max_levels","Number of levels (of grids) allowed","None",jac->maxlevels,&jac->maxlevels,&flg);
298:   if (flg) {
299:     if (jac->maxlevels < 2) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Number of levels %d must be at least two",jac->maxlevels);
300:     HYPRE_BoomerAMGSetMaxLevels(jac->hsolver,jac->maxlevels);
301:   }
302:   PetscOptionsInt("-pc_hypre_boomeramg_max_iter","Maximum iterations used","None",jac->maxiter,&jac->maxiter,&flg);
303:   if (flg) {
304:     if (jac->maxiter < 1) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Number of iterations %d must be at least one",jac->maxiter);
305:     HYPRE_BoomerAMGSetMaxIter(jac->hsolver,jac->maxiter);
306:   }
307:   PetscOptionsScalar("-pc_hypre_boomeramg_tol","Convergence tolerance","None",jac->tol,&jac->tol,&flg);
308:   if (flg) {
309:     if (jac->tol < 0.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Tolerance %G must be great than or equal zero",jac->tol);
310:     HYPRE_BoomerAMGSetTol(jac->hsolver,jac->tol);
311:   }

313:   PetscOptionsScalar("-pc_hypre_boomeramg_truncfactor","Truncation factor","None",jac->truncfactor,&jac->truncfactor,&flg);
314:   if (flg) {
315:     if (jac->truncfactor < 0.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Truncation factor %G must be great than or equal zero",jac->truncfactor);
316:     HYPRE_BoomerAMGSetTruncFactor(jac->hsolver,jac->truncfactor);
317:   }

319:   PetscOptionsScalar("-pc_hypre_boomeramg_strong_threshold","Threshold for being strongly connected","None",jac->strongthreshold,&jac->strongthreshold,&flg);
320:   if (flg) {
321:     if (jac->strongthreshold < 0.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Strong threshold %G must be great than or equal zero",jac->strongthreshold);
322:     HYPRE_BoomerAMGSetStrongThreshold(jac->hsolver,jac->strongthreshold);
323:   }
324:   PetscOptionsScalar("-pc_hypre_boomeramg_max_row_sum","Maximum row sum","None",jac->maxrowsum,&jac->maxrowsum,&flg);
325:   if (flg) {
326:     if (jac->maxrowsum < 0.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Maximum row sum %G must be greater than zero",jac->maxrowsum);
327:     if (jac->maxrowsum > 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Maximum row sum %G must be less than or equal one",jac->maxrowsum);
328:     HYPRE_BoomerAMGSetMaxRowSum(jac->hsolver,jac->maxrowsum);
329:   }

331:   /* Grid sweeps */
332:   PetscOptionsInt("-pc_hypre_boomeramg_grid_sweeps_all","Number of sweeps for all grid levels (fine, up, and down)","None", jac->gridsweeps[0], &indx ,&flg);
333:   if (flg) {
334:     HYPRE_BoomerAMGSetNumSweeps(jac->hsolver,indx);
335:     /* modify the jac structure so we can view the updated options with PC_View */
336:     jac->gridsweeps[0] = indx;
337:     jac->gridsweeps[1] = jac->gridsweeps[2] = jac->gridsweeps[0];
338:     jac->gridsweeps[3] = 1;  /*The coarse level is not affected by this function - hypre code sets to 1*/
339:   }
340:   PetscOptionsInt("-pc_hypre_boomeramg_grid_sweeps_fine","Number of sweeps for the fine level","None", jac->gridsweeps[0], &indx ,&flg);
341:   if (flg) {
342:     HYPRE_BoomerAMGSetCycleNumSweeps(jac->hsolver,indx, 0);
343:     jac->gridsweeps[0] = indx;
344:   }
345:   PetscOptionsInt("-pc_hypre_boomeramg_grid_sweeps_down","Number of sweeps for the down cycles","None",jac->gridsweeps[2], &indx ,&flg);
346:   if (flg) {
347:     HYPRE_BoomerAMGSetCycleNumSweeps(jac->hsolver,indx, 1);
348:     jac->gridsweeps[1] = indx;
349:   }
350:   PetscOptionsInt("-pc_hypre_boomeramg_grid_sweeps_up","Number of sweeps for the up cycles","None", jac->gridsweeps[1], &indx ,&flg);
351:   if (flg) {
352:     HYPRE_BoomerAMGSetCycleNumSweeps(jac->hsolver,indx, 2);
353:     jac->gridsweeps[2] = indx;
354:   }
355:   PetscOptionsInt("-pc_hypre_boomeramg_grid_sweeps_coarse","Number of sweeps for the coarse level","None", jac->gridsweeps[3], &indx ,&flg);
356:   if (flg) {
357:     HYPRE_BoomerAMGSetCycleNumSweeps(jac->hsolver,indx, 3);
358:     jac->gridsweeps[3] = indx;
359:   }

361:   /* Relax type */
362:   PetscOptionsEList("-pc_hypre_boomeramg_relax_type_all","Relax type for fine, up, and down cycles (coarse level set to gaussian elimination)","None",HYPREBoomerAMGRelaxType,10,HYPREBoomerAMGRelaxType[3],&indx,&flg);
363:   if (flg) {
364:     jac->relaxtype[0] = jac->relaxtype[1] = jac->relaxtype[2] = indx;
365:     jac->relaxtype[3] = 9; /* hypre code sets coarse grid to 9 (G.E.)*/
366:     hypre_BoomerAMGSetRelaxType(jac->hsolver, indx);
367:   }
368:   PetscOptionsEList("-pc_hypre_boomeramg_relax_type_fine","Relax type on fine grid","None",HYPREBoomerAMGRelaxType,10,HYPREBoomerAMGRelaxType[3],&indx,&flg);
369:   if (flg) {
370:     jac->relaxtype[0] = indx;
371:     HYPRE_BoomerAMGSetCycleRelaxType(jac->hsolver, indx, 0);
372:   }
373:   PetscOptionsEList("-pc_hypre_boomeramg_relax_type_down","Relax type for the down cycles","None",HYPREBoomerAMGRelaxType,10,HYPREBoomerAMGRelaxType[3],&indx,&flg);
374:   if (flg) {
375:     jac->relaxtype[1] = indx;
376:     HYPRE_BoomerAMGSetCycleRelaxType(jac->hsolver, indx, 1);
377:   }
378:   PetscOptionsEList("-pc_hypre_boomeramg_relax_type_up","Relax type for the up cycles","None",HYPREBoomerAMGRelaxType,10,HYPREBoomerAMGRelaxType[3],&indx,&flg);
379:   if (flg) {
380:     jac->relaxtype[2] = indx;
381:     HYPRE_BoomerAMGSetCycleRelaxType(jac->hsolver, indx, 2);
382:   }
383:   PetscOptionsEList("-pc_hypre_boomeramg_relax_type_coarse","Relax type on coarse grid","None",HYPREBoomerAMGRelaxType,10,HYPREBoomerAMGRelaxType[9],&indx,&flg);
384:   if (flg) {
385:     jac->relaxtype[3] = indx;
386:     HYPRE_BoomerAMGSetCycleRelaxType(jac->hsolver, indx, 3);
387:   }

389:   /* Relaxation Weight */
390:   PetscOptionsReal("-pc_hypre_boomeramg_relax_weight_all","Relaxation weight for all levels (0 = hypre estimates, -k = determined with k CG steps)","None",jac->relaxweight, &tmpdbl ,&flg);
391:   if (flg) {
392:     hypre_BoomerAMGSetRelaxWt( jac->hsolver, tmpdbl);
393:     jac->relaxweight = tmpdbl;
394:   }

396:   n=2;
397:   twodbl[0] = twodbl[1] = 1.0;
398:   PetscOptionsRealArray("-pc_hypre_boomeramg_relax_weight_level","Set the relaxation weight for a particular level (weight,level)","None",twodbl, &n, &flg);
399:   if (flg) {
400:     if (n == 2) {
401:       indx =  (int)PetscAbsReal(twodbl[1]);
402:       hypre_BoomerAMGSetLevelRelaxWt( jac->hsolver, twodbl[0], indx);
403:     } else {
404:       SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Relax weight level: you must provide 2 values separated by a comma (and no space), you provided %d",n);
405:     }
406:   }

408:   /* Outer relaxation Weight */
409:   PetscOptionsReal("-pc_hypre_boomeramg_outer_relax_weight_all","Outer relaxation weight for all levels ( -k = determined with k CG steps)","None",jac->outerrelaxweight, &tmpdbl ,&flg);
410:   if (flg) {
411:     hypre_BoomerAMGSetOuterWt( jac->hsolver, tmpdbl);
412:     jac->outerrelaxweight = tmpdbl;
413:   }

415:   n=2;
416:   twodbl[0] = twodbl[1] = 1.0;
417:   PetscOptionsRealArray("-pc_hypre_boomeramg_outer_relax_weight_level","Set the outer relaxation weight for a particular level (weight,level)","None",twodbl, &n, &flg);
418:   if (flg) {
419:     if (n == 2) {
420:       indx =  (int)PetscAbsReal(twodbl[1]);
421:       hypre_BoomerAMGSetLevelOuterWt( jac->hsolver, twodbl[0], indx);
422:     } else {
423:       SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Relax weight outer level: You must provide 2 values separated by a comma (and no space), you provided %d",n);
424:     }
425:   }

427:   /* the Relax Order */
428:   /* PetscOptionsName("-pc_hypre_boomeramg_no_CF", "Do not use CF-relaxation", "None", &flg); */
429:   PetscOptionsTruth( "-pc_hypre_boomeramg_no_CF", "Do not use CF-relaxation", "None", PETSC_FALSE, &tmp_truth, &flg);

431:   if (flg) {
432:     jac->relaxorder = 0;
433:     hypre_BoomerAMGSetRelaxOrder( jac->hsolver, jac->relaxorder);
434:   }
435:   PetscOptionsEList("-pc_hypre_boomeramg_measure_type","Measure type","None",HYPREBoomerAMGMeasureType,2,HYPREBoomerAMGMeasureType[0],&indx,&flg);
436:   if (flg) {
437:     jac->measuretype = indx;
438:     HYPRE_BoomerAMGSetMeasureType(jac->hsolver,jac->measuretype);
439:   }
440:   PetscOptionsEList("-pc_hypre_boomeramg_coarsen_type","Coarsen type","None",HYPREBoomerAMGCoarsenType,7,HYPREBoomerAMGCoarsenType[6],&indx,&flg);
441:   if (flg) {
442:     jac->coarsentype = indx;
443:     HYPRE_BoomerAMGSetCoarsenType(jac->hsolver,jac->coarsentype);
444:   }
445:   PetscOptionsName("-pc_hypre_boomeramg_print_statistics","Print statistics","None",&flg);
446:   if (flg) {
447:     int level=3;
448:     jac->printstatistics = PETSC_TRUE;
449:     PetscOptionsInt("-pc_hypre_boomeramg_print_statistics","Print statistics","None",level,&level,PETSC_NULL);
450:     HYPRE_BoomerAMGSetPrintLevel(jac->hsolver,level);
451:     HYPRE_BoomerAMGSetDebugFlag(jac->hsolver,level);
452:   }
453:   PetscOptionsTail();
454:   return(0);
455: }

459: static PetscErrorCode PCApplyRichardson_BoomerAMG(PC pc,Vec b,Vec y,Vec w,PetscReal rtol,PetscReal abstol, PetscReal dtol,PetscInt its)
460: {
461:   PC_HYPRE       *jac = (PC_HYPRE*)pc->data;

465:   HYPRE_BoomerAMGSetMaxIter(jac->hsolver,its);
466:   HYPRE_BoomerAMGSetTol(jac->hsolver,rtol);
467:   jac->applyrichardson = PETSC_TRUE;
468:   PCApply_HYPRE(pc,b,y);
469:   jac->applyrichardson = PETSC_FALSE;
470:   HYPRE_BoomerAMGSetTol(jac->hsolver,jac->tol);
471:   HYPRE_BoomerAMGSetMaxIter(jac->hsolver,jac->maxiter);
472:   return(0);
473: }


478: static PetscErrorCode PCView_HYPRE_BoomerAMG(PC pc,PetscViewer viewer)
479: {
480:   PC_HYPRE       *jac = (PC_HYPRE*)pc->data;
482:   PetscTruth     iascii;

485:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
486:   if (iascii) {
487:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG preconditioning\n");
488:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Maximum number of levels %d\n",jac->maxlevels);
489:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Maximum number of iterations %d\n",jac->maxiter);
490:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Convergence tolerance %G\n",jac->tol);
491:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Threshold for strong coupling %G\n",jac->strongthreshold);
492:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Maximum row sums %G\n",jac->maxrowsum);

494:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Sweeps on fine grid %d\n",jac->gridsweeps[0]);
495:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Sweeps down         %d\n",jac->gridsweeps[1]);
496:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Sweeps up           %d\n",jac->gridsweeps[2]);
497:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Sweeps on coarse    %d\n",jac->gridsweeps[3]);

499:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Relax on fine grid  %s\n",HYPREBoomerAMGRelaxType[jac->relaxtype[0]]);
500:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Relax down          %s\n",HYPREBoomerAMGRelaxType[jac->relaxtype[1]]);
501:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Relax up            %s\n",HYPREBoomerAMGRelaxType[jac->relaxtype[2]]);
502:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Relax on coarse     %s\n",HYPREBoomerAMGRelaxType[jac->relaxtype[3]]);

504:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Relax weight  (all)      %G\n",jac->relaxweight);
505:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Outer relax weight (all) %G\n",jac->outerrelaxweight);

507:     if (jac->relaxorder) {
508:       PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Using CF-relaxation\n");
509:     } else {
510:       PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Not using CF-relaxation\n");
511:     }
512:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Measure type        %s\n",HYPREBoomerAMGMeasureType[jac->measuretype]);
513:     PetscViewerASCIIPrintf(viewer,"  HYPRE BoomerAMG: Coarsen type        %s\n",HYPREBoomerAMGCoarsenType[jac->coarsentype]);
514:   }
515:   return(0);
516: }

518: /* --------------------------------------------------------------------------------------------*/
521: static PetscErrorCode PCSetFromOptions_HYPRE_ParaSails(PC pc)
522: {
523:   PC_HYPRE       *jac = (PC_HYPRE*)pc->data;
525:   int            indx;
526:   PetscTruth     flag;
527:   const char     *symtlist[] = {"nonsymmetric","SPD","nonsymmetric,SPD"};

530:   PetscOptionsHead("HYPRE ParaSails Options");
531:   PetscOptionsInt("-pc_hypre_parasails_nlevels","Number of number of levels","None",jac->nlevels,&jac->nlevels,0);
532:   PetscOptionsReal("-pc_hypre_parasails_thresh","Threshold","None",jac->threshhold,&jac->threshhold,&flag);
533:   if (flag) {
534:     HYPRE_ParaSailsSetParams(jac->hsolver,jac->threshhold,jac->nlevels);
535:   }

537:   PetscOptionsReal("-pc_hypre_parasails_filter","filter","None",jac->filter,&jac->filter,&flag);
538:   if (flag) {
539:     HYPRE_ParaSailsSetFilter(jac->hsolver,jac->filter);
540:   }

542:   PetscOptionsReal("-pc_hypre_parasails_loadbal","Load balance","None",jac->loadbal,&jac->loadbal,&flag);
543:   if (flag) {
544:     HYPRE_ParaSailsSetLoadbal(jac->hsolver,jac->loadbal);
545:   }

547:   PetscOptionsTruth("-pc_hypre_parasails_logging","Print info to screen","None",(PetscTruth)jac->logging,(PetscTruth*)&jac->logging,&flag);
548:   if (flag) {
549:     HYPRE_ParaSailsSetLogging(jac->hsolver,jac->logging);
550:   }

552:   PetscOptionsTruth("-pc_hypre_parasails_reuse","Reuse nonzero pattern in preconditioner","None",(PetscTruth)jac->ruse,(PetscTruth*)&jac->ruse,&flag);
553:   if (flag) {
554:     HYPRE_ParaSailsSetReuse(jac->hsolver,jac->ruse);
555:   }

557:   PetscOptionsEList("-pc_hypre_parasails_sym","Symmetry of matrix and preconditioner","None",symtlist,3,symtlist[0],&indx,&flag);
558:   if (flag) {
559:     jac->symt = indx;
560:     HYPRE_ParaSailsSetSym(jac->hsolver,jac->symt);
561:   }

563:   PetscOptionsTail();
564:   return(0);
565: }

569: static PetscErrorCode PCView_HYPRE_ParaSails(PC pc,PetscViewer viewer)
570: {
571:   PC_HYPRE       *jac = (PC_HYPRE*)pc->data;
573:   PetscTruth     iascii;
574:   const char     *symt = 0;;

577:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
578:   if (iascii) {
579:     PetscViewerASCIIPrintf(viewer,"  HYPRE ParaSails preconditioning\n");
580:     PetscViewerASCIIPrintf(viewer,"  HYPRE ParaSails: nlevels %d\n",jac->nlevels);
581:     PetscViewerASCIIPrintf(viewer,"  HYPRE ParaSails: threshold %G\n",jac->threshhold);
582:     PetscViewerASCIIPrintf(viewer,"  HYPRE ParaSails: filter %G\n",jac->filter);
583:     PetscViewerASCIIPrintf(viewer,"  HYPRE ParaSails: load balance %G\n",jac->loadbal);
584:     PetscViewerASCIIPrintf(viewer,"  HYPRE ParaSails: reuse nonzero structure %s\n",PetscTruths[jac->ruse]);
585:     PetscViewerASCIIPrintf(viewer,"  HYPRE ParaSails: print info to screen %s\n",PetscTruths[jac->logging]);
586:     if (!jac->symt) {
587:       symt = "nonsymmetric matrix and preconditioner";
588:     } else if (jac->symt == 1) {
589:       symt = "SPD matrix and preconditioner";
590:     } else if (jac->symt == 2) {
591:       symt = "nonsymmetric matrix but SPD preconditioner";
592:     } else {
593:       SETERRQ1(PETSC_ERR_ARG_WRONG,"Unknown HYPRE ParaSails symmetric option %d",jac->symt);
594:     }
595:     PetscViewerASCIIPrintf(viewer,"  HYPRE ParaSails: %s\n",symt);
596:   }
597:   return(0);
598: }
599: /* ---------------------------------------------------------------------------------*/

604: PetscErrorCode PETSCKSP_DLLEXPORT PCHYPRESetType_HYPRE(PC pc,const char name[])
605: {
606:   PC_HYPRE       *jac = (PC_HYPRE*)pc->data;
608:   PetscTruth     flag;
609:   PetscInt       bs;

612:   if (pc->ops->setup) {
613:     SETERRQ(PETSC_ERR_ORDER,"Cannot set the HYPRE preconditioner type once it has been set");
614:   }

616:   pc->ops->setup          = PCSetUp_HYPRE;
617:   pc->ops->apply          = PCApply_HYPRE;
618:   pc->ops->destroy        = PCDestroy_HYPRE;

620:   jac->maxiter            = PETSC_DEFAULT;
621:   jac->tol                = PETSC_DEFAULT;
622:   jac->printstatistics    = PetscLogPrintInfo;

624:   PetscStrcmp("pilut",name,&flag);
625:   if (flag) {
626:     HYPRE_ParCSRPilutCreate(jac->comm_hypre,&jac->hsolver);
627:     pc->ops->setfromoptions = PCSetFromOptions_HYPRE_Pilut;
628:     pc->ops->view           = PCView_HYPRE_Pilut;
629:     jac->destroy            = HYPRE_ParCSRPilutDestroy;
630:     jac->setup              = HYPRE_ParCSRPilutSetup;
631:     jac->solve              = HYPRE_ParCSRPilutSolve;
632:     jac->factorrowsize      = PETSC_DEFAULT;
633:     return(0);
634:   }
635:   PetscStrcmp("parasails",name,&flag);
636:   if (flag) {
637:     HYPRE_ParaSailsCreate(jac->comm_hypre,&jac->hsolver);
638:     pc->ops->setfromoptions = PCSetFromOptions_HYPRE_ParaSails;
639:     pc->ops->view           = PCView_HYPRE_ParaSails;
640:     jac->destroy            = HYPRE_ParaSailsDestroy;
641:     jac->setup              = HYPRE_ParaSailsSetup;
642:     jac->solve              = HYPRE_ParaSailsSolve;
643:     /* initialize */
644:     jac->nlevels     = 1;
645:     jac->threshhold  = .1;
646:     jac->filter      = .1;
647:     jac->loadbal     = 0;
648:     if (PetscLogPrintInfo) {
649:       jac->logging     = (int) PETSC_TRUE;
650:     } else {
651:       jac->logging     = (int) PETSC_FALSE;
652:     }
653:     jac->ruse = (int) PETSC_FALSE;
654:     jac->symt   = 0;
655:     HYPRE_ParaSailsSetParams(jac->hsolver,jac->threshhold,jac->nlevels);
656:     HYPRE_ParaSailsSetFilter(jac->hsolver,jac->filter);
657:     HYPRE_ParaSailsSetLoadbal(jac->hsolver,jac->loadbal);
658:     HYPRE_ParaSailsSetLogging(jac->hsolver,jac->logging);
659:     HYPRE_ParaSailsSetReuse(jac->hsolver,jac->ruse);
660:     HYPRE_ParaSailsSetSym(jac->hsolver,jac->symt);
661:     return(0);
662:   }
663:   PetscStrcmp("euclid",name,&flag);
664:   if (flag) {
665:     HYPRE_EuclidCreate(jac->comm_hypre,&jac->hsolver);
666:     pc->ops->setfromoptions = PCSetFromOptions_HYPRE_Euclid;
667:     pc->ops->view           = PCView_HYPRE_Euclid;
668:     jac->destroy            = HYPRE_EuclidDestroy;
669:     jac->setup              = HYPRE_EuclidSetup;
670:     jac->solve              = HYPRE_EuclidSolve;
671:     /* initialization */
672:     jac->bjilu              = PETSC_FALSE;
673:     jac->levels             = 1;
674:     return(0);
675:   }
676:   PetscStrcmp("boomeramg",name,&flag);
677:   if (flag) {
678:     HYPRE_BoomerAMGCreate(&jac->hsolver);
679:     pc->ops->setfromoptions  = PCSetFromOptions_HYPRE_BoomerAMG;
680:     pc->ops->view            = PCView_HYPRE_BoomerAMG;
681:     jac->destroy             = HYPRE_BoomerAMGDestroy;
682:     jac->setup               = HYPRE_BoomerAMGSetup;
683:     jac->solve               = HYPRE_BoomerAMGSolve;
684:     pc->ops->applyrichardson = PCApplyRichardson_BoomerAMG;
685:     /* these defaults match the hypre defaults */
686:     jac->maxlevels        = 25;
687:     jac->maxiter          = 1;
688:     jac->tol              = 1.e-7;
689:     jac->strongthreshold  = .25;
690:     jac->maxrowsum        = .9;
691:     jac->coarsentype      = 6;
692:     jac->measuretype      = 0;
693:     jac->applyrichardson  = PETSC_FALSE;
694:     jac->gridsweeps[0]    = jac->gridsweeps[1] = jac->gridsweeps[2] = jac->gridsweeps[3]  = 1;
695:     jac->relaxtype[0]     = jac->relaxtype[1]  = jac->relaxtype[2]  = 3;
696:     jac->relaxtype[3]     = 9;
697:     jac->relaxweight      = 1.0;
698:     jac->outerrelaxweight = 1.0;
699:     jac->relaxorder       = 1;
700:     HYPRE_BoomerAMGSetMaxLevels(jac->hsolver,jac->maxlevels);
701:     HYPRE_BoomerAMGSetMaxIter(jac->hsolver,jac->maxiter);
702:     HYPRE_BoomerAMGSetTol(jac->hsolver,jac->tol);
703:     HYPRE_BoomerAMGSetTruncFactor(jac->hsolver,jac->truncfactor);
704:     HYPRE_BoomerAMGSetStrongThreshold(jac->hsolver,jac->strongthreshold);
705:     HYPRE_BoomerAMGSetMaxRowSum(jac->hsolver,jac->maxrowsum);
706:     HYPRE_BoomerAMGSetMeasureType(jac->hsolver,jac->measuretype);
707:     HYPRE_BoomerAMGSetCoarsenType(jac->hsolver,jac->coarsentype);
708:     MatGetBlockSize(pc->pmat,&bs);
709:     HYPRE_BoomerAMGSetNumFunctions(jac->hsolver,bs);
710:     return(0);
711:   }
712:   SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown HYPRE preconditioner %s; Choices are pilut, parasails, euclid, boomeramg",name);
713:   return(0);
714: }

717: /*
718:     It only gets here if the HYPRE type has not been set before the call to 
719:    ...SetFromOptions() which actually is most of the time
720: */
723: static PetscErrorCode PCSetFromOptions_HYPRE(PC pc)
724: {
726:   int            indx;
727:   const char     *type[] = {"pilut","parasails","boomeramg","euclid"};
728:   PetscTruth     flg;

731:   PetscOptionsHead("HYPRE preconditioner options");
732:   PetscOptionsEList("-pc_hypre_type","HYPRE preconditioner type","PCHYPRESetType",type,4,"pilut",&indx,&flg);
733:   if (PetscOptionsPublishCount) {   /* force the default if it was not yet set and user did not set with option */
734:     if (!flg && !pc->ops->apply) {
735:       flg   = PETSC_TRUE;
736:       indx = 0;
737:     }
738:   }
739:   if (flg) {
740:     PCHYPRESetType_HYPRE(pc,type[indx]);
741:   }
742:   if (pc->ops->setfromoptions) {
743:     pc->ops->setfromoptions(pc);
744:   }
745:   PetscOptionsTail();
746:   return(0);
747: }

751: /*@C
752:      PCHYPRESetType - Sets which hypre preconditioner you wish to use

754:    Input Parameters:
755: +     pc - the preconditioner context
756: -     name - either  pilut, parasails, boomeramg, euclid

758:    Options Database Keys:
759:    -pc_hypre_type - One of pilut, parasails, boomeramg, euclid
760:  
761:    Level: intermediate

763: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC,
764:            PCHYPRE

766: @*/
767: PetscErrorCode PETSCKSP_DLLEXPORT PCHYPRESetType(PC pc,const char name[])
768: {
769:   PetscErrorCode ierr,(*f)(PC,const char[]);

774:   PetscObjectQueryFunction((PetscObject)pc,"PCHYPRESetType_C",(void (**)(void))&f);
775:   if (f) {
776:     (*f)(pc,name);
777:   }
778:   return(0);
779: }

781: /*MC
782:      PCHYPRE - Allows you to use the matrix element based preconditioners in the LLNL package hypre

784:    Options Database Keys:
785: +   -pc_hypre_type - One of pilut, parasails, boomeramg, euclid
786: -   Too many others to list, run with -pc_type hypre -pc_hypre_type XXX -help to see options for the XXX
787:           preconditioner
788:  
789:    Level: intermediate

791:    Notes: Apart from pc_hypre_type (for which there is PCHYPRESetType()),
792:           the many hypre options can ONLY be set via the options database (e.g. the command line
793:           or with PetscOptionsSetValue(), there are no functions to set them)

795:           If you wish to use boomerAMG WITHOUT a Krylov method use -ksp_type richardson NOT -ksp_type preonly
796:           (see the PETSc FAQ.html at the PETSc website under the Documentation tab).

798: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC,
799:            PCHYPRESetType()

801: M*/

806: PetscErrorCode PETSCKSP_DLLEXPORT PCCreate_HYPRE(PC pc)
807: {
808:   PC_HYPRE       *jac;

812:   PetscNew(PC_HYPRE,&jac);
813:   pc->data                 = jac;
814:   pc->ops->setfromoptions  = PCSetFromOptions_HYPRE;
815:   /* Com_dup for hypre */
816:   MPI_Comm_dup(pc->comm,&(jac->comm_hypre));
817:   PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCHYPRESetType_C","PCHYPRESetType_HYPRE",PCHYPRESetType_HYPRE);
818:   return(0);
819: }