Actual source code: ihypre.c

  1: /*
  2:    Provides an interface to the LLNL package hypre
  3: */

  5: #include <petscpkg_version.h>
  6: #include <petsc/private/pcimpl.h>
  7: /* this include is needed ONLY to allow access to the private data inside the Mat object specific to hypre */
  8: #include <petsc/private/matimpl.h>
  9: #include <petsc/private/vecimpl.h>
 10: #include <../src/vec/vec/impls/hypre/vhyp.h>
 11: #include <../src/mat/impls/hypre/mhypre.h>
 12: #include <../src/dm/impls/da/hypre/mhyp.h>
 13: #include <_hypre_parcsr_ls.h>
 14: #include <petscmathypre.h>

 16: #if defined(PETSC_HAVE_HYPRE_DEVICE)
 17: #include <petsc/private/deviceimpl.h>
 18: #endif

 20: static PetscBool  cite            = PETSC_FALSE;
 21: static const char hypreCitation[] = "@manual{hypre-web-page,\n  title  = {{\\sl hypre}: High Performance Preconditioners},\n  organization = {Lawrence Livermore National Laboratory},\n  note  = "
 22:                                     "{\\url{https://www.llnl.gov/casc/hypre}}\n}\n";

 24: /*
 25:    Private context (data structure) for the  preconditioner.
 26: */
 27: typedef struct {
 28:   HYPRE_Solver hsolver;
 29:   Mat          hpmat; /* MatHYPRE */

 31:   HYPRE_Int (*destroy)(HYPRE_Solver);
 32:   HYPRE_Int (*solve)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
 33:   HYPRE_Int (*setup)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);

 35:   MPI_Comm comm_hypre;
 36:   char    *hypre_type;

 38:   /* options for Pilut and BoomerAMG*/
 39:   PetscInt  maxiter;
 40:   PetscReal tol;

 42:   /* options for Pilut */
 43:   PetscInt factorrowsize;

 45:   /* options for ParaSails */
 46:   PetscInt  nlevels;
 47:   PetscReal threshold;
 48:   PetscReal filter;
 49:   PetscReal loadbal;
 50:   PetscInt  logging;
 51:   PetscInt  ruse;
 52:   PetscInt  symt;

 54:   /* options for BoomerAMG */
 55:   PetscBool printstatistics;

 57:   /* options for BoomerAMG */
 58:   PetscInt  cycletype;
 59:   PetscInt  maxlevels;
 60:   PetscReal strongthreshold;
 61:   PetscReal maxrowsum;
 62:   PetscInt  gridsweeps[3];
 63:   PetscObjectParameterDeclare(PetscInt, coarsentype);
 64:   PetscInt  measuretype;
 65:   PetscInt  smoothtype;
 66:   PetscInt  smoothsweeps;
 67:   PetscInt  smoothnumlevels;
 68:   PetscInt  eu_level;         /* Number of levels for ILU(k) in Euclid */
 69:   PetscReal eu_droptolerance; /* Drop tolerance for ILU(k) in Euclid */
 70:   PetscInt  eu_bj;            /* Defines use of Block Jacobi ILU in Euclid */
 71:   PetscObjectParameterDeclare(PetscInt, relaxtype[3]);
 72:   PetscReal relaxweight;
 73:   PetscReal outerrelaxweight;
 74:   PetscObjectParameterDeclare(PetscInt, relaxorder);
 75:   PetscReal truncfactor;
 76:   PetscBool applyrichardson;
 77:   PetscInt  pmax;
 78:   PetscObjectParameterDeclare(PetscInt, interptype);
 79:   PetscInt maxc;
 80:   PetscInt minc;
 81: #if PETSC_PKG_HYPRE_VERSION_GE(2, 23, 0)
 82:   PetscObjectParameterDeclarePtr(const char, spgemm_type); // this is a global hypre parameter but is closely associated with BoomerAMG
 83: #endif
 84:   /* GPU */
 85:   PetscObjectParameterDeclare(PetscBool3, keeptranspose);
 86:   PetscInt rap2;
 87:   PetscObjectParameterDeclare(PetscInt, mod_rap2);

 89:   /* AIR */
 90:   PetscInt  Rtype;
 91:   PetscReal Rstrongthreshold;
 92:   PetscReal Rfilterthreshold;
 93:   PetscInt  Adroptype;
 94:   PetscReal Adroptol;

 96:   PetscInt agg_nl;
 97:   PetscObjectParameterDeclare(PetscInt, agg_interptype);
 98:   PetscInt  agg_num_paths;
 99:   PetscBool nodal_relax;
100:   PetscInt  nodal_relax_levels;

102:   PetscInt  nodal_coarsening;
103:   PetscInt  nodal_coarsening_diag;
104:   PetscInt  vec_interp_variant;
105:   PetscInt  vec_interp_qmax;
106:   PetscBool vec_interp_smooth;
107:   PetscInt  interp_refine;

109:   /* NearNullSpace support */
110:   VecHYPRE_IJVector *hmnull;
111:   HYPRE_ParVector   *phmnull;
112:   PetscInt           n_hmnull;
113:   Vec                hmnull_constant;

115:   /* options for AS (Auxiliary Space preconditioners) */
116:   PetscInt  as_print;
117:   PetscInt  as_max_iter;
118:   PetscReal as_tol;
119:   PetscInt  as_relax_type;
120:   PetscInt  as_relax_times;
121:   PetscReal as_relax_weight;
122:   PetscReal as_omega;
123:   PetscInt  as_amg_alpha_opts[5]; /* AMG coarsen type, agg_levels, relax_type, interp_type, Pmax for vector Poisson (AMS) or Curl problem (ADS) */
124:   PetscReal as_amg_alpha_theta;   /* AMG strength for vector Poisson (AMS) or Curl problem (ADS) */
125:   PetscInt  as_amg_beta_opts[5];  /* AMG coarsen type, agg_levels, relax_type, interp_type, Pmax for scalar Poisson (AMS) or vector Poisson (ADS) */
126:   PetscReal as_amg_beta_theta;    /* AMG strength for scalar Poisson (AMS) or vector Poisson (ADS)  */
127:   PetscInt  ams_cycle_type;
128:   PetscInt  ads_cycle_type;

130:   /* additional data */
131:   Mat G;             /* MatHYPRE */
132:   Mat C;             /* MatHYPRE */
133:   Mat alpha_Poisson; /* MatHYPRE */
134:   Mat beta_Poisson;  /* MatHYPRE */

136:   /* extra information for AMS */
137:   PetscInt          dim; /* geometrical dimension */
138:   VecHYPRE_IJVector coords[3];
139:   VecHYPRE_IJVector constants[3];
140:   VecHYPRE_IJVector interior;
141:   Mat               RT_PiFull, RT_Pi[3];
142:   Mat               ND_PiFull, ND_Pi[3];
143:   PetscBool         ams_beta_is_zero;
144:   PetscBool         ams_beta_is_zero_part;
145:   PetscInt          ams_proj_freq;
146: } PC_HYPRE;

148: /*
149:   Matrices with AIJ format are created IN PLACE with using (I,J,data) from BoomerAMG. Since the data format in hypre_ParCSRMatrix
150:   is different from that used in PETSc, the original hypre_ParCSRMatrix can not be used any more after call this routine.
151:   It is used in PCHMG. Other users should avoid using this function.
152: */
153: static PetscErrorCode PCGetCoarseOperators_BoomerAMG(PC pc, PetscInt *nlevels, Mat *operators[])
154: {
155:   PC_HYPRE            *jac = (PC_HYPRE *)pc->data;
156:   PetscBool            same;
157:   PetscInt             num_levels;
158:   Mat                 *mattmp;
159:   hypre_ParCSRMatrix **A_array;

161:   PetscFunctionBegin;
162:   PetscCall(PetscStrcmp(jac->hypre_type, "boomeramg", &same));
163:   PetscCheck(same, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_NOTSAMETYPE, "Hypre type is not BoomerAMG");
164:   num_levels = hypre_ParAMGDataNumLevels((hypre_ParAMGData *)jac->hsolver);
165:   PetscCall(PetscMalloc1(num_levels, &mattmp));
166:   A_array = hypre_ParAMGDataAArray((hypre_ParAMGData *)jac->hsolver);
167:   for (PetscInt l = 1; l < num_levels; l++) {
168:     PetscCall(MatCreateFromParCSR(A_array[l], MATAIJ, PETSC_OWN_POINTER, &mattmp[num_levels - 1 - l]));
169:     /* We want to own the data, and HYPRE can not touch this matrix any more */
170:     A_array[l] = NULL;
171:   }
172:   *nlevels   = num_levels;
173:   *operators = mattmp;
174:   PetscFunctionReturn(PETSC_SUCCESS);
175: }

177: /*
178:   Matrices with AIJ format are created IN PLACE with using (I,J,data) from BoomerAMG. Since the data format in hypre_ParCSRMatrix
179:   is different from that used in PETSc, the original hypre_ParCSRMatrix can not be used any more after call this routine.
180:   It is used in PCHMG. Other users should avoid using this function.
181: */
182: static PetscErrorCode PCGetInterpolations_BoomerAMG(PC pc, PetscInt *nlevels, Mat *interpolations[])
183: {
184:   PC_HYPRE            *jac = (PC_HYPRE *)pc->data;
185:   PetscBool            same;
186:   PetscInt             num_levels;
187:   Mat                 *mattmp;
188:   hypre_ParCSRMatrix **P_array;

190:   PetscFunctionBegin;
191:   PetscCall(PetscStrcmp(jac->hypre_type, "boomeramg", &same));
192:   PetscCheck(same, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_NOTSAMETYPE, "Hypre type is not BoomerAMG");
193:   num_levels = hypre_ParAMGDataNumLevels((hypre_ParAMGData *)jac->hsolver);
194:   PetscCall(PetscMalloc1(num_levels, &mattmp));
195:   P_array = hypre_ParAMGDataPArray((hypre_ParAMGData *)jac->hsolver);
196:   for (PetscInt l = 1; l < num_levels; l++) {
197:     PetscCall(MatCreateFromParCSR(P_array[num_levels - 1 - l], MATAIJ, PETSC_OWN_POINTER, &mattmp[l - 1]));
198:     /* We want to own the data, and HYPRE can not touch this matrix any more */
199:     P_array[num_levels - 1 - l] = NULL;
200:   }
201:   *nlevels        = num_levels;
202:   *interpolations = mattmp;
203:   PetscFunctionReturn(PETSC_SUCCESS);
204: }

206: /*
207:   Boolean Vecs are created IN PLACE with using data from BoomerAMG.
208: */
209: static PetscErrorCode PCHYPREGetCFMarkers_BoomerAMG(PC pc, PetscInt *n_per_level[], PetscBT *CFMarkers[])
210: {
211:   PC_HYPRE        *jac = (PC_HYPRE *)pc->data;
212:   PetscBool        same;
213:   PetscInt         num_levels, fine_nodes = 0, coarse_nodes;
214:   PetscInt        *n_per_temp;
215:   PetscBT         *markertmp;
216:   hypre_IntArray **CF_marker_array;

218:   PetscFunctionBegin;
219:   PetscCall(PetscStrcmp(jac->hypre_type, "boomeramg", &same));
220:   PetscCheck(same, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_NOTSAMETYPE, "Hypre type is not BoomerAMG");
221:   num_levels = hypre_ParAMGDataNumLevels((hypre_ParAMGData *)jac->hsolver);
222:   PetscCall(PetscMalloc1(num_levels, &n_per_temp));
223:   PetscCall(PetscMalloc1(num_levels - 1, &markertmp));
224:   CF_marker_array = hypre_ParAMGDataCFMarkerArray((hypre_ParAMGData *)jac->hsolver);
225:   for (PetscInt l = 0, CFMaxIndex = num_levels - 2; CFMaxIndex >= 0; l++, CFMaxIndex--) {
226:     fine_nodes   = hypre_IntArraySize(CF_marker_array[CFMaxIndex]);
227:     coarse_nodes = 0;
228:     PetscCall(PetscBTCreate(fine_nodes, &markertmp[l]));
229:     for (PetscInt k = 0; k < fine_nodes; k++) {
230:       if (hypre_IntArrayDataI(CF_marker_array[CFMaxIndex], k) > 0) {
231:         PetscCall(PetscBTSet(markertmp[l], k));
232:         coarse_nodes++;
233:       }
234:     }
235:     n_per_temp[l] = coarse_nodes;
236:   }
237:   n_per_temp[num_levels - 1] = fine_nodes;
238:   *n_per_level               = n_per_temp;
239:   *CFMarkers                 = markertmp;
240:   PetscFunctionReturn(PETSC_SUCCESS);
241: }

243: /* Resets (frees) Hypre's representation of the near null space */
244: static PetscErrorCode PCHYPREResetNearNullSpace_Private(PC pc)
245: {
246:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
247:   PetscInt  i;

249:   PetscFunctionBegin;
250:   for (i = 0; i < jac->n_hmnull; i++) PetscCall(VecHYPRE_IJVectorDestroy(&jac->hmnull[i]));
251:   PetscCall(PetscFree(jac->hmnull));
252:   PetscCall(PetscFree(jac->phmnull));
253:   PetscCall(VecDestroy(&jac->hmnull_constant));
254:   jac->n_hmnull = 0;
255:   PetscFunctionReturn(PETSC_SUCCESS);
256: }

258: static const char    *HYPRESpgemmTypes[] = {"cusparse", "hypre"};
259: static PetscErrorCode PCMGGalerkinSetMatProductAlgorithm_HYPRE_BoomerAMG(PC pc, const char name[])
260: {
261:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;

263: #if PETSC_PKG_HYPRE_VERSION_GE(2, 23, 0)
264:   PetscFunctionBegin;
265:   jac->spgemm_type = name;
266:   PetscFunctionReturn(PETSC_SUCCESS);
267: #endif
268: }

270: static PetscErrorCode PCSetUp_HYPRE(PC pc)
271: {
272:   PC_HYPRE          *jac = (PC_HYPRE *)pc->data;
273:   Mat_HYPRE         *hjac;
274:   HYPRE_ParCSRMatrix hmat;
275:   HYPRE_ParVector    bv, xv;
276:   PetscBool          ishypre;

278:   PetscFunctionBegin;
279:   /* default type is boomerAMG */
280:   if (!jac->hypre_type) PetscCall(PCHYPRESetType(pc, "boomeramg"));

282:   /* get hypre matrix */
283:   if (pc->flag == DIFFERENT_NONZERO_PATTERN) PetscCall(MatDestroy(&jac->hpmat));
284:   PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATHYPRE, &ishypre));
285:   if (!ishypre) {
286: #if defined(PETSC_HAVE_HYPRE_DEVICE) && PETSC_PKG_HYPRE_VERSION_LE(2, 30, 0)
287:     /* Temporary fix since we do not support MAT_REUSE_MATRIX with HYPRE device */
288:     PetscBool iscuda, iship, iskokkos;

290:     PetscCall(PetscObjectTypeCompareAny((PetscObject)pc->pmat, &iscuda, MATSEQAIJCUSPARSE, MATMPIAIJCUSPARSE, ""));
291:     PetscCall(PetscObjectTypeCompareAny((PetscObject)pc->pmat, &iship, MATSEQAIJHIPSPARSE, MATMPIAIJHIPSPARSE, ""));
292:     PetscCall(PetscObjectTypeCompareAny((PetscObject)pc->pmat, &iskokkos, MATSEQAIJKOKKOS, MATMPIAIJKOKKOS, ""));
293:     if (iscuda || iship || iskokkos) PetscCall(MatDestroy(&jac->hpmat));
294: #endif
295:     PetscCall(MatConvert(pc->pmat, MATHYPRE, jac->hpmat ? MAT_REUSE_MATRIX : MAT_INITIAL_MATRIX, &jac->hpmat));
296:   } else {
297:     PetscCall(PetscObjectReference((PetscObject)pc->pmat));
298:     PetscCall(MatDestroy(&jac->hpmat));
299:     jac->hpmat = pc->pmat;
300:   }

302:   /* allow debug */
303:   PetscCall(MatViewFromOptions(jac->hpmat, NULL, "-pc_hypre_mat_view"));
304:   hjac = (Mat_HYPRE *)jac->hpmat->data;

306:   /* special case for BoomerAMG */
307:   if (jac->setup == HYPRE_BoomerAMGSetup) {
308:     MatNullSpace mnull;
309:     PetscBool    has_const;
310:     PetscInt     bs, nvec, i;
311:     PetscMemType memtype;
312:     const Vec   *vecs;

314:     PetscCall(MatGetCurrentMemType(jac->hpmat, &memtype));
315:     if (PetscMemTypeDevice(memtype)) {
316:       /* GPU defaults
317:          From https://hypre.readthedocs.io/en/latest/solvers-boomeramg.html#gpu-supported-options
318:          and /src/parcsr_ls/par_amg.c
319:          First handle options which users have interfaces for changing */
320:       PetscObjectParameterSetDefault(jac, coarsentype, 8);
321:       PetscObjectParameterSetDefault(jac, relaxorder, 0);
322:       PetscObjectParameterSetDefault(jac, interptype, 6);
323:       PetscObjectParameterSetDefault(jac, relaxtype[0], 18);
324:       PetscObjectParameterSetDefault(jac, relaxtype[1], 18);
325: #if PETSC_PKG_HYPRE_VERSION_GE(2, 23, 0)
326:       PetscObjectParameterSetDefault(jac, spgemm_type, HYPRESpgemmTypes[0]);
327: #endif
328: #if PETSC_PKG_HYPRE_VERSION_GE(2, 18, 0)
329:       PetscObjectParameterSetDefault(jac, keeptranspose, PETSC_BOOL3_TRUE);
330:       PetscObjectParameterSetDefault(jac, mod_rap2, 1);
331: #endif
332:       PetscObjectParameterSetDefault(jac, agg_interptype, 7);
333:     } else {
334:       PetscObjectParameterSetDefault(jac, coarsentype, 6);
335:       PetscObjectParameterSetDefault(jac, relaxorder, 1);
336:       PetscObjectParameterSetDefault(jac, interptype, 0);
337:       PetscObjectParameterSetDefault(jac, relaxtype[0], 6);
338:       PetscObjectParameterSetDefault(jac, relaxtype[1], 6); /* Defaults to SYMMETRIC since in PETSc we are using a PC - most likely with CG */
339: #if PETSC_PKG_HYPRE_VERSION_GE(2, 23, 0)
340:       PetscObjectParameterSetDefault(jac, spgemm_type, "hypre");
341: #endif
342: #if PETSC_PKG_HYPRE_VERSION_GE(2, 18, 0)
343:       PetscObjectParameterSetDefault(jac, keeptranspose, PETSC_BOOL3_FALSE);
344:       PetscObjectParameterSetDefault(jac, mod_rap2, 0);
345: #endif
346:       PetscObjectParameterSetDefault(jac, agg_interptype, 4);
347:     }
348:     PetscCallHYPRE(HYPRE_BoomerAMGSetCycleType(jac->hsolver, (HYPRE_Int)jac->cycletype));
349:     PetscCallHYPRE(HYPRE_BoomerAMGSetMaxLevels(jac->hsolver, (HYPRE_Int)jac->maxlevels));
350:     PetscCallHYPRE(HYPRE_BoomerAMGSetMaxIter(jac->hsolver, (HYPRE_Int)jac->maxiter));
351:     PetscCallHYPRE(HYPRE_BoomerAMGSetTol(jac->hsolver, jac->tol));
352:     PetscCallHYPRE(HYPRE_BoomerAMGSetTruncFactor(jac->hsolver, jac->truncfactor));
353:     PetscCallHYPRE(HYPRE_BoomerAMGSetStrongThreshold(jac->hsolver, jac->strongthreshold));
354:     PetscCallHYPRE(HYPRE_BoomerAMGSetMaxRowSum(jac->hsolver, jac->maxrowsum));
355:     PetscCallHYPRE(HYPRE_BoomerAMGSetMeasureType(jac->hsolver, (HYPRE_Int)jac->measuretype));
356:     PetscCallHYPRE(HYPRE_BoomerAMGSetAggNumLevels(jac->hsolver, (HYPRE_Int)jac->agg_nl));
357:     PetscCallHYPRE(HYPRE_BoomerAMGSetPMaxElmts(jac->hsolver, (HYPRE_Int)jac->pmax));
358:     PetscCallHYPRE(HYPRE_BoomerAMGSetNumPaths(jac->hsolver, (HYPRE_Int)jac->agg_num_paths));
359:     PetscCallHYPRE(HYPRE_BoomerAMGSetCycleNumSweeps(jac->hsolver, (HYPRE_Int)jac->gridsweeps[0], 1));
360:     PetscCallHYPRE(HYPRE_BoomerAMGSetCycleNumSweeps(jac->hsolver, (HYPRE_Int)jac->gridsweeps[1], 2));
361:     PetscCallHYPRE(HYPRE_BoomerAMGSetCycleNumSweeps(jac->hsolver, (HYPRE_Int)jac->gridsweeps[2], 3));
362:     PetscCallHYPRE(HYPRE_BoomerAMGSetMaxCoarseSize(jac->hsolver, (HYPRE_Int)jac->maxc));
363:     PetscCallHYPRE(HYPRE_BoomerAMGSetMinCoarseSize(jac->hsolver, (HYPRE_Int)jac->minc));
364:     PetscCallHYPRE(HYPRE_BoomerAMGSetCoarsenType(jac->hsolver, (HYPRE_Int)jac->coarsentype));
365:     PetscCallHYPRE(HYPRE_BoomerAMGSetRelaxOrder(jac->hsolver, (HYPRE_Int)jac->relaxorder));
366:     PetscCallHYPRE(HYPRE_BoomerAMGSetInterpType(jac->hsolver, (HYPRE_Int)jac->interptype));
367:     PetscCallHYPRE(HYPRE_BoomerAMGSetRelaxType(jac->hsolver, (HYPRE_Int)jac->relaxtype[0]));
368:     PetscCallHYPRE(HYPRE_BoomerAMGSetCycleRelaxType(jac->hsolver, (HYPRE_Int)jac->relaxtype[0], 1));
369:     PetscCallHYPRE(HYPRE_BoomerAMGSetCycleRelaxType(jac->hsolver, (HYPRE_Int)jac->relaxtype[1], 2));
370:     PetscCallHYPRE(HYPRE_BoomerAMGSetCycleRelaxType(jac->hsolver, (HYPRE_Int)jac->relaxtype[2], 3));
371:     /* GPU */
372: #if PETSC_PKG_HYPRE_VERSION_GE(2, 23, 0)
373:     {
374:       PetscBool flg_cusparse, flg_hypre;

376:       PetscCall(PetscStrcmp("cusparse", jac->spgemm_type, &flg_cusparse));
377:       PetscCall(PetscStrcmp("hypre", jac->spgemm_type, &flg_hypre));
378:       if (flg_cusparse) PetscCallHYPRE(HYPRE_SetSpGemmUseCusparse(1));
379:       else if (flg_hypre) PetscCallHYPRE(HYPRE_SetSpGemmUseCusparse(0));
380:       else SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown HYPRE SpGEMM type %s; Choices are cusparse, hypre", jac->spgemm_type);
381:     }
382: #endif
383: #if PETSC_PKG_HYPRE_VERSION_GE(2, 18, 0)
384:     PetscCallHYPRE(HYPRE_BoomerAMGSetKeepTranspose(jac->hsolver, jac->keeptranspose == PETSC_BOOL3_TRUE ? 1 : 0));
385:     PetscCallHYPRE(HYPRE_BoomerAMGSetRAP2(jac->hsolver, (HYPRE_Int)jac->rap2));
386:     PetscCallHYPRE(HYPRE_BoomerAMGSetModuleRAP2(jac->hsolver, (HYPRE_Int)jac->mod_rap2));
387: #endif
388:     PetscCallHYPRE(HYPRE_BoomerAMGSetAggInterpType(jac->hsolver, (HYPRE_Int)jac->agg_interptype));

390:     /* AIR */
391: #if PETSC_PKG_HYPRE_VERSION_GE(2, 18, 0)
392:     PetscCallHYPRE(HYPRE_BoomerAMGSetRestriction(jac->hsolver, (HYPRE_Int)jac->Rtype));
393:     PetscCallHYPRE(HYPRE_BoomerAMGSetStrongThresholdR(jac->hsolver, jac->Rstrongthreshold));
394:     PetscCallHYPRE(HYPRE_BoomerAMGSetFilterThresholdR(jac->hsolver, jac->Rfilterthreshold));
395:     PetscCallHYPRE(HYPRE_BoomerAMGSetADropTol(jac->hsolver, jac->Adroptol));
396:     PetscCallHYPRE(HYPRE_BoomerAMGSetADropType(jac->hsolver, (HYPRE_Int)jac->Adroptype));
397: #endif

399:     PetscCall(MatGetBlockSize(pc->pmat, &bs));
400:     if (bs > 1) PetscCallHYPRE(HYPRE_BoomerAMGSetNumFunctions(jac->hsolver, (HYPRE_Int)bs));
401:     PetscCall(MatGetNearNullSpace(pc->mat, &mnull));
402:     if (mnull) {
403:       PetscCall(PCHYPREResetNearNullSpace_Private(pc));
404:       PetscCall(MatNullSpaceGetVecs(mnull, &has_const, &nvec, &vecs));
405:       PetscCall(PetscMalloc1(nvec + 1, &jac->hmnull));
406:       PetscCall(PetscMalloc1(nvec + 1, &jac->phmnull));
407:       for (i = 0; i < nvec; i++) {
408:         PetscCall(VecHYPRE_IJVectorCreate(vecs[i]->map, &jac->hmnull[i]));
409:         PetscCall(VecHYPRE_IJVectorCopy(vecs[i], jac->hmnull[i]));
410:         PetscCallHYPRE(HYPRE_IJVectorGetObject(jac->hmnull[i]->ij, (void **)&jac->phmnull[i]));
411:       }
412:       if (has_const) {
413:         PetscCall(MatCreateVecs(pc->pmat, &jac->hmnull_constant, NULL));
414:         PetscCall(VecSet(jac->hmnull_constant, 1));
415:         PetscCall(VecNormalize(jac->hmnull_constant, NULL));
416:         PetscCall(VecHYPRE_IJVectorCreate(jac->hmnull_constant->map, &jac->hmnull[nvec]));
417:         PetscCall(VecHYPRE_IJVectorCopy(jac->hmnull_constant, jac->hmnull[nvec]));
418:         PetscCallHYPRE(HYPRE_IJVectorGetObject(jac->hmnull[nvec]->ij, (void **)&jac->phmnull[nvec]));
419:         nvec++;
420:       }
421:       PetscCallHYPRE(HYPRE_BoomerAMGSetInterpVectors(jac->hsolver, (HYPRE_Int)nvec, jac->phmnull));
422:       jac->n_hmnull = nvec;
423:     }
424:   }

426:   /* special case for AMS */
427:   if (jac->setup == HYPRE_AMSSetup) {
428:     Mat_HYPRE         *hm;
429:     HYPRE_ParCSRMatrix parcsr;
430:     PetscCheck(jac->coords[0] || jac->constants[0] || jac->ND_PiFull || (jac->ND_Pi[0] && jac->ND_Pi[1]), PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "HYPRE AMS preconditioner needs either the coordinate vectors via PCSetCoordinates() or the edge constant vectors via PCHYPRESetEdgeConstantVectors() or the interpolation matrix via PCHYPRESetInterpolations()");
431:     if (jac->dim) PetscCallHYPRE(HYPRE_AMSSetDimension(jac->hsolver, (HYPRE_Int)jac->dim));
432:     if (jac->constants[0]) {
433:       HYPRE_ParVector ozz, zoz, zzo = NULL;
434:       PetscCallHYPRE(HYPRE_IJVectorGetObject(jac->constants[0]->ij, (void **)(&ozz)));
435:       PetscCallHYPRE(HYPRE_IJVectorGetObject(jac->constants[1]->ij, (void **)(&zoz)));
436:       if (jac->constants[2]) PetscCallHYPRE(HYPRE_IJVectorGetObject(jac->constants[2]->ij, (void **)(&zzo)));
437:       PetscCallHYPRE(HYPRE_AMSSetEdgeConstantVectors(jac->hsolver, ozz, zoz, zzo));
438:     }
439:     if (jac->coords[0]) {
440:       HYPRE_ParVector coords[3];
441:       coords[0] = NULL;
442:       coords[1] = NULL;
443:       coords[2] = NULL;
444:       if (jac->coords[0]) PetscCallHYPRE(HYPRE_IJVectorGetObject(jac->coords[0]->ij, (void **)(&coords[0])));
445:       if (jac->coords[1]) PetscCallHYPRE(HYPRE_IJVectorGetObject(jac->coords[1]->ij, (void **)(&coords[1])));
446:       if (jac->coords[2]) PetscCallHYPRE(HYPRE_IJVectorGetObject(jac->coords[2]->ij, (void **)(&coords[2])));
447:       PetscCallHYPRE(HYPRE_AMSSetCoordinateVectors(jac->hsolver, coords[0], coords[1], coords[2]));
448:     }
449:     PetscCheck(jac->G, PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "HYPRE AMS preconditioner needs the discrete gradient operator via PCHYPRESetDiscreteGradient");
450:     hm = (Mat_HYPRE *)jac->G->data;
451:     PetscCallHYPRE(HYPRE_IJMatrixGetObject(hm->ij, (void **)(&parcsr)));
452:     PetscCallHYPRE(HYPRE_AMSSetDiscreteGradient(jac->hsolver, parcsr));
453:     if (jac->alpha_Poisson) {
454:       hm = (Mat_HYPRE *)jac->alpha_Poisson->data;
455:       PetscCallHYPRE(HYPRE_IJMatrixGetObject(hm->ij, (void **)(&parcsr)));
456:       PetscCallHYPRE(HYPRE_AMSSetAlphaPoissonMatrix(jac->hsolver, parcsr));
457:     }
458:     if (jac->ams_beta_is_zero) {
459:       PetscCallHYPRE(HYPRE_AMSSetBetaPoissonMatrix(jac->hsolver, NULL));
460:     } else if (jac->beta_Poisson) {
461:       hm = (Mat_HYPRE *)jac->beta_Poisson->data;
462:       PetscCallHYPRE(HYPRE_IJMatrixGetObject(hm->ij, (void **)(&parcsr)));
463:       PetscCallHYPRE(HYPRE_AMSSetBetaPoissonMatrix(jac->hsolver, parcsr));
464:     } else if (jac->ams_beta_is_zero_part) {
465:       if (jac->interior) {
466:         HYPRE_ParVector interior = NULL;
467:         PetscCallHYPRE(HYPRE_IJVectorGetObject(jac->interior->ij, (void **)(&interior)));
468:         PetscCallHYPRE(HYPRE_AMSSetInteriorNodes(jac->hsolver, interior));
469:       } else {
470:         jac->ams_beta_is_zero_part = PETSC_FALSE;
471:       }
472:     }
473:     if (jac->ND_PiFull || (jac->ND_Pi[0] && jac->ND_Pi[1])) {
474:       HYPRE_ParCSRMatrix nd_parcsrfull, nd_parcsr[3];
475:       if (jac->ND_PiFull) {
476:         hm = (Mat_HYPRE *)jac->ND_PiFull->data;
477:         PetscCallHYPRE(HYPRE_IJMatrixGetObject(hm->ij, (void **)(&nd_parcsrfull)));
478:       } else {
479:         nd_parcsrfull = NULL;
480:       }
481:       for (PetscInt i = 0; i < 3; ++i) {
482:         if (jac->ND_Pi[i]) {
483:           hm = (Mat_HYPRE *)jac->ND_Pi[i]->data;
484:           PetscCallHYPRE(HYPRE_IJMatrixGetObject(hm->ij, (void **)(&nd_parcsr[i])));
485:         } else {
486:           nd_parcsr[i] = NULL;
487:         }
488:       }
489:       PetscCallHYPRE(HYPRE_AMSSetInterpolations(jac->hsolver, nd_parcsrfull, nd_parcsr[0], nd_parcsr[1], nd_parcsr[2]));
490:     }
491:   }
492:   /* special case for ADS */
493:   if (jac->setup == HYPRE_ADSSetup) {
494:     Mat_HYPRE         *hm;
495:     HYPRE_ParCSRMatrix parcsr;
496:     if (!jac->coords[0] && !((jac->RT_PiFull || (jac->RT_Pi[0] && jac->RT_Pi[1])) && (jac->ND_PiFull || (jac->ND_Pi[0] && jac->ND_Pi[1])))) {
497:       SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "HYPRE ADS preconditioner needs either the coordinate vectors via PCSetCoordinates() or the interpolation matrices via PCHYPRESetInterpolations");
498:     } else PetscCheck(jac->coords[1] && jac->coords[2], PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "HYPRE ADS preconditioner has been designed for three dimensional problems! For two dimensional problems, use HYPRE AMS instead");
499:     PetscCheck(jac->G, PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "HYPRE ADS preconditioner needs the discrete gradient operator via PCHYPRESetDiscreteGradient");
500:     PetscCheck(jac->C, PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "HYPRE ADS preconditioner needs the discrete curl operator via PCHYPRESetDiscreteGradient");
501:     if (jac->coords[0]) {
502:       HYPRE_ParVector coords[3];
503:       coords[0] = NULL;
504:       coords[1] = NULL;
505:       coords[2] = NULL;
506:       if (jac->coords[0]) PetscCallHYPRE(HYPRE_IJVectorGetObject(jac->coords[0]->ij, (void **)(&coords[0])));
507:       if (jac->coords[1]) PetscCallHYPRE(HYPRE_IJVectorGetObject(jac->coords[1]->ij, (void **)(&coords[1])));
508:       if (jac->coords[2]) PetscCallHYPRE(HYPRE_IJVectorGetObject(jac->coords[2]->ij, (void **)(&coords[2])));
509:       PetscCallHYPRE(HYPRE_ADSSetCoordinateVectors(jac->hsolver, coords[0], coords[1], coords[2]));
510:     }
511:     hm = (Mat_HYPRE *)jac->G->data;
512:     PetscCallHYPRE(HYPRE_IJMatrixGetObject(hm->ij, (void **)(&parcsr)));
513:     PetscCallHYPRE(HYPRE_ADSSetDiscreteGradient(jac->hsolver, parcsr));
514:     hm = (Mat_HYPRE *)jac->C->data;
515:     PetscCallHYPRE(HYPRE_IJMatrixGetObject(hm->ij, (void **)(&parcsr)));
516:     PetscCallHYPRE(HYPRE_ADSSetDiscreteCurl(jac->hsolver, parcsr));
517:     if ((jac->RT_PiFull || (jac->RT_Pi[0] && jac->RT_Pi[1])) && (jac->ND_PiFull || (jac->ND_Pi[0] && jac->ND_Pi[1]))) {
518:       HYPRE_ParCSRMatrix rt_parcsrfull, rt_parcsr[3];
519:       HYPRE_ParCSRMatrix nd_parcsrfull, nd_parcsr[3];
520:       if (jac->RT_PiFull) {
521:         hm = (Mat_HYPRE *)jac->RT_PiFull->data;
522:         PetscCallHYPRE(HYPRE_IJMatrixGetObject(hm->ij, (void **)(&rt_parcsrfull)));
523:       } else {
524:         rt_parcsrfull = NULL;
525:       }
526:       for (PetscInt i = 0; i < 3; ++i) {
527:         if (jac->RT_Pi[i]) {
528:           hm = (Mat_HYPRE *)jac->RT_Pi[i]->data;
529:           PetscCallHYPRE(HYPRE_IJMatrixGetObject(hm->ij, (void **)(&rt_parcsr[i])));
530:         } else {
531:           rt_parcsr[i] = NULL;
532:         }
533:       }
534:       if (jac->ND_PiFull) {
535:         hm = (Mat_HYPRE *)jac->ND_PiFull->data;
536:         PetscCallHYPRE(HYPRE_IJMatrixGetObject(hm->ij, (void **)(&nd_parcsrfull)));
537:       } else {
538:         nd_parcsrfull = NULL;
539:       }
540:       for (PetscInt i = 0; i < 3; ++i) {
541:         if (jac->ND_Pi[i]) {
542:           hm = (Mat_HYPRE *)jac->ND_Pi[i]->data;
543:           PetscCallHYPRE(HYPRE_IJMatrixGetObject(hm->ij, (void **)(&nd_parcsr[i])));
544:         } else {
545:           nd_parcsr[i] = NULL;
546:         }
547:       }
548:       PetscCallHYPRE(HYPRE_ADSSetInterpolations(jac->hsolver, rt_parcsrfull, rt_parcsr[0], rt_parcsr[1], rt_parcsr[2], nd_parcsrfull, nd_parcsr[0], nd_parcsr[1], nd_parcsr[2]));
549:     }
550:   }
551:   PetscCallHYPRE(HYPRE_IJMatrixGetObject(hjac->ij, (void **)&hmat));
552:   PetscCallHYPRE(HYPRE_IJVectorGetObject(hjac->b->ij, (void **)&bv));
553:   PetscCallHYPRE(HYPRE_IJVectorGetObject(hjac->x->ij, (void **)&xv));
554:   PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
555:   PetscCallHYPRE((*jac->setup)(jac->hsolver, hmat, bv, xv));
556:   PetscCall(PetscFPTrapPop());
557:   PetscFunctionReturn(PETSC_SUCCESS);
558: }

560: static PetscErrorCode PCApply_HYPRE(PC pc, Vec b, Vec x)
561: {
562:   PC_HYPRE          *jac  = (PC_HYPRE *)pc->data;
563:   Mat_HYPRE         *hjac = (Mat_HYPRE *)jac->hpmat->data;
564:   HYPRE_ParCSRMatrix hmat;
565:   HYPRE_ParVector    jbv, jxv;

567:   PetscFunctionBegin;
568:   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
569:   if (!jac->applyrichardson) PetscCall(VecSet(x, 0.0));
570:   PetscCall(VecHYPRE_IJVectorPushVecRead(hjac->b, b));
571:   if (jac->applyrichardson) PetscCall(VecHYPRE_IJVectorPushVec(hjac->x, x));
572:   else PetscCall(VecHYPRE_IJVectorPushVecWrite(hjac->x, x));
573:   PetscCallHYPRE(HYPRE_IJMatrixGetObject(hjac->ij, (void **)&hmat));
574:   PetscCallHYPRE(HYPRE_IJVectorGetObject(hjac->b->ij, (void **)&jbv));
575:   PetscCallHYPRE(HYPRE_IJVectorGetObject(hjac->x->ij, (void **)&jxv));
576:   PetscCallExternalVoid(
577:     "Hypre solve", do {
578:       HYPRE_Int hierr = (*jac->solve)(jac->hsolver, hmat, jbv, jxv);
579:       if (hierr) {
580:         PetscCheck(hierr == HYPRE_ERROR_CONV, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in HYPRE solver, error code %d", (int)hierr);
581:         HYPRE_ClearAllErrors();
582:       }
583:     } while (0));

585:   if (jac->setup == HYPRE_AMSSetup && jac->ams_beta_is_zero_part) PetscCallHYPRE(HYPRE_AMSProjectOutGradients(jac->hsolver, jxv));
586:   PetscCall(VecHYPRE_IJVectorPopVec(hjac->x));
587:   PetscCall(VecHYPRE_IJVectorPopVec(hjac->b));
588:   PetscFunctionReturn(PETSC_SUCCESS);
589: }

591: static PetscErrorCode PCMatApply_HYPRE_BoomerAMG(PC pc, Mat B, Mat X)
592: {
593:   PC_HYPRE           *jac  = (PC_HYPRE *)pc->data;
594:   Mat_HYPRE          *hjac = (Mat_HYPRE *)jac->hpmat->data;
595:   hypre_ParCSRMatrix *par_matrix;
596:   HYPRE_ParVector     hb, hx;
597:   const PetscScalar  *b;
598:   PetscScalar        *x;
599:   PetscInt            m, N, lda;
600:   hypre_Vector       *x_local;
601:   PetscMemType        type;

603:   PetscFunctionBegin;
604:   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
605:   PetscCallHYPRE(HYPRE_IJMatrixGetObject(hjac->ij, (void **)&par_matrix));
606:   PetscCall(MatGetLocalSize(B, &m, NULL));
607:   PetscCall(MatGetSize(B, NULL, &N));
608:   PetscCallHYPRE(HYPRE_ParMultiVectorCreate(hypre_ParCSRMatrixComm(par_matrix), hypre_ParCSRMatrixGlobalNumRows(par_matrix), hypre_ParCSRMatrixRowStarts(par_matrix), (HYPRE_Int)N, &hb));
609:   PetscCallHYPRE(HYPRE_ParMultiVectorCreate(hypre_ParCSRMatrixComm(par_matrix), hypre_ParCSRMatrixGlobalNumRows(par_matrix), hypre_ParCSRMatrixRowStarts(par_matrix), (HYPRE_Int)N, &hx));
610:   PetscCall(MatZeroEntries(X));
611:   PetscCall(MatDenseGetArrayReadAndMemType(B, &b, &type));
612:   PetscCall(MatDenseGetLDA(B, &lda));
613:   PetscCheck(lda == m, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Cannot use a LDA different than the number of local rows: % " PetscInt_FMT " != % " PetscInt_FMT, lda, m);
614:   PetscCall(MatDenseGetLDA(X, &lda));
615:   PetscCheck(lda == m, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Cannot use a LDA different than the number of local rows: % " PetscInt_FMT " != % " PetscInt_FMT, lda, m);
616:   x_local = hypre_ParVectorLocalVector(hb);
617:   PetscCallHYPRE(hypre_SeqVectorSetDataOwner(x_local, 0));
618:   hypre_VectorData(x_local) = (HYPRE_Complex *)b;
619:   PetscCall(MatDenseGetArrayWriteAndMemType(X, &x, NULL));
620:   x_local = hypre_ParVectorLocalVector(hx);
621:   PetscCallHYPRE(hypre_SeqVectorSetDataOwner(x_local, 0));
622:   hypre_VectorData(x_local) = (HYPRE_Complex *)x;
623:   PetscCallHYPRE(hypre_ParVectorInitialize_v2(hb, type == PETSC_MEMTYPE_HOST ? HYPRE_MEMORY_HOST : HYPRE_MEMORY_DEVICE));
624:   PetscCallHYPRE(hypre_ParVectorInitialize_v2(hx, type == PETSC_MEMTYPE_HOST ? HYPRE_MEMORY_HOST : HYPRE_MEMORY_DEVICE));
625:   PetscCallExternalVoid(
626:     "Hypre solve", do {
627:       HYPRE_Int hierr = (*jac->solve)(jac->hsolver, par_matrix, hb, hx);
628:       if (hierr) {
629:         PetscCheck(hierr == HYPRE_ERROR_CONV, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in HYPRE solver, error code %d", (int)hierr);
630:         HYPRE_ClearAllErrors();
631:       }
632:     } while (0));
633:   PetscCallHYPRE(HYPRE_ParVectorDestroy(hb));
634:   PetscCallHYPRE(HYPRE_ParVectorDestroy(hx));
635:   PetscCall(MatDenseRestoreArrayReadAndMemType(B, &b));
636:   PetscCall(MatDenseRestoreArrayWriteAndMemType(X, &x));
637:   PetscFunctionReturn(PETSC_SUCCESS);
638: }

640: static PetscErrorCode PCReset_HYPRE(PC pc)
641: {
642:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;

644:   PetscFunctionBegin;
645:   PetscCall(MatDestroy(&jac->hpmat));
646:   PetscCall(MatDestroy(&jac->G));
647:   PetscCall(MatDestroy(&jac->C));
648:   PetscCall(MatDestroy(&jac->alpha_Poisson));
649:   PetscCall(MatDestroy(&jac->beta_Poisson));
650:   PetscCall(MatDestroy(&jac->RT_PiFull));
651:   PetscCall(MatDestroy(&jac->RT_Pi[0]));
652:   PetscCall(MatDestroy(&jac->RT_Pi[1]));
653:   PetscCall(MatDestroy(&jac->RT_Pi[2]));
654:   PetscCall(MatDestroy(&jac->ND_PiFull));
655:   PetscCall(MatDestroy(&jac->ND_Pi[0]));
656:   PetscCall(MatDestroy(&jac->ND_Pi[1]));
657:   PetscCall(MatDestroy(&jac->ND_Pi[2]));
658:   PetscCall(VecHYPRE_IJVectorDestroy(&jac->coords[0]));
659:   PetscCall(VecHYPRE_IJVectorDestroy(&jac->coords[1]));
660:   PetscCall(VecHYPRE_IJVectorDestroy(&jac->coords[2]));
661:   PetscCall(VecHYPRE_IJVectorDestroy(&jac->constants[0]));
662:   PetscCall(VecHYPRE_IJVectorDestroy(&jac->constants[1]));
663:   PetscCall(VecHYPRE_IJVectorDestroy(&jac->constants[2]));
664:   PetscCall(VecHYPRE_IJVectorDestroy(&jac->interior));
665:   PetscCall(PCHYPREResetNearNullSpace_Private(pc));
666:   jac->ams_beta_is_zero      = PETSC_FALSE;
667:   jac->ams_beta_is_zero_part = PETSC_FALSE;
668:   jac->dim                   = 0;
669:   PetscFunctionReturn(PETSC_SUCCESS);
670: }

672: static PetscErrorCode PCDestroy_HYPRE(PC pc)
673: {
674:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;

676:   PetscFunctionBegin;
677:   PetscCall(PCReset_HYPRE(pc));
678:   if (jac->destroy) PetscCallHYPRE((*jac->destroy)(jac->hsolver));
679:   PetscCall(PetscFree(jac->hypre_type));
680:   if (jac->comm_hypre != MPI_COMM_NULL) PetscCall(PetscCommRestoreComm(PetscObjectComm((PetscObject)pc), &jac->comm_hypre));
681:   PetscCall(PetscFree(pc->data));

683:   PetscCall(PetscObjectChangeTypeName((PetscObject)pc, 0));
684:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetType_C", NULL));
685:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPREGetType_C", NULL));
686:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetDiscreteGradient_C", NULL));
687:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetDiscreteCurl_C", NULL));
688:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetInterpolations_C", NULL));
689:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetPoissonMatrix_C", NULL));
690:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetEdgeConstantVectors_C", NULL));
691:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPREAMSSetInteriorNodes_C", NULL));
692:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetInterpolations_C", NULL));
693:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetCoarseOperators_C", NULL));
694:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPREGetCFMarkers_C", NULL));
695:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGalerkinSetMatProductAlgorithm_C", NULL));
696:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGalerkinGetMatProductAlgorithm_C", NULL));
697:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCSetCoordinates_C", NULL));
698:   PetscFunctionReturn(PETSC_SUCCESS);
699: }

701: static PetscErrorCode PCSetFromOptions_HYPRE_Pilut(PC pc, PetscOptionItems PetscOptionsObject)
702: {
703:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
704:   PetscBool flag;

706:   PetscFunctionBegin;
707:   PetscOptionsHeadBegin(PetscOptionsObject, "HYPRE Pilut Options");
708:   PetscCall(PetscOptionsInt("-pc_hypre_pilut_maxiter", "Number of iterations", "None", jac->maxiter, &jac->maxiter, &flag));
709:   if (flag) PetscCallHYPRE(HYPRE_ParCSRPilutSetMaxIter(jac->hsolver, (HYPRE_Int)jac->maxiter));
710:   PetscCall(PetscOptionsReal("-pc_hypre_pilut_tol", "Drop tolerance", "None", jac->tol, &jac->tol, &flag));
711:   if (flag) PetscCallHYPRE(HYPRE_ParCSRPilutSetDropTolerance(jac->hsolver, jac->tol));
712:   PetscCall(PetscOptionsInt("-pc_hypre_pilut_factorrowsize", "FactorRowSize", "None", jac->factorrowsize, &jac->factorrowsize, &flag));
713:   if (flag) PetscCallHYPRE(HYPRE_ParCSRPilutSetFactorRowSize(jac->hsolver, (HYPRE_Int)jac->factorrowsize));
714:   PetscOptionsHeadEnd();
715:   PetscFunctionReturn(PETSC_SUCCESS);
716: }

718: static PetscErrorCode PCView_HYPRE_Pilut(PC pc, PetscViewer viewer)
719: {
720:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
721:   PetscBool isascii;

723:   PetscFunctionBegin;
724:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
725:   if (isascii) {
726:     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE Pilut preconditioning\n"));
727:     if (jac->maxiter != PETSC_DEFAULT) {
728:       PetscCall(PetscViewerASCIIPrintf(viewer, "    maximum number of iterations %" PetscInt_FMT "\n", jac->maxiter));
729:     } else {
730:       PetscCall(PetscViewerASCIIPrintf(viewer, "    default maximum number of iterations \n"));
731:     }
732:     if (jac->tol != PETSC_DEFAULT) {
733:       PetscCall(PetscViewerASCIIPrintf(viewer, "    drop tolerance %g\n", (double)jac->tol));
734:     } else {
735:       PetscCall(PetscViewerASCIIPrintf(viewer, "    default drop tolerance \n"));
736:     }
737:     if (jac->factorrowsize != PETSC_DEFAULT) {
738:       PetscCall(PetscViewerASCIIPrintf(viewer, "    factor row size %" PetscInt_FMT "\n", jac->factorrowsize));
739:     } else {
740:       PetscCall(PetscViewerASCIIPrintf(viewer, "    default factor row size \n"));
741:     }
742:   }
743:   PetscFunctionReturn(PETSC_SUCCESS);
744: }

746: static const char *HYPREILUType[] = {
747:   "Block-Jacobi-ILUk", "Block-Jacobi-ILUT", "", "", "", "", "", "", "", "", /* 0-9 */
748:   "GMRES-ILUk",        "GMRES-ILUT",        "", "", "", "", "", "", "", "", /* 10-19 */
749:   "NSH-ILUk",          "NSH-ILUT",          "", "", "", "", "", "", "", "", /* 20-29 */
750:   "RAS-ILUk",          "RAS-ILUT",          "", "", "", "", "", "", "", "", /* 30-39 */
751:   "ddPQ-GMRES-ILUk",   "ddPQ-GMRES-ILUT",   "", "", "", "", "", "", "", "", /* 40-49 */
752:   "GMRES-ILU0"                                                              /* 50 */
753: };

755: static const char *HYPREILUIterSetup[] = {"default", "async-in-place", "async-explicit", "sync-explicit", "semisync-explicit"};

757: static PetscErrorCode PCSetFromOptions_HYPRE_ILU(PC pc, PetscOptionItems PetscOptionsObject)
758: {
759:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
760:   PetscBool flg;
761:   PetscInt  indx;
762:   PetscReal tmpdbl;
763:   PetscBool tmp_truth;

765:   PetscFunctionBegin;
766:   PetscOptionsHeadBegin(PetscOptionsObject, "HYPRE ILU Options");

768:   /* ILU: ILU Type */
769:   PetscCall(PetscOptionsEList("-pc_hypre_ilu_type", "Choose ILU Type", "None", HYPREILUType, PETSC_STATIC_ARRAY_LENGTH(HYPREILUType), HYPREILUType[0], &indx, &flg));
770:   if (flg) PetscCallHYPRE(HYPRE_ILUSetType(jac->hsolver, (HYPRE_Int)indx));

772:   /* ILU: ILU iterative setup type*/
773:   PetscCall(PetscOptionsEList("-pc_hypre_ilu_iterative_setup_type", "Set ILU iterative setup type", "None", HYPREILUIterSetup, PETSC_STATIC_ARRAY_LENGTH(HYPREILUIterSetup), HYPREILUIterSetup[0], &indx, &flg));
774:   if (flg) PetscCallHYPRE(HYPRE_ILUSetIterativeSetupType(jac->hsolver, (HYPRE_Int)indx));

776:   /* ILU: ILU iterative setup option*/
777:   PetscCall(PetscOptionsInt("-pc_hypre_ilu_iterative_setup_option", "Set ILU iterative setup option", "None", 0, &indx, &flg));
778:   if (flg) PetscCallHYPRE(HYPRE_ILUSetIterativeSetupOption(jac->hsolver, (HYPRE_Int)indx));

780:   /* ILU: ILU iterative setup maxiter */
781:   PetscCall(PetscOptionsInt("-pc_hypre_ilu_iterative_setup_maxiter", "Set ILU iterative setup maximum iteration count", "None", 0, &indx, &flg));
782:   if (flg) PetscCallHYPRE(HYPRE_ILUSetIterativeSetupMaxIter(jac->hsolver, (HYPRE_Int)indx));

784:   /* ILU: ILU iterative setup tolerance */
785:   PetscCall(PetscOptionsReal("-pc_hypre_ilu_iterative_setup_tolerance", "Set ILU iterative setup tolerance", "None", 0, &tmpdbl, &flg));
786:   if (flg) PetscCallHYPRE(HYPRE_ILUSetIterativeSetupTolerance(jac->hsolver, tmpdbl));

788:   /* ILU: ILU Print Level */
789:   PetscCall(PetscOptionsInt("-pc_hypre_ilu_print_level", "Set ILU print level", "None", 0, &indx, &flg));
790:   if (flg) PetscCallHYPRE(HYPRE_ILUSetPrintLevel(jac->hsolver, (HYPRE_Int)indx));

792:   /* ILU: Logging */
793:   PetscCall(PetscOptionsInt("-pc_hypre_ilu_logging", "Set ILU logging level", "None", 0, &indx, &flg));
794:   if (flg) PetscCallHYPRE(HYPRE_ILUSetLogging(jac->hsolver, (HYPRE_Int)indx));

796:   /* ILU: ILU Level */
797:   PetscCall(PetscOptionsInt("-pc_hypre_ilu_level", "Set ILU level", "None", 0, &indx, &flg));
798:   if (flg) PetscCallHYPRE(HYPRE_ILUSetLevelOfFill(jac->hsolver, (HYPRE_Int)indx));

800:   /* ILU: ILU Max NNZ per row */
801:   PetscCall(PetscOptionsInt("-pc_hypre_ilu_max_nnz_per_row", "Set maximum NNZ per row", "None", 0, &indx, &flg));
802:   if (flg) PetscCallHYPRE(HYPRE_ILUSetMaxNnzPerRow(jac->hsolver, (HYPRE_Int)indx));

804:   /* ILU: tolerance */
805:   PetscCall(PetscOptionsReal("-pc_hypre_ilu_tol", "Tolerance for ILU", "None", 0, &tmpdbl, &flg));
806:   if (flg) PetscCallHYPRE(HYPRE_ILUSetTol(jac->hsolver, tmpdbl));

808:   /* ILU: maximum iteration count */
809:   PetscCall(PetscOptionsInt("-pc_hypre_ilu_maxiter", "Set ILU max iterations", "None", 0, &indx, &flg));
810:   if (flg) PetscCallHYPRE(HYPRE_ILUSetMaxIter(jac->hsolver, (HYPRE_Int)indx));

812:   /* ILU: drop threshold */
813:   PetscCall(PetscOptionsReal("-pc_hypre_ilu_drop_threshold", "Drop threshold for ILU", "None", 0, &tmpdbl, &flg));
814:   if (flg) PetscCallHYPRE(HYPRE_ILUSetDropThreshold(jac->hsolver, tmpdbl));

816:   /* ILU: Triangular Solve */
817:   PetscCall(PetscOptionsBool("-pc_hypre_ilu_tri_solve", "Enable triangular solve", "None", PETSC_FALSE, &tmp_truth, &flg));
818:   if (flg) PetscCallHYPRE(HYPRE_ILUSetTriSolve(jac->hsolver, tmp_truth));

820:   /* ILU: Lower Jacobi iteration */
821:   PetscCall(PetscOptionsInt("-pc_hypre_ilu_lower_jacobi_iters", "Set lower Jacobi iteration count", "None", 0, &indx, &flg));
822:   if (flg) PetscCallHYPRE(HYPRE_ILUSetLowerJacobiIters(jac->hsolver, (HYPRE_Int)indx));

824:   /* ILU: Upper Jacobi iteration */
825:   PetscCall(PetscOptionsInt("-pc_hypre_ilu_upper_jacobi_iters", "Set upper Jacobi iteration count", "None", 0, &indx, &flg));
826:   if (flg) PetscCallHYPRE(HYPRE_ILUSetUpperJacobiIters(jac->hsolver, (HYPRE_Int)indx));

828:   /* ILU: local reordering */
829:   PetscCall(PetscOptionsBool("-pc_hypre_ilu_local_reordering", "Enable local reordering", "None", PETSC_FALSE, &tmp_truth, &flg));
830:   if (flg) PetscCallHYPRE(HYPRE_ILUSetLocalReordering(jac->hsolver, tmp_truth));

832:   PetscOptionsHeadEnd();
833:   PetscFunctionReturn(PETSC_SUCCESS);
834: }

836: static PetscErrorCode PCView_HYPRE_ILU(PC pc, PetscViewer viewer)
837: {
838:   PC_HYPRE         *jac      = (PC_HYPRE *)pc->data;
839:   hypre_ParILUData *ilu_data = (hypre_ParILUData *)jac->hsolver;
840:   PetscBool         isascii;
841:   PetscInt          indx;
842:   PetscReal         tmpdbl;
843:   PetscReal        *tmpdbl3;

845:   PetscFunctionBegin;
846:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
847:   if (isascii) {
848:     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE ILU preconditioning\n"));
849:     PetscCallExternalVoid("hypre_ParILUDataIluType", indx = hypre_ParILUDataIluType(ilu_data));
850:     PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU type              %s (%" PetscInt_FMT ")\n", HYPREILUType[indx], indx));
851:     PetscCallExternalVoid("hypre_ParILUDataLfil", indx = hypre_ParILUDataLfil(ilu_data));
852:     PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU level             %" PetscInt_FMT "\n", indx));
853:     PetscCallExternalVoid("hypre_ParILUDataMaxIter", indx = hypre_ParILUDataMaxIter(ilu_data));
854:     PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU max iterations    %" PetscInt_FMT "\n", indx));
855:     PetscCallExternalVoid("hypre_ParILUDataMaxRowNnz", indx = hypre_ParILUDataMaxRowNnz(ilu_data));
856:     PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU max NNZ per row   %" PetscInt_FMT "\n", indx));
857:     PetscCallExternalVoid("hypre_ParILUDataTriSolve", indx = hypre_ParILUDataTriSolve(ilu_data));
858:     PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU triangular solve  %" PetscInt_FMT "\n", indx));
859:     PetscCallExternalVoid("hypre_ParILUDataTol", tmpdbl = hypre_ParILUDataTol(ilu_data));
860:     PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU tolerance         %e\n", tmpdbl));
861:     PetscCallExternalVoid("hypre_ParILUDataDroptol", tmpdbl3 = hypre_ParILUDataDroptol(ilu_data));
862:     PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU drop tolerance    %e / %e / %e\n", tmpdbl3[0], tmpdbl3[1], tmpdbl3[2]));
863:     PetscCallExternalVoid("hypre_ParILUDataReorderingType", indx = hypre_ParILUDataReorderingType(ilu_data));
864:     PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU local reordering  %" PetscInt_FMT "\n", indx));
865:     PetscCallExternalVoid("hypre_ParILUDataLowerJacobiIters", indx = hypre_ParILUDataLowerJacobiIters(ilu_data));
866:     PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU lower Jacobi iterations  %" PetscInt_FMT "\n", indx));
867:     PetscCallExternalVoid("hypre_ParILUDataUpperJacobiIters", indx = hypre_ParILUDataUpperJacobiIters(ilu_data));
868:     PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU upper Jacobi iterations  %" PetscInt_FMT "\n", indx));
869:     PetscCallExternalVoid("hypre_ParILUDataPrintLevel", indx = hypre_ParILUDataPrintLevel(ilu_data));
870:     PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU print level      %" PetscInt_FMT "\n", indx));
871:     PetscCallExternalVoid("hypre_ParILUDataLogging", indx = hypre_ParILUDataLogging(ilu_data));
872:     PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU logging level    %" PetscInt_FMT "\n", indx));
873:     PetscCallExternalVoid("hypre_ParILUDataIterativeSetupType", indx = hypre_ParILUDataIterativeSetupType(ilu_data));
874:     PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU iterative setup type           %s (%" PetscInt_FMT ")\n", HYPREILUIterSetup[indx], indx));
875:     PetscCallExternalVoid("hypre_ParILUDataIterativeSetupOption", indx = hypre_ParILUDataIterativeSetupOption(ilu_data));
876:     PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU iterative setup option         %" PetscInt_FMT "\n", indx));
877:     PetscCallExternalVoid("hypre_ParILUDataIterativeSetupMaxIter", indx = hypre_ParILUDataIterativeSetupMaxIter(ilu_data));
878:     PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU iterative setup max iterations %" PetscInt_FMT "\n", indx));
879:     PetscCallExternalVoid("hypre_ParILUDataIterativeSetupTolerance", tmpdbl = hypre_ParILUDataIterativeSetupTolerance(ilu_data));
880:     PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU iterative setup tolerance      %e\n", tmpdbl));
881:   }
882:   PetscFunctionReturn(PETSC_SUCCESS);
883: }

885: static PetscErrorCode PCSetFromOptions_HYPRE_Euclid(PC pc, PetscOptionItems PetscOptionsObject)
886: {
887:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
888:   PetscBool flag, eu_bj = jac->eu_bj ? PETSC_TRUE : PETSC_FALSE;

890:   PetscFunctionBegin;
891:   PetscOptionsHeadBegin(PetscOptionsObject, "HYPRE Euclid Options");
892:   PetscCall(PetscOptionsInt("-pc_hypre_euclid_level", "Factorization levels", "None", jac->eu_level, &jac->eu_level, &flag));
893:   if (flag) PetscCallHYPRE(HYPRE_EuclidSetLevel(jac->hsolver, (HYPRE_Int)jac->eu_level));

895:   PetscCall(PetscOptionsReal("-pc_hypre_euclid_droptolerance", "Drop tolerance for ILU(k) in Euclid", "None", jac->eu_droptolerance, &jac->eu_droptolerance, &flag));
896:   if (flag) {
897:     PetscMPIInt size;

899:     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
900:     PetscCheck(size == 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "hypre's Euclid does not support a parallel drop tolerance");
901:     PetscCallHYPRE(HYPRE_EuclidSetILUT(jac->hsolver, jac->eu_droptolerance));
902:   }

904:   PetscCall(PetscOptionsBool("-pc_hypre_euclid_bj", "Use Block Jacobi for ILU in Euclid", "None", eu_bj, &eu_bj, &flag));
905:   if (flag) {
906:     jac->eu_bj = eu_bj ? 1 : 0;
907:     PetscCallHYPRE(HYPRE_EuclidSetBJ(jac->hsolver, (HYPRE_Int)jac->eu_bj));
908:   }
909:   PetscOptionsHeadEnd();
910:   PetscFunctionReturn(PETSC_SUCCESS);
911: }

913: static PetscErrorCode PCView_HYPRE_Euclid(PC pc, PetscViewer viewer)
914: {
915:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
916:   PetscBool isascii;

918:   PetscFunctionBegin;
919:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
920:   if (isascii) {
921:     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE Euclid preconditioning\n"));
922:     if (jac->eu_level != PETSC_DEFAULT) {
923:       PetscCall(PetscViewerASCIIPrintf(viewer, "    factorization levels %" PetscInt_FMT "\n", jac->eu_level));
924:     } else {
925:       PetscCall(PetscViewerASCIIPrintf(viewer, "    default factorization levels \n"));
926:     }
927:     PetscCall(PetscViewerASCIIPrintf(viewer, "    drop tolerance %g\n", (double)jac->eu_droptolerance));
928:     PetscCall(PetscViewerASCIIPrintf(viewer, "    use Block-Jacobi? %" PetscInt_FMT "\n", jac->eu_bj));
929:   }
930:   PetscFunctionReturn(PETSC_SUCCESS);
931: }

933: static PetscErrorCode PCApplyTranspose_HYPRE_BoomerAMG(PC pc, Vec b, Vec x)
934: {
935:   PC_HYPRE          *jac  = (PC_HYPRE *)pc->data;
936:   Mat_HYPRE         *hjac = (Mat_HYPRE *)jac->hpmat->data;
937:   HYPRE_ParCSRMatrix hmat;
938:   HYPRE_ParVector    jbv, jxv;

940:   PetscFunctionBegin;
941:   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
942:   PetscCall(VecSet(x, 0.0));
943:   PetscCall(VecHYPRE_IJVectorPushVecRead(hjac->b, b));
944:   PetscCall(VecHYPRE_IJVectorPushVecWrite(hjac->x, x));

946:   PetscCallHYPRE(HYPRE_IJMatrixGetObject(hjac->ij, (void **)&hmat));
947:   PetscCallHYPRE(HYPRE_IJVectorGetObject(hjac->b->ij, (void **)&jbv));
948:   PetscCallHYPRE(HYPRE_IJVectorGetObject(hjac->x->ij, (void **)&jxv));

950:   PetscCallExternalVoid(
951:     "Hypre Transpose solve", do {
952:       HYPRE_Int hierr = HYPRE_BoomerAMGSolveT(jac->hsolver, hmat, jbv, jxv);
953:       if (hierr) {
954:         /* error code of 1 in BoomerAMG merely means convergence not achieved */
955:         PetscCheck(hierr == 1, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in HYPRE solver, error code %d", (int)hierr);
956:         HYPRE_ClearAllErrors();
957:       }
958:     } while (0));

960:   PetscCall(VecHYPRE_IJVectorPopVec(hjac->x));
961:   PetscCall(VecHYPRE_IJVectorPopVec(hjac->b));
962:   PetscFunctionReturn(PETSC_SUCCESS);
963: }

965: static PetscErrorCode PCMGGalerkinGetMatProductAlgorithm_HYPRE_BoomerAMG(PC pc, const char *spgemm[])
966: {
967:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;

969:   PetscFunctionBegin;
971: #if PETSC_PKG_HYPRE_VERSION_GE(2, 23, 0)
972:   *spgemm = jac->spgemm_type;
973: #endif
974:   PetscFunctionReturn(PETSC_SUCCESS);
975: }

977: static const char *HYPREBoomerAMGCycleType[]   = {"", "V", "W"};
978: static const char *HYPREBoomerAMGCoarsenType[] = {"CLJP", "Ruge-Stueben", "", "modifiedRuge-Stueben", "", "", "Falgout", "", "PMIS", "", "HMIS"};
979: static const char *HYPREBoomerAMGMeasureType[] = {"local", "global"};
980: /* The following corresponds to HYPRE_BoomerAMGSetRelaxType which has many missing numbers in the enum */
981: static const char *HYPREBoomerAMGSmoothType[] = {"ILU", "Schwarz-smoothers", "Pilut", "ParaSails", "Euclid"};
982: static const char *HYPREBoomerAMGRelaxType[] = {"Jacobi", "sequential-Gauss-Seidel", "seqboundary-Gauss-Seidel", "SOR/Jacobi", "backward-SOR/Jacobi", "" /* [5] hybrid chaotic Gauss-Seidel (works only with OpenMP) */, "symmetric-SOR/Jacobi", "" /* 7 */, "l1scaled-SOR/Jacobi", "Gaussian-elimination", "" /* 10 */, "" /* 11 */, "" /* 12 */, "l1-Gauss-Seidel" /* nonsymmetric */, "backward-l1-Gauss-Seidel" /* nonsymmetric */, "CG" /* non-stationary */, "Chebyshev", "FCF-Jacobi", "l1scaled-Jacobi"};
983: static const char *HYPREBoomerAMGInterpType[] = {"classical", "", "", "direct", "multipass", "multipass-wts", "ext+i", "ext+i-cc", "standard", "standard-wts", "block", "block-wtd", "FF", "FF1", "ext", "ad-wts", "ext-mm", "ext+i-mm", "ext+e-mm"};

985: static PetscErrorCode PCSetFromOptions_HYPRE_BoomerAMG(PC pc, PetscOptionItems PetscOptionsObject)
986: {
987:   PC_HYPRE   *jac = (PC_HYPRE *)pc->data;
988:   PetscInt    bs, n, indx, level;
989:   PetscBool   flg, tmp_truth;
990:   PetscReal   tmpdbl, twodbl[2];
991:   const char *symtlist[] = {"nonsymmetric", "SPD", "nonsymmetric,SPD"};

993:   PetscFunctionBegin;
994:   PetscOptionsHeadBegin(PetscOptionsObject, "HYPRE BoomerAMG Options");
995:   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_cycle_type", "Cycle type", "None", HYPREBoomerAMGCycleType + 1, 2, HYPREBoomerAMGCycleType[jac->cycletype], &indx, &flg));
996:   if (flg) {
997:     jac->cycletype = indx + 1;
998:     PetscCallHYPRE(HYPRE_BoomerAMGSetCycleType(jac->hsolver, (HYPRE_Int)jac->cycletype));
999:   }
1000:   PetscCall(PetscOptionsBoundedInt("-pc_hypre_boomeramg_max_levels", "Number of levels (of grids) allowed", "None", jac->maxlevels, &jac->maxlevels, &flg, 2));
1001:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetMaxLevels(jac->hsolver, (HYPRE_Int)jac->maxlevels));
1002:   PetscCall(PetscOptionsBoundedInt("-pc_hypre_boomeramg_max_iter", "Maximum iterations used PER hypre call", "None", jac->maxiter, &jac->maxiter, &flg, 1));
1003:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetMaxIter(jac->hsolver, (HYPRE_Int)jac->maxiter));
1004:   PetscCall(PetscOptionsBoundedReal("-pc_hypre_boomeramg_tol", "Convergence tolerance PER hypre call (0.0 = use a fixed number of iterations)", "None", jac->tol, &jac->tol, &flg, 0.0));
1005:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetTol(jac->hsolver, jac->tol));
1006:   bs = 1;
1007:   if (pc->pmat) PetscCall(MatGetBlockSize(pc->pmat, &bs));
1008:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_numfunctions", "Number of functions", "HYPRE_BoomerAMGSetNumFunctions", bs, &bs, &flg));
1009:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetNumFunctions(jac->hsolver, (HYPRE_Int)bs));

1011:   PetscCall(PetscOptionsBoundedReal("-pc_hypre_boomeramg_truncfactor", "Truncation factor for interpolation (0=no truncation)", "None", jac->truncfactor, &jac->truncfactor, &flg, 0.0));
1012:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetTruncFactor(jac->hsolver, jac->truncfactor));

1014:   PetscCall(PetscOptionsBoundedInt("-pc_hypre_boomeramg_P_max", "Max elements per row for interpolation operator (0=unlimited)", "None", jac->pmax, &jac->pmax, &flg, 0));
1015:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetPMaxElmts(jac->hsolver, (HYPRE_Int)jac->pmax));

1017:   PetscCall(PetscOptionsRangeInt("-pc_hypre_boomeramg_agg_nl", "Number of levels of aggressive coarsening", "None", jac->agg_nl, &jac->agg_nl, &flg, 0, jac->maxlevels));
1018:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetAggNumLevels(jac->hsolver, (HYPRE_Int)jac->agg_nl));

1020:   PetscCall(PetscOptionsBoundedInt("-pc_hypre_boomeramg_agg_num_paths", "Number of paths for aggressive coarsening", "None", jac->agg_num_paths, &jac->agg_num_paths, &flg, 1));
1021:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetNumPaths(jac->hsolver, (HYPRE_Int)jac->agg_num_paths));

1023:   PetscCall(PetscOptionsBoundedReal("-pc_hypre_boomeramg_strong_threshold", "Threshold for being strongly connected", "None", jac->strongthreshold, &jac->strongthreshold, &flg, 0.0));
1024:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetStrongThreshold(jac->hsolver, jac->strongthreshold));
1025:   PetscCall(PetscOptionsRangeReal("-pc_hypre_boomeramg_max_row_sum", "Maximum row sum", "None", jac->maxrowsum, &jac->maxrowsum, &flg, 0.0, 1.0));
1026:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetMaxRowSum(jac->hsolver, jac->maxrowsum));

1028:   /* Grid sweeps */
1029:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_grid_sweeps_all", "Number of sweeps for the up and down grid levels", "None", jac->gridsweeps[0], &indx, &flg));
1030:   if (flg) {
1031:     /* modify the jac structure so we can view the updated options with PC_View */
1032:     jac->gridsweeps[0] = indx;
1033:     jac->gridsweeps[1] = indx;
1034:     /*defaults coarse to 1 */
1035:     jac->gridsweeps[2] = 1;
1036:   }
1037:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_nodal_coarsen", "Use a nodal based coarsening 1-6", "HYPRE_BoomerAMGSetNodal", jac->nodal_coarsening, &jac->nodal_coarsening, &flg));
1038:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetNodal(jac->hsolver, (HYPRE_Int)jac->nodal_coarsening));
1039:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_nodal_coarsen_diag", "Diagonal in strength matrix for nodal based coarsening 0-2", "HYPRE_BoomerAMGSetNodalDiag", jac->nodal_coarsening_diag, &jac->nodal_coarsening_diag, &flg));
1040:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetNodalDiag(jac->hsolver, (HYPRE_Int)jac->nodal_coarsening_diag));
1041:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_vec_interp_variant", "Variant of algorithm 1-3", "HYPRE_BoomerAMGSetInterpVecVariant", jac->vec_interp_variant, &jac->vec_interp_variant, &flg));
1042:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetInterpVecVariant(jac->hsolver, (HYPRE_Int)jac->vec_interp_variant));
1043:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_vec_interp_qmax", "Max elements per row for each Q", "HYPRE_BoomerAMGSetInterpVecQMax", jac->vec_interp_qmax, &jac->vec_interp_qmax, &flg));
1044:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetInterpVecQMax(jac->hsolver, (HYPRE_Int)jac->vec_interp_qmax));
1045:   PetscCall(PetscOptionsBool("-pc_hypre_boomeramg_vec_interp_smooth", "Whether to smooth the interpolation vectors", "HYPRE_BoomerAMGSetSmoothInterpVectors", jac->vec_interp_smooth, &jac->vec_interp_smooth, &flg));
1046:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetSmoothInterpVectors(jac->hsolver, jac->vec_interp_smooth));
1047:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_interp_refine", "Preprocess the interpolation matrix through iterative weight refinement", "HYPRE_BoomerAMGSetInterpRefine", jac->interp_refine, &jac->interp_refine, &flg));
1048:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetInterpRefine(jac->hsolver, (HYPRE_Int)jac->interp_refine));
1049:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_grid_sweeps_down", "Number of sweeps for the down cycles", "None", jac->gridsweeps[0], &indx, &flg));
1050:   if (flg) {
1051:     PetscCallHYPRE(HYPRE_BoomerAMGSetCycleNumSweeps(jac->hsolver, (HYPRE_Int)indx, 1));
1052:     jac->gridsweeps[0] = indx;
1053:   }
1054:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_grid_sweeps_up", "Number of sweeps for the up cycles", "None", jac->gridsweeps[1], &indx, &flg));
1055:   if (flg) {
1056:     PetscCallHYPRE(HYPRE_BoomerAMGSetCycleNumSweeps(jac->hsolver, (HYPRE_Int)indx, 2));
1057:     jac->gridsweeps[1] = indx;
1058:   }
1059:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_grid_sweeps_coarse", "Number of sweeps for the coarse level", "None", jac->gridsweeps[2], &indx, &flg));
1060:   if (flg) {
1061:     PetscCallHYPRE(HYPRE_BoomerAMGSetCycleNumSweeps(jac->hsolver, (HYPRE_Int)indx, 3));
1062:     jac->gridsweeps[2] = indx;
1063:   }

1065:   /* Smooth type */
1066:   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_smooth_type", "Enable more complex smoothers", "None", HYPREBoomerAMGSmoothType, PETSC_STATIC_ARRAY_LENGTH(HYPREBoomerAMGSmoothType), HYPREBoomerAMGSmoothType[0], &indx, &flg));
1067:   if (flg) {
1068:     jac->smoothtype = indx;
1069:     PetscCallHYPRE(HYPRE_BoomerAMGSetSmoothType(jac->hsolver, (HYPRE_Int)indx + 5));
1070:     jac->smoothnumlevels = 25;
1071:     PetscCallHYPRE(HYPRE_BoomerAMGSetSmoothNumLevels(jac->hsolver, 25));
1072:   }

1074:   /* Number of smoothing levels */
1075:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_smooth_num_levels", "Number of levels on which more complex smoothers are used", "None", 25, &indx, &flg));
1076:   if (flg && (jac->smoothtype != -1)) {
1077:     jac->smoothnumlevels = indx;
1078:     PetscCallHYPRE(HYPRE_BoomerAMGSetSmoothNumLevels(jac->hsolver, (HYPRE_Int)indx));
1079:   }

1081:   /* Smooth num sweeps */
1082:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_smooth_num_sweeps", "Set number of smoother sweeps", "None", 1, &indx, &flg));
1083:   if (flg && indx > 0) {
1084:     jac->smoothsweeps = indx;
1085:     PetscCallHYPRE(HYPRE_BoomerAMGSetSmoothNumSweeps(jac->hsolver, (HYPRE_Int)indx));
1086:   }

1088:   /* ILU: ILU Type */
1089:   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_ilu_type", "Choose ILU Type", "None", HYPREILUType, PETSC_STATIC_ARRAY_LENGTH(HYPREILUType), HYPREILUType[0], &indx, &flg));
1090:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetILUType(jac->hsolver, (HYPRE_Int)indx));

1092:   /* ILU: ILU iterative setup type*/
1093:   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_ilu_iterative_setup_type", "Set ILU iterative setup type", "None", HYPREILUIterSetup, PETSC_STATIC_ARRAY_LENGTH(HYPREILUIterSetup), HYPREILUIterSetup[0], &indx, &flg));
1094:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetILUIterSetupType(jac->hsolver, (HYPRE_Int)indx));

1096:   /* ILU: ILU iterative setup option*/
1097:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_ilu_iterative_setup_option", "Set ILU iterative setup option", "None", 0, &indx, &flg));
1098:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetILUIterSetupOption(jac->hsolver, (HYPRE_Int)indx));

1100:   /* ILU: ILU iterative setup maxiter */
1101:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_ilu_iterative_setup_maxiter", "Set ILU iterative setup maximum iteration count", "None", 0, &indx, &flg));
1102:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetILUIterSetupMaxIter(jac->hsolver, (HYPRE_Int)indx));

1104:   /* ILU: ILU iterative setup tolerance */
1105:   PetscCall(PetscOptionsReal("-pc_hypre_boomeramg_ilu_iterative_setup_tolerance", "Set ILU iterative setup tolerance", "None", 0, &tmpdbl, &flg));
1106:   if (flg) PetscCallHYPRE(hypre_BoomerAMGSetILUIterSetupTolerance(jac->hsolver, tmpdbl));

1108:   /* ILU: ILU Print Level */
1109:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_ilu_print_level", "Set ILU print level", "None", 0, &indx, &flg));
1110:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetPrintLevel(jac->hsolver, (HYPRE_Int)indx));

1112:   /* ILU: Logging */
1113:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_ilu_logging", "Set ILU logging level", "None", 0, &indx, &flg));
1114:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetLogging(jac->hsolver, (HYPRE_Int)indx));

1116:   /* ILU: ILU Level */
1117:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_ilu_level", "Set ILU level", "None", 0, &indx, &flg));
1118:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetILULevel(jac->hsolver, (HYPRE_Int)indx));

1120:   /* ILU: ILU Max NNZ per row */
1121:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_ilu_max_nnz_per_row", "Set maximum NNZ per row", "None", 0, &indx, &flg));
1122:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetILUMaxRowNnz(jac->hsolver, (HYPRE_Int)indx));

1124:   /* ILU: maximum iteration count */
1125:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_ilu_maxiter", "Set ILU max iterations", "None", 0, &indx, &flg));
1126:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetILUMaxIter(jac->hsolver, (HYPRE_Int)indx));

1128:   /* ILU: drop threshold */
1129:   PetscCall(PetscOptionsReal("-pc_hypre_boomeramg_ilu_drop_tol", "Drop tolerance for ILU", "None", 0, &tmpdbl, &flg));
1130:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetILUDroptol(jac->hsolver, tmpdbl));

1132:   /* ILU: Triangular Solve */
1133:   PetscCall(PetscOptionsBool("-pc_hypre_boomeramg_ilu_tri_solve", "Enable triangular solve", "None", PETSC_FALSE, &tmp_truth, &flg));
1134:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetILUTriSolve(jac->hsolver, tmp_truth));

1136:   /* ILU: Lower Jacobi iteration */
1137:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_ilu_lower_jacobi_iters", "Set lower Jacobi iteration count", "None", 0, &indx, &flg));
1138:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetILULowerJacobiIters(jac->hsolver, (HYPRE_Int)indx));

1140:   /* ILU: Upper Jacobi iteration */
1141:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_ilu_upper_jacobi_iters", "Set upper Jacobi iteration count", "None", 0, &indx, &flg));
1142:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetILUUpperJacobiIters(jac->hsolver, (HYPRE_Int)indx));

1144:   /* ILU: local reordering */
1145:   PetscCall(PetscOptionsBool("-pc_hypre_boomeramg_ilu_local_reordering", "Enable local reordering", "None", PETSC_FALSE, &tmp_truth, &flg));
1146:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetILULocalReordering(jac->hsolver, tmp_truth));

1148:   /* Number of levels for ILU(k) for Euclid */
1149:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_eu_level", "Number of levels for ILU(k) in Euclid smoother", "None", 0, &indx, &flg));
1150:   if (flg && (jac->smoothtype == 4)) {
1151:     jac->eu_level = indx;
1152:     PetscCallHYPRE(HYPRE_BoomerAMGSetEuLevel(jac->hsolver, (HYPRE_Int)indx));
1153:   }

1155:   /* Filter for ILU(k) for Euclid */
1156:   PetscReal droptolerance;
1157:   PetscCall(PetscOptionsReal("-pc_hypre_boomeramg_eu_droptolerance", "Drop tolerance for ILU(k) in Euclid smoother", "None", 0, &droptolerance, &flg));
1158:   if (flg && (jac->smoothtype == 4)) {
1159:     jac->eu_droptolerance = droptolerance;
1160:     PetscCallHYPRE(HYPRE_BoomerAMGSetEuLevel(jac->hsolver, droptolerance));
1161:   }

1163:   /* Use Block Jacobi ILUT for Euclid */
1164:   PetscCall(PetscOptionsBool("-pc_hypre_boomeramg_eu_bj", "Use Block Jacobi for ILU in Euclid smoother?", "None", PETSC_FALSE, &tmp_truth, &flg));
1165:   if (flg && (jac->smoothtype == 4)) {
1166:     jac->eu_bj = tmp_truth;
1167:     PetscCallHYPRE(HYPRE_BoomerAMGSetEuBJ(jac->hsolver, (HYPRE_Int)jac->eu_bj));
1168:   }

1170:   /* Relax type */
1171:   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_relax_type_all", "Relax type for the up and down cycles", "None", HYPREBoomerAMGRelaxType, PETSC_STATIC_ARRAY_LENGTH(HYPREBoomerAMGRelaxType),
1172:                               jac->relaxtype[0] < 0 ? "not yet set" : HYPREBoomerAMGRelaxType[jac->relaxtype[0]], &indx, &flg));
1173:   if (flg) jac->relaxtype[0] = jac->relaxtype[1] = indx;
1174:   PetscCall(
1175:     PetscOptionsEList("-pc_hypre_boomeramg_relax_type_down", "Relax type for the down cycles", "None", HYPREBoomerAMGRelaxType, PETSC_STATIC_ARRAY_LENGTH(HYPREBoomerAMGRelaxType), jac->relaxtype[0] < 0 ? "not yet set" : HYPREBoomerAMGRelaxType[jac->relaxtype[0]], &indx, &flg));
1176:   if (flg) jac->relaxtype[0] = indx;
1177:   PetscCall(
1178:     PetscOptionsEList("-pc_hypre_boomeramg_relax_type_up", "Relax type for the up cycles", "None", HYPREBoomerAMGRelaxType, PETSC_STATIC_ARRAY_LENGTH(HYPREBoomerAMGRelaxType), jac->relaxtype[1] < 0 ? "not yet set" : HYPREBoomerAMGRelaxType[jac->relaxtype[1]], &indx, &flg));
1179:   if (flg) jac->relaxtype[1] = indx;
1180:   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_relax_type_coarse", "Relax type on coarse grid", "None", HYPREBoomerAMGRelaxType, PETSC_STATIC_ARRAY_LENGTH(HYPREBoomerAMGRelaxType), HYPREBoomerAMGRelaxType[jac->relaxtype[2]], &indx, &flg));
1181:   if (flg) jac->relaxtype[2] = indx;

1183:   /* Relaxation Weight */
1184:   PetscCall(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));
1185:   if (flg) {
1186:     PetscCallHYPRE(HYPRE_BoomerAMGSetRelaxWt(jac->hsolver, tmpdbl));
1187:     jac->relaxweight = tmpdbl;
1188:   }

1190:   n         = 2;
1191:   twodbl[0] = twodbl[1] = 1.0;
1192:   PetscCall(PetscOptionsRealArray("-pc_hypre_boomeramg_relax_weight_level", "Set the relaxation weight for a particular level (weight,level)", "None", twodbl, &n, &flg));
1193:   if (flg) {
1194:     PetscCheck(n == 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Relax weight level: you must provide 2 values separated by a comma (and no space), you provided %" PetscInt_FMT, n);
1195:     indx = (int)PetscAbsReal(twodbl[1]);
1196:     PetscCallHYPRE(HYPRE_BoomerAMGSetLevelRelaxWt(jac->hsolver, twodbl[0], (HYPRE_Int)indx));
1197:   }

1199:   /* Outer relaxation Weight */
1200:   PetscCall(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));
1201:   if (flg) {
1202:     PetscCallHYPRE(HYPRE_BoomerAMGSetOuterWt(jac->hsolver, tmpdbl));
1203:     jac->outerrelaxweight = tmpdbl;
1204:   }

1206:   n         = 2;
1207:   twodbl[0] = twodbl[1] = 1.0;
1208:   PetscCall(PetscOptionsRealArray("-pc_hypre_boomeramg_outer_relax_weight_level", "Set the outer relaxation weight for a particular level (weight,level)", "None", twodbl, &n, &flg));
1209:   if (flg) {
1210:     PetscCheck(n == 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Relax weight outer level: You must provide 2 values separated by a comma (and no space), you provided %" PetscInt_FMT, n);
1211:     indx = (int)PetscAbsReal(twodbl[1]);
1212:     PetscCallHYPRE(HYPRE_BoomerAMGSetLevelOuterWt(jac->hsolver, twodbl[0], (HYPRE_Int)indx));
1213:   }

1215:   /* the Relax Order */
1216:   PetscCall(PetscOptionsBool("-pc_hypre_boomeramg_no_CF", "Do not use CF-relaxation", "None", PETSC_FALSE, &tmp_truth, &flg));
1217:   if (flg) jac->relaxorder = !tmp_truth;
1218:   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_measure_type", "Measure type", "None", HYPREBoomerAMGMeasureType, PETSC_STATIC_ARRAY_LENGTH(HYPREBoomerAMGMeasureType), HYPREBoomerAMGMeasureType[0], &indx, &flg));
1219:   if (flg) {
1220:     jac->measuretype = indx;
1221:     PetscCallHYPRE(HYPRE_BoomerAMGSetMeasureType(jac->hsolver, (HYPRE_Int)jac->measuretype));
1222:   }
1223:   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_coarsen_type", "Coarsen type", "None", HYPREBoomerAMGCoarsenType, PETSC_STATIC_ARRAY_LENGTH(HYPREBoomerAMGCoarsenType), jac->coarsentype < 0 ? "unknown" : HYPREBoomerAMGCoarsenType[jac->coarsentype], &indx, &flg));
1224:   if (flg) jac->coarsentype = indx;

1226:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_max_coarse_size", "Maximum size of coarsest grid", "None", jac->maxc, &jac->maxc, &flg));
1227:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetMaxCoarseSize(jac->hsolver, (HYPRE_Int)jac->maxc));
1228:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_min_coarse_size", "Minimum size of coarsest grid", "None", jac->minc, &jac->minc, &flg));
1229:   if (flg) PetscCallHYPRE(HYPRE_BoomerAMGSetMinCoarseSize(jac->hsolver, (HYPRE_Int)jac->minc));
1230: #if PETSC_PKG_HYPRE_VERSION_GE(2, 23, 0)
1231:   // global parameter but is closely associated with BoomerAMG
1232:   PetscCall(PetscOptionsEList("-pc_mg_galerkin_mat_product_algorithm", "Type of SpGEMM to use in hypre (only for now)", "PCMGGalerkinSetMatProductAlgorithm", HYPRESpgemmTypes, PETSC_STATIC_ARRAY_LENGTH(HYPRESpgemmTypes), jac->spgemm_type, &indx, &flg));
1233:   if (flg) PetscCall(PCMGGalerkinSetMatProductAlgorithm_HYPRE_BoomerAMG(pc, HYPRESpgemmTypes[indx]));
1234: #endif
1235:   /* AIR */
1236: #if PETSC_PKG_HYPRE_VERSION_GE(2, 18, 0)
1237:   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_restriction_type", "Type of AIR method (distance 1 or 2, 0 means no AIR)", "None", jac->Rtype, &jac->Rtype, NULL));
1238:   PetscCallHYPRE(HYPRE_BoomerAMGSetRestriction(jac->hsolver, (HYPRE_Int)jac->Rtype));
1239:   if (jac->Rtype) {
1240:     HYPRE_Int **grid_relax_points = hypre_TAlloc(HYPRE_Int *, 4, HYPRE_MEMORY_HOST);
1241:     char       *prerelax[256];
1242:     char       *postrelax[256];
1243:     char        stringF[2] = "F", stringC[2] = "C", stringA[2] = "A";
1244:     PetscInt    ns_down = 256, ns_up = 256;
1245:     PetscBool   matchF, matchC, matchA;

1247:     jac->interptype = 100; /* no way we can pass this with strings... Set it as default as in MFEM, then users can still customize it back to a different one */

1249:     PetscCall(PetscOptionsReal("-pc_hypre_boomeramg_strongthresholdR", "Threshold for R", "None", jac->Rstrongthreshold, &jac->Rstrongthreshold, NULL));
1250:     PetscCallHYPRE(HYPRE_BoomerAMGSetStrongThresholdR(jac->hsolver, jac->Rstrongthreshold));

1252:     PetscCall(PetscOptionsReal("-pc_hypre_boomeramg_filterthresholdR", "Filter threshold for R", "None", jac->Rfilterthreshold, &jac->Rfilterthreshold, NULL));
1253:     PetscCallHYPRE(HYPRE_BoomerAMGSetFilterThresholdR(jac->hsolver, jac->Rfilterthreshold));

1255:     PetscCall(PetscOptionsReal("-pc_hypre_boomeramg_Adroptol", "Defines the drop tolerance for the A-matrices from the 2nd level of AMG", "None", jac->Adroptol, &jac->Adroptol, NULL));
1256:     PetscCallHYPRE(HYPRE_BoomerAMGSetADropTol(jac->hsolver, (HYPRE_Int)jac->Adroptol));

1258:     PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_Adroptype", "Drops the entries that are not on the diagonal and smaller than its row norm: type 1: 1-norm, 2: 2-norm, -1: infinity norm", "None", jac->Adroptype, &jac->Adroptype, NULL));
1259:     PetscCallHYPRE(HYPRE_BoomerAMGSetADropType(jac->hsolver, (HYPRE_Int)jac->Adroptype));
1260:     PetscCall(PetscOptionsStringArray("-pc_hypre_boomeramg_prerelax", "Defines prerelax scheme", "None", prerelax, &ns_down, NULL));
1261:     PetscCall(PetscOptionsStringArray("-pc_hypre_boomeramg_postrelax", "Defines postrelax scheme", "None", postrelax, &ns_up, NULL));
1262:     PetscCheck(ns_down == jac->gridsweeps[0], PetscObjectComm((PetscObject)jac), PETSC_ERR_ARG_SIZ, "The number of arguments passed to -pc_hypre_boomeramg_prerelax must match the number passed to -pc_hypre_bomeramg_grid_sweeps_down");
1263:     PetscCheck(ns_up == jac->gridsweeps[1], PetscObjectComm((PetscObject)jac), PETSC_ERR_ARG_SIZ, "The number of arguments passed to -pc_hypre_boomeramg_postrelax must match the number passed to -pc_hypre_bomeramg_grid_sweeps_up");

1265:     grid_relax_points[0]    = NULL;
1266:     grid_relax_points[1]    = hypre_TAlloc(HYPRE_Int, ns_down, HYPRE_MEMORY_HOST);
1267:     grid_relax_points[2]    = hypre_TAlloc(HYPRE_Int, ns_up, HYPRE_MEMORY_HOST);
1268:     grid_relax_points[3]    = hypre_TAlloc(HYPRE_Int, jac->gridsweeps[2], HYPRE_MEMORY_HOST);
1269:     grid_relax_points[3][0] = 0;

1271:     // set down relax scheme
1272:     for (PetscInt i = 0; i < ns_down; i++) {
1273:       PetscCall(PetscStrcasecmp(prerelax[i], stringF, &matchF));
1274:       PetscCall(PetscStrcasecmp(prerelax[i], stringC, &matchC));
1275:       PetscCall(PetscStrcasecmp(prerelax[i], stringA, &matchA));
1276:       PetscCheck(matchF || matchC || matchA, PetscObjectComm((PetscObject)jac), PETSC_ERR_ARG_WRONG, "Valid argument options for -pc_hypre_boomeramg_prerelax are C, F, and A");
1277:       if (matchF) grid_relax_points[1][i] = -1;
1278:       else if (matchC) grid_relax_points[1][i] = 1;
1279:       else if (matchA) grid_relax_points[1][i] = 0;
1280:     }

1282:     // set up relax scheme
1283:     for (PetscInt i = 0; i < ns_up; i++) {
1284:       PetscCall(PetscStrcasecmp(postrelax[i], stringF, &matchF));
1285:       PetscCall(PetscStrcasecmp(postrelax[i], stringC, &matchC));
1286:       PetscCall(PetscStrcasecmp(postrelax[i], stringA, &matchA));
1287:       PetscCheck(matchF || matchC || matchA, PetscObjectComm((PetscObject)jac), PETSC_ERR_ARG_WRONG, "Valid argument options for -pc_hypre_boomeramg_postrelax are C, F, and A");
1288:       if (matchF) grid_relax_points[2][i] = -1;
1289:       else if (matchC) grid_relax_points[2][i] = 1;
1290:       else if (matchA) grid_relax_points[2][i] = 0;
1291:     }

1293:     // set coarse relax scheme
1294:     for (PetscInt i = 0; i < jac->gridsweeps[2]; i++) grid_relax_points[3][i] = 0;

1296:     // Pass relax schemes to hypre
1297:     PetscCallHYPRE(HYPRE_BoomerAMGSetGridRelaxPoints(jac->hsolver, grid_relax_points));

1299:     // cleanup memory
1300:     for (PetscInt i = 0; i < ns_down; i++) PetscCall(PetscFree(prerelax[i]));
1301:     for (PetscInt i = 0; i < ns_up; i++) PetscCall(PetscFree(postrelax[i]));
1302:   }
1303: #endif

1305: #if PETSC_PKG_HYPRE_VERSION_LE(9, 9, 9)
1306:   PetscCheck(!jac->Rtype || !jac->agg_nl, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "-pc_hypre_boomeramg_restriction_type (%" PetscInt_FMT ") and -pc_hypre_boomeramg_agg_nl (%" PetscInt_FMT ")", jac->Rtype, jac->agg_nl);
1307: #endif

1309:   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_interp_type", "Interpolation type", "None", HYPREBoomerAMGInterpType, PETSC_STATIC_ARRAY_LENGTH(HYPREBoomerAMGInterpType), jac->interptype < 0 ? "unknown" : HYPREBoomerAMGInterpType[jac->interptype], &indx, &flg));
1310:   if (flg) jac->interptype = indx;

1312:   PetscCall(PetscOptionsName("-pc_hypre_boomeramg_print_statistics", "Print statistics", "None", &flg));
1313:   if (flg) {
1314:     level = 3;
1315:     PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_print_statistics", "Print statistics", "None", level, &level, NULL));

1317:     jac->printstatistics = PETSC_TRUE;
1318:     PetscCallHYPRE(HYPRE_BoomerAMGSetPrintLevel(jac->hsolver, (HYPRE_Int)level));
1319:   }

1321:   PetscCall(PetscOptionsName("-pc_hypre_boomeramg_print_debug", "Print debug information", "None", &flg));
1322:   if (flg) {
1323:     level = 3;
1324:     PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_print_debug", "Print debug information", "None", level, &level, NULL));

1326:     jac->printstatistics = PETSC_TRUE;
1327:     PetscCallHYPRE(HYPRE_BoomerAMGSetDebugFlag(jac->hsolver, (HYPRE_Int)level));
1328:   }

1330:   PetscCall(PetscOptionsBool("-pc_hypre_boomeramg_nodal_relaxation", "Nodal relaxation via Schwarz", "None", PETSC_FALSE, &tmp_truth, &flg));
1331:   if (flg && tmp_truth) {
1332:     PetscInt tmp_int;
1333:     PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_nodal_relaxation", "Nodal relaxation via Schwarz", "None", (HYPRE_Int)jac->nodal_relax_levels, &tmp_int, &flg));
1334:     if (flg) jac->nodal_relax_levels = tmp_int;
1335:     PetscCallHYPRE(HYPRE_BoomerAMGSetSmoothType(jac->hsolver, 6));
1336:     PetscCallHYPRE(HYPRE_BoomerAMGSetDomainType(jac->hsolver, 1));
1337:     PetscCallHYPRE(HYPRE_BoomerAMGSetOverlap(jac->hsolver, 0));
1338:     PetscCallHYPRE(HYPRE_BoomerAMGSetSmoothNumLevels(jac->hsolver, (HYPRE_Int)jac->nodal_relax_levels));
1339:   }

1341:   PetscCall(PetscOptionsBool3("-pc_hypre_boomeramg_keeptranspose", "Avoid transpose matvecs in preconditioner application", "None", jac->keeptranspose, &jac->keeptranspose, NULL));

1343:   /* options for ParaSails solvers */
1344:   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_parasails_sym", "Symmetry of matrix and preconditioner", "None", symtlist, PETSC_STATIC_ARRAY_LENGTH(symtlist), symtlist[0], &indx, &flg));
1345:   if (flg) {
1346:     jac->symt = indx;
1347:     PetscCallHYPRE(HYPRE_BoomerAMGSetSym(jac->hsolver, (HYPRE_Int)jac->symt));
1348:   }

1350:   PetscOptionsHeadEnd();
1351:   PetscFunctionReturn(PETSC_SUCCESS);
1352: }

1354: static PetscErrorCode PCApplyRichardson_HYPRE_BoomerAMG(PC pc, Vec b, Vec y, Vec w, PetscReal rtol, PetscReal abstol, PetscReal dtol, PetscInt its, PetscBool guesszero, PetscInt *outits, PCRichardsonConvergedReason *reason)
1355: {
1356:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1357:   HYPRE_Int oits;

1359:   PetscFunctionBegin;
1360:   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
1361:   PetscCallHYPRE(HYPRE_BoomerAMGSetMaxIter(jac->hsolver, (HYPRE_Int)(its * jac->maxiter)));
1362:   PetscCallHYPRE(HYPRE_BoomerAMGSetTol(jac->hsolver, rtol));
1363:   jac->applyrichardson = PETSC_TRUE;
1364:   PetscCall(PCApply_HYPRE(pc, b, y));
1365:   jac->applyrichardson = PETSC_FALSE;
1366:   PetscCallHYPRE(HYPRE_BoomerAMGGetNumIterations(jac->hsolver, &oits));
1367:   *outits = oits;
1368:   if (oits == its) *reason = PCRICHARDSON_CONVERGED_ITS;
1369:   else *reason = PCRICHARDSON_CONVERGED_RTOL;
1370:   PetscCallHYPRE(HYPRE_BoomerAMGSetTol(jac->hsolver, jac->tol));
1371:   PetscCallHYPRE(HYPRE_BoomerAMGSetMaxIter(jac->hsolver, (HYPRE_Int)jac->maxiter));
1372:   PetscFunctionReturn(PETSC_SUCCESS);
1373: }

1375: static PetscErrorCode PCView_HYPRE_BoomerAMG(PC pc, PetscViewer viewer)
1376: {
1377:   PC_HYPRE         *jac      = (PC_HYPRE *)pc->data;
1378:   hypre_ParAMGData *amg_data = (hypre_ParAMGData *)jac->hsolver;
1379:   PetscBool         isascii;
1380:   PetscInt          indx;
1381:   PetscReal         val;

1383:   PetscFunctionBegin;
1384:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1385:   if (isascii) {
1386:     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE BoomerAMG preconditioning\n"));
1387:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Cycle type %s\n", HYPREBoomerAMGCycleType[jac->cycletype]));
1388:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Maximum number of levels %" PetscInt_FMT "\n", jac->maxlevels));
1389:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Maximum number of iterations PER hypre call %" PetscInt_FMT "\n", jac->maxiter));
1390:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Convergence tolerance PER hypre call %g\n", (double)jac->tol));
1391:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Threshold for strong coupling %g\n", (double)jac->strongthreshold));
1392:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Interpolation truncation factor %g\n", (double)jac->truncfactor));
1393:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Interpolation: max elements per row %" PetscInt_FMT "\n", jac->pmax));
1394:     if (jac->interp_refine) PetscCall(PetscViewerASCIIPrintf(viewer, "    Interpolation: number of steps of weighted refinement %" PetscInt_FMT "\n", jac->interp_refine));
1395:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Number of levels of aggressive coarsening %" PetscInt_FMT "\n", jac->agg_nl));
1396:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Number of paths for aggressive coarsening %" PetscInt_FMT "\n", jac->agg_num_paths));

1398:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Maximum row sums %g\n", (double)jac->maxrowsum));

1400:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Sweeps down         %" PetscInt_FMT "\n", jac->gridsweeps[0]));
1401:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Sweeps up           %" PetscInt_FMT "\n", jac->gridsweeps[1]));
1402:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Sweeps on coarse    %" PetscInt_FMT "\n", jac->gridsweeps[2]));

1404:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Relax down          %s\n", jac->relaxtype[0] < 0 ? "not yet set" : HYPREBoomerAMGRelaxType[jac->relaxtype[0]]));
1405:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Relax up            %s\n", jac->relaxtype[1] < 0 ? "not yet set" : HYPREBoomerAMGRelaxType[jac->relaxtype[1]]));
1406:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Relax on coarse     %s\n", HYPREBoomerAMGRelaxType[jac->relaxtype[2]]));

1408:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Relax weight  (all)      %g\n", (double)jac->relaxweight));
1409:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Outer relax weight (all) %g\n", (double)jac->outerrelaxweight));

1411:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Maximum size of coarsest grid %" PetscInt_FMT "\n", jac->maxc));
1412:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Minimum size of coarsest grid %" PetscInt_FMT "\n", jac->minc));

1414:     if (jac->relaxorder == PETSC_DECIDE) {
1415:       PetscCall(PetscViewerASCIIPrintf(viewer, "    CF-relaxation option not yet determined\n"));
1416:     } else if (jac->relaxorder) {
1417:       PetscCall(PetscViewerASCIIPrintf(viewer, "    Using CF-relaxation\n"));
1418:     } else {
1419:       PetscCall(PetscViewerASCIIPrintf(viewer, "    Not using CF-relaxation\n"));
1420:     }
1421:     if (jac->smoothtype != -1) {
1422:       PetscCall(PetscViewerASCIIPrintf(viewer, "    Smooth type          %s\n", HYPREBoomerAMGSmoothType[jac->smoothtype]));
1423:       PetscCall(PetscViewerASCIIPrintf(viewer, "    Smooth num levels    %" PetscInt_FMT "\n", jac->smoothnumlevels));
1424:       PetscCall(PetscViewerASCIIPrintf(viewer, "    Smooth num sweeps    %" PetscInt_FMT "\n", jac->smoothsweeps));
1425:       if (jac->smoothtype == 0) {
1426:         PetscCallExternalVoid("hypre_ParAMGDataILUType", indx = hypre_ParAMGDataILUType(amg_data));
1427:         PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU type              %s (%" PetscInt_FMT ")\n", HYPREILUType[indx], indx));
1428:         PetscCallExternalVoid("hypre_ParAMGDataILULevel", indx = hypre_ParAMGDataILULevel(amg_data));
1429:         PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU level             %" PetscInt_FMT "\n", indx));
1430:         PetscCallExternalVoid("hypre_ParAMGDataILUMaxIter", indx = hypre_ParAMGDataILUMaxIter(amg_data));
1431:         PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU max iterations    %" PetscInt_FMT "\n", indx));
1432:         PetscCallExternalVoid("hypre_ParAMGDataILUMaxRowNnz", indx = hypre_ParAMGDataILUMaxRowNnz(amg_data));
1433:         PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU max NNZ per row   %" PetscInt_FMT "\n", indx));
1434:         PetscCallExternalVoid("hypre_ParAMGDataILUTriSolve", indx = hypre_ParAMGDataILUTriSolve(amg_data));
1435:         PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU triangular solve  %" PetscInt_FMT "\n", indx));
1436:         PetscCallExternalVoid("hypre_ParAMGDataTol", val = hypre_ParAMGDataTol(amg_data));
1437:         PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU tolerance         %e\n", val));
1438:         PetscCallExternalVoid("hypre_ParAMGDataILUDroptol", val = hypre_ParAMGDataILUDroptol(amg_data));
1439:         PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU drop tolerance    %e\n", val));
1440:         PetscCallExternalVoid("hypre_ParAMGDataILULocalReordering", indx = hypre_ParAMGDataILULocalReordering(amg_data));
1441:         PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU local reordering  %" PetscInt_FMT "\n", indx));
1442:         PetscCallExternalVoid("hypre_ParAMGDataILULowerJacobiIters", indx = hypre_ParAMGDataILULowerJacobiIters(amg_data));
1443:         PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU lower Jacobi iterations  %" PetscInt_FMT "\n", indx));
1444:         PetscCallExternalVoid("hypre_ParAMGDataILUUpperJacobiIters", indx = hypre_ParAMGDataILUUpperJacobiIters(amg_data));
1445:         PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU upper Jacobi iterations  %" PetscInt_FMT "\n", indx));
1446:         PetscCallExternalVoid("hypre_ParAMGDataPrintLevel", indx = hypre_ParAMGDataPrintLevel(amg_data));
1447:         PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU print level      %" PetscInt_FMT "\n", indx));
1448:         PetscCallExternalVoid("hypre_ParAMGDataLogging", indx = hypre_ParAMGDataLogging(amg_data));
1449:         PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU logging level    %" PetscInt_FMT "\n", indx));
1450:         PetscCallExternalVoid("hypre_ParAMGDataILUIterSetupType", indx = hypre_ParAMGDataILUIterSetupType(amg_data));
1451:         PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU iterative setup type           %s (%" PetscInt_FMT ")\n", HYPREILUIterSetup[indx], indx));
1452:         PetscCallExternalVoid("hypre_ParAMGDataILUIterSetupOption", indx = hypre_ParAMGDataILUIterSetupOption(amg_data));
1453:         PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU iterative setup option         %" PetscInt_FMT "\n", indx));
1454:         PetscCallExternalVoid("hypre_ParAMGDataILUIterSetupMaxIter", indx = hypre_ParAMGDataILUIterSetupMaxIter(amg_data));
1455:         PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU iterative setup max iterations %" PetscInt_FMT "\n", indx));
1456:         PetscCallExternalVoid("hypre_ParAMGDataILUIterSetupTolerance", val = hypre_ParAMGDataILUIterSetupTolerance(amg_data));
1457:         PetscCall(PetscViewerASCIIPrintf(viewer, "    ILU iterative setup tolerance      %e\n", val));
1458:       }
1459:     } else {
1460:       PetscCall(PetscViewerASCIIPrintf(viewer, "    Not using more complex smoothers.\n"));
1461:     }
1462:     if (jac->smoothtype == 3) {
1463:       PetscCall(PetscViewerASCIIPrintf(viewer, "    Euclid ILU(k) levels %" PetscInt_FMT "\n", jac->eu_level));
1464:       PetscCall(PetscViewerASCIIPrintf(viewer, "    Euclid ILU(k) drop tolerance %g\n", (double)jac->eu_droptolerance));
1465:       PetscCall(PetscViewerASCIIPrintf(viewer, "    Euclid ILU use Block-Jacobi? %" PetscInt_FMT "\n", jac->eu_bj));
1466:     }
1467:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Measure type        %s\n", HYPREBoomerAMGMeasureType[jac->measuretype]));
1468:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Coarsen type        %s\n", jac->coarsentype < 0 ? "not yet set" : HYPREBoomerAMGCoarsenType[jac->coarsentype]));
1469:     PetscCall(PetscViewerASCIIPrintf(viewer, "    Interpolation type  %s\n", jac->interptype != 100 ? (jac->interptype < 0 ? "not yet set" : HYPREBoomerAMGInterpType[jac->interptype]) : "1pt"));
1470:     if (jac->nodal_coarsening) PetscCall(PetscViewerASCIIPrintf(viewer, "    Using nodal coarsening with HYPRE_BOOMERAMGSetNodal() %" PetscInt_FMT "\n", jac->nodal_coarsening));
1471:     if (jac->vec_interp_variant) {
1472:       PetscCall(PetscViewerASCIIPrintf(viewer, "    HYPRE_BoomerAMGSetInterpVecVariant() %" PetscInt_FMT "\n", jac->vec_interp_variant));
1473:       PetscCall(PetscViewerASCIIPrintf(viewer, "    HYPRE_BoomerAMGSetInterpVecQMax() %" PetscInt_FMT "\n", jac->vec_interp_qmax));
1474:       PetscCall(PetscViewerASCIIPrintf(viewer, "    HYPRE_BoomerAMGSetSmoothInterpVectors() %d\n", jac->vec_interp_smooth));
1475:     }
1476:     if (jac->nodal_relax) PetscCall(PetscViewerASCIIPrintf(viewer, "    Using nodal relaxation via Schwarz smoothing on levels %" PetscInt_FMT "\n", jac->nodal_relax_levels));
1477: #if PETSC_PKG_HYPRE_VERSION_GE(2, 23, 0)
1478:     PetscCall(PetscViewerASCIIPrintf(viewer, "    SpGEMM type         %s\n", jac->spgemm_type));
1479: #else
1480:     PetscCall(PetscViewerASCIIPrintf(viewer, "    SpGEMM type         %s\n", "hypre"));
1481: #endif
1482:     /* AIR */
1483:     if (jac->Rtype) {
1484:       PetscCall(PetscViewerASCIIPrintf(viewer, "    Using approximate ideal restriction type %" PetscInt_FMT "\n", jac->Rtype));
1485:       PetscCall(PetscViewerASCIIPrintf(viewer, "      Threshold for R %g\n", (double)jac->Rstrongthreshold));
1486:       PetscCall(PetscViewerASCIIPrintf(viewer, "      Filter for R %g\n", (double)jac->Rfilterthreshold));
1487:       PetscCall(PetscViewerASCIIPrintf(viewer, "      A drop tolerance %g\n", (double)jac->Adroptol));
1488:       PetscCall(PetscViewerASCIIPrintf(viewer, "      A drop type %" PetscInt_FMT "\n", jac->Adroptype));
1489:     }
1490:   }
1491:   PetscFunctionReturn(PETSC_SUCCESS);
1492: }

1494: static PetscErrorCode PCSetFromOptions_HYPRE_ParaSails(PC pc, PetscOptionItems PetscOptionsObject)
1495: {
1496:   PC_HYPRE   *jac = (PC_HYPRE *)pc->data;
1497:   PetscInt    indx;
1498:   PetscBool   flag;
1499:   const char *symtlist[] = {"nonsymmetric", "SPD", "nonsymmetric,SPD"};

1501:   PetscFunctionBegin;
1502:   PetscOptionsHeadBegin(PetscOptionsObject, "HYPRE ParaSails Options");
1503:   PetscCall(PetscOptionsInt("-pc_hypre_parasails_nlevels", "Number of number of levels", "None", jac->nlevels, &jac->nlevels, 0));
1504:   PetscCall(PetscOptionsReal("-pc_hypre_parasails_thresh", "Threshold", "None", jac->threshold, &jac->threshold, &flag));
1505:   if (flag) PetscCallHYPRE(HYPRE_ParaSailsSetParams(jac->hsolver, jac->threshold, (HYPRE_Int)jac->nlevels));

1507:   PetscCall(PetscOptionsReal("-pc_hypre_parasails_filter", "filter", "None", jac->filter, &jac->filter, &flag));
1508:   if (flag) PetscCallHYPRE(HYPRE_ParaSailsSetFilter(jac->hsolver, jac->filter));

1510:   PetscCall(PetscOptionsReal("-pc_hypre_parasails_loadbal", "Load balance", "None", jac->loadbal, &jac->loadbal, &flag));
1511:   if (flag) PetscCallHYPRE(HYPRE_ParaSailsSetLoadbal(jac->hsolver, (HYPRE_Int)jac->loadbal));

1513:   PetscCall(PetscOptionsBool("-pc_hypre_parasails_logging", "Print info to screen", "None", (PetscBool)jac->logging, (PetscBool *)&jac->logging, &flag));
1514:   if (flag) PetscCallHYPRE(HYPRE_ParaSailsSetLogging(jac->hsolver, (HYPRE_Int)jac->logging));

1516:   PetscCall(PetscOptionsBool("-pc_hypre_parasails_reuse", "Reuse nonzero pattern in preconditioner", "None", (PetscBool)jac->ruse, (PetscBool *)&jac->ruse, &flag));
1517:   if (flag) PetscCallHYPRE(HYPRE_ParaSailsSetReuse(jac->hsolver, (HYPRE_Int)jac->ruse));

1519:   PetscCall(PetscOptionsEList("-pc_hypre_parasails_sym", "Symmetry of matrix and preconditioner", "None", symtlist, PETSC_STATIC_ARRAY_LENGTH(symtlist), symtlist[0], &indx, &flag));
1520:   if (flag) {
1521:     jac->symt = indx;
1522:     PetscCallHYPRE(HYPRE_ParaSailsSetSym(jac->hsolver, (HYPRE_Int)jac->symt));
1523:   }

1525:   PetscOptionsHeadEnd();
1526:   PetscFunctionReturn(PETSC_SUCCESS);
1527: }

1529: static PetscErrorCode PCView_HYPRE_ParaSails(PC pc, PetscViewer viewer)
1530: {
1531:   PC_HYPRE   *jac = (PC_HYPRE *)pc->data;
1532:   PetscBool   isascii;
1533:   const char *symt = 0;

1535:   PetscFunctionBegin;
1536:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1537:   if (isascii) {
1538:     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE ParaSails preconditioning\n"));
1539:     PetscCall(PetscViewerASCIIPrintf(viewer, "    nlevels %" PetscInt_FMT "\n", jac->nlevels));
1540:     PetscCall(PetscViewerASCIIPrintf(viewer, "    threshold %g\n", (double)jac->threshold));
1541:     PetscCall(PetscViewerASCIIPrintf(viewer, "    filter %g\n", (double)jac->filter));
1542:     PetscCall(PetscViewerASCIIPrintf(viewer, "    load balance %g\n", (double)jac->loadbal));
1543:     PetscCall(PetscViewerASCIIPrintf(viewer, "    reuse nonzero structure %s\n", PetscBools[jac->ruse]));
1544:     PetscCall(PetscViewerASCIIPrintf(viewer, "    print info to screen %s\n", PetscBools[jac->logging]));
1545:     if (!jac->symt) symt = "nonsymmetric matrix and preconditioner";
1546:     else if (jac->symt == 1) symt = "SPD matrix and preconditioner";
1547:     else if (jac->symt == 2) symt = "nonsymmetric matrix but SPD preconditioner";
1548:     else SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Unknown HYPRE ParaSails symmetric option %" PetscInt_FMT, jac->symt);
1549:     PetscCall(PetscViewerASCIIPrintf(viewer, "    %s\n", symt));
1550:   }
1551:   PetscFunctionReturn(PETSC_SUCCESS);
1552: }

1554: static PetscErrorCode PCSetFromOptions_HYPRE_AMS(PC pc, PetscOptionItems PetscOptionsObject)
1555: {
1556:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1557:   PetscInt  n;
1558:   PetscBool flag, flag2, flag3, flag4;

1560:   PetscFunctionBegin;
1561:   PetscOptionsHeadBegin(PetscOptionsObject, "HYPRE AMS Options");
1562:   PetscCall(PetscOptionsInt("-pc_hypre_ams_print_level", "Debugging output level for AMS", "None", jac->as_print, &jac->as_print, &flag));
1563:   if (flag) PetscCallHYPRE(HYPRE_AMSSetPrintLevel(jac->hsolver, (HYPRE_Int)jac->as_print));
1564:   PetscCall(PetscOptionsInt("-pc_hypre_ams_max_iter", "Maximum number of AMS multigrid iterations within PCApply", "None", jac->as_max_iter, &jac->as_max_iter, &flag));
1565:   if (flag) PetscCallHYPRE(HYPRE_AMSSetMaxIter(jac->hsolver, (HYPRE_Int)jac->as_max_iter));
1566:   PetscCall(PetscOptionsInt("-pc_hypre_ams_cycle_type", "Cycle type for AMS multigrid", "None", jac->ams_cycle_type, &jac->ams_cycle_type, &flag));
1567:   if (flag) PetscCallHYPRE(HYPRE_AMSSetCycleType(jac->hsolver, (HYPRE_Int)jac->ams_cycle_type));
1568:   PetscCall(PetscOptionsReal("-pc_hypre_ams_tol", "Error tolerance for AMS multigrid", "None", jac->as_tol, &jac->as_tol, &flag));
1569:   if (flag) PetscCallHYPRE(HYPRE_AMSSetTol(jac->hsolver, jac->as_tol));
1570:   PetscCall(PetscOptionsInt("-pc_hypre_ams_relax_type", "Relaxation type for AMS smoother", "None", jac->as_relax_type, &jac->as_relax_type, &flag));
1571:   PetscCall(PetscOptionsInt("-pc_hypre_ams_relax_times", "Number of relaxation steps for AMS smoother", "None", jac->as_relax_times, &jac->as_relax_times, &flag2));
1572:   PetscCall(PetscOptionsReal("-pc_hypre_ams_relax_weight", "Relaxation weight for AMS smoother", "None", jac->as_relax_weight, &jac->as_relax_weight, &flag3));
1573:   PetscCall(PetscOptionsReal("-pc_hypre_ams_omega", "SSOR coefficient for AMS smoother", "None", jac->as_omega, &jac->as_omega, &flag4));
1574:   if (flag || flag2 || flag3 || flag4) PetscCallHYPRE(HYPRE_AMSSetSmoothingOptions(jac->hsolver, (HYPRE_Int)jac->as_relax_type, (HYPRE_Int)jac->as_relax_times, jac->as_relax_weight, jac->as_omega));
1575:   PetscCall(PetscOptionsReal("-pc_hypre_ams_amg_alpha_theta", "Threshold for strong coupling of vector Poisson AMG solver", "None", jac->as_amg_alpha_theta, &jac->as_amg_alpha_theta, &flag));
1576:   n = 5;
1577:   PetscCall(PetscOptionsIntArray("-pc_hypre_ams_amg_alpha_options", "AMG options for vector Poisson", "None", jac->as_amg_alpha_opts, &n, &flag2));
1578:   if (flag || flag2) {
1579:     PetscCallHYPRE(HYPRE_AMSSetAlphaAMGOptions(jac->hsolver, (HYPRE_Int)jac->as_amg_alpha_opts[0],            /* AMG coarsen type */
1580:                                                (HYPRE_Int)jac->as_amg_alpha_opts[1],                          /* AMG agg_levels */
1581:                                                (HYPRE_Int)jac->as_amg_alpha_opts[2],                          /* AMG relax_type */
1582:                                                jac->as_amg_alpha_theta, (HYPRE_Int)jac->as_amg_alpha_opts[3], /* AMG interp_type */
1583:                                                (HYPRE_Int)jac->as_amg_alpha_opts[4]));                        /* AMG Pmax */
1584:   }
1585:   PetscCall(PetscOptionsReal("-pc_hypre_ams_amg_beta_theta", "Threshold for strong coupling of scalar Poisson AMG solver", "None", jac->as_amg_beta_theta, &jac->as_amg_beta_theta, &flag));
1586:   n = 5;
1587:   PetscCall(PetscOptionsIntArray("-pc_hypre_ams_amg_beta_options", "AMG options for scalar Poisson solver", "None", jac->as_amg_beta_opts, &n, &flag2));
1588:   if (flag || flag2) {
1589:     PetscCallHYPRE(HYPRE_AMSSetBetaAMGOptions(jac->hsolver, (HYPRE_Int)jac->as_amg_beta_opts[0],           /* AMG coarsen type */
1590:                                               (HYPRE_Int)jac->as_amg_beta_opts[1],                         /* AMG agg_levels */
1591:                                               (HYPRE_Int)jac->as_amg_beta_opts[2],                         /* AMG relax_type */
1592:                                               jac->as_amg_beta_theta, (HYPRE_Int)jac->as_amg_beta_opts[3], /* AMG interp_type */
1593:                                               (HYPRE_Int)jac->as_amg_beta_opts[4]));                       /* AMG Pmax */
1594:   }
1595:   PetscCall(PetscOptionsInt("-pc_hypre_ams_projection_frequency", "Frequency at which a projection onto the compatible subspace for problems with zero conductivity regions is performed", "None", jac->ams_proj_freq, &jac->ams_proj_freq, &flag));
1596:   if (flag) { /* override HYPRE's default only if the options is used */
1597:     PetscCallHYPRE(HYPRE_AMSSetProjectionFrequency(jac->hsolver, (HYPRE_Int)jac->ams_proj_freq));
1598:   }
1599:   PetscOptionsHeadEnd();
1600:   PetscFunctionReturn(PETSC_SUCCESS);
1601: }

1603: static PetscErrorCode PCView_HYPRE_AMS(PC pc, PetscViewer viewer)
1604: {
1605:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1606:   PetscBool isascii;

1608:   PetscFunctionBegin;
1609:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1610:   if (isascii) {
1611:     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE AMS preconditioning\n"));
1612:     PetscCall(PetscViewerASCIIPrintf(viewer, "    subspace iterations per application %" PetscInt_FMT "\n", jac->as_max_iter));
1613:     PetscCall(PetscViewerASCIIPrintf(viewer, "    subspace cycle type %" PetscInt_FMT "\n", jac->ams_cycle_type));
1614:     PetscCall(PetscViewerASCIIPrintf(viewer, "    subspace iteration tolerance %g\n", (double)jac->as_tol));
1615:     PetscCall(PetscViewerASCIIPrintf(viewer, "    smoother type %" PetscInt_FMT "\n", jac->as_relax_type));
1616:     PetscCall(PetscViewerASCIIPrintf(viewer, "    number of smoothing steps %" PetscInt_FMT "\n", jac->as_relax_times));
1617:     PetscCall(PetscViewerASCIIPrintf(viewer, "    smoother weight %g\n", (double)jac->as_relax_weight));
1618:     PetscCall(PetscViewerASCIIPrintf(viewer, "    smoother omega %g\n", (double)jac->as_omega));
1619:     if (jac->alpha_Poisson) {
1620:       PetscCall(PetscViewerASCIIPrintf(viewer, "    vector Poisson solver (passed in by user)\n"));
1621:     } else {
1622:       PetscCall(PetscViewerASCIIPrintf(viewer, "    vector Poisson solver (computed) \n"));
1623:     }
1624:     PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG coarsening type %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[0]));
1625:     PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG levels of aggressive coarsening %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[1]));
1626:     PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG relaxation type %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[2]));
1627:     PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG interpolation type %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[3]));
1628:     PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG max nonzero elements in interpolation rows %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[4]));
1629:     PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG strength threshold %g\n", (double)jac->as_amg_alpha_theta));
1630:     if (!jac->ams_beta_is_zero) {
1631:       if (jac->beta_Poisson) {
1632:         PetscCall(PetscViewerASCIIPrintf(viewer, "    scalar Poisson solver (passed in by user)\n"));
1633:       } else {
1634:         PetscCall(PetscViewerASCIIPrintf(viewer, "    scalar Poisson solver (computed) \n"));
1635:       }
1636:       PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG coarsening type %" PetscInt_FMT "\n", jac->as_amg_beta_opts[0]));
1637:       PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG levels of aggressive coarsening %" PetscInt_FMT "\n", jac->as_amg_beta_opts[1]));
1638:       PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG relaxation type %" PetscInt_FMT "\n", jac->as_amg_beta_opts[2]));
1639:       PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG interpolation type %" PetscInt_FMT "\n", jac->as_amg_beta_opts[3]));
1640:       PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG max nonzero elements in interpolation rows %" PetscInt_FMT "\n", jac->as_amg_beta_opts[4]));
1641:       PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG strength threshold %g\n", (double)jac->as_amg_beta_theta));
1642:       if (jac->ams_beta_is_zero_part) PetscCall(PetscViewerASCIIPrintf(viewer, "        compatible subspace projection frequency %" PetscInt_FMT " (-1 HYPRE uses default)\n", jac->ams_proj_freq));
1643:     } else {
1644:       PetscCall(PetscViewerASCIIPrintf(viewer, "    scalar Poisson solver not used (zero-conductivity everywhere) \n"));
1645:     }
1646:   }
1647:   PetscFunctionReturn(PETSC_SUCCESS);
1648: }

1650: static PetscErrorCode PCSetFromOptions_HYPRE_ADS(PC pc, PetscOptionItems PetscOptionsObject)
1651: {
1652:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1653:   PetscInt  n;
1654:   PetscBool flag, flag2, flag3, flag4;

1656:   PetscFunctionBegin;
1657:   PetscOptionsHeadBegin(PetscOptionsObject, "HYPRE ADS Options");
1658:   PetscCall(PetscOptionsInt("-pc_hypre_ads_print_level", "Debugging output level for ADS", "None", jac->as_print, &jac->as_print, &flag));
1659:   if (flag) PetscCallHYPRE(HYPRE_ADSSetPrintLevel(jac->hsolver, (HYPRE_Int)jac->as_print));
1660:   PetscCall(PetscOptionsInt("-pc_hypre_ads_max_iter", "Maximum number of ADS multigrid iterations within PCApply", "None", jac->as_max_iter, &jac->as_max_iter, &flag));
1661:   if (flag) PetscCallHYPRE(HYPRE_ADSSetMaxIter(jac->hsolver, (HYPRE_Int)jac->as_max_iter));
1662:   PetscCall(PetscOptionsInt("-pc_hypre_ads_cycle_type", "Cycle type for ADS multigrid", "None", jac->ads_cycle_type, &jac->ads_cycle_type, &flag));
1663:   if (flag) PetscCallHYPRE(HYPRE_ADSSetCycleType(jac->hsolver, (HYPRE_Int)jac->ads_cycle_type));
1664:   PetscCall(PetscOptionsReal("-pc_hypre_ads_tol", "Error tolerance for ADS multigrid", "None", jac->as_tol, &jac->as_tol, &flag));
1665:   if (flag) PetscCallHYPRE(HYPRE_ADSSetTol(jac->hsolver, jac->as_tol));
1666:   PetscCall(PetscOptionsInt("-pc_hypre_ads_relax_type", "Relaxation type for ADS smoother", "None", jac->as_relax_type, &jac->as_relax_type, &flag));
1667:   PetscCall(PetscOptionsInt("-pc_hypre_ads_relax_times", "Number of relaxation steps for ADS smoother", "None", jac->as_relax_times, &jac->as_relax_times, &flag2));
1668:   PetscCall(PetscOptionsReal("-pc_hypre_ads_relax_weight", "Relaxation weight for ADS smoother", "None", jac->as_relax_weight, &jac->as_relax_weight, &flag3));
1669:   PetscCall(PetscOptionsReal("-pc_hypre_ads_omega", "SSOR coefficient for ADS smoother", "None", jac->as_omega, &jac->as_omega, &flag4));
1670:   if (flag || flag2 || flag3 || flag4) PetscCallHYPRE(HYPRE_ADSSetSmoothingOptions(jac->hsolver, (HYPRE_Int)jac->as_relax_type, (HYPRE_Int)jac->as_relax_times, jac->as_relax_weight, jac->as_omega));
1671:   PetscCall(PetscOptionsReal("-pc_hypre_ads_ams_theta", "Threshold for strong coupling of AMS solver inside ADS", "None", jac->as_amg_alpha_theta, &jac->as_amg_alpha_theta, &flag));
1672:   n = 5;
1673:   PetscCall(PetscOptionsIntArray("-pc_hypre_ads_ams_options", "AMG options for AMS solver inside ADS", "None", jac->as_amg_alpha_opts, &n, &flag2));
1674:   PetscCall(PetscOptionsInt("-pc_hypre_ads_ams_cycle_type", "Cycle type for AMS solver inside ADS", "None", jac->ams_cycle_type, &jac->ams_cycle_type, &flag3));
1675:   if (flag || flag2 || flag3) {
1676:     PetscCallHYPRE(HYPRE_ADSSetAMSOptions(jac->hsolver, (HYPRE_Int)jac->ams_cycle_type,                  /* AMS cycle type */
1677:                                           (HYPRE_Int)jac->as_amg_alpha_opts[0],                          /* AMG coarsen type */
1678:                                           (HYPRE_Int)jac->as_amg_alpha_opts[1],                          /* AMG agg_levels */
1679:                                           (HYPRE_Int)jac->as_amg_alpha_opts[2],                          /* AMG relax_type */
1680:                                           jac->as_amg_alpha_theta, (HYPRE_Int)jac->as_amg_alpha_opts[3], /* AMG interp_type */
1681:                                           (HYPRE_Int)jac->as_amg_alpha_opts[4]));                        /* AMG Pmax */
1682:   }
1683:   PetscCall(PetscOptionsReal("-pc_hypre_ads_amg_theta", "Threshold for strong coupling of vector AMG solver inside ADS", "None", jac->as_amg_beta_theta, &jac->as_amg_beta_theta, &flag));
1684:   n = 5;
1685:   PetscCall(PetscOptionsIntArray("-pc_hypre_ads_amg_options", "AMG options for vector AMG solver inside ADS", "None", jac->as_amg_beta_opts, &n, &flag2));
1686:   if (flag || flag2) {
1687:     PetscCallHYPRE(HYPRE_ADSSetAMGOptions(jac->hsolver, (HYPRE_Int)jac->as_amg_beta_opts[0],           /* AMG coarsen type */
1688:                                           (HYPRE_Int)jac->as_amg_beta_opts[1],                         /* AMG agg_levels */
1689:                                           (HYPRE_Int)jac->as_amg_beta_opts[2],                         /* AMG relax_type */
1690:                                           jac->as_amg_beta_theta, (HYPRE_Int)jac->as_amg_beta_opts[3], /* AMG interp_type */
1691:                                           (HYPRE_Int)jac->as_amg_beta_opts[4]));                       /* AMG Pmax */
1692:   }
1693:   PetscOptionsHeadEnd();
1694:   PetscFunctionReturn(PETSC_SUCCESS);
1695: }

1697: static PetscErrorCode PCView_HYPRE_ADS(PC pc, PetscViewer viewer)
1698: {
1699:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1700:   PetscBool isascii;

1702:   PetscFunctionBegin;
1703:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1704:   if (isascii) {
1705:     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE ADS preconditioning\n"));
1706:     PetscCall(PetscViewerASCIIPrintf(viewer, "    subspace iterations per application %" PetscInt_FMT "\n", jac->as_max_iter));
1707:     PetscCall(PetscViewerASCIIPrintf(viewer, "    subspace cycle type %" PetscInt_FMT "\n", jac->ads_cycle_type));
1708:     PetscCall(PetscViewerASCIIPrintf(viewer, "    subspace iteration tolerance %g\n", (double)jac->as_tol));
1709:     PetscCall(PetscViewerASCIIPrintf(viewer, "    smoother type %" PetscInt_FMT "\n", jac->as_relax_type));
1710:     PetscCall(PetscViewerASCIIPrintf(viewer, "    number of smoothing steps %" PetscInt_FMT "\n", jac->as_relax_times));
1711:     PetscCall(PetscViewerASCIIPrintf(viewer, "    smoother weight %g\n", (double)jac->as_relax_weight));
1712:     PetscCall(PetscViewerASCIIPrintf(viewer, "    smoother omega %g\n", (double)jac->as_omega));
1713:     PetscCall(PetscViewerASCIIPrintf(viewer, "    AMS solver using boomerAMG\n"));
1714:     PetscCall(PetscViewerASCIIPrintf(viewer, "        subspace cycle type %" PetscInt_FMT "\n", jac->ams_cycle_type));
1715:     PetscCall(PetscViewerASCIIPrintf(viewer, "        coarsening type %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[0]));
1716:     PetscCall(PetscViewerASCIIPrintf(viewer, "        levels of aggressive coarsening %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[1]));
1717:     PetscCall(PetscViewerASCIIPrintf(viewer, "        relaxation type %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[2]));
1718:     PetscCall(PetscViewerASCIIPrintf(viewer, "        interpolation type %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[3]));
1719:     PetscCall(PetscViewerASCIIPrintf(viewer, "        max nonzero elements in interpolation rows %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[4]));
1720:     PetscCall(PetscViewerASCIIPrintf(viewer, "        strength threshold %g\n", (double)jac->as_amg_alpha_theta));
1721:     PetscCall(PetscViewerASCIIPrintf(viewer, "    vector Poisson solver using boomerAMG\n"));
1722:     PetscCall(PetscViewerASCIIPrintf(viewer, "        coarsening type %" PetscInt_FMT "\n", jac->as_amg_beta_opts[0]));
1723:     PetscCall(PetscViewerASCIIPrintf(viewer, "        levels of aggressive coarsening %" PetscInt_FMT "\n", jac->as_amg_beta_opts[1]));
1724:     PetscCall(PetscViewerASCIIPrintf(viewer, "        relaxation type %" PetscInt_FMT "\n", jac->as_amg_beta_opts[2]));
1725:     PetscCall(PetscViewerASCIIPrintf(viewer, "        interpolation type %" PetscInt_FMT "\n", jac->as_amg_beta_opts[3]));
1726:     PetscCall(PetscViewerASCIIPrintf(viewer, "        max nonzero elements in interpolation rows %" PetscInt_FMT "\n", jac->as_amg_beta_opts[4]));
1727:     PetscCall(PetscViewerASCIIPrintf(viewer, "        strength threshold %g\n", (double)jac->as_amg_beta_theta));
1728:   }
1729:   PetscFunctionReturn(PETSC_SUCCESS);
1730: }

1732: static PetscErrorCode PCHYPRESetDiscreteGradient_HYPRE(PC pc, Mat G)
1733: {
1734:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1735:   PetscBool ishypre;

1737:   PetscFunctionBegin;
1738:   PetscCall(PetscObjectTypeCompare((PetscObject)G, MATHYPRE, &ishypre));
1739:   if (ishypre) {
1740:     PetscCall(PetscObjectReference((PetscObject)G));
1741:     PetscCall(MatDestroy(&jac->G));
1742:     jac->G = G;
1743:   } else {
1744:     PetscCall(MatDestroy(&jac->G));
1745:     PetscCall(MatConvert(G, MATHYPRE, MAT_INITIAL_MATRIX, &jac->G));
1746:   }
1747:   PetscFunctionReturn(PETSC_SUCCESS);
1748: }

1750: static PetscErrorCode PCHYPRESetDiscreteCurl_HYPRE(PC pc, Mat C)
1751: {
1752:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1753:   PetscBool ishypre;

1755:   PetscFunctionBegin;
1756:   PetscCall(PetscObjectTypeCompare((PetscObject)C, MATHYPRE, &ishypre));
1757:   if (ishypre) {
1758:     PetscCall(PetscObjectReference((PetscObject)C));
1759:     PetscCall(MatDestroy(&jac->C));
1760:     jac->C = C;
1761:   } else {
1762:     PetscCall(MatDestroy(&jac->C));
1763:     PetscCall(MatConvert(C, MATHYPRE, MAT_INITIAL_MATRIX, &jac->C));
1764:   }
1765:   PetscFunctionReturn(PETSC_SUCCESS);
1766: }

1768: static PetscErrorCode PCHYPRESetInterpolations_HYPRE(PC pc, PetscInt dim, Mat RT_PiFull, Mat RT_Pi[], Mat ND_PiFull, Mat ND_Pi[])
1769: {
1770:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1771:   PetscBool ishypre;

1773:   PetscFunctionBegin;
1774:   PetscCall(MatDestroy(&jac->RT_PiFull));
1775:   PetscCall(MatDestroy(&jac->ND_PiFull));
1776:   for (PetscInt i = 0; i < 3; ++i) {
1777:     PetscCall(MatDestroy(&jac->RT_Pi[i]));
1778:     PetscCall(MatDestroy(&jac->ND_Pi[i]));
1779:   }

1781:   jac->dim = dim;
1782:   if (RT_PiFull) {
1783:     PetscCall(PetscObjectTypeCompare((PetscObject)RT_PiFull, MATHYPRE, &ishypre));
1784:     if (ishypre) {
1785:       PetscCall(PetscObjectReference((PetscObject)RT_PiFull));
1786:       jac->RT_PiFull = RT_PiFull;
1787:     } else {
1788:       PetscCall(MatConvert(RT_PiFull, MATHYPRE, MAT_INITIAL_MATRIX, &jac->RT_PiFull));
1789:     }
1790:   }
1791:   if (RT_Pi) {
1792:     for (PetscInt i = 0; i < dim; ++i) {
1793:       if (RT_Pi[i]) {
1794:         PetscCall(PetscObjectTypeCompare((PetscObject)RT_Pi[i], MATHYPRE, &ishypre));
1795:         if (ishypre) {
1796:           PetscCall(PetscObjectReference((PetscObject)RT_Pi[i]));
1797:           jac->RT_Pi[i] = RT_Pi[i];
1798:         } else {
1799:           PetscCall(MatConvert(RT_Pi[i], MATHYPRE, MAT_INITIAL_MATRIX, &jac->RT_Pi[i]));
1800:         }
1801:       }
1802:     }
1803:   }
1804:   if (ND_PiFull) {
1805:     PetscCall(PetscObjectTypeCompare((PetscObject)ND_PiFull, MATHYPRE, &ishypre));
1806:     if (ishypre) {
1807:       PetscCall(PetscObjectReference((PetscObject)ND_PiFull));
1808:       jac->ND_PiFull = ND_PiFull;
1809:     } else {
1810:       PetscCall(MatConvert(ND_PiFull, MATHYPRE, MAT_INITIAL_MATRIX, &jac->ND_PiFull));
1811:     }
1812:   }
1813:   if (ND_Pi) {
1814:     for (PetscInt i = 0; i < dim; ++i) {
1815:       if (ND_Pi[i]) {
1816:         PetscCall(PetscObjectTypeCompare((PetscObject)ND_Pi[i], MATHYPRE, &ishypre));
1817:         if (ishypre) {
1818:           PetscCall(PetscObjectReference((PetscObject)ND_Pi[i]));
1819:           jac->ND_Pi[i] = ND_Pi[i];
1820:         } else {
1821:           PetscCall(MatConvert(ND_Pi[i], MATHYPRE, MAT_INITIAL_MATRIX, &jac->ND_Pi[i]));
1822:         }
1823:       }
1824:     }
1825:   }
1826:   PetscFunctionReturn(PETSC_SUCCESS);
1827: }

1829: static PetscErrorCode PCHYPRESetPoissonMatrix_HYPRE(PC pc, Mat A, PetscBool isalpha)
1830: {
1831:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1832:   PetscBool ishypre;

1834:   PetscFunctionBegin;
1835:   PetscCall(PetscObjectTypeCompare((PetscObject)A, MATHYPRE, &ishypre));
1836:   if (ishypre) {
1837:     if (isalpha) {
1838:       PetscCall(PetscObjectReference((PetscObject)A));
1839:       PetscCall(MatDestroy(&jac->alpha_Poisson));
1840:       jac->alpha_Poisson = A;
1841:     } else {
1842:       if (A) {
1843:         PetscCall(PetscObjectReference((PetscObject)A));
1844:       } else {
1845:         jac->ams_beta_is_zero = PETSC_TRUE;
1846:       }
1847:       PetscCall(MatDestroy(&jac->beta_Poisson));
1848:       jac->beta_Poisson = A;
1849:     }
1850:   } else {
1851:     if (isalpha) {
1852:       PetscCall(MatDestroy(&jac->alpha_Poisson));
1853:       PetscCall(MatConvert(A, MATHYPRE, MAT_INITIAL_MATRIX, &jac->alpha_Poisson));
1854:     } else {
1855:       if (A) {
1856:         PetscCall(MatDestroy(&jac->beta_Poisson));
1857:         PetscCall(MatConvert(A, MATHYPRE, MAT_INITIAL_MATRIX, &jac->beta_Poisson));
1858:       } else {
1859:         PetscCall(MatDestroy(&jac->beta_Poisson));
1860:         jac->ams_beta_is_zero = PETSC_TRUE;
1861:       }
1862:     }
1863:   }
1864:   PetscFunctionReturn(PETSC_SUCCESS);
1865: }

1867: static PetscErrorCode PCHYPRESetEdgeConstantVectors_HYPRE(PC pc, Vec ozz, Vec zoz, Vec zzo)
1868: {
1869:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;

1871:   PetscFunctionBegin;
1872:   /* throw away any vector if already set */
1873:   PetscCall(VecHYPRE_IJVectorDestroy(&jac->constants[0]));
1874:   PetscCall(VecHYPRE_IJVectorDestroy(&jac->constants[1]));
1875:   PetscCall(VecHYPRE_IJVectorDestroy(&jac->constants[2]));
1876:   PetscCall(VecHYPRE_IJVectorCreate(ozz->map, &jac->constants[0]));
1877:   PetscCall(VecHYPRE_IJVectorCopy(ozz, jac->constants[0]));
1878:   PetscCall(VecHYPRE_IJVectorCreate(zoz->map, &jac->constants[1]));
1879:   PetscCall(VecHYPRE_IJVectorCopy(zoz, jac->constants[1]));
1880:   jac->dim = 2;
1881:   if (zzo) {
1882:     PetscCall(VecHYPRE_IJVectorCreate(zzo->map, &jac->constants[2]));
1883:     PetscCall(VecHYPRE_IJVectorCopy(zzo, jac->constants[2]));
1884:     jac->dim++;
1885:   }
1886:   PetscFunctionReturn(PETSC_SUCCESS);
1887: }

1889: static PetscErrorCode PCHYPREAMSSetInteriorNodes_HYPRE(PC pc, Vec interior)
1890: {
1891:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;

1893:   PetscFunctionBegin;
1894:   PetscCall(VecHYPRE_IJVectorDestroy(&jac->interior));
1895:   PetscCall(VecHYPRE_IJVectorCreate(interior->map, &jac->interior));
1896:   PetscCall(VecHYPRE_IJVectorCopy(interior, jac->interior));
1897:   jac->ams_beta_is_zero_part = PETSC_TRUE;
1898:   PetscFunctionReturn(PETSC_SUCCESS);
1899: }

1901: static PetscErrorCode PCSetCoordinates_HYPRE(PC pc, PetscInt dim, PetscInt nloc, PetscReal *coords)
1902: {
1903:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1904:   Vec       tv;

1906:   PetscFunctionBegin;
1907:   /* throw away any coordinate vector if already set */
1908:   PetscCall(VecHYPRE_IJVectorDestroy(&jac->coords[0]));
1909:   PetscCall(VecHYPRE_IJVectorDestroy(&jac->coords[1]));
1910:   PetscCall(VecHYPRE_IJVectorDestroy(&jac->coords[2]));
1911:   jac->dim = dim;

1913:   /* compute IJ vector for coordinates */
1914:   PetscCall(VecCreate(PetscObjectComm((PetscObject)pc), &tv));
1915:   PetscCall(VecSetType(tv, VECSTANDARD));
1916:   PetscCall(VecSetSizes(tv, nloc, PETSC_DECIDE));
1917:   for (PetscInt i = 0; i < dim; i++) {
1918:     PetscScalar *array;

1920:     PetscCall(VecHYPRE_IJVectorCreate(tv->map, &jac->coords[i]));
1921:     PetscCall(VecGetArrayWrite(tv, &array));
1922:     for (PetscInt j = 0; j < nloc; j++) array[j] = coords[j * dim + i];
1923:     PetscCall(VecRestoreArrayWrite(tv, &array));
1924:     PetscCall(VecHYPRE_IJVectorCopy(tv, jac->coords[i]));
1925:   }
1926:   PetscCall(VecDestroy(&tv));
1927:   PetscFunctionReturn(PETSC_SUCCESS);
1928: }

1930: static PetscErrorCode PCHYPREGetType_HYPRE(PC pc, const char *name[])
1931: {
1932:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;

1934:   PetscFunctionBegin;
1935:   *name = jac->hypre_type;
1936:   PetscFunctionReturn(PETSC_SUCCESS);
1937: }

1939: static PetscErrorCode PCHYPRESetType_HYPRE(PC pc, const char name[])
1940: {
1941:   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1942:   PetscBool flag;

1944:   PetscFunctionBegin;
1945:   if (jac->hypre_type) {
1946:     PetscCall(PetscStrcmp(jac->hypre_type, name, &flag));
1947:     if (flag) PetscFunctionReturn(PETSC_SUCCESS);
1948:   }

1950:   PetscCall(PCReset_HYPRE(pc));
1951:   PetscCall(PetscFree(jac->hypre_type));
1952:   PetscCall(PetscStrallocpy(name, &jac->hypre_type));

1954:   jac->maxiter         = PETSC_DEFAULT;
1955:   jac->tol             = PETSC_DEFAULT;
1956:   jac->printstatistics = PetscLogPrintInfo;

1958:   PetscCall(PetscStrcmp("ilu", jac->hypre_type, &flag));
1959:   if (flag) {
1960:     PetscCall(PetscCommGetComm(PetscObjectComm((PetscObject)pc), &jac->comm_hypre));
1961:     PetscCallHYPRE(HYPRE_ILUCreate(&jac->hsolver));
1962:     pc->ops->setfromoptions = PCSetFromOptions_HYPRE_ILU;
1963:     pc->ops->view           = PCView_HYPRE_ILU;
1964:     jac->destroy            = HYPRE_ILUDestroy;
1965:     jac->setup              = HYPRE_ILUSetup;
1966:     jac->solve              = HYPRE_ILUSolve;
1967:     jac->factorrowsize      = PETSC_DEFAULT;
1968:     PetscFunctionReturn(PETSC_SUCCESS);
1969:   }

1971:   PetscCall(PetscStrcmp("pilut", jac->hypre_type, &flag));
1972:   if (flag) {
1973:     PetscCall(PetscCommGetComm(PetscObjectComm((PetscObject)pc), &jac->comm_hypre));
1974:     PetscCallHYPRE(HYPRE_ParCSRPilutCreate(jac->comm_hypre, &jac->hsolver));
1975:     pc->ops->setfromoptions = PCSetFromOptions_HYPRE_Pilut;
1976:     pc->ops->view           = PCView_HYPRE_Pilut;
1977:     jac->destroy            = HYPRE_ParCSRPilutDestroy;
1978:     jac->setup              = HYPRE_ParCSRPilutSetup;
1979:     jac->solve              = HYPRE_ParCSRPilutSolve;
1980:     jac->factorrowsize      = PETSC_DEFAULT;
1981:     PetscFunctionReturn(PETSC_SUCCESS);
1982:   }
1983:   PetscCall(PetscStrcmp("euclid", jac->hypre_type, &flag));
1984:   if (flag) {
1985: #if defined(PETSC_USE_64BIT_INDICES)
1986:     SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Hypre Euclid does not support 64-bit indices");
1987: #endif
1988:     PetscCall(PetscCommGetComm(PetscObjectComm((PetscObject)pc), &jac->comm_hypre));
1989:     PetscCallHYPRE(HYPRE_EuclidCreate(jac->comm_hypre, &jac->hsolver));
1990:     pc->ops->setfromoptions = PCSetFromOptions_HYPRE_Euclid;
1991:     pc->ops->view           = PCView_HYPRE_Euclid;
1992:     jac->destroy            = HYPRE_EuclidDestroy;
1993:     jac->setup              = HYPRE_EuclidSetup;
1994:     jac->solve              = HYPRE_EuclidSolve;
1995:     jac->factorrowsize      = PETSC_DEFAULT;
1996:     jac->eu_level           = PETSC_DEFAULT; /* default */
1997:     PetscFunctionReturn(PETSC_SUCCESS);
1998:   }
1999:   PetscCall(PetscStrcmp("parasails", jac->hypre_type, &flag));
2000:   if (flag) {
2001:     PetscCall(PetscCommGetComm(PetscObjectComm((PetscObject)pc), &jac->comm_hypre));
2002:     PetscCallHYPRE(HYPRE_ParaSailsCreate(jac->comm_hypre, &jac->hsolver));
2003:     pc->ops->setfromoptions = PCSetFromOptions_HYPRE_ParaSails;
2004:     pc->ops->view           = PCView_HYPRE_ParaSails;
2005:     jac->destroy            = HYPRE_ParaSailsDestroy;
2006:     jac->setup              = HYPRE_ParaSailsSetup;
2007:     jac->solve              = HYPRE_ParaSailsSolve;
2008:     /* initialize */
2009:     jac->nlevels   = 1;
2010:     jac->threshold = .1;
2011:     jac->filter    = .1;
2012:     jac->loadbal   = 0;
2013:     if (PetscLogPrintInfo) jac->logging = (int)PETSC_TRUE;
2014:     else jac->logging = (int)PETSC_FALSE;

2016:     jac->ruse = (int)PETSC_FALSE;
2017:     jac->symt = 0;
2018:     PetscCallHYPRE(HYPRE_ParaSailsSetParams(jac->hsolver, jac->threshold, (HYPRE_Int)jac->nlevels));
2019:     PetscCallHYPRE(HYPRE_ParaSailsSetFilter(jac->hsolver, jac->filter));
2020:     PetscCallHYPRE(HYPRE_ParaSailsSetLoadbal(jac->hsolver, (HYPRE_Int)jac->loadbal));
2021:     PetscCallHYPRE(HYPRE_ParaSailsSetLogging(jac->hsolver, (HYPRE_Int)jac->logging));
2022:     PetscCallHYPRE(HYPRE_ParaSailsSetReuse(jac->hsolver, (HYPRE_Int)jac->ruse));
2023:     PetscCallHYPRE(HYPRE_ParaSailsSetSym(jac->hsolver, (HYPRE_Int)jac->symt));
2024:     PetscFunctionReturn(PETSC_SUCCESS);
2025:   }
2026:   PetscCall(PetscStrcmp("boomeramg", jac->hypre_type, &flag));
2027:   if (flag) {
2028:     PetscCallHYPRE(HYPRE_BoomerAMGCreate(&jac->hsolver));
2029:     pc->ops->setfromoptions  = PCSetFromOptions_HYPRE_BoomerAMG;
2030:     pc->ops->view            = PCView_HYPRE_BoomerAMG;
2031:     pc->ops->applytranspose  = PCApplyTranspose_HYPRE_BoomerAMG;
2032:     pc->ops->applyrichardson = PCApplyRichardson_HYPRE_BoomerAMG;
2033:     pc->ops->matapply        = PCMatApply_HYPRE_BoomerAMG;
2034:     PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetInterpolations_C", PCGetInterpolations_BoomerAMG));
2035:     PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetCoarseOperators_C", PCGetCoarseOperators_BoomerAMG));
2036:     PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPREGetCFMarkers_C", PCHYPREGetCFMarkers_BoomerAMG));
2037:     jac->destroy         = HYPRE_BoomerAMGDestroy;
2038:     jac->setup           = HYPRE_BoomerAMGSetup;
2039:     jac->solve           = HYPRE_BoomerAMGSolve;
2040:     jac->applyrichardson = PETSC_FALSE;
2041:     /* these defaults match the hypre defaults */
2042:     jac->cycletype       = 1;
2043:     jac->maxlevels       = 25;
2044:     jac->maxiter         = 1;
2045:     jac->tol             = 0.0; /* tolerance of zero indicates use as preconditioner (suppresses convergence errors) */
2046:     jac->truncfactor     = 0.0;
2047:     jac->strongthreshold = .25;
2048:     jac->maxrowsum       = .9;
2049:     jac->measuretype     = 0;
2050:     jac->gridsweeps[0] = jac->gridsweeps[1] = jac->gridsweeps[2] = 1;
2051:     jac->smoothtype                                              = -1; /* Not set by default */
2052:     jac->smoothnumlevels                                         = 25;
2053:     jac->eu_level                                                = 0;
2054:     jac->eu_droptolerance                                        = 0;
2055:     jac->eu_bj                                                   = 0;
2056:     jac->relaxweight                                             = 1.0;
2057:     jac->outerrelaxweight                                        = 1.0;
2058:     jac->Rtype                                                   = 0;
2059:     jac->Rstrongthreshold                                        = 0.25;
2060:     jac->Rfilterthreshold                                        = 0.0;
2061:     jac->Adroptype                                               = -1;
2062:     jac->Adroptol                                                = 0.0;
2063:     jac->agg_nl                                                  = 0;
2064:     jac->pmax                                                    = 0;
2065:     jac->truncfactor                                             = 0.0;
2066:     jac->agg_num_paths                                           = 1;
2067:     jac->maxc                                                    = 9;
2068:     jac->minc                                                    = 1;
2069:     jac->nodal_coarsening                                        = 0;
2070:     jac->nodal_coarsening_diag                                   = 0;
2071:     jac->vec_interp_variant                                      = 0;
2072:     jac->vec_interp_qmax                                         = 0;
2073:     jac->vec_interp_smooth                                       = PETSC_FALSE;
2074:     jac->interp_refine                                           = 0;
2075:     jac->nodal_relax                                             = PETSC_FALSE;
2076:     jac->nodal_relax_levels                                      = 1;
2077:     jac->rap2                                                    = 0;
2078:     PetscObjectParameterSetDefault(jac, relaxtype[2], 9); /* G.E. */

2080:     /*
2081:       Initialize the following parameters with invalid value so we can recognize user input that sets the parameter.
2082:       If there is no user input they are overwritten in PCSetUp_HYPRE() depending on if the matrix is on the CPU or the GPU
2083:     */
2084:     PetscObjectParameterSetDefault(jac, relaxorder, PETSC_DECIDE);
2085:     PetscObjectParameterSetDefault(jac, coarsentype, PETSC_DECIDE);
2086:     PetscObjectParameterSetDefault(jac, interptype, PETSC_DECIDE);
2087:     PetscObjectParameterSetDefault(jac, relaxtype[0], PETSC_DECIDE);
2088:     PetscObjectParameterSetDefault(jac, relaxtype[1], PETSC_DECIDE);
2089: #if PETSC_PKG_HYPRE_VERSION_GE(2, 23, 0)
2090:     PetscObjectParameterSetDefault(jac, spgemm_type, "not yet set");
2091: #endif
2092: #if PETSC_PKG_HYPRE_VERSION_GE(2, 18, 0)
2093:     PetscObjectParameterSetDefault(jac, keeptranspose, PETSC_BOOL3_UNKNOWN);
2094:     PetscObjectParameterSetDefault(jac, mod_rap2, PETSC_DECIDE);
2095: #endif
2096:     PetscObjectParameterSetDefault(jac, agg_interptype, PETSC_DECIDE);
2097:     PetscFunctionReturn(PETSC_SUCCESS);
2098:   }
2099:   PetscCall(PetscStrcmp("ams", jac->hypre_type, &flag));
2100:   if (flag) {
2101:     PetscCallHYPRE(HYPRE_AMSCreate(&jac->hsolver));
2102:     pc->ops->setfromoptions = PCSetFromOptions_HYPRE_AMS;
2103:     pc->ops->view           = PCView_HYPRE_AMS;
2104:     jac->destroy            = HYPRE_AMSDestroy;
2105:     jac->setup              = HYPRE_AMSSetup;
2106:     jac->solve              = HYPRE_AMSSolve;
2107:     jac->coords[0]          = NULL;
2108:     jac->coords[1]          = NULL;
2109:     jac->coords[2]          = NULL;
2110:     jac->interior           = NULL;
2111:     /* solver parameters: these are borrowed from mfem package, and they are not the default values from HYPRE AMS */
2112:     jac->as_print       = 0;
2113:     jac->as_max_iter    = 1;  /* used as a preconditioner */
2114:     jac->as_tol         = 0.; /* used as a preconditioner */
2115:     jac->ams_cycle_type = 13;
2116:     /* Smoothing options */
2117:     jac->as_relax_type   = 2;
2118:     jac->as_relax_times  = 1;
2119:     jac->as_relax_weight = 1.0;
2120:     jac->as_omega        = 1.0;
2121:     /* Vector valued Poisson AMG solver parameters: coarsen type, agg_levels, relax_type, interp_type, Pmax */
2122:     jac->as_amg_alpha_opts[0] = 10;
2123:     jac->as_amg_alpha_opts[1] = 1;
2124:     jac->as_amg_alpha_opts[2] = 6;
2125:     jac->as_amg_alpha_opts[3] = 6;
2126:     jac->as_amg_alpha_opts[4] = 4;
2127:     jac->as_amg_alpha_theta   = 0.25;
2128:     /* Scalar Poisson AMG solver parameters: coarsen type, agg_levels, relax_type, interp_type, Pmax */
2129:     jac->as_amg_beta_opts[0] = 10;
2130:     jac->as_amg_beta_opts[1] = 1;
2131:     jac->as_amg_beta_opts[2] = 6;
2132:     jac->as_amg_beta_opts[3] = 6;
2133:     jac->as_amg_beta_opts[4] = 4;
2134:     jac->as_amg_beta_theta   = 0.25;
2135:     PetscCallHYPRE(HYPRE_AMSSetPrintLevel(jac->hsolver, (HYPRE_Int)jac->as_print));
2136:     PetscCallHYPRE(HYPRE_AMSSetMaxIter(jac->hsolver, (HYPRE_Int)jac->as_max_iter));
2137:     PetscCallHYPRE(HYPRE_AMSSetCycleType(jac->hsolver, (HYPRE_Int)jac->ams_cycle_type));
2138:     PetscCallHYPRE(HYPRE_AMSSetTol(jac->hsolver, jac->as_tol));
2139:     PetscCallHYPRE(HYPRE_AMSSetSmoothingOptions(jac->hsolver, (HYPRE_Int)jac->as_relax_type, (HYPRE_Int)jac->as_relax_times, jac->as_relax_weight, jac->as_omega));
2140:     PetscCallHYPRE(HYPRE_AMSSetAlphaAMGOptions(jac->hsolver, (HYPRE_Int)jac->as_amg_alpha_opts[0],            /* AMG coarsen type */
2141:                                                (HYPRE_Int)jac->as_amg_alpha_opts[1],                          /* AMG agg_levels */
2142:                                                (HYPRE_Int)jac->as_amg_alpha_opts[2],                          /* AMG relax_type */
2143:                                                jac->as_amg_alpha_theta, (HYPRE_Int)jac->as_amg_alpha_opts[3], /* AMG interp_type */
2144:                                                (HYPRE_Int)jac->as_amg_alpha_opts[4]));                        /* AMG Pmax */
2145:     PetscCallHYPRE(HYPRE_AMSSetBetaAMGOptions(jac->hsolver, (HYPRE_Int)jac->as_amg_beta_opts[0],              /* AMG coarsen type */
2146:                                               (HYPRE_Int)jac->as_amg_beta_opts[1],                            /* AMG agg_levels */
2147:                                               (HYPRE_Int)jac->as_amg_beta_opts[2],                            /* AMG relax_type */
2148:                                               jac->as_amg_beta_theta, (HYPRE_Int)jac->as_amg_beta_opts[3],    /* AMG interp_type */
2149:                                               (HYPRE_Int)jac->as_amg_beta_opts[4]));                          /* AMG Pmax */
2150:     /* Zero conductivity */
2151:     jac->ams_beta_is_zero      = PETSC_FALSE;
2152:     jac->ams_beta_is_zero_part = PETSC_FALSE;
2153:     PetscFunctionReturn(PETSC_SUCCESS);
2154:   }
2155:   PetscCall(PetscStrcmp("ads", jac->hypre_type, &flag));
2156:   if (flag) {
2157:     PetscCallHYPRE(HYPRE_ADSCreate(&jac->hsolver));
2158:     pc->ops->setfromoptions = PCSetFromOptions_HYPRE_ADS;
2159:     pc->ops->view           = PCView_HYPRE_ADS;
2160:     jac->destroy            = HYPRE_ADSDestroy;
2161:     jac->setup              = HYPRE_ADSSetup;
2162:     jac->solve              = HYPRE_ADSSolve;
2163:     jac->coords[0]          = NULL;
2164:     jac->coords[1]          = NULL;
2165:     jac->coords[2]          = NULL;
2166:     /* solver parameters: these are borrowed from mfem package, and they are not the default values from HYPRE ADS */
2167:     jac->as_print       = 0;
2168:     jac->as_max_iter    = 1;  /* used as a preconditioner */
2169:     jac->as_tol         = 0.; /* used as a preconditioner */
2170:     jac->ads_cycle_type = 13;
2171:     /* Smoothing options */
2172:     jac->as_relax_type   = 2;
2173:     jac->as_relax_times  = 1;
2174:     jac->as_relax_weight = 1.0;
2175:     jac->as_omega        = 1.0;
2176:     /* AMS solver parameters: cycle_type, coarsen type, agg_levels, relax_type, interp_type, Pmax */
2177:     jac->ams_cycle_type       = 14;
2178:     jac->as_amg_alpha_opts[0] = 10;
2179:     jac->as_amg_alpha_opts[1] = 1;
2180:     jac->as_amg_alpha_opts[2] = 6;
2181:     jac->as_amg_alpha_opts[3] = 6;
2182:     jac->as_amg_alpha_opts[4] = 4;
2183:     jac->as_amg_alpha_theta   = 0.25;
2184:     /* Vector Poisson AMG solver parameters: coarsen type, agg_levels, relax_type, interp_type, Pmax */
2185:     jac->as_amg_beta_opts[0] = 10;
2186:     jac->as_amg_beta_opts[1] = 1;
2187:     jac->as_amg_beta_opts[2] = 6;
2188:     jac->as_amg_beta_opts[3] = 6;
2189:     jac->as_amg_beta_opts[4] = 4;
2190:     jac->as_amg_beta_theta   = 0.25;
2191:     PetscCallHYPRE(HYPRE_ADSSetPrintLevel(jac->hsolver, (HYPRE_Int)jac->as_print));
2192:     PetscCallHYPRE(HYPRE_ADSSetMaxIter(jac->hsolver, (HYPRE_Int)jac->as_max_iter));
2193:     PetscCallHYPRE(HYPRE_ADSSetCycleType(jac->hsolver, (HYPRE_Int)jac->ams_cycle_type));
2194:     PetscCallHYPRE(HYPRE_ADSSetTol(jac->hsolver, jac->as_tol));
2195:     PetscCallHYPRE(HYPRE_ADSSetSmoothingOptions(jac->hsolver, (HYPRE_Int)jac->as_relax_type, (HYPRE_Int)jac->as_relax_times, jac->as_relax_weight, jac->as_omega));
2196:     PetscCallHYPRE(HYPRE_ADSSetAMSOptions(jac->hsolver, (HYPRE_Int)jac->ams_cycle_type,                  /* AMG coarsen type */
2197:                                           (HYPRE_Int)jac->as_amg_alpha_opts[0],                          /* AMG coarsen type */
2198:                                           (HYPRE_Int)jac->as_amg_alpha_opts[1],                          /* AMG agg_levels */
2199:                                           (HYPRE_Int)jac->as_amg_alpha_opts[2],                          /* AMG relax_type */
2200:                                           jac->as_amg_alpha_theta, (HYPRE_Int)jac->as_amg_alpha_opts[3], /* AMG interp_type */
2201:                                           (HYPRE_Int)jac->as_amg_alpha_opts[4]));                        /* AMG Pmax */
2202:     PetscCallHYPRE(HYPRE_ADSSetAMGOptions(jac->hsolver, (HYPRE_Int)jac->as_amg_beta_opts[0],             /* AMG coarsen type */
2203:                                           (HYPRE_Int)jac->as_amg_beta_opts[1],                           /* AMG agg_levels */
2204:                                           (HYPRE_Int)jac->as_amg_beta_opts[2],                           /* AMG relax_type */
2205:                                           jac->as_amg_beta_theta, (HYPRE_Int)jac->as_amg_beta_opts[3],   /* AMG interp_type */
2206:                                           (HYPRE_Int)jac->as_amg_beta_opts[4]));                         /* AMG Pmax */
2207:     PetscFunctionReturn(PETSC_SUCCESS);
2208:   }
2209:   PetscCall(PetscFree(jac->hypre_type));

2211:   jac->hypre_type = NULL;
2212:   SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown HYPRE preconditioner %s; Choices are euclid, ilu, pilut, parasails, boomeramg, ams, ads", name);
2213: }

2215: /*
2216:     It only gets here if the HYPRE type has not been set before the call to
2217:    ...SetFromOptions() which actually is most of the time
2218: */
2219: static PetscErrorCode PCSetFromOptions_HYPRE(PC pc, PetscOptionItems PetscOptionsObject)
2220: {
2221:   PetscInt    indx;
2222:   const char *type[] = {"ilu", "euclid", "pilut", "parasails", "boomeramg", "ams", "ads"};
2223:   PetscBool   flg;
2224:   PC_HYPRE   *jac = (PC_HYPRE *)pc->data;

2226:   PetscFunctionBegin;
2227:   PetscOptionsHeadBegin(PetscOptionsObject, "HYPRE preconditioner options");
2228:   PetscCall(PetscOptionsEList("-pc_hypre_type", "HYPRE preconditioner type", "PCHYPRESetType", type, PETSC_STATIC_ARRAY_LENGTH(type), "boomeramg", &indx, &flg));
2229:   if (flg) PetscCall(PCHYPRESetType_HYPRE(pc, type[indx]));
2230:   /*
2231:     Set the type if it was never set.
2232:   */
2233:   if (!jac->hypre_type) PetscCall(PCHYPRESetType_HYPRE(pc, "boomeramg"));
2234:   PetscTryTypeMethod(pc, setfromoptions, PetscOptionsObject);
2235:   PetscOptionsHeadEnd();
2236:   PetscFunctionReturn(PETSC_SUCCESS);
2237: }

2239: /*MC
2240:   PCHYPRE - Allows you to use the matrix element based preconditioners in the LLNL package hypre as PETSc `PC`

2242:   Options Database Keys:
2243: +   -pc_hypre_type                         - One of `euclid`, `ilu`, `pilut`, `parasails`, `boomeramg`, `ams`, or `ads`
2244: . -pc_hypre_boomeramg_nodal_coarsen n      - where `n` is from 1 to 6 (see `HYPRE_BoomerAMGSetNodal()`)
2245: . -pc_hypre_boomeramg_vec_interp_variant v - where `v` is from 1 to 3 (see `HYPRE_BoomerAMGSetInterpVecVariant()`)
2246: - Many others - run with `-pc_type hypre` `-pc_hypre_type XXX` `-help` to see options for the XXX preconditioner

2248:   Level: intermediate

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

2255:   The options `-pc_hypre_boomeramg_max_iter` and `-pc_hypre_boomeramg_tol` refer to the number of iterations
2256:   (V-cycles) and tolerance that boomerAMG does EACH time it is called. So for example, if
2257:   `-pc_hypre_boomeramg_max_iter` is set to 2 then 2-V-cycles are being used to define the preconditioner
2258:   (`-pc_hypre_boomeramg_tol` should be set to 0.0 - the default - to strictly use a fixed number of
2259:   iterations per hypre call). `-ksp_max_it` and `-ksp_rtol` STILL determine the total number of iterations
2260:   and tolerance for the Krylov solver. For example, if `-pc_hypre_boomeramg_max_iter` is 2 and `-ksp_max_it` is 10
2261:   then AT MOST twenty V-cycles of BoomerAMG will be used.

2263:   Note that the option `-pc_hypre_boomeramg_relax_type_all` defaults to symmetric relaxation
2264:   (symmetric-SOR/Jacobi), which is required for Krylov solvers like CG that expect symmetry.
2265:   Otherwise, you may want to use `-pc_hypre_boomeramg_relax_type_all SOR/Jacobi`.

2267:   If you provide a near null space to your matrix with `MatSetNearNullSpace()` it is ignored by hypre's BoomerAMG UNLESS you also use
2268:   the following two options: `-pc_hypre_boomeramg_nodal_coarsen <n> -pc_hypre_boomeramg_vec_interp_variant <v>`

2270:   See `PCPFMG`, `PCSMG`, and `PCSYSPFMG` for access to hypre's other (nonalgebraic) multigrid solvers

2272:   For `PCHYPRE` type of `ams` or `ads` auxiliary data must be provided to the preconditioner with `PCHYPRESetDiscreteGradient()`,
2273:   `PCHYPRESetDiscreteCurl()`, `PCHYPRESetInterpolations()`, `PCHYPRESetAlphaPoissonMatrix()`, `PCHYPRESetBetaPoissonMatrix()`, `PCHYPRESetEdgeConstantVectors()`,
2274:   `PCHYPREAMSSetInteriorNodes()`

2276:   Sometimes people want to try algebraic multigrid as a "standalone" solver, that is not accelerating it with a Krylov method. Though we generally do not recommend this
2277:   since it is usually slower, one should use a `KSPType` of `KSPRICHARDSON`
2278:   (or equivalently `-ksp_type richardson`) to achieve this. Using `KSPPREONLY` will not work since it only applies a single cycle of multigrid.

2280:   PETSc provides its own geometric and algebraic multigrid solvers `PCMG` and `PCGAMG`, also see `PCHMG` which is useful for certain multicomponent problems.

2282:   hypre supports performance logging via the `Caliper` library.  With `--download-hypre --download-caliper`, hypre will be automatically configured with the support.

2284:   Enabling Caliper logging requires setting the `CALI_CONFIG` environment variable before running your hypre code. For example,

2286:   .vb
2287:     export CALI_CONFIG=runtime-report,max_column_width=200,calc.inclusive,mpi-report,output=stdout
2288:   .ve

2290:   Then run a hypre code, and you will see profiling results on `stdout`. See https://software.llnl.gov/Caliper/#guides for more options.

2292:   GPU Notes:
2293:   To configure hypre BoomerAMG so that it can utilize NVIDIA GPUs run `./configure --download-hypre --with-cuda`
2294:   Then pass `VECCUDA` vectors and `MATAIJCUSPARSE` matrices to the solvers and PETSc will automatically utilize hypre's GPU solvers.

2296:   To configure hypre BoomerAMG so that it can utilize AMD GPUs run ./configure --download-hypre --with-hip
2297:   Then pass `VECHIP` vectors to the solvers and PETSc will automatically utilize hypre's GPU solvers.

2299: .seealso: [](ch_ksp), `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCHYPRESetType()`, `PCPFMG`, `PCGAMG`, `PCSYSPFMG`, `PCSMG`, `PCHYPRESetDiscreteGradient()`,
2300:           `PCHYPRESetDiscreteCurl()`, `PCHYPRESetInterpolations()`, `PCHYPRESetAlphaPoissonMatrix()`, `PCHYPRESetBetaPoissonMatrix()`, `PCHYPRESetEdgeConstantVectors()`,
2301:           `PCHYPREAMSSetInteriorNodes()`
2302: M*/

2304: PETSC_EXTERN PetscErrorCode PCCreate_HYPRE(PC pc)
2305: {
2306:   PC_HYPRE *jac;

2308:   PetscFunctionBegin;
2309:   PetscCall(PetscNew(&jac));

2311:   pc->data                = jac;
2312:   pc->ops->reset          = PCReset_HYPRE;
2313:   pc->ops->destroy        = PCDestroy_HYPRE;
2314:   pc->ops->setfromoptions = PCSetFromOptions_HYPRE;
2315:   pc->ops->setup          = PCSetUp_HYPRE;
2316:   pc->ops->apply          = PCApply_HYPRE;
2317:   jac->hypre_type         = NULL;
2318:   jac->comm_hypre         = MPI_COMM_NULL;
2319:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetType_C", PCHYPRESetType_HYPRE));
2320:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPREGetType_C", PCHYPREGetType_HYPRE));
2321:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCSetCoordinates_C", PCSetCoordinates_HYPRE));
2322:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetDiscreteGradient_C", PCHYPRESetDiscreteGradient_HYPRE));
2323:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetDiscreteCurl_C", PCHYPRESetDiscreteCurl_HYPRE));
2324:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetInterpolations_C", PCHYPRESetInterpolations_HYPRE));
2325:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetEdgeConstantVectors_C", PCHYPRESetEdgeConstantVectors_HYPRE));
2326:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPREAMSSetInteriorNodes_C", PCHYPREAMSSetInteriorNodes_HYPRE));
2327:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetPoissonMatrix_C", PCHYPRESetPoissonMatrix_HYPRE));
2328:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGalerkinSetMatProductAlgorithm_C", PCMGGalerkinSetMatProductAlgorithm_HYPRE_BoomerAMG));
2329:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGalerkinGetMatProductAlgorithm_C", PCMGGalerkinGetMatProductAlgorithm_HYPRE_BoomerAMG));
2330: #if defined(PETSC_HAVE_HYPRE_DEVICE)
2331:   #if defined(HYPRE_USING_HIP)
2332:   PetscCall(PetscDeviceInitialize(PETSC_DEVICE_HIP));
2333:   #endif
2334:   #if defined(HYPRE_USING_CUDA)
2335:   PetscCall(PetscDeviceInitialize(PETSC_DEVICE_CUDA));
2336:   #endif
2337: #endif
2338:   PetscCall(PetscHYPREInitialize());
2339:   PetscFunctionReturn(PETSC_SUCCESS);
2340: }

2342: typedef struct {
2343:   MPI_Comm           hcomm; /* does not share comm with HYPRE_StructMatrix because need to create solver before getting matrix */
2344:   HYPRE_StructSolver hsolver;

2346:   /* keep copy of PFMG options used so may view them */
2347:   PetscInt  its;
2348:   PetscReal tol;
2349:   PetscInt  relax_type;
2350:   PetscInt  rap_type;
2351:   PetscInt  num_pre_relax, num_post_relax;
2352:   PetscInt  max_levels;
2353:   PetscInt  skip_relax;
2354:   PetscBool print_statistics;
2355: } PC_PFMG;

2357: static PetscErrorCode PCDestroy_PFMG(PC pc)
2358: {
2359:   PC_PFMG *ex = (PC_PFMG *)pc->data;

2361:   PetscFunctionBegin;
2362:   if (ex->hsolver) PetscCallHYPRE(HYPRE_StructPFMGDestroy(ex->hsolver));
2363:   PetscCall(PetscCommRestoreComm(PetscObjectComm((PetscObject)pc), &ex->hcomm));
2364:   PetscCall(PetscFree(pc->data));
2365:   PetscFunctionReturn(PETSC_SUCCESS);
2366: }

2368: static const char *PFMGRelaxType[] = {"Jacobi", "Weighted-Jacobi", "symmetric-Red/Black-Gauss-Seidel", "Red/Black-Gauss-Seidel"};
2369: static const char *PFMGRAPType[]   = {"Galerkin", "non-Galerkin"};

2371: static PetscErrorCode PCView_PFMG(PC pc, PetscViewer viewer)
2372: {
2373:   PetscBool isascii;
2374:   PC_PFMG  *ex = (PC_PFMG *)pc->data;

2376:   PetscFunctionBegin;
2377:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
2378:   if (isascii) {
2379:     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE PFMG preconditioning\n"));
2380:     PetscCall(PetscViewerASCIIPrintf(viewer, "    max iterations %" PetscInt_FMT "\n", ex->its));
2381:     PetscCall(PetscViewerASCIIPrintf(viewer, "    tolerance %g\n", ex->tol));
2382:     PetscCall(PetscViewerASCIIPrintf(viewer, "    relax type %s\n", PFMGRelaxType[ex->relax_type]));
2383:     PetscCall(PetscViewerASCIIPrintf(viewer, "    RAP type %s\n", PFMGRAPType[ex->rap_type]));
2384:     PetscCall(PetscViewerASCIIPrintf(viewer, "    number pre-relax %" PetscInt_FMT " post-relax %" PetscInt_FMT "\n", ex->num_pre_relax, ex->num_post_relax));
2385:     PetscCall(PetscViewerASCIIPrintf(viewer, "    max levels %" PetscInt_FMT "\n", ex->max_levels));
2386:     PetscCall(PetscViewerASCIIPrintf(viewer, "    skip relax %" PetscInt_FMT "\n", ex->skip_relax));
2387:   }
2388:   PetscFunctionReturn(PETSC_SUCCESS);
2389: }

2391: static PetscErrorCode PCSetFromOptions_PFMG(PC pc, PetscOptionItems PetscOptionsObject)
2392: {
2393:   PC_PFMG *ex = (PC_PFMG *)pc->data;

2395:   PetscFunctionBegin;
2396:   PetscOptionsHeadBegin(PetscOptionsObject, "PFMG options");
2397:   PetscCall(PetscOptionsBool("-pc_pfmg_print_statistics", "Print statistics", "HYPRE_StructPFMGSetPrintLevel", ex->print_statistics, &ex->print_statistics, NULL));
2398:   PetscCall(PetscOptionsInt("-pc_pfmg_its", "Number of iterations of PFMG to use as preconditioner", "HYPRE_StructPFMGSetMaxIter", ex->its, &ex->its, NULL));
2399:   PetscCallHYPRE(HYPRE_StructPFMGSetMaxIter(ex->hsolver, (HYPRE_Int)ex->its));
2400:   PetscCall(PetscOptionsInt("-pc_pfmg_num_pre_relax", "Number of smoothing steps before coarse grid", "HYPRE_StructPFMGSetNumPreRelax", ex->num_pre_relax, &ex->num_pre_relax, NULL));
2401:   PetscCallHYPRE(HYPRE_StructPFMGSetNumPreRelax(ex->hsolver, (HYPRE_Int)ex->num_pre_relax));
2402:   PetscCall(PetscOptionsInt("-pc_pfmg_num_post_relax", "Number of smoothing steps after coarse grid", "HYPRE_StructPFMGSetNumPostRelax", ex->num_post_relax, &ex->num_post_relax, NULL));
2403:   PetscCallHYPRE(HYPRE_StructPFMGSetNumPostRelax(ex->hsolver, (HYPRE_Int)ex->num_post_relax));

2405:   PetscCall(PetscOptionsInt("-pc_pfmg_max_levels", "Max Levels for MG hierarchy", "HYPRE_StructPFMGSetMaxLevels", ex->max_levels, &ex->max_levels, NULL));
2406:   PetscCallHYPRE(HYPRE_StructPFMGSetMaxLevels(ex->hsolver, (HYPRE_Int)ex->max_levels));

2408:   PetscCall(PetscOptionsReal("-pc_pfmg_tol", "Tolerance of PFMG", "HYPRE_StructPFMGSetTol", ex->tol, &ex->tol, NULL));
2409:   PetscCallHYPRE(HYPRE_StructPFMGSetTol(ex->hsolver, ex->tol));
2410:   PetscCall(PetscOptionsEList("-pc_pfmg_relax_type", "Relax type for the up and down cycles", "HYPRE_StructPFMGSetRelaxType", PFMGRelaxType, PETSC_STATIC_ARRAY_LENGTH(PFMGRelaxType), PFMGRelaxType[ex->relax_type], &ex->relax_type, NULL));
2411:   PetscCallHYPRE(HYPRE_StructPFMGSetRelaxType(ex->hsolver, (HYPRE_Int)ex->relax_type));
2412:   PetscCall(PetscOptionsEList("-pc_pfmg_rap_type", "RAP type", "HYPRE_StructPFMGSetRAPType", PFMGRAPType, PETSC_STATIC_ARRAY_LENGTH(PFMGRAPType), PFMGRAPType[ex->rap_type], &ex->rap_type, NULL));
2413:   PetscCallHYPRE(HYPRE_StructPFMGSetRAPType(ex->hsolver, (HYPRE_Int)ex->rap_type));
2414:   PetscCall(PetscOptionsInt("-pc_pfmg_skip_relax", "Skip relaxation on certain grids for isotropic problems. This can greatly improve efficiency by eliminating unnecessary relaxations when the underlying problem is isotropic", "HYPRE_StructPFMGSetSkipRelax", ex->skip_relax, &ex->skip_relax, NULL));
2415:   PetscCallHYPRE(HYPRE_StructPFMGSetSkipRelax(ex->hsolver, (HYPRE_Int)ex->skip_relax));
2416:   PetscOptionsHeadEnd();
2417:   PetscFunctionReturn(PETSC_SUCCESS);
2418: }

2420: static PetscErrorCode PCApply_PFMG(PC pc, Vec x, Vec y)
2421: {
2422:   PC_PFMG           *ex = (PC_PFMG *)pc->data;
2423:   PetscScalar       *yy;
2424:   const PetscScalar *xx;
2425:   PetscInt           ilower[3], iupper[3];
2426:   HYPRE_Int          hlower[3], hupper[3];
2427:   Mat_HYPREStruct   *mx = (Mat_HYPREStruct *)pc->pmat->data;

2429:   PetscFunctionBegin;
2430:   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
2431:   PetscCall(DMDAGetCorners(mx->da, &ilower[0], &ilower[1], &ilower[2], &iupper[0], &iupper[1], &iupper[2]));
2432:   /* when HYPRE_MIXEDINT is defined, sizeof(HYPRE_Int) == 32 */
2433:   iupper[0] += ilower[0] - 1;
2434:   iupper[1] += ilower[1] - 1;
2435:   iupper[2] += ilower[2] - 1;
2436:   hlower[0] = (HYPRE_Int)ilower[0];
2437:   hlower[1] = (HYPRE_Int)ilower[1];
2438:   hlower[2] = (HYPRE_Int)ilower[2];
2439:   hupper[0] = (HYPRE_Int)iupper[0];
2440:   hupper[1] = (HYPRE_Int)iupper[1];
2441:   hupper[2] = (HYPRE_Int)iupper[2];

2443:   /* copy x values over to hypre */
2444:   PetscCallHYPRE(HYPRE_StructVectorSetConstantValues(mx->hb, 0.0));
2445:   PetscCall(VecGetArrayRead(x, &xx));
2446:   PetscCallHYPRE(HYPRE_StructVectorSetBoxValues(mx->hb, hlower, hupper, (HYPRE_Complex *)xx));
2447:   PetscCall(VecRestoreArrayRead(x, &xx));
2448:   PetscCallHYPRE(HYPRE_StructVectorAssemble(mx->hb));
2449:   PetscCallHYPRE(HYPRE_StructPFMGSolve(ex->hsolver, mx->hmat, mx->hb, mx->hx));

2451:   /* copy solution values back to PETSc */
2452:   PetscCall(VecGetArray(y, &yy));
2453:   PetscCallHYPRE(HYPRE_StructVectorGetBoxValues(mx->hx, hlower, hupper, (HYPRE_Complex *)yy));
2454:   PetscCall(VecRestoreArray(y, &yy));
2455:   PetscFunctionReturn(PETSC_SUCCESS);
2456: }

2458: static PetscErrorCode PCApplyRichardson_PFMG(PC pc, Vec b, Vec y, Vec w, PetscReal rtol, PetscReal abstol, PetscReal dtol, PetscInt its, PetscBool guesszero, PetscInt *outits, PCRichardsonConvergedReason *reason)
2459: {
2460:   PC_PFMG  *jac = (PC_PFMG *)pc->data;
2461:   HYPRE_Int oits;

2463:   PetscFunctionBegin;
2464:   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
2465:   PetscCallHYPRE(HYPRE_StructPFMGSetMaxIter(jac->hsolver, (HYPRE_Int)(its * jac->its)));
2466:   PetscCallHYPRE(HYPRE_StructPFMGSetTol(jac->hsolver, rtol));

2468:   PetscCall(PCApply_PFMG(pc, b, y));
2469:   PetscCallHYPRE(HYPRE_StructPFMGGetNumIterations(jac->hsolver, &oits));
2470:   *outits = oits;
2471:   if (oits == its) *reason = PCRICHARDSON_CONVERGED_ITS;
2472:   else *reason = PCRICHARDSON_CONVERGED_RTOL;
2473:   PetscCallHYPRE(HYPRE_StructPFMGSetTol(jac->hsolver, jac->tol));
2474:   PetscCallHYPRE(HYPRE_StructPFMGSetMaxIter(jac->hsolver, (HYPRE_Int)jac->its));
2475:   PetscFunctionReturn(PETSC_SUCCESS);
2476: }

2478: static PetscErrorCode PCSetUp_PFMG(PC pc)
2479: {
2480:   PC_PFMG         *ex = (PC_PFMG *)pc->data;
2481:   Mat_HYPREStruct *mx = (Mat_HYPREStruct *)pc->pmat->data;
2482:   PetscBool        flg;

2484:   PetscFunctionBegin;
2485:   PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATHYPRESTRUCT, &flg));
2486:   PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "Must use MATHYPRESTRUCT with this preconditioner");

2488:   /* create the hypre solver object and set its information */
2489:   if (ex->hsolver) PetscCallHYPRE(HYPRE_StructPFMGDestroy(ex->hsolver));
2490:   PetscCallHYPRE(HYPRE_StructPFMGCreate(ex->hcomm, &ex->hsolver));

2492:   // Print Hypre statistics about the solve process
2493:   if (ex->print_statistics) PetscCallHYPRE(HYPRE_StructPFMGSetPrintLevel(ex->hsolver, 3));

2495:   // The hypre options must be repeated here because the StructPFMG was destroyed and recreated
2496:   PetscCallHYPRE(HYPRE_StructPFMGSetMaxIter(ex->hsolver, (HYPRE_Int)ex->its));
2497:   PetscCallHYPRE(HYPRE_StructPFMGSetNumPreRelax(ex->hsolver, (HYPRE_Int)ex->num_pre_relax));
2498:   PetscCallHYPRE(HYPRE_StructPFMGSetNumPostRelax(ex->hsolver, (HYPRE_Int)ex->num_post_relax));
2499:   PetscCallHYPRE(HYPRE_StructPFMGSetMaxLevels(ex->hsolver, (HYPRE_Int)ex->max_levels));
2500:   PetscCallHYPRE(HYPRE_StructPFMGSetTol(ex->hsolver, ex->tol));
2501:   PetscCallHYPRE(HYPRE_StructPFMGSetRelaxType(ex->hsolver, (HYPRE_Int)ex->relax_type));
2502:   PetscCallHYPRE(HYPRE_StructPFMGSetRAPType(ex->hsolver, (HYPRE_Int)ex->rap_type));

2504:   PetscCallHYPRE(HYPRE_StructPFMGSetup(ex->hsolver, mx->hmat, mx->hb, mx->hx));
2505:   PetscCallHYPRE(HYPRE_StructPFMGSetZeroGuess(ex->hsolver));
2506:   PetscFunctionReturn(PETSC_SUCCESS);
2507: }

2509: /*MC
2510:   PCPFMG - the hypre PFMG multigrid solver

2512:   Options Database Keys:
2513: + -pc_pfmg_its its              - number of iterations of PFMG to use as preconditioner
2514: . -pc_pfmg_num_pre_relax steps  - number of smoothing steps before coarse grid solve
2515: . -pc_pfmg_num_post_relax steps - number of smoothing steps after coarse grid solve
2516: . -pc_pfmg_tol tol              - tolerance of PFMG
2517: . -pc_pfmg_relax_type type      - relaxation type for the up and down cycles, one of Jacobi,Weighted-Jacobi,symmetric-Red/Black-Gauss-Seidel,Red/Black-Gauss-Seidel
2518: . -pc_pfmg_rap_type type        - type of coarse matrix generation, one of Galerkin,non-Galerkin
2519: - -pc_pfmg_skip_relax (0|1)     - skip relaxation on certain grids for isotropic problems. This can greatly improve efficiency by eliminating unnecessary relaxations
2520:                                   when the underlying problem is isotropic, one of 0,1

2522:   Level: advanced

2524:   Notes:
2525:   This is for CELL-centered descretizations

2527:   See `PCSYSPFMG` for a version suitable for systems of PDEs, and `PCSMG`

2529:   See `PCHYPRE` for hypre's BoomerAMG algebraic multigrid solver

2531:   This must be used with the `MATHYPRESTRUCT` matrix type.

2533:   This provides only some of the functionality of PFMG, it supports only one block per process defined by a PETSc `DMDA`.

2535: .seealso: [](ch_ksp), `PCMG`, `MATHYPRESTRUCT`, `PCHYPRE`, `PCGAMG`, `PCSYSPFMG`, `PCSMG`
2536: M*/

2538: PETSC_EXTERN PetscErrorCode PCCreate_PFMG(PC pc)
2539: {
2540:   PC_PFMG *ex;

2542:   PetscFunctionBegin;
2543:   PetscCall(PetscNew(&ex));
2544:   pc->data = ex;

2546:   ex->its              = 1;
2547:   ex->tol              = 1.e-8;
2548:   ex->relax_type       = 1;
2549:   ex->rap_type         = 0;
2550:   ex->num_pre_relax    = 1;
2551:   ex->num_post_relax   = 1;
2552:   ex->max_levels       = 0;
2553:   ex->skip_relax       = 0;
2554:   ex->print_statistics = PETSC_FALSE;

2556:   pc->ops->setfromoptions  = PCSetFromOptions_PFMG;
2557:   pc->ops->view            = PCView_PFMG;
2558:   pc->ops->destroy         = PCDestroy_PFMG;
2559:   pc->ops->apply           = PCApply_PFMG;
2560:   pc->ops->applyrichardson = PCApplyRichardson_PFMG;
2561:   pc->ops->setup           = PCSetUp_PFMG;

2563:   PetscCall(PetscCommGetComm(PetscObjectComm((PetscObject)pc), &ex->hcomm));
2564:   PetscCall(PetscHYPREInitialize());
2565:   PetscCallHYPRE(HYPRE_StructPFMGCreate(ex->hcomm, &ex->hsolver));
2566:   PetscFunctionReturn(PETSC_SUCCESS);
2567: }

2569: /* we know we are working with a HYPRE_SStructMatrix */
2570: typedef struct {
2571:   MPI_Comm            hcomm; /* does not share comm with HYPRE_SStructMatrix because need to create solver before getting matrix */
2572:   HYPRE_SStructSolver ss_solver;

2574:   /* keep copy of SYSPFMG options used so may view them */
2575:   PetscInt  its;
2576:   PetscReal tol;
2577:   PetscInt  relax_type;
2578:   PetscInt  num_pre_relax, num_post_relax;
2579: } PC_SysPFMG;

2581: static PetscErrorCode PCDestroy_SysPFMG(PC pc)
2582: {
2583:   PC_SysPFMG *ex = (PC_SysPFMG *)pc->data;

2585:   PetscFunctionBegin;
2586:   if (ex->ss_solver) PetscCallHYPRE(HYPRE_SStructSysPFMGDestroy(ex->ss_solver));
2587:   PetscCall(PetscCommRestoreComm(PetscObjectComm((PetscObject)pc), &ex->hcomm));
2588:   PetscCall(PetscFree(pc->data));
2589:   PetscFunctionReturn(PETSC_SUCCESS);
2590: }

2592: static const char *SysPFMGRelaxType[] = {"Weighted-Jacobi", "Red/Black-Gauss-Seidel"};

2594: static PetscErrorCode PCView_SysPFMG(PC pc, PetscViewer viewer)
2595: {
2596:   PetscBool   isascii;
2597:   PC_SysPFMG *ex = (PC_SysPFMG *)pc->data;

2599:   PetscFunctionBegin;
2600:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
2601:   if (isascii) {
2602:     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE SysPFMG preconditioning\n"));
2603:     PetscCall(PetscViewerASCIIPrintf(viewer, "  max iterations %" PetscInt_FMT "\n", ex->its));
2604:     PetscCall(PetscViewerASCIIPrintf(viewer, "  tolerance %g\n", ex->tol));
2605:     PetscCall(PetscViewerASCIIPrintf(viewer, "  relax type %s\n", PFMGRelaxType[ex->relax_type]));
2606:     PetscCall(PetscViewerASCIIPrintf(viewer, "  number pre-relax %" PetscInt_FMT " post-relax %" PetscInt_FMT "\n", ex->num_pre_relax, ex->num_post_relax));
2607:   }
2608:   PetscFunctionReturn(PETSC_SUCCESS);
2609: }

2611: static PetscErrorCode PCSetFromOptions_SysPFMG(PC pc, PetscOptionItems PetscOptionsObject)
2612: {
2613:   PC_SysPFMG *ex  = (PC_SysPFMG *)pc->data;
2614:   PetscBool   flg = PETSC_FALSE;

2616:   PetscFunctionBegin;
2617:   PetscOptionsHeadBegin(PetscOptionsObject, "SysPFMG options");
2618:   PetscCall(PetscOptionsBool("-pc_syspfmg_print_statistics", "Print statistics", "HYPRE_SStructSysPFMGSetPrintLevel", flg, &flg, NULL));
2619:   if (flg) PetscCallHYPRE(HYPRE_SStructSysPFMGSetPrintLevel(ex->ss_solver, 3));
2620:   PetscCall(PetscOptionsInt("-pc_syspfmg_its", "Number of iterations of SysPFMG to use as preconditioner", "HYPRE_SStructSysPFMGSetMaxIter", ex->its, &ex->its, NULL));
2621:   PetscCallHYPRE(HYPRE_SStructSysPFMGSetMaxIter(ex->ss_solver, (HYPRE_Int)ex->its));
2622:   PetscCall(PetscOptionsInt("-pc_syspfmg_num_pre_relax", "Number of smoothing steps before coarse grid", "HYPRE_SStructSysPFMGSetNumPreRelax", ex->num_pre_relax, &ex->num_pre_relax, NULL));
2623:   PetscCallHYPRE(HYPRE_SStructSysPFMGSetNumPreRelax(ex->ss_solver, (HYPRE_Int)ex->num_pre_relax));
2624:   PetscCall(PetscOptionsInt("-pc_syspfmg_num_post_relax", "Number of smoothing steps after coarse grid", "HYPRE_SStructSysPFMGSetNumPostRelax", ex->num_post_relax, &ex->num_post_relax, NULL));
2625:   PetscCallHYPRE(HYPRE_SStructSysPFMGSetNumPostRelax(ex->ss_solver, (HYPRE_Int)ex->num_post_relax));

2627:   PetscCall(PetscOptionsReal("-pc_syspfmg_tol", "Tolerance of SysPFMG", "HYPRE_SStructSysPFMGSetTol", ex->tol, &ex->tol, NULL));
2628:   PetscCallHYPRE(HYPRE_SStructSysPFMGSetTol(ex->ss_solver, ex->tol));
2629:   PetscCall(PetscOptionsEList("-pc_syspfmg_relax_type", "Relax type for the up and down cycles", "HYPRE_SStructSysPFMGSetRelaxType", SysPFMGRelaxType, PETSC_STATIC_ARRAY_LENGTH(SysPFMGRelaxType), SysPFMGRelaxType[ex->relax_type], &ex->relax_type, NULL));
2630:   PetscCallHYPRE(HYPRE_SStructSysPFMGSetRelaxType(ex->ss_solver, (HYPRE_Int)ex->relax_type));
2631:   PetscOptionsHeadEnd();
2632:   PetscFunctionReturn(PETSC_SUCCESS);
2633: }

2635: static PetscErrorCode PCApply_SysPFMG(PC pc, Vec x, Vec y)
2636: {
2637:   PC_SysPFMG        *ex = (PC_SysPFMG *)pc->data;
2638:   PetscScalar       *yy;
2639:   const PetscScalar *xx;
2640:   PetscInt           ilower[3], iupper[3];
2641:   HYPRE_Int          hlower[3], hupper[3];
2642:   Mat_HYPRESStruct  *mx       = (Mat_HYPRESStruct *)pc->pmat->data;
2643:   PetscInt           ordering = mx->dofs_order;
2644:   PetscInt           nvars    = mx->nvars;
2645:   HYPRE_Int          part     = 0;
2646:   PetscInt           size;

2648:   PetscFunctionBegin;
2649:   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
2650:   PetscCall(DMDAGetCorners(mx->da, &ilower[0], &ilower[1], &ilower[2], &iupper[0], &iupper[1], &iupper[2]));
2651:   /* when HYPRE_MIXEDINT is defined, sizeof(HYPRE_Int) == 32 */
2652:   iupper[0] += ilower[0] - 1;
2653:   iupper[1] += ilower[1] - 1;
2654:   iupper[2] += ilower[2] - 1;
2655:   hlower[0] = (HYPRE_Int)ilower[0];
2656:   hlower[1] = (HYPRE_Int)ilower[1];
2657:   hlower[2] = (HYPRE_Int)ilower[2];
2658:   hupper[0] = (HYPRE_Int)iupper[0];
2659:   hupper[1] = (HYPRE_Int)iupper[1];
2660:   hupper[2] = (HYPRE_Int)iupper[2];

2662:   size = 1;
2663:   for (PetscInt i = 0; i < 3; i++) size *= (iupper[i] - ilower[i] + 1);

2665:   /* copy x values over to hypre for variable ordering */
2666:   if (ordering) {
2667:     PetscCallHYPRE(HYPRE_SStructVectorSetConstantValues(mx->ss_b, 0.0));
2668:     PetscCall(VecGetArrayRead(x, &xx));
2669:     for (PetscInt i = 0; i < nvars; i++) PetscCallHYPRE(HYPRE_SStructVectorSetBoxValues(mx->ss_b, part, hlower, hupper, (HYPRE_Int)i, (HYPRE_Complex *)(xx + (size * i))));
2670:     PetscCall(VecRestoreArrayRead(x, &xx));
2671:     PetscCallHYPRE(HYPRE_SStructVectorAssemble(mx->ss_b));
2672:     PetscCallHYPRE(HYPRE_SStructMatrixMatvec(1.0, mx->ss_mat, mx->ss_b, 0.0, mx->ss_x));
2673:     PetscCallHYPRE(HYPRE_SStructSysPFMGSolve(ex->ss_solver, mx->ss_mat, mx->ss_b, mx->ss_x));

2675:     /* copy solution values back to PETSc */
2676:     PetscCall(VecGetArray(y, &yy));
2677:     for (PetscInt i = 0; i < nvars; i++) PetscCallHYPRE(HYPRE_SStructVectorGetBoxValues(mx->ss_x, part, hlower, hupper, (HYPRE_Int)i, (HYPRE_Complex *)(yy + (size * i))));
2678:     PetscCall(VecRestoreArray(y, &yy));
2679:   } else { /* nodal ordering must be mapped to variable ordering for sys_pfmg */
2680:     PetscScalar *z;
2681:     PetscInt     k;

2683:     PetscCall(PetscMalloc1(nvars * size, &z));
2684:     PetscCallHYPRE(HYPRE_SStructVectorSetConstantValues(mx->ss_b, 0.0));
2685:     PetscCall(VecGetArrayRead(x, &xx));

2687:     /* transform nodal to hypre's variable ordering for sys_pfmg */
2688:     for (PetscInt i = 0; i < size; i++) {
2689:       k = i * nvars;
2690:       for (PetscInt j = 0; j < nvars; j++) z[j * size + i] = xx[k + j];
2691:     }
2692:     for (PetscInt i = 0; i < nvars; i++) PetscCallHYPRE(HYPRE_SStructVectorSetBoxValues(mx->ss_b, part, hlower, hupper, (HYPRE_Int)i, (HYPRE_Complex *)(z + (size * i))));
2693:     PetscCall(VecRestoreArrayRead(x, &xx));
2694:     PetscCallHYPRE(HYPRE_SStructVectorAssemble(mx->ss_b));
2695:     PetscCallHYPRE(HYPRE_SStructSysPFMGSolve(ex->ss_solver, mx->ss_mat, mx->ss_b, mx->ss_x));

2697:     /* copy solution values back to PETSc */
2698:     PetscCall(VecGetArray(y, &yy));
2699:     for (PetscInt i = 0; i < nvars; i++) PetscCallHYPRE(HYPRE_SStructVectorGetBoxValues(mx->ss_x, part, hlower, hupper, (HYPRE_Int)i, (HYPRE_Complex *)(z + (size * i))));
2700:     /* transform hypre's variable ordering for sys_pfmg to nodal ordering */
2701:     for (PetscInt i = 0; i < size; i++) {
2702:       k = i * nvars;
2703:       for (PetscInt j = 0; j < nvars; j++) yy[k + j] = z[j * size + i];
2704:     }
2705:     PetscCall(VecRestoreArray(y, &yy));
2706:     PetscCall(PetscFree(z));
2707:   }
2708:   PetscFunctionReturn(PETSC_SUCCESS);
2709: }

2711: static PetscErrorCode PCApplyRichardson_SysPFMG(PC pc, Vec b, Vec y, Vec w, PetscReal rtol, PetscReal abstol, PetscReal dtol, PetscInt its, PetscBool guesszero, PetscInt *outits, PCRichardsonConvergedReason *reason)
2712: {
2713:   PC_SysPFMG *jac = (PC_SysPFMG *)pc->data;
2714:   HYPRE_Int   oits;

2716:   PetscFunctionBegin;
2717:   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
2718:   PetscCallHYPRE(HYPRE_SStructSysPFMGSetMaxIter(jac->ss_solver, (HYPRE_Int)(its * jac->its)));
2719:   PetscCallHYPRE(HYPRE_SStructSysPFMGSetTol(jac->ss_solver, rtol));
2720:   PetscCall(PCApply_SysPFMG(pc, b, y));
2721:   PetscCallHYPRE(HYPRE_SStructSysPFMGGetNumIterations(jac->ss_solver, &oits));
2722:   *outits = oits;
2723:   if (oits == its) *reason = PCRICHARDSON_CONVERGED_ITS;
2724:   else *reason = PCRICHARDSON_CONVERGED_RTOL;
2725:   PetscCallHYPRE(HYPRE_SStructSysPFMGSetTol(jac->ss_solver, jac->tol));
2726:   PetscCallHYPRE(HYPRE_SStructSysPFMGSetMaxIter(jac->ss_solver, (HYPRE_Int)jac->its));
2727:   PetscFunctionReturn(PETSC_SUCCESS);
2728: }

2730: static PetscErrorCode PCSetUp_SysPFMG(PC pc)
2731: {
2732:   PC_SysPFMG       *ex = (PC_SysPFMG *)pc->data;
2733:   Mat_HYPRESStruct *mx = (Mat_HYPRESStruct *)pc->pmat->data;
2734:   PetscBool         flg;

2736:   PetscFunctionBegin;
2737:   PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATHYPRESSTRUCT, &flg));
2738:   PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "Must use MATHYPRESSTRUCT with this preconditioner");

2740:   /* create the hypre sstruct solver object and set its information */
2741:   if (ex->ss_solver) PetscCallHYPRE(HYPRE_SStructSysPFMGDestroy(ex->ss_solver));
2742:   PetscCallHYPRE(HYPRE_SStructSysPFMGCreate(ex->hcomm, &ex->ss_solver));
2743:   PetscCallHYPRE(HYPRE_SStructSysPFMGSetZeroGuess(ex->ss_solver));
2744:   PetscCallHYPRE(HYPRE_SStructSysPFMGSetup(ex->ss_solver, mx->ss_mat, mx->ss_b, mx->ss_x));
2745:   PetscFunctionReturn(PETSC_SUCCESS);
2746: }

2748: /*MC
2749:    PCSYSPFMG - the hypre SysPFMG multigrid solver

2751:    Level: advanced

2753:    Options Database Keys:
2754: + -pc_syspfmg_its its                                             - number of iterations of SysPFMG to use as preconditioner
2755: . -pc_syspfmg_num_pre_relax steps                                 - number of smoothing steps before coarse grid
2756: . -pc_syspfmg_num_post_relax steps                                - number of smoothing steps after coarse grid
2757: . -pc_syspfmg_tol tol                                             - tolerance of SysPFMG
2758: - -pc_syspfmg_relax_type (Weighted-Jacobi|Red/Black-Gauss-Seidel) - relaxation type for the up and down cycles

2760:    Notes:
2761:    See `PCPFMG` for hypre's PFMG that works for a scalar PDE and `PCSMG`

2763:    See `PCHYPRE` for hypre's BoomerAMG algebraic multigrid solver

2765:    This is for CELL-centered descretizations

2767:    This must be used with the `MATHYPRESSTRUCT` matrix type.

2769:    This does not give access to all the functionality of hypres SysPFMG, it supports only one part, and one block per process defined by a PETSc `DMDA`.

2771: .seealso: [](ch_ksp), `PCMG`, `MATHYPRESSTRUCT`, `PCPFMG`, `PCHYPRE`, `PCGAMG`, `PCSMG`
2772: M*/

2774: PETSC_EXTERN PetscErrorCode PCCreate_SysPFMG(PC pc)
2775: {
2776:   PC_SysPFMG *ex;

2778:   PetscFunctionBegin;
2779:   PetscCall(PetscNew(&ex));
2780:   pc->data = ex;

2782:   ex->its            = 1;
2783:   ex->tol            = 1.e-8;
2784:   ex->relax_type     = 1;
2785:   ex->num_pre_relax  = 1;
2786:   ex->num_post_relax = 1;

2788:   pc->ops->setfromoptions  = PCSetFromOptions_SysPFMG;
2789:   pc->ops->view            = PCView_SysPFMG;
2790:   pc->ops->destroy         = PCDestroy_SysPFMG;
2791:   pc->ops->apply           = PCApply_SysPFMG;
2792:   pc->ops->applyrichardson = PCApplyRichardson_SysPFMG;
2793:   pc->ops->setup           = PCSetUp_SysPFMG;

2795:   PetscCall(PetscCommGetComm(PetscObjectComm((PetscObject)pc), &ex->hcomm));
2796:   PetscCall(PetscHYPREInitialize());
2797:   PetscCallHYPRE(HYPRE_SStructSysPFMGCreate(ex->hcomm, &ex->ss_solver));
2798:   PetscFunctionReturn(PETSC_SUCCESS);
2799: }

2801: /* PC SMG */
2802: typedef struct {
2803:   MPI_Comm           hcomm; /* does not share comm with HYPRE_StructMatrix because need to create solver before getting matrix */
2804:   HYPRE_StructSolver hsolver;
2805:   PetscInt           its; /* keep copy of SMG options used so may view them */
2806:   PetscReal          tol;
2807:   PetscBool          print_statistics;
2808:   PetscInt           num_pre_relax, num_post_relax;
2809: } PC_SMG;

2811: static PetscErrorCode PCDestroy_SMG(PC pc)
2812: {
2813:   PC_SMG *ex = (PC_SMG *)pc->data;

2815:   PetscFunctionBegin;
2816:   if (ex->hsolver) PetscCallHYPRE(HYPRE_StructSMGDestroy(ex->hsolver));
2817:   PetscCall(PetscCommRestoreComm(PetscObjectComm((PetscObject)pc), &ex->hcomm));
2818:   PetscCall(PetscFree(pc->data));
2819:   PetscFunctionReturn(PETSC_SUCCESS);
2820: }

2822: static PetscErrorCode PCView_SMG(PC pc, PetscViewer viewer)
2823: {
2824:   PetscBool isascii;
2825:   PC_SMG   *ex = (PC_SMG *)pc->data;

2827:   PetscFunctionBegin;
2828:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
2829:   if (isascii) {
2830:     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE SMG preconditioning\n"));
2831:     PetscCall(PetscViewerASCIIPrintf(viewer, "    max iterations %" PetscInt_FMT "\n", ex->its));
2832:     PetscCall(PetscViewerASCIIPrintf(viewer, "    tolerance %g\n", ex->tol));
2833:     PetscCall(PetscViewerASCIIPrintf(viewer, "    number pre-relax %" PetscInt_FMT " post-relax %" PetscInt_FMT "\n", ex->num_pre_relax, ex->num_post_relax));
2834:   }
2835:   PetscFunctionReturn(PETSC_SUCCESS);
2836: }

2838: static PetscErrorCode PCSetFromOptions_SMG(PC pc, PetscOptionItems PetscOptionsObject)
2839: {
2840:   PC_SMG *ex = (PC_SMG *)pc->data;

2842:   PetscFunctionBegin;
2843:   PetscOptionsHeadBegin(PetscOptionsObject, "SMG options");

2845:   PetscCall(PetscOptionsInt("-pc_smg_its", "Number of iterations of SMG to use as preconditioner", "HYPRE_StructSMGSetMaxIter", ex->its, &ex->its, NULL));
2846:   PetscCall(PetscOptionsInt("-pc_smg_num_pre_relax", "Number of smoothing steps before coarse grid", "HYPRE_StructSMGSetNumPreRelax", ex->num_pre_relax, &ex->num_pre_relax, NULL));
2847:   PetscCall(PetscOptionsInt("-pc_smg_num_post_relax", "Number of smoothing steps after coarse grid", "HYPRE_StructSMGSetNumPostRelax", ex->num_post_relax, &ex->num_post_relax, NULL));
2848:   PetscCall(PetscOptionsReal("-pc_smg_tol", "Tolerance of SMG", "HYPRE_StructSMGSetTol", ex->tol, &ex->tol, NULL));

2850:   PetscOptionsHeadEnd();
2851:   PetscFunctionReturn(PETSC_SUCCESS);
2852: }

2854: static PetscErrorCode PCApply_SMG(PC pc, Vec x, Vec y)
2855: {
2856:   PC_SMG            *ex = (PC_SMG *)pc->data;
2857:   PetscScalar       *yy;
2858:   const PetscScalar *xx;
2859:   PetscInt           ilower[3], iupper[3];
2860:   HYPRE_Int          hlower[3], hupper[3];
2861:   Mat_HYPREStruct   *mx = (Mat_HYPREStruct *)pc->pmat->data;

2863:   PetscFunctionBegin;
2864:   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
2865:   PetscCall(DMDAGetCorners(mx->da, &ilower[0], &ilower[1], &ilower[2], &iupper[0], &iupper[1], &iupper[2]));
2866:   /* when HYPRE_MIXEDINT is defined, sizeof(HYPRE_Int) == 32 */
2867:   iupper[0] += ilower[0] - 1;
2868:   iupper[1] += ilower[1] - 1;
2869:   iupper[2] += ilower[2] - 1;
2870:   hlower[0] = (HYPRE_Int)ilower[0];
2871:   hlower[1] = (HYPRE_Int)ilower[1];
2872:   hlower[2] = (HYPRE_Int)ilower[2];
2873:   hupper[0] = (HYPRE_Int)iupper[0];
2874:   hupper[1] = (HYPRE_Int)iupper[1];
2875:   hupper[2] = (HYPRE_Int)iupper[2];

2877:   /* copy x values over to hypre */
2878:   PetscCallHYPRE(HYPRE_StructVectorSetConstantValues(mx->hb, 0.0));
2879:   PetscCall(VecGetArrayRead(x, &xx));
2880:   PetscCallHYPRE(HYPRE_StructVectorSetBoxValues(mx->hb, hlower, hupper, (HYPRE_Complex *)xx));
2881:   PetscCall(VecRestoreArrayRead(x, &xx));
2882:   PetscCallHYPRE(HYPRE_StructVectorAssemble(mx->hb));
2883:   PetscCallHYPRE(HYPRE_StructSMGSolve(ex->hsolver, mx->hmat, mx->hb, mx->hx));

2885:   /* copy solution values back to PETSc */
2886:   PetscCall(VecGetArray(y, &yy));
2887:   PetscCallHYPRE(HYPRE_StructVectorGetBoxValues(mx->hx, hlower, hupper, (HYPRE_Complex *)yy));
2888:   PetscCall(VecRestoreArray(y, &yy));
2889:   PetscFunctionReturn(PETSC_SUCCESS);
2890: }

2892: static PetscErrorCode PCApplyRichardson_SMG(PC pc, Vec b, Vec y, Vec w, PetscReal rtol, PetscReal abstol, PetscReal dtol, PetscInt its, PetscBool guesszero, PetscInt *outits, PCRichardsonConvergedReason *reason)
2893: {
2894:   PC_SMG   *jac = (PC_SMG *)pc->data;
2895:   HYPRE_Int oits;

2897:   PetscFunctionBegin;
2898:   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
2899:   PetscCallHYPRE(HYPRE_StructSMGSetMaxIter(jac->hsolver, (HYPRE_Int)(its * jac->its)));
2900:   PetscCallHYPRE(HYPRE_StructSMGSetTol(jac->hsolver, rtol));

2902:   PetscCall(PCApply_SMG(pc, b, y));
2903:   PetscCallHYPRE(HYPRE_StructSMGGetNumIterations(jac->hsolver, &oits));
2904:   *outits = oits;
2905:   if (oits == its) *reason = PCRICHARDSON_CONVERGED_ITS;
2906:   else *reason = PCRICHARDSON_CONVERGED_RTOL;
2907:   PetscCallHYPRE(HYPRE_StructSMGSetTol(jac->hsolver, jac->tol));
2908:   PetscCallHYPRE(HYPRE_StructSMGSetMaxIter(jac->hsolver, (HYPRE_Int)jac->its));
2909:   PetscFunctionReturn(PETSC_SUCCESS);
2910: }

2912: static PetscErrorCode PCSetUp_SMG(PC pc)
2913: {
2914:   PetscInt         dim;
2915:   PC_SMG          *ex = (PC_SMG *)pc->data;
2916:   Mat_HYPREStruct *mx = (Mat_HYPREStruct *)pc->pmat->data;
2917:   PetscBool        flg;
2918:   DMBoundaryType   p[3];
2919:   PetscInt         M[3];

2921:   PetscFunctionBegin;
2922:   PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATHYPRESTRUCT, &flg));
2923:   PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "Must use MATHYPRESTRUCT with this preconditioner");

2925:   PetscCall(DMDAGetInfo(mx->da, &dim, &M[0], &M[1], &M[2], 0, 0, 0, 0, 0, &p[0], &p[1], &p[2], 0));
2926:   // Check if power of 2 in periodic directions
2927:   for (PetscInt i = 0; i < dim; i++) {
2928:     PetscCheck((M[i] & (M[i] - 1)) == 0 || p[i] != DM_BOUNDARY_PERIODIC, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "With SMG, the number of points in a periodic direction must be a power of 2, but is here %" PetscInt_FMT ".", M[i]);
2929:   }

2931:   /* create the hypre solver object and set its information */
2932:   if (ex->hsolver) PetscCallHYPRE(HYPRE_StructSMGDestroy(ex->hsolver));
2933:   PetscCallHYPRE(HYPRE_StructSMGCreate(ex->hcomm, &ex->hsolver));
2934:   // The hypre options must be set here and not in SetFromOptions because it is created here!
2935:   PetscCallHYPRE(HYPRE_StructSMGSetMaxIter(ex->hsolver, (HYPRE_Int)ex->its));
2936:   PetscCallHYPRE(HYPRE_StructSMGSetNumPreRelax(ex->hsolver, (HYPRE_Int)ex->num_pre_relax));
2937:   PetscCallHYPRE(HYPRE_StructSMGSetNumPostRelax(ex->hsolver, (HYPRE_Int)ex->num_post_relax));
2938:   PetscCallHYPRE(HYPRE_StructSMGSetTol(ex->hsolver, ex->tol));

2940:   PetscCallHYPRE(HYPRE_StructSMGSetup(ex->hsolver, mx->hmat, mx->hb, mx->hx));
2941:   PetscCallHYPRE(HYPRE_StructSMGSetZeroGuess(ex->hsolver));
2942:   PetscFunctionReturn(PETSC_SUCCESS);
2943: }

2945: /*MC
2946:   PCSMG - the hypre (structured grid) SMG multigrid solver

2948:   Level: advanced

2950:   Options Database Keys:
2951: + -pc_smg_its its              - number of iterations of SMG to use as preconditioner
2952: . -pc_smg_num_pre_relax steps  - number of smoothing steps before coarse grid
2953: . -pc_smg_num_post_relax steps - number of smoothing steps after coarse grid
2954: - -pc_smg_tol tol              - tolerance of SMG

2956:   Notes:
2957:   This is for CELL-centered descretizations

2959:   This must be used with the `MATHYPRESTRUCT` `MatType`.

2961:   This does not provide all the functionality of  hypre's SMG solver, it supports only one block per process defined by a PETSc `DMDA`.

2963:   See `PCSYSPFMG`, `PCSMG`, `PCPFMG`, and `PCHYPRE` for access to hypre's other preconditioners

2965: .seealso:  `PCMG`, `MATHYPRESTRUCT`, `PCPFMG`, `PCSYSPFMG`, `PCHYPRE`, `PCGAMG`
2966: M*/

2968: PETSC_EXTERN PetscErrorCode PCCreate_SMG(PC pc)
2969: {
2970:   PC_SMG *ex;

2972:   PetscFunctionBegin;
2973:   PetscCall(PetscNew(&ex));
2974:   pc->data = ex;

2976:   ex->its            = 1;
2977:   ex->tol            = 1.e-8;
2978:   ex->num_pre_relax  = 1;
2979:   ex->num_post_relax = 1;

2981:   pc->ops->setfromoptions  = PCSetFromOptions_SMG;
2982:   pc->ops->view            = PCView_SMG;
2983:   pc->ops->destroy         = PCDestroy_SMG;
2984:   pc->ops->apply           = PCApply_SMG;
2985:   pc->ops->applyrichardson = PCApplyRichardson_SMG;
2986:   pc->ops->setup           = PCSetUp_SMG;

2988:   PetscCall(PetscCommGetComm(PetscObjectComm((PetscObject)pc), &ex->hcomm));
2989:   PetscCall(PetscHYPREInitialize());
2990:   PetscCallHYPRE(HYPRE_StructSMGCreate(ex->hcomm, &ex->hsolver));
2991:   PetscFunctionReturn(PETSC_SUCCESS);
2992: }