Actual source code: coarsen.c

  1: #include <petsc/private/matimpl.h>

  3: /* Logging support */
  4: PetscClassId MAT_COARSEN_CLASSID;

  6: PetscFunctionList MatCoarsenList              = NULL;
  7: PetscBool         MatCoarsenRegisterAllCalled = PETSC_FALSE;

  9: /*@
 10:   MatCoarsenRegister - Adds a new sparse matrix coarsening algorithm to the matrix package.

 12:   Logically Collective, No Fortran Support

 14:   Input Parameters:
 15: + sname    - name of coarsen (for example `MATCOARSENMIS`)
 16: - function - function pointer that creates the coarsen type

 18:   Level: developer

 20:   Example Usage:
 21: .vb
 22:    MatCoarsenRegister("my_agg", MyAggCreate);
 23: .ve

 25:   Then, your aggregator can be chosen with the procedural interface via `MatCoarsenSetType(agg, "my_agg")` or at runtime via the option `-mat_coarsen_type my_agg`

 27: .seealso: `MatCoarsen`, `MatCoarsenType`, `MatCoarsenSetType()`, `MatCoarsenCreate()`, `MatCoarsenRegisterDestroy()`, `MatCoarsenRegisterAll()`
 28: @*/
 29: PetscErrorCode MatCoarsenRegister(const char sname[], PetscErrorCode (*function)(MatCoarsen))
 30: {
 31:   PetscFunctionBegin;
 32:   PetscCall(MatInitializePackage());
 33:   PetscCall(PetscFunctionListAdd(&MatCoarsenList, sname, function));
 34:   PetscFunctionReturn(PETSC_SUCCESS);
 35: }

 37: /*@
 38:   MatCoarsenGetType - Gets the Coarsen method type and name (as a string)
 39:   from the coarsen context.

 41:   Not Collective

 43:   Input Parameter:
 44: . coarsen - the coarsen context

 46:   Output Parameter:
 47: . type - coarsener type

 49:   Level: advanced

 51: .seealso: `MatCoarsen`, `MatCoarsenCreate()`, `MatCoarsenType`, `MatCoarsenSetType()`, `MatCoarsenRegister()`
 52: @*/
 53: PetscErrorCode MatCoarsenGetType(MatCoarsen coarsen, MatCoarsenType *type)
 54: {
 55:   PetscFunctionBegin;
 57:   PetscAssertPointer(type, 2);
 58:   *type = ((PetscObject)coarsen)->type_name;
 59:   PetscFunctionReturn(PETSC_SUCCESS);
 60: }

 62: /*@
 63:   MatCoarsenApply - Gets a coarsen for a matrix.

 65:   Collective

 67:   Input Parameter:
 68: . coarser - the coarsen

 70:   Options Database Key:
 71: . -mat_coarsen_type (mis|hem|misk) - `mis`: maximal independent set based; `misk`: distance k MIS; `hem`: heavy edge matching

 73:   Level: advanced

 75:   Notes:
 76:   When the coarsening is used inside `PCGAMG` then the options database keys are prefixed with `-pc_gamg_`

 78:   Use `MatCoarsenGetData()` to access the results of the coarsening

 80:   The user can define additional coarsens; see `MatCoarsenRegister()`.

 82: .seealso: `MatCoarsen`, `MatCoarsenSetFromOptions()`, `MatCoarsenSetType()`, `MatCoarsenRegister()`, `MatCoarsenCreate()`,
 83:           `MatCoarsenDestroy()`, `MatCoarsenSetAdjacency()`,
 84:           `MatCoarsenGetData()`
 85: @*/
 86: PetscErrorCode MatCoarsenApply(MatCoarsen coarser)
 87: {
 88:   PetscFunctionBegin;
 90:   PetscAssertPointer(coarser, 1);
 91:   PetscCheck(coarser->graph->assembled, PetscObjectComm((PetscObject)coarser), PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
 92:   PetscCheck(!coarser->graph->factortype, PetscObjectComm((PetscObject)coarser), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
 93:   PetscCall(PetscLogEventBegin(MAT_Coarsen, coarser, 0, 0, 0));
 94:   PetscUseTypeMethod(coarser, apply);
 95:   PetscCall(PetscLogEventEnd(MAT_Coarsen, coarser, 0, 0, 0));
 96:   PetscFunctionReturn(PETSC_SUCCESS);
 97: }

 99: /*@
100:   MatCoarsenSetAdjacency - Sets the adjacency graph (matrix) of the thing to be coarsened.

102:   Collective

104:   Input Parameters:
105: + agg - the coarsen context
106: - adj - the adjacency matrix

108:   Level: advanced

110: .seealso: `MatCoarsen`, `MatCoarsenSetFromOptions()`, `Mat`, `MatCoarsenCreate()`, `MatCoarsenApply()`
111: @*/
112: PetscErrorCode MatCoarsenSetAdjacency(MatCoarsen agg, Mat adj)
113: {
114:   PetscFunctionBegin;
117:   agg->graph = adj;
118:   PetscFunctionReturn(PETSC_SUCCESS);
119: }

121: /*@
122:   MatCoarsenSetStrictAggs - Set whether to keep strict (non overlapping) aggregates in the linked list of aggregates for a coarsen context

124:   Logically Collective

126:   Input Parameters:
127: + agg - the coarsen context
128: - str - `PETSC_TRUE` keep strict aggregates, `PETSC_FALSE` allow overlap

130:   Level: advanced

132: .seealso: `MatCoarsen`, `MatCoarsenCreate()`, `MatCoarsenSetFromOptions()`
133: @*/
134: PetscErrorCode MatCoarsenSetStrictAggs(MatCoarsen agg, PetscBool str)
135: {
136:   PetscFunctionBegin;
138:   agg->strict_aggs = str;
139:   PetscFunctionReturn(PETSC_SUCCESS);
140: }

142: /*@
143:   MatCoarsenDestroy - Destroys the coarsen context.

145:   Collective

147:   Input Parameter:
148: . agg - the coarsen context

150:   Level: advanced

152: .seealso: `MatCoarsen`, `MatCoarsenCreate()`
153: @*/
154: PetscErrorCode MatCoarsenDestroy(MatCoarsen *agg)
155: {
156:   PetscFunctionBegin;
157:   if (!*agg) PetscFunctionReturn(PETSC_SUCCESS);
159:   if (--((PetscObject)*agg)->refct > 0) {
160:     *agg = NULL;
161:     PetscFunctionReturn(PETSC_SUCCESS);
162:   }

164:   PetscTryTypeMethod(*agg, destroy);
165:   if ((*agg)->agg_lists) PetscCall(PetscCDDestroy((*agg)->agg_lists));
166:   PetscCall(PetscObjectComposeFunction((PetscObject)*agg, "MatCoarsenSetMaximumIterations_C", NULL));
167:   PetscCall(PetscObjectComposeFunction((PetscObject)*agg, "MatCoarsenSetThreshold_C", NULL));
168:   PetscCall(PetscObjectComposeFunction((PetscObject)*agg, "MatCoarsenSetStrengthIndex_C", NULL));

170:   PetscCall(PetscHeaderDestroy(agg));
171:   PetscFunctionReturn(PETSC_SUCCESS);
172: }

174: /*@
175:   MatCoarsenViewFromOptions - View the coarsener from the options database

177:   Collective

179:   Input Parameters:
180: + A    - the coarsen context
181: . obj  - optional object that provides the prefix for the option name, pass `NULL` to use the options prefix of `A`
182: - name - command line option (usually `-mat_coarsen_view`)

184:   Options Database Key:
185: . -name viewer_specification - See `PetscOptionsCreateViewer()` for the values of `viewer_specification`

187:   Level: intermediate

189:   Note:
190:   This checks the options database, creates the viewer on-the-fly, uses it and then destroys it. Hence it should not be called in heavily used routines,
191:   rather `PetscOptionsCreateViewer()` should be used to construct the viewer once which can then be utilized in the heavily used routine.

193: .seealso: `MatCoarsen`, `MatCoarsenView()`, `PetscObjectViewFromOptions()`, `MatCoarsenCreate()`, `PetscOptionsCreateViewer()`
194: @*/
195: PetscErrorCode MatCoarsenViewFromOptions(MatCoarsen A, PetscObject obj, const char name[])
196: {
197:   PetscFunctionBegin;
199:   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
200:   PetscFunctionReturn(PETSC_SUCCESS);
201: }

203: /*@
204:   MatCoarsenView - Prints the coarsen data structure.

206:   Collective

208:   Input Parameters:
209: + agg    - the coarsen context
210: - viewer - optional visualization context

212:    For viewing the options database see `MatCoarsenViewFromOptions()`

214:   Level: advanced

216: .seealso: `MatCoarsen`, `PetscViewer`, `PetscViewerASCIIOpen()`, `MatCoarsenViewFromOptions`
217: @*/
218: PetscErrorCode MatCoarsenView(MatCoarsen agg, PetscViewer viewer)
219: {
220:   PetscBool isascii;

222:   PetscFunctionBegin;
224:   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)agg), &viewer));
226:   PetscCheckSameComm(agg, 1, viewer, 2);

228:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
229:   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)agg, viewer));
230:   if (agg->ops->view) {
231:     PetscCall(PetscViewerASCIIPushTab(viewer));
232:     PetscUseTypeMethod(agg, view, viewer);
233:     PetscCall(PetscViewerASCIIPopTab(viewer));
234:   }
235:   if (agg->strength_index_size > 0) PetscCall(PetscViewerASCIIPrintf(viewer, " Using scalar strength-of-connection index[%" PetscInt_FMT "] = {%" PetscInt_FMT ", ..}\n", agg->strength_index_size, agg->strength_index[0]));
236:   PetscFunctionReturn(PETSC_SUCCESS);
237: }

239: /*@
240:   MatCoarsenSetType - Sets the type of aggregator to use

242:   Collective

244:   Input Parameters:
245: + coarser - the coarsen context.
246: - type    - a known coarsening method

248:   Options Database Key:
249: . -mat_coarsen_type  type - maximal independent set based; distance k MIS; heavy edge matching

251:   Level: advanced

253: .seealso: `MatCoarsen`, `MatCoarsenCreate()`, `MatCoarsenApply()`, `MatCoarsenType`, `MatCoarsenGetType()`
254: @*/
255: PetscErrorCode MatCoarsenSetType(MatCoarsen coarser, MatCoarsenType type)
256: {
257:   PetscBool match;
258:   PetscErrorCode (*r)(MatCoarsen);

260:   PetscFunctionBegin;
262:   PetscAssertPointer(type, 2);

264:   PetscCall(PetscObjectTypeCompare((PetscObject)coarser, type, &match));
265:   if (match) PetscFunctionReturn(PETSC_SUCCESS);

267:   PetscTryTypeMethod(coarser, destroy);
268:   PetscCall(PetscMemzero(coarser->ops, sizeof(struct _MatCoarsenOps)));

270:   PetscCall(PetscFunctionListFind(MatCoarsenList, type, &r));
271:   PetscCheck(r, PetscObjectComm((PetscObject)coarser), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown coarsen type %s", type);
272:   PetscCall((*r)(coarser));

274:   PetscCall(PetscFree(((PetscObject)coarser)->type_name));
275:   PetscCall(PetscStrallocpy(type, &((PetscObject)coarser)->type_name));
276:   PetscFunctionReturn(PETSC_SUCCESS);
277: }

279: /*@
280:   MatCoarsenSetGreedyOrdering - Sets the ordering of the vertices to use with a greedy coarsening method

282:   Logically Collective

284:   Input Parameters:
285: + coarser - the coarsen context
286: - perm    - vertex ordering of (greedy) algorithm

288:   Level: advanced

290:   Note:
291:   The `IS` weights is freed by PETSc, the user should not destroy it or change it after this call

293: .seealso: `MatCoarsen`, `MatCoarsenType`, `MatCoarsenCreate()`, `MatCoarsenSetType()`
294: @*/
295: PetscErrorCode MatCoarsenSetGreedyOrdering(MatCoarsen coarser, const IS perm)
296: {
297:   PetscFunctionBegin;
299:   coarser->perm = perm;
300:   PetscFunctionReturn(PETSC_SUCCESS);
301: }

303: /*@
304:   MatCoarsenGetData - Gets the weights for vertices for a coarsener.

306:   Logically Collective, No Fortran Support

308:   Input Parameter:
309: . coarser - the coarsen context

311:   Output Parameter:
312: . llist - linked list of aggregates

314:   Level: advanced

316:   Note:
317:   This passes ownership to the caller and nullifies the value of weights (`PetscCoarsenData`) within the `MatCoarsen`

319: .seealso: `MatCoarsen`, `MatCoarsenApply()`, `MatCoarsenCreate()`, `MatCoarsenSetType()`, `PetscCoarsenData`
320: @*/
321: PetscErrorCode MatCoarsenGetData(MatCoarsen coarser, PetscCoarsenData **llist)
322: {
323:   PetscFunctionBegin;
325:   PetscCheck(coarser->agg_lists, PetscObjectComm((PetscObject)coarser), PETSC_ERR_ARG_WRONGSTATE, "No linked list - generate it or call ApplyCoarsen");
326:   *llist             = coarser->agg_lists;
327:   coarser->agg_lists = NULL; /* giving up ownership */
328:   PetscFunctionReturn(PETSC_SUCCESS);
329: }

331: /*@
332:   MatCoarsenSetFromOptions - Sets various coarsen options from the options database.

334:   Collective

336:   Input Parameter:
337: . coarser - the coarsen context.

339:   Options Database Key:
340: + -mat_coarsen_type  (mis|hem|misk) - see `MatCoarsenType`
341: . -mat_coarsen_max_it its           - number of iterations to use in the coarsening process, see `MatCoarsenSetMaximumIterations()`
342: - -mat_coarsen_threshold threshold  - see `MatCoarsenSetThreshold()`, for `MATCOARSENHEM` only

344:   Level: advanced

346:   Notes:
347:   When the coarsening is used inside `PCGAMG` then the options database keys are prefixed with `-pc_gamg_`

349:   Sets the `MatCoarsenType` to `MATCOARSENMISK` if has not been set previously

351: .seealso: `MatCoarsen`, `MatCoarsenType`, `MatCoarsenApply()`, `MatCoarsenCreate()`, `MatCoarsenSetType()`,
352:           `MatCoarsenSetMaximumIterations()`, `MATCOARSENHEM`, `MATCOARSENMIS`, `MATCOARSENMISK`
353: @*/
354: PetscErrorCode MatCoarsenSetFromOptions(MatCoarsen coarser)
355: {
356:   PetscBool   flag;
357:   char        type[256];
358:   const char *def;

360:   PetscFunctionBegin;
361:   PetscObjectOptionsBegin((PetscObject)coarser);
362:   if (!((PetscObject)coarser)->type_name) {
363:     def = MATCOARSENMISK;
364:   } else {
365:     def = ((PetscObject)coarser)->type_name;
366:   }
367:   PetscCall(PetscOptionsFList("-mat_coarsen_type", "Type of aggregator", "MatCoarsenSetType", MatCoarsenList, def, type, sizeof(type), &flag));
368:   if (flag) PetscCall(MatCoarsenSetType(coarser, type));

370:   PetscCall(PetscOptionsInt("-mat_coarsen_max_it", "Number of iterations (for HEM)", "MatCoarsenSetMaximumIterations", coarser->max_it, &coarser->max_it, NULL));
371:   PetscCall(PetscOptionsReal("-mat_coarsen_threshold", "Threshold (for HEM)", "MatCoarsenSetThreshold", coarser->threshold, &coarser->threshold, NULL));
372:   coarser->strength_index_size = MAT_COARSEN_STRENGTH_INDEX_SIZE;
373:   PetscCall(PetscOptionsIntArray("-mat_coarsen_strength_index", "Array of indices to use strength of connection measure (default is all indices)", "MatCoarsenSetStrengthIndex", coarser->strength_index, &coarser->strength_index_size, NULL));
374:   /*
375:    Set the type if it was never set.
376:    */
377:   if (!((PetscObject)coarser)->type_name) PetscCall(MatCoarsenSetType(coarser, def));

379:   PetscTryTypeMethod(coarser, setfromoptions, PetscOptionsObject);
380:   PetscOptionsEnd();
381:   PetscFunctionReturn(PETSC_SUCCESS);
382: }

384: /*@
385:   MatCoarsenSetMaximumIterations - Maximum `MATCOARSENHEM` iterations to use

387:   Logically Collective

389:   Input Parameters:
390: + coarse - the coarsen context
391: - n      - number of HEM iterations

393:   Options Database Key:
394: . -mat_coarsen_max_it n - Maximum `MATCOARSENHEM` iterations to use

396:   Level: intermediate

398:   Note:
399:   When the coarsening is used inside `PCGAMG` then the options database keys are prefixed with `-pc_gamg_`

401: .seealso: `MatCoarsen`, `MatCoarsenType`, `MatCoarsenApply()`, `MatCoarsenCreate()`, `MatCoarsenSetType()`
402: @*/
403: PetscErrorCode MatCoarsenSetMaximumIterations(MatCoarsen coarse, PetscInt n)
404: {
405:   PetscFunctionBegin;
408:   PetscTryMethod(coarse, "MatCoarsenSetMaximumIterations_C", (MatCoarsen, PetscInt), (coarse, n));
409:   PetscFunctionReturn(PETSC_SUCCESS);
410: }

412: static PetscErrorCode MatCoarsenSetMaximumIterations_MATCOARSEN(MatCoarsen coarse, PetscInt b)
413: {
414:   PetscFunctionBegin;
415:   coarse->max_it = b;
416:   PetscFunctionReturn(PETSC_SUCCESS);
417: }

419: /*@
420:   MatCoarsenSetStrengthIndex -  Index array to use for index to use for strength of connection

422:   Logically Collective

424:   Input Parameters:
425: + coarse - the coarsen context
426: . n      - number of indices
427: - idx    - array of indices

429:   Options Database Key:
430: . -mat_coarsen_strength_index - array of subset of variables per vertex to use for strength norm, -1 for using all (default)

432:   Level: intermediate

434:   Note:
435:   When the coarsening is used inside `PCGAMG` then the options database keys are prefixed with `-pc_gamg_`

437: .seealso: `MatCoarsen`, `MatCoarsenType`, `MatCoarsenApply()`, `MatCoarsenCreate()`, `MatCoarsenSetType()`
438: @*/
439: PetscErrorCode MatCoarsenSetStrengthIndex(MatCoarsen coarse, PetscInt n, PetscInt idx[])
440: {
441:   PetscFunctionBegin;
444:   PetscTryMethod(coarse, "MatCoarsenSetStrengthIndex_C", (MatCoarsen, PetscInt, PetscInt[]), (coarse, n, idx));
445:   PetscFunctionReturn(PETSC_SUCCESS);
446: }

448: static PetscErrorCode MatCoarsenSetStrengthIndex_MATCOARSEN(MatCoarsen coarse, PetscInt n, PetscInt idx[])
449: {
450:   PetscFunctionBegin;
451:   coarse->strength_index_size = n;
452:   for (int iii = 0; iii < n; iii++) coarse->strength_index[iii] = idx[iii];
453:   PetscFunctionReturn(PETSC_SUCCESS);
454: }

456: /*@
457:   MatCoarsenSetThreshold - Set the threshold for HEM

459:   Logically Collective

461:   Input Parameters:
462: + coarse - the coarsen context
463: - b      - threshold value, default is 0

465:   Options Database Key:
466: . -mat_coarsen_threshold b - threshold

468:   Level: intermediate

470:   Note:
471:   When the coarsening is used inside `PCGAMG` then the options database keys are prefixed with `-pc_gamg_`

473:   Developer Note:
474:   It is not documented how this threshold is used

476: .seealso: `MatCoarsen`, `MatCoarsenType`, `MatCoarsenApply()`, `MatCoarsenCreate()`, `MatCoarsenSetType()`
477: @*/
478: PetscErrorCode MatCoarsenSetThreshold(MatCoarsen coarse, PetscReal b)
479: {
480:   PetscFunctionBegin;
483:   PetscTryMethod(coarse, "MatCoarsenSetThreshold_C", (MatCoarsen, PetscReal), (coarse, b));
484:   PetscFunctionReturn(PETSC_SUCCESS);
485: }

487: static PetscErrorCode MatCoarsenSetThreshold_MATCOARSEN(MatCoarsen coarse, PetscReal b)
488: {
489:   PetscFunctionBegin;
490:   coarse->threshold = b;
491:   PetscFunctionReturn(PETSC_SUCCESS);
492: }

494: /*@
495:   MatCoarsenCreate - Creates a coarsen context.

497:   Collective

499:   Input Parameter:
500: . comm - MPI communicator

502:   Output Parameter:
503: . newcrs - location to put the context

505:   Level: advanced

507: .seealso: `MatCoarsen`, `MatCoarsenSetType()`, `MatCoarsenApply()`, `MatCoarsenDestroy()`,
508:           `MatCoarsenSetAdjacency()`, `MatCoarsenGetData()`
509: @*/
510: PetscErrorCode MatCoarsenCreate(MPI_Comm comm, MatCoarsen *newcrs)
511: {
512:   MatCoarsen agg;

514:   PetscFunctionBegin;
515:   PetscAssertPointer(newcrs, 2);
516:   PetscCall(MatInitializePackage());

518:   PetscCall(PetscHeaderCreate(agg, MAT_COARSEN_CLASSID, "MatCoarsen", "Matrix/graph coarsen", "MatCoarsen", comm, MatCoarsenDestroy, MatCoarsenView));
519:   PetscCall(PetscObjectComposeFunction((PetscObject)agg, "MatCoarsenSetMaximumIterations_C", MatCoarsenSetMaximumIterations_MATCOARSEN));
520:   PetscCall(PetscObjectComposeFunction((PetscObject)agg, "MatCoarsenSetThreshold_C", MatCoarsenSetThreshold_MATCOARSEN));
521:   PetscCall(PetscObjectComposeFunction((PetscObject)agg, "MatCoarsenSetStrengthIndex_C", MatCoarsenSetStrengthIndex_MATCOARSEN));
522:   agg->strength_index_size = 0;
523:   *newcrs                  = agg;
524:   PetscFunctionReturn(PETSC_SUCCESS);
525: }