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: /*@C
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 Keys:
71: + -mat_coarsen_type (mis|hem|misk) - `mis`: maximal independent set based; `misk`: distance k MIS; `hem`: heavy edge matching
72: - -mat_coarsen_view - view the coarsening object
74: Level: advanced
76: Notes:
77: When the coarsening is used inside `PCGAMG` then the options database keys are prefixed with `-pc_gamg_`
79: Use `MatCoarsenGetData()` to access the results of the coarsening
81: The user can define additional coarsens; see `MatCoarsenRegister()`.
83: .seealso: `MatCoarsen`, `MatCoarsenSetFromOptions()`, `MatCoarsenSetType()`, `MatCoarsenRegister()`, `MatCoarsenCreate()`,
84: `MatCoarsenDestroy()`, `MatCoarsenSetAdjacency()`,
85: `MatCoarsenGetData()`
86: @*/
87: PetscErrorCode MatCoarsenApply(MatCoarsen coarser)
88: {
89: PetscFunctionBegin;
91: PetscAssertPointer(coarser, 1);
92: PetscCheck(coarser->graph->assembled, PetscObjectComm((PetscObject)coarser), PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
93: PetscCheck(!coarser->graph->factortype, PetscObjectComm((PetscObject)coarser), PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
94: PetscCall(PetscLogEventBegin(MAT_Coarsen, coarser, 0, 0, 0));
95: PetscUseTypeMethod(coarser, apply);
96: PetscCall(PetscLogEventEnd(MAT_Coarsen, coarser, 0, 0, 0));
97: PetscFunctionReturn(PETSC_SUCCESS);
98: }
100: /*@
101: MatCoarsenSetAdjacency - Sets the adjacency graph (matrix) of the thing to be coarsened.
103: Collective
105: Input Parameters:
106: + agg - the coarsen context
107: - adj - the adjacency matrix
109: Level: advanced
111: .seealso: `MatCoarsen`, `MatCoarsenSetFromOptions()`, `Mat`, `MatCoarsenCreate()`, `MatCoarsenApply()`
112: @*/
113: PetscErrorCode MatCoarsenSetAdjacency(MatCoarsen agg, Mat adj)
114: {
115: PetscFunctionBegin;
118: agg->graph = adj;
119: PetscFunctionReturn(PETSC_SUCCESS);
120: }
122: /*@
123: MatCoarsenSetStrictAggs - Set whether to keep strict (non overlapping) aggregates in the linked list of aggregates for a coarsen context
125: Logically Collective
127: Input Parameters:
128: + agg - the coarsen context
129: - str - `PETSC_TRUE` keep strict aggregates, `PETSC_FALSE` allow overlap
131: Level: advanced
133: .seealso: `MatCoarsen`, `MatCoarsenCreate()`, `MatCoarsenSetFromOptions()`
134: @*/
135: PetscErrorCode MatCoarsenSetStrictAggs(MatCoarsen agg, PetscBool str)
136: {
137: PetscFunctionBegin;
139: agg->strict_aggs = str;
140: PetscFunctionReturn(PETSC_SUCCESS);
141: }
143: /*@
144: MatCoarsenDestroy - Destroys the coarsen context.
146: Collective
148: Input Parameter:
149: . agg - the coarsen context
151: Level: advanced
153: .seealso: `MatCoarsen`, `MatCoarsenCreate()`
154: @*/
155: PetscErrorCode MatCoarsenDestroy(MatCoarsen *agg)
156: {
157: PetscFunctionBegin;
158: if (!*agg) PetscFunctionReturn(PETSC_SUCCESS);
160: if (--((PetscObject)*agg)->refct > 0) {
161: *agg = NULL;
162: PetscFunctionReturn(PETSC_SUCCESS);
163: }
165: PetscTryTypeMethod(*agg, destroy);
166: if ((*agg)->agg_lists) PetscCall(PetscCDDestroy((*agg)->agg_lists));
167: PetscCall(PetscObjectComposeFunction((PetscObject)*agg, "MatCoarsenSetMaximumIterations_C", NULL));
168: PetscCall(PetscObjectComposeFunction((PetscObject)*agg, "MatCoarsenSetThreshold_C", NULL));
169: PetscCall(PetscObjectComposeFunction((PetscObject)*agg, "MatCoarsenSetStrengthIndex_C", NULL));
171: PetscCall(PetscHeaderDestroy(agg));
172: PetscFunctionReturn(PETSC_SUCCESS);
173: }
175: /*@
176: MatCoarsenViewFromOptions - View the coarsener from the options database
178: Collective
180: Input Parameters:
181: + A - the coarsen context
182: . obj - Optional object that provides the prefix for the option name
183: - name - command line option (usually `-mat_coarsen_view`)
185: Options Database Key:
186: . -name [viewertype][:...] - option name and values. See `PetscObjectViewFromOptions()` for the possible arguments
188: Level: intermediate
190: .seealso: `MatCoarsen`, `MatCoarsenView()`, `PetscObjectViewFromOptions()`, `MatCoarsenCreate()`
191: @*/
192: PetscErrorCode MatCoarsenViewFromOptions(MatCoarsen A, PetscObject obj, const char name[])
193: {
194: PetscFunctionBegin;
196: PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
197: PetscFunctionReturn(PETSC_SUCCESS);
198: }
200: /*@
201: MatCoarsenView - Prints the coarsen data structure.
203: Collective
205: Input Parameters:
206: + agg - the coarsen context
207: - viewer - optional visualization context
209: For viewing the options database see `MatCoarsenViewFromOptions()`
211: Level: advanced
213: .seealso: `MatCoarsen`, `PetscViewer`, `PetscViewerASCIIOpen()`, `MatCoarsenViewFromOptions`
214: @*/
215: PetscErrorCode MatCoarsenView(MatCoarsen agg, PetscViewer viewer)
216: {
217: PetscBool isascii;
219: PetscFunctionBegin;
221: if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)agg), &viewer));
223: PetscCheckSameComm(agg, 1, viewer, 2);
225: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
226: PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)agg, viewer));
227: if (agg->ops->view) {
228: PetscCall(PetscViewerASCIIPushTab(viewer));
229: PetscUseTypeMethod(agg, view, viewer);
230: PetscCall(PetscViewerASCIIPopTab(viewer));
231: }
232: 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]));
233: PetscFunctionReturn(PETSC_SUCCESS);
234: }
236: /*@
237: MatCoarsenSetType - Sets the type of aggregator to use
239: Collective
241: Input Parameters:
242: + coarser - the coarsen context.
243: - type - a known coarsening method
245: Options Database Key:
246: . -mat_coarsen_type type - maximal independent set based; distance k MIS; heavy edge matching
248: Level: advanced
250: .seealso: `MatCoarsen`, `MatCoarsenCreate()`, `MatCoarsenApply()`, `MatCoarsenType`, `MatCoarsenGetType()`
251: @*/
252: PetscErrorCode MatCoarsenSetType(MatCoarsen coarser, MatCoarsenType type)
253: {
254: PetscBool match;
255: PetscErrorCode (*r)(MatCoarsen);
257: PetscFunctionBegin;
259: PetscAssertPointer(type, 2);
261: PetscCall(PetscObjectTypeCompare((PetscObject)coarser, type, &match));
262: if (match) PetscFunctionReturn(PETSC_SUCCESS);
264: PetscTryTypeMethod(coarser, destroy);
265: coarser->ops->destroy = NULL;
266: PetscCall(PetscMemzero(coarser->ops, sizeof(struct _MatCoarsenOps)));
268: PetscCall(PetscFunctionListFind(MatCoarsenList, type, &r));
269: PetscCheck(r, PetscObjectComm((PetscObject)coarser), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown coarsen type %s", type);
270: PetscCall((*r)(coarser));
272: PetscCall(PetscFree(((PetscObject)coarser)->type_name));
273: PetscCall(PetscStrallocpy(type, &((PetscObject)coarser)->type_name));
274: PetscFunctionReturn(PETSC_SUCCESS);
275: }
277: /*@
278: MatCoarsenSetGreedyOrdering - Sets the ordering of the vertices to use with a greedy coarsening method
280: Logically Collective
282: Input Parameters:
283: + coarser - the coarsen context
284: - perm - vertex ordering of (greedy) algorithm
286: Level: advanced
288: Note:
289: The `IS` weights is freed by PETSc, the user should not destroy it or change it after this call
291: .seealso: `MatCoarsen`, `MatCoarsenType`, `MatCoarsenCreate()`, `MatCoarsenSetType()`
292: @*/
293: PetscErrorCode MatCoarsenSetGreedyOrdering(MatCoarsen coarser, const IS perm)
294: {
295: PetscFunctionBegin;
297: coarser->perm = perm;
298: PetscFunctionReturn(PETSC_SUCCESS);
299: }
301: /*@C
302: MatCoarsenGetData - Gets the weights for vertices for a coarsener.
304: Logically Collective, No Fortran Support
306: Input Parameter:
307: . coarser - the coarsen context
309: Output Parameter:
310: . llist - linked list of aggregates
312: Level: advanced
314: Note:
315: This passes ownership to the caller and nullifies the value of weights (`PetscCoarsenData`) within the `MatCoarsen`
317: .seealso: `MatCoarsen`, `MatCoarsenApply()`, `MatCoarsenCreate()`, `MatCoarsenSetType()`, `PetscCoarsenData`
318: @*/
319: PetscErrorCode MatCoarsenGetData(MatCoarsen coarser, PetscCoarsenData **llist)
320: {
321: PetscFunctionBegin;
323: PetscCheck(coarser->agg_lists, PetscObjectComm((PetscObject)coarser), PETSC_ERR_ARG_WRONGSTATE, "No linked list - generate it or call ApplyCoarsen");
324: *llist = coarser->agg_lists;
325: coarser->agg_lists = NULL; /* giving up ownership */
326: PetscFunctionReturn(PETSC_SUCCESS);
327: }
329: /*@
330: MatCoarsenSetFromOptions - Sets various coarsen options from the options database.
332: Collective
334: Input Parameter:
335: . coarser - the coarsen context.
337: Options Database Key:
338: + -mat_coarsen_type (mis|hem|misk) - see `MatCoarsenType`
339: . -mat_coarsen_max_it its - number of iterations to use in the coarsening process, see `MatCoarsenSetMaximumIterations()`
340: - -mat_coarsen_threshold threshold - see `MatCoarsenSetThreshold()`, for `MATCOARSENHEM` only
342: Level: advanced
344: Notes:
345: When the coarsening is used inside `PCGAMG` then the options database keys are prefixed with `-pc_gamg_`
347: Sets the `MatCoarsenType` to `MATCOARSENMISK` if has not been set previously
349: .seealso: `MatCoarsen`, `MatCoarsenType`, `MatCoarsenApply()`, `MatCoarsenCreate()`, `MatCoarsenSetType()`,
350: `MatCoarsenSetMaximumIterations()`, `MATCOARSENHEM`, `MATCOARSENMIS`, `MATCOARSENMISK`
351: @*/
352: PetscErrorCode MatCoarsenSetFromOptions(MatCoarsen coarser)
353: {
354: PetscBool flag;
355: char type[256];
356: const char *def;
358: PetscFunctionBegin;
359: PetscObjectOptionsBegin((PetscObject)coarser);
360: if (!((PetscObject)coarser)->type_name) {
361: def = MATCOARSENMISK;
362: } else {
363: def = ((PetscObject)coarser)->type_name;
364: }
365: PetscCall(PetscOptionsFList("-mat_coarsen_type", "Type of aggregator", "MatCoarsenSetType", MatCoarsenList, def, type, 256, &flag));
366: if (flag) PetscCall(MatCoarsenSetType(coarser, type));
368: PetscCall(PetscOptionsInt("-mat_coarsen_max_it", "Number of iterations (for HEM)", "MatCoarsenSetMaximumIterations", coarser->max_it, &coarser->max_it, NULL));
369: PetscCall(PetscOptionsReal("-mat_coarsen_threshold", "Threshold (for HEM)", "MatCoarsenSetThreshold", coarser->threshold, &coarser->threshold, NULL));
370: coarser->strength_index_size = MAT_COARSEN_STRENGTH_INDEX_SIZE;
371: 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));
372: /*
373: Set the type if it was never set.
374: */
375: if (!((PetscObject)coarser)->type_name) PetscCall(MatCoarsenSetType(coarser, def));
377: PetscTryTypeMethod(coarser, setfromoptions, PetscOptionsObject);
378: PetscOptionsEnd();
379: PetscFunctionReturn(PETSC_SUCCESS);
380: }
382: /*@
383: MatCoarsenSetMaximumIterations - Maximum `MATCOARSENHEM` iterations to use
385: Logically Collective
387: Input Parameters:
388: + coarse - the coarsen context
389: - n - number of HEM iterations
391: Options Database Key:
392: . -mat_coarsen_max_it n - Maximum `MATCOARSENHEM` iterations to use
394: Level: intermediate
396: Note:
397: When the coarsening is used inside `PCGAMG` then the options database keys are prefixed with `-pc_gamg_`
399: .seealso: `MatCoarsen`, `MatCoarsenType`, `MatCoarsenApply()`, `MatCoarsenCreate()`, `MatCoarsenSetType()`
400: @*/
401: PetscErrorCode MatCoarsenSetMaximumIterations(MatCoarsen coarse, PetscInt n)
402: {
403: PetscFunctionBegin;
406: PetscTryMethod(coarse, "MatCoarsenSetMaximumIterations_C", (MatCoarsen, PetscInt), (coarse, n));
407: PetscFunctionReturn(PETSC_SUCCESS);
408: }
410: static PetscErrorCode MatCoarsenSetMaximumIterations_MATCOARSEN(MatCoarsen coarse, PetscInt b)
411: {
412: PetscFunctionBegin;
413: coarse->max_it = b;
414: PetscFunctionReturn(PETSC_SUCCESS);
415: }
417: /*@
418: MatCoarsenSetStrengthIndex - Index array to use for index to use for strength of connection
420: Logically Collective
422: Input Parameters:
423: + coarse - the coarsen context
424: . n - number of indices
425: - idx - array of indices
427: Options Database Key:
428: . -mat_coarsen_strength_index - array of subset of variables per vertex to use for strength norm, -1 for using all (default)
430: Level: intermediate
432: Note:
433: When the coarsening is used inside `PCGAMG` then the options database keys are prefixed with `-pc_gamg_`
435: .seealso: `MatCoarsen`, `MatCoarsenType`, `MatCoarsenApply()`, `MatCoarsenCreate()`, `MatCoarsenSetType()`
436: @*/
437: PetscErrorCode MatCoarsenSetStrengthIndex(MatCoarsen coarse, PetscInt n, PetscInt idx[])
438: {
439: PetscFunctionBegin;
442: PetscTryMethod(coarse, "MatCoarsenSetStrengthIndex_C", (MatCoarsen, PetscInt, PetscInt[]), (coarse, n, idx));
443: PetscFunctionReturn(PETSC_SUCCESS);
444: }
446: static PetscErrorCode MatCoarsenSetStrengthIndex_MATCOARSEN(MatCoarsen coarse, PetscInt n, PetscInt idx[])
447: {
448: PetscFunctionBegin;
449: coarse->strength_index_size = n;
450: for (int iii = 0; iii < n; iii++) coarse->strength_index[iii] = idx[iii];
451: PetscFunctionReturn(PETSC_SUCCESS);
452: }
454: /*@
455: MatCoarsenSetThreshold - Set the threshold for HEM
457: Logically Collective
459: Input Parameters:
460: + coarse - the coarsen context
461: - b - threshold value, default is 0
463: Options Database Key:
464: . -mat_coarsen_threshold b - threshold
466: Level: intermediate
468: Note:
469: When the coarsening is used inside `PCGAMG` then the options database keys are prefixed with `-pc_gamg_`
471: Developer Note:
472: It is not documented how this threshold is used
474: .seealso: `MatCoarsen`, `MatCoarsenType`, `MatCoarsenApply()`, `MatCoarsenCreate()`, `MatCoarsenSetType()`
475: @*/
476: PetscErrorCode MatCoarsenSetThreshold(MatCoarsen coarse, PetscReal b)
477: {
478: PetscFunctionBegin;
481: PetscTryMethod(coarse, "MatCoarsenSetThreshold_C", (MatCoarsen, PetscReal), (coarse, b));
482: PetscFunctionReturn(PETSC_SUCCESS);
483: }
485: static PetscErrorCode MatCoarsenSetThreshold_MATCOARSEN(MatCoarsen coarse, PetscReal b)
486: {
487: PetscFunctionBegin;
488: coarse->threshold = b;
489: PetscFunctionReturn(PETSC_SUCCESS);
490: }
492: /*@
493: MatCoarsenCreate - Creates a coarsen context.
495: Collective
497: Input Parameter:
498: . comm - MPI communicator
500: Output Parameter:
501: . newcrs - location to put the context
503: Level: advanced
505: .seealso: `MatCoarsen`, `MatCoarsenSetType()`, `MatCoarsenApply()`, `MatCoarsenDestroy()`,
506: `MatCoarsenSetAdjacency()`, `MatCoarsenGetData()`
507: @*/
508: PetscErrorCode MatCoarsenCreate(MPI_Comm comm, MatCoarsen *newcrs)
509: {
510: MatCoarsen agg;
512: PetscFunctionBegin;
513: PetscAssertPointer(newcrs, 2);
514: PetscCall(MatInitializePackage());
516: PetscCall(PetscHeaderCreate(agg, MAT_COARSEN_CLASSID, "MatCoarsen", "Matrix/graph coarsen", "MatCoarsen", comm, MatCoarsenDestroy, MatCoarsenView));
517: PetscCall(PetscObjectComposeFunction((PetscObject)agg, "MatCoarsenSetMaximumIterations_C", MatCoarsenSetMaximumIterations_MATCOARSEN));
518: PetscCall(PetscObjectComposeFunction((PetscObject)agg, "MatCoarsenSetThreshold_C", MatCoarsenSetThreshold_MATCOARSEN));
519: PetscCall(PetscObjectComposeFunction((PetscObject)agg, "MatCoarsenSetStrengthIndex_C", MatCoarsenSetStrengthIndex_MATCOARSEN));
520: agg->strength_index_size = 0;
521: *newcrs = agg;
522: PetscFunctionReturn(PETSC_SUCCESS);
523: }