xref: /petsc/src/ksp/pc/impls/gamg/agg.c (revision bb4b53ef092968f72b740b90dbab8a2b6700db0d)
1 /*
2  GAMG geometric-algebric multigrid PC - Mark Adams 2011
3  */
4 
5 #include <../src/ksp/pc/impls/gamg/gamg.h> /*I "petscpc.h" I*/
6 #include <petscblaslapack.h>
7 #include <petscdm.h>
8 #include <petsc/private/kspimpl.h>
9 
10 typedef struct {
11   PetscInt   nsmooths;                     // number of smoothing steps to construct prolongation
12   PetscInt   aggressive_coarsening_levels; // number of aggressive coarsening levels (square or MISk)
13   PetscInt   aggressive_mis_k;             // the k in MIS-k
14   PetscBool  use_aggressive_square_graph;
15   PetscBool  use_minimum_degree_ordering;
16   PetscBool  use_low_mem_filter;
17   PetscBool  graph_symmetrize;
18   MatCoarsen crs;
19 } PC_GAMG_AGG;
20 
21 /*@
22   PCGAMGSetNSmooths - Set number of smoothing steps (1 is typical) used to construct the prolongation operator
23 
24   Logically Collective
25 
26   Input Parameters:
27 + pc - the preconditioner context
28 - n  - the number of smooths
29 
30   Options Database Key:
31 . -pc_gamg_agg_nsmooths <nsmooth, default=1> - number of smoothing steps to use
32 
33   Level: intermediate
34 
35   Note:
36   This is a different concept from the number smoothing steps used during the linear solution process which
37   can be set with `-mg_levels_ksp_max_it`
38 
39   Developer Note:
40   This should be named `PCGAMGAGGSetNSmooths()`.
41 
42 .seealso: [the Users Manual section on PCGAMG](sec_amg), [the Users Manual section on PCMG](sec_mg), [](ch_ksp), `PCMG`, `PCGAMG`
43 @*/
44 PetscErrorCode PCGAMGSetNSmooths(PC pc, PetscInt n)
45 {
46   PetscFunctionBegin;
47   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
48   PetscValidLogicalCollectiveInt(pc, n, 2);
49   PetscTryMethod(pc, "PCGAMGSetNSmooths_C", (PC, PetscInt), (pc, n));
50   PetscFunctionReturn(PETSC_SUCCESS);
51 }
52 
53 static PetscErrorCode PCGAMGSetNSmooths_AGG(PC pc, PetscInt n)
54 {
55   PC_MG       *mg          = (PC_MG *)pc->data;
56   PC_GAMG     *pc_gamg     = (PC_GAMG *)mg->innerctx;
57   PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG *)pc_gamg->subctx;
58 
59   PetscFunctionBegin;
60   pc_gamg_agg->nsmooths = n;
61   PetscFunctionReturn(PETSC_SUCCESS);
62 }
63 
64 /*@
65   PCGAMGSetAggressiveLevels -  Use aggressive coarsening on first n levels
66 
67   Logically Collective
68 
69   Input Parameters:
70 + pc - the preconditioner context
71 - n  - 0, 1 or more
72 
73   Options Database Key:
74 . -pc_gamg_aggressive_coarsening <n,default = 1> - Number of levels on which to square the graph on before aggregating it
75 
76   Level: intermediate
77 
78 .seealso: [the Users Manual section on PCGAMG](sec_amg), [the Users Manual section on PCMG](sec_mg), [](ch_ksp), `PCGAMG`, `PCGAMGSetThreshold()`, `PCGAMGMISkSetAggressive()`, `PCGAMGSetAggressiveSquareGraph()`, `PCGAMGMISkSetMinDegreeOrdering()`, `PCGAMGSetLowMemoryFilter()`
79 @*/
80 PetscErrorCode PCGAMGSetAggressiveLevels(PC pc, PetscInt n)
81 {
82   PetscFunctionBegin;
83   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
84   PetscValidLogicalCollectiveInt(pc, n, 2);
85   PetscTryMethod(pc, "PCGAMGSetAggressiveLevels_C", (PC, PetscInt), (pc, n));
86   PetscFunctionReturn(PETSC_SUCCESS);
87 }
88 
89 /*@
90   PCGAMGMISkSetAggressive - Number (k) distance in MIS coarsening (>2 is 'aggressive')
91 
92   Logically Collective
93 
94   Input Parameters:
95 + pc - the preconditioner context
96 - n  - 1 or more (default = 2)
97 
98   Options Database Key:
99 . -pc_gamg_aggressive_mis_k <n,default=2> - Number (k) distance in MIS coarsening (>2 is 'aggressive')
100 
101   Level: intermediate
102 
103 .seealso: [the Users Manual section on PCGAMG](sec_amg), [the Users Manual section on PCMG](sec_mg), [](ch_ksp), `PCGAMG`, `PCGAMGSetThreshold()`, `PCGAMGSetAggressiveLevels()`, `PCGAMGSetAggressiveSquareGraph()`, `PCGAMGMISkSetMinDegreeOrdering()`, `PCGAMGSetLowMemoryFilter()`
104 @*/
105 PetscErrorCode PCGAMGMISkSetAggressive(PC pc, PetscInt n)
106 {
107   PetscFunctionBegin;
108   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
109   PetscValidLogicalCollectiveInt(pc, n, 2);
110   PetscTryMethod(pc, "PCGAMGMISkSetAggressive_C", (PC, PetscInt), (pc, n));
111   PetscFunctionReturn(PETSC_SUCCESS);
112 }
113 
114 /*@
115   PCGAMGSetAggressiveSquareGraph - Use graph square A'A for aggressive coarsening, old method
116 
117   Logically Collective
118 
119   Input Parameters:
120 + pc - the preconditioner context
121 - b  - default false - MIS-k is faster
122 
123   Options Database Key:
124 . -pc_gamg_aggressive_square_graph <bool,default=false> - Use square graph (A'A) or MIS-k (k=2) for aggressive coarsening
125 
126   Level: intermediate
127 
128 .seealso: [the Users Manual section on PCGAMG](sec_amg), [the Users Manual section on PCMG](sec_mg), [](ch_ksp), `PCGAMG`, `PCGAMGSetThreshold()`, `PCGAMGSetAggressiveLevels()`, `PCGAMGMISkSetAggressive()`, `PCGAMGMISkSetMinDegreeOrdering()`, `PCGAMGSetLowMemoryFilter()`
129 @*/
130 PetscErrorCode PCGAMGSetAggressiveSquareGraph(PC pc, PetscBool b)
131 {
132   PetscFunctionBegin;
133   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
134   PetscValidLogicalCollectiveBool(pc, b, 2);
135   PetscTryMethod(pc, "PCGAMGSetAggressiveSquareGraph_C", (PC, PetscBool), (pc, b));
136   PetscFunctionReturn(PETSC_SUCCESS);
137 }
138 
139 /*@
140   PCGAMGMISkSetMinDegreeOrdering - Use minimum degree ordering in greedy MIS algorithm
141 
142   Logically Collective
143 
144   Input Parameters:
145 + pc - the preconditioner context
146 - b  - default true
147 
148   Options Database Key:
149 . -pc_gamg_mis_k_minimum_degree_ordering <bool,default=true> - Use minimum degree ordering in greedy MIS algorithm
150 
151   Level: intermediate
152 
153 .seealso: [the Users Manual section on PCGAMG](sec_amg), [the Users Manual section on PCMG](sec_mg), [](ch_ksp), `PCGAMG`, `PCGAMGSetThreshold()`, `PCGAMGSetAggressiveLevels()`, `PCGAMGMISkSetAggressive()`, `PCGAMGSetAggressiveSquareGraph()`, `PCGAMGSetLowMemoryFilter()`
154 @*/
155 PetscErrorCode PCGAMGMISkSetMinDegreeOrdering(PC pc, PetscBool b)
156 {
157   PetscFunctionBegin;
158   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
159   PetscValidLogicalCollectiveBool(pc, b, 2);
160   PetscTryMethod(pc, "PCGAMGMISkSetMinDegreeOrdering_C", (PC, PetscBool), (pc, b));
161   PetscFunctionReturn(PETSC_SUCCESS);
162 }
163 
164 /*@
165   PCGAMGSetLowMemoryFilter - Use low memory graph/matrix filter
166 
167   Logically Collective
168 
169   Input Parameters:
170 + pc - the preconditioner context
171 - b  - default false
172 
173   Options Database Key:
174 . -pc_gamg_low_memory_threshold_filter <bool,default=false> - Use low memory graph/matrix filter
175 
176   Level: intermediate
177 
178 .seealso: [the Users Manual section on PCGAMG](sec_amg), [the Users Manual section on PCMG](sec_mg), `PCGAMG`, `PCGAMGSetThreshold()`, `PCGAMGSetAggressiveLevels()`,
179   `PCGAMGMISkSetAggressive()`, `PCGAMGSetAggressiveSquareGraph()`, `PCGAMGMISkSetMinDegreeOrdering()`
180 @*/
181 PetscErrorCode PCGAMGSetLowMemoryFilter(PC pc, PetscBool b)
182 {
183   PetscFunctionBegin;
184   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
185   PetscValidLogicalCollectiveBool(pc, b, 2);
186   PetscTryMethod(pc, "PCGAMGSetLowMemoryFilter_C", (PC, PetscBool), (pc, b));
187   PetscFunctionReturn(PETSC_SUCCESS);
188 }
189 
190 /*@
191   PCGAMGSetGraphSymmetrize - Set the flag to symmetrize the graph used in coarsening
192 
193   Logically Collective
194 
195   Input Parameters:
196 + pc - the preconditioner context
197 - b  - default false
198 
199   Options Database Key:
200 . -pc_gamg_graph_symmetrize <bool,default=false> - Symmetrize the graph
201 
202   Level: intermediate
203 
204 .seealso: [the Users Manual section on PCGAMG](sec_amg), [the Users Manual section on PCMG](sec_mg), `PCGAMG`, `PCGAMGSetThreshold()`, `PCGAMGSetAggressiveLevels()`,
205   `PCGAMGMISkSetAggressive()`, `PCGAMGSetAggressiveSquareGraph()`, `PCGAMGMISkSetMinDegreeOrdering()`
206 @*/
207 PetscErrorCode PCGAMGSetGraphSymmetrize(PC pc, PetscBool b)
208 {
209   PetscFunctionBegin;
210   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
211   PetscValidLogicalCollectiveBool(pc, b, 2);
212   PetscTryMethod(pc, "PCGAMGSetGraphSymmetrize_C", (PC, PetscBool), (pc, b));
213   PetscFunctionReturn(PETSC_SUCCESS);
214 }
215 
216 static PetscErrorCode PCGAMGSetAggressiveLevels_AGG(PC pc, PetscInt n)
217 {
218   PC_MG       *mg          = (PC_MG *)pc->data;
219   PC_GAMG     *pc_gamg     = (PC_GAMG *)mg->innerctx;
220   PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG *)pc_gamg->subctx;
221 
222   PetscFunctionBegin;
223   pc_gamg_agg->aggressive_coarsening_levels = n;
224   PetscFunctionReturn(PETSC_SUCCESS);
225 }
226 
227 static PetscErrorCode PCGAMGMISkSetAggressive_AGG(PC pc, PetscInt n)
228 {
229   PC_MG       *mg          = (PC_MG *)pc->data;
230   PC_GAMG     *pc_gamg     = (PC_GAMG *)mg->innerctx;
231   PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG *)pc_gamg->subctx;
232 
233   PetscFunctionBegin;
234   pc_gamg_agg->aggressive_mis_k = n;
235   PetscFunctionReturn(PETSC_SUCCESS);
236 }
237 
238 static PetscErrorCode PCGAMGSetAggressiveSquareGraph_AGG(PC pc, PetscBool b)
239 {
240   PC_MG       *mg          = (PC_MG *)pc->data;
241   PC_GAMG     *pc_gamg     = (PC_GAMG *)mg->innerctx;
242   PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG *)pc_gamg->subctx;
243 
244   PetscFunctionBegin;
245   pc_gamg_agg->use_aggressive_square_graph = b;
246   PetscFunctionReturn(PETSC_SUCCESS);
247 }
248 
249 static PetscErrorCode PCGAMGSetLowMemoryFilter_AGG(PC pc, PetscBool b)
250 {
251   PC_MG       *mg          = (PC_MG *)pc->data;
252   PC_GAMG     *pc_gamg     = (PC_GAMG *)mg->innerctx;
253   PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG *)pc_gamg->subctx;
254 
255   PetscFunctionBegin;
256   pc_gamg_agg->use_low_mem_filter = b;
257   PetscFunctionReturn(PETSC_SUCCESS);
258 }
259 
260 static PetscErrorCode PCGAMGSetGraphSymmetrize_AGG(PC pc, PetscBool b)
261 {
262   PC_MG       *mg          = (PC_MG *)pc->data;
263   PC_GAMG     *pc_gamg     = (PC_GAMG *)mg->innerctx;
264   PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG *)pc_gamg->subctx;
265 
266   PetscFunctionBegin;
267   pc_gamg_agg->graph_symmetrize = b;
268   PetscFunctionReturn(PETSC_SUCCESS);
269 }
270 
271 static PetscErrorCode PCGAMGMISkSetMinDegreeOrdering_AGG(PC pc, PetscBool b)
272 {
273   PC_MG       *mg          = (PC_MG *)pc->data;
274   PC_GAMG     *pc_gamg     = (PC_GAMG *)mg->innerctx;
275   PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG *)pc_gamg->subctx;
276 
277   PetscFunctionBegin;
278   pc_gamg_agg->use_minimum_degree_ordering = b;
279   PetscFunctionReturn(PETSC_SUCCESS);
280 }
281 
282 static PetscErrorCode PCSetFromOptions_GAMG_AGG(PC pc, PetscOptionItems *PetscOptionsObject)
283 {
284   PC_MG       *mg          = (PC_MG *)pc->data;
285   PC_GAMG     *pc_gamg     = (PC_GAMG *)mg->innerctx;
286   PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG *)pc_gamg->subctx;
287   PetscBool    n_aggressive_flg, old_sq_provided = PETSC_FALSE, new_sq_provided = PETSC_FALSE, new_sqr_graph = pc_gamg_agg->use_aggressive_square_graph;
288   PetscInt     nsq_graph_old = 0;
289 
290   PetscFunctionBegin;
291   PetscOptionsHeadBegin(PetscOptionsObject, "GAMG-AGG options");
292   PetscCall(PetscOptionsInt("-pc_gamg_agg_nsmooths", "number of smoothing steps to construct prolongation, usually 1", "PCGAMGSetNSmooths", pc_gamg_agg->nsmooths, &pc_gamg_agg->nsmooths, NULL));
293   // aggressive coarsening logic with deprecated -pc_gamg_square_graph
294   PetscCall(PetscOptionsInt("-pc_gamg_aggressive_coarsening", "Number of aggressive coarsening (MIS-2) levels from finest", "PCGAMGSetAggressiveLevels", pc_gamg_agg->aggressive_coarsening_levels, &pc_gamg_agg->aggressive_coarsening_levels, &n_aggressive_flg));
295   if (!n_aggressive_flg)
296     PetscCall(PetscOptionsInt("-pc_gamg_square_graph", "Number of aggressive coarsening (MIS-2) levels from finest (deprecated alias for -pc_gamg_aggressive_coarsening)", "PCGAMGSetAggressiveLevels", nsq_graph_old, &nsq_graph_old, &old_sq_provided));
297   PetscCall(PetscOptionsBool("-pc_gamg_aggressive_square_graph", "Use square graph (A'A) or MIS-k (k=2) for aggressive coarsening", "PCGAMGSetAggressiveSquareGraph", new_sqr_graph, &pc_gamg_agg->use_aggressive_square_graph, &new_sq_provided));
298   if (!new_sq_provided && old_sq_provided) {
299     pc_gamg_agg->aggressive_coarsening_levels = nsq_graph_old; // could be zero
300     pc_gamg_agg->use_aggressive_square_graph  = PETSC_TRUE;
301   }
302   if (new_sq_provided && old_sq_provided)
303     PetscCall(PetscInfo(pc, "Warning: both -pc_gamg_square_graph and -pc_gamg_aggressive_coarsening are used. -pc_gamg_square_graph is deprecated, Number of aggressive levels is %d\n", (int)pc_gamg_agg->aggressive_coarsening_levels));
304   PetscCall(PetscOptionsBool("-pc_gamg_mis_k_minimum_degree_ordering", "Use minimum degree ordering for greedy MIS", "PCGAMGMISkSetMinDegreeOrdering", pc_gamg_agg->use_minimum_degree_ordering, &pc_gamg_agg->use_minimum_degree_ordering, NULL));
305   PetscCall(PetscOptionsBool("-pc_gamg_low_memory_threshold_filter", "Use the (built-in) low memory graph/matrix filter", "PCGAMGSetLowMemoryFilter", pc_gamg_agg->use_low_mem_filter, &pc_gamg_agg->use_low_mem_filter, NULL));
306   PetscCall(PetscOptionsInt("-pc_gamg_aggressive_mis_k", "Number of levels of multigrid to use.", "PCGAMGMISkSetAggressive", pc_gamg_agg->aggressive_mis_k, &pc_gamg_agg->aggressive_mis_k, NULL));
307   PetscCall(PetscOptionsBool("-pc_gamg_graph_symmetrize", "Symmetrize graph for coarsening", "PCGAMGSetGraphSymmetrize", pc_gamg_agg->graph_symmetrize, &pc_gamg_agg->graph_symmetrize, NULL));
308   PetscOptionsHeadEnd();
309   PetscFunctionReturn(PETSC_SUCCESS);
310 }
311 
312 static PetscErrorCode PCDestroy_GAMG_AGG(PC pc)
313 {
314   PC_MG       *mg          = (PC_MG *)pc->data;
315   PC_GAMG     *pc_gamg     = (PC_GAMG *)mg->innerctx;
316   PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG *)pc_gamg->subctx;
317 
318   PetscFunctionBegin;
319   PetscCall(MatCoarsenDestroy(&pc_gamg_agg->crs));
320   PetscCall(PetscFree(pc_gamg->subctx));
321   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetNSmooths_C", NULL));
322   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetAggressiveLevels_C", NULL));
323   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGMISkSetAggressive_C", NULL));
324   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGMISkSetMinDegreeOrdering_C", NULL));
325   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetLowMemoryFilter_C", NULL));
326   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetAggressiveSquareGraph_C", NULL));
327   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetGraphSymmetrize_C", NULL));
328   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCSetCoordinates_C", NULL));
329   PetscFunctionReturn(PETSC_SUCCESS);
330 }
331 
332 /*
333    PCSetCoordinates_AGG
334 
335    Collective
336 
337    Input Parameter:
338    . pc - the preconditioner context
339    . ndm - dimension of data (used for dof/vertex for Stokes)
340    . a_nloc - number of vertices local
341    . coords - [a_nloc][ndm] - interleaved coordinate data: {x_0, y_0, z_0, x_1, y_1, ...}
342 */
343 
344 static PetscErrorCode PCSetCoordinates_AGG(PC pc, PetscInt ndm, PetscInt a_nloc, PetscReal *coords)
345 {
346   PC_MG   *mg      = (PC_MG *)pc->data;
347   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
348   PetscInt arrsz, kk, ii, jj, nloc, ndatarows, ndf;
349   Mat      mat = pc->pmat;
350 
351   PetscFunctionBegin;
352   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
353   PetscValidHeaderSpecific(mat, MAT_CLASSID, 1);
354   nloc = a_nloc;
355 
356   /* SA: null space vectors */
357   PetscCall(MatGetBlockSize(mat, &ndf));               /* this does not work for Stokes */
358   if (coords && ndf == 1) pc_gamg->data_cell_cols = 1; /* scalar w/ coords and SA (not needed) */
359   else if (coords) {
360     PetscCheck(ndm <= ndf, PETSC_COMM_SELF, PETSC_ERR_PLIB, "degrees of motion %" PetscInt_FMT " > block size %" PetscInt_FMT, ndm, ndf);
361     pc_gamg->data_cell_cols = (ndm == 2 ? 3 : 6); /* displacement elasticity */
362     if (ndm != ndf) PetscCheck(pc_gamg->data_cell_cols == ndf, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Don't know how to create null space for ndm=%" PetscInt_FMT ", ndf=%" PetscInt_FMT ".  Use MatSetNearNullSpace().", ndm, ndf);
363   } else pc_gamg->data_cell_cols = ndf; /* no data, force SA with constant null space vectors */
364   pc_gamg->data_cell_rows = ndatarows = ndf;
365   PetscCheck(pc_gamg->data_cell_cols > 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "pc_gamg->data_cell_cols %" PetscInt_FMT " <= 0", pc_gamg->data_cell_cols);
366   arrsz = nloc * pc_gamg->data_cell_rows * pc_gamg->data_cell_cols;
367 
368   if (!pc_gamg->data || (pc_gamg->data_sz != arrsz)) {
369     PetscCall(PetscFree(pc_gamg->data));
370     PetscCall(PetscMalloc1(arrsz + 1, &pc_gamg->data));
371   }
372   /* copy data in - column-oriented */
373   for (kk = 0; kk < nloc; kk++) {
374     const PetscInt M    = nloc * pc_gamg->data_cell_rows; /* stride into data */
375     PetscReal     *data = &pc_gamg->data[kk * ndatarows]; /* start of cell */
376 
377     if (pc_gamg->data_cell_cols == 1) *data = 1.0;
378     else {
379       /* translational modes */
380       for (ii = 0; ii < ndatarows; ii++) {
381         for (jj = 0; jj < ndatarows; jj++) {
382           if (ii == jj) data[ii * M + jj] = 1.0;
383           else data[ii * M + jj] = 0.0;
384         }
385       }
386 
387       /* rotational modes */
388       if (coords) {
389         if (ndm == 2) {
390           data += 2 * M;
391           data[0] = -coords[2 * kk + 1];
392           data[1] = coords[2 * kk];
393         } else {
394           data += 3 * M;
395           data[0]         = 0.0;
396           data[M + 0]     = coords[3 * kk + 2];
397           data[2 * M + 0] = -coords[3 * kk + 1];
398           data[1]         = -coords[3 * kk + 2];
399           data[M + 1]     = 0.0;
400           data[2 * M + 1] = coords[3 * kk];
401           data[2]         = coords[3 * kk + 1];
402           data[M + 2]     = -coords[3 * kk];
403           data[2 * M + 2] = 0.0;
404         }
405       }
406     }
407   }
408   pc_gamg->data_sz = arrsz;
409   PetscFunctionReturn(PETSC_SUCCESS);
410 }
411 
412 /*
413    PCSetData_AGG - called if data is not set with PCSetCoordinates.
414       Looks in Mat for near null space.
415       Does not work for Stokes
416 
417   Input Parameter:
418    . pc -
419    . a_A - matrix to get (near) null space out of.
420 */
421 static PetscErrorCode PCSetData_AGG(PC pc, Mat a_A)
422 {
423   PC_MG       *mg      = (PC_MG *)pc->data;
424   PC_GAMG     *pc_gamg = (PC_GAMG *)mg->innerctx;
425   MatNullSpace mnull;
426 
427   PetscFunctionBegin;
428   PetscCall(MatGetNearNullSpace(a_A, &mnull));
429   if (!mnull) {
430     DM dm;
431 
432     PetscCall(PCGetDM(pc, &dm));
433     if (!dm) PetscCall(MatGetDM(a_A, &dm));
434     if (dm) {
435       PetscObject deformation;
436       PetscInt    Nf;
437 
438       PetscCall(DMGetNumFields(dm, &Nf));
439       if (Nf) {
440         PetscCall(DMGetField(dm, 0, NULL, &deformation));
441         PetscCall(PetscObjectQuery((PetscObject)deformation, "nearnullspace", (PetscObject *)&mnull));
442         if (!mnull) PetscCall(PetscObjectQuery((PetscObject)deformation, "nullspace", (PetscObject *)&mnull));
443       }
444     }
445   }
446 
447   if (!mnull) {
448     PetscInt bs, NN, MM;
449 
450     PetscCall(MatGetBlockSize(a_A, &bs));
451     PetscCall(MatGetLocalSize(a_A, &MM, &NN));
452     PetscCheck(MM % bs == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MM %" PetscInt_FMT " must be divisible by bs %" PetscInt_FMT, MM, bs);
453     PetscCall(PCSetCoordinates_AGG(pc, bs, MM / bs, NULL));
454   } else {
455     PetscReal         *nullvec;
456     PetscBool          has_const;
457     PetscInt           i, j, mlocal, nvec, bs;
458     const Vec         *vecs;
459     const PetscScalar *v;
460 
461     PetscCall(MatGetLocalSize(a_A, &mlocal, NULL));
462     PetscCall(MatNullSpaceGetVecs(mnull, &has_const, &nvec, &vecs));
463     for (i = 0; i < nvec; i++) {
464       PetscCall(VecGetLocalSize(vecs[i], &j));
465       PetscCheck(j == mlocal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Attached null space vector size %" PetscInt_FMT " != matrix size %" PetscInt_FMT, j, mlocal);
466     }
467     pc_gamg->data_sz = (nvec + !!has_const) * mlocal;
468     PetscCall(PetscMalloc1((nvec + !!has_const) * mlocal, &nullvec));
469     if (has_const)
470       for (i = 0; i < mlocal; i++) nullvec[i] = 1.0;
471     for (i = 0; i < nvec; i++) {
472       PetscCall(VecGetArrayRead(vecs[i], &v));
473       for (j = 0; j < mlocal; j++) nullvec[(i + !!has_const) * mlocal + j] = PetscRealPart(v[j]);
474       PetscCall(VecRestoreArrayRead(vecs[i], &v));
475     }
476     pc_gamg->data           = nullvec;
477     pc_gamg->data_cell_cols = (nvec + !!has_const);
478     PetscCall(MatGetBlockSize(a_A, &bs));
479     pc_gamg->data_cell_rows = bs;
480   }
481   PetscFunctionReturn(PETSC_SUCCESS);
482 }
483 
484 /*
485   formProl0 - collect null space data for each aggregate, do QR, put R in coarse grid data and Q in P_0
486 
487   Input Parameter:
488    . agg_llists - list of arrays with aggregates -- list from selected vertices of aggregate unselected vertices
489    . bs - row block size
490    . nSAvec - column bs of new P
491    . my0crs - global index of start of locals
492    . data_stride - bs*(nloc nodes + ghost nodes) [data_stride][nSAvec]
493    . data_in[data_stride*nSAvec] - local data on fine grid
494    . flid_fgid[data_stride/bs] - make local to global IDs, includes ghosts in 'locals_llist'
495 
496   Output Parameter:
497    . a_data_out - in with fine grid data (w/ghosts), out with coarse grid data
498    . a_Prol - prolongation operator
499 */
500 static PetscErrorCode formProl0(PetscCoarsenData *agg_llists, PetscInt bs, PetscInt nSAvec, PetscInt my0crs, PetscInt data_stride, PetscReal data_in[], const PetscInt flid_fgid[], PetscReal **a_data_out, Mat a_Prol)
501 {
502   PetscInt        Istart, my0, Iend, nloc, clid, flid = 0, aggID, kk, jj, ii, mm, nSelected, minsz, nghosts, out_data_stride;
503   MPI_Comm        comm;
504   PetscReal      *out_data;
505   PetscCDIntNd   *pos;
506   PCGAMGHashTable fgid_flid;
507 
508   PetscFunctionBegin;
509   PetscCall(PetscObjectGetComm((PetscObject)a_Prol, &comm));
510   PetscCall(MatGetOwnershipRange(a_Prol, &Istart, &Iend));
511   nloc = (Iend - Istart) / bs;
512   my0  = Istart / bs;
513   PetscCheck((Iend - Istart) % bs == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Iend %" PetscInt_FMT " - Istart %" PetscInt_FMT " must be divisible by bs %" PetscInt_FMT, Iend, Istart, bs);
514   Iend /= bs;
515   nghosts = data_stride / bs - nloc;
516 
517   PetscCall(PCGAMGHashTableCreate(2 * nghosts + 1, &fgid_flid));
518   for (kk = 0; kk < nghosts; kk++) PetscCall(PCGAMGHashTableAdd(&fgid_flid, flid_fgid[nloc + kk], nloc + kk));
519 
520   /* count selected -- same as number of cols of P */
521   for (nSelected = mm = 0; mm < nloc; mm++) {
522     PetscBool ise;
523 
524     PetscCall(PetscCDIsEmptyAt(agg_llists, mm, &ise));
525     if (!ise) nSelected++;
526   }
527   PetscCall(MatGetOwnershipRangeColumn(a_Prol, &ii, &jj));
528   PetscCheck((ii / nSAvec) == my0crs, PETSC_COMM_SELF, PETSC_ERR_PLIB, "ii %" PetscInt_FMT " /nSAvec %" PetscInt_FMT "  != my0crs %" PetscInt_FMT, ii, nSAvec, my0crs);
529   PetscCheck(nSelected == (jj - ii) / nSAvec, PETSC_COMM_SELF, PETSC_ERR_PLIB, "nSelected %" PetscInt_FMT " != (jj %" PetscInt_FMT " - ii %" PetscInt_FMT ")/nSAvec %" PetscInt_FMT, nSelected, jj, ii, nSAvec);
530 
531   /* aloc space for coarse point data (output) */
532   out_data_stride = nSelected * nSAvec;
533 
534   PetscCall(PetscMalloc1(out_data_stride * nSAvec, &out_data));
535   for (ii = 0; ii < out_data_stride * nSAvec; ii++) out_data[ii] = PETSC_MAX_REAL;
536   *a_data_out = out_data; /* output - stride nSelected*nSAvec */
537 
538   /* find points and set prolongation */
539   minsz = 100;
540   for (mm = clid = 0; mm < nloc; mm++) {
541     PetscCall(PetscCDCountAt(agg_llists, mm, &jj));
542     if (jj > 0) {
543       const PetscInt lid = mm, cgid = my0crs + clid;
544       PetscInt       cids[100]; /* max bs */
545       PetscBLASInt   asz = jj, M = asz * bs, N = nSAvec, INFO;
546       PetscBLASInt   Mdata = M + ((N - M > 0) ? N - M : 0), LDA = Mdata, LWORK = N * bs;
547       PetscScalar   *qqc, *qqr, *TAU, *WORK;
548       PetscInt      *fids;
549       PetscReal     *data;
550 
551       /* count agg */
552       if (asz < minsz) minsz = asz;
553 
554       /* get block */
555       PetscCall(PetscMalloc5(Mdata * N, &qqc, M * N, &qqr, N, &TAU, LWORK, &WORK, M, &fids));
556 
557       aggID = 0;
558       PetscCall(PetscCDGetHeadPos(agg_llists, lid, &pos));
559       while (pos) {
560         PetscInt gid1;
561 
562         PetscCall(PetscCDIntNdGetID(pos, &gid1));
563         PetscCall(PetscCDGetNextPos(agg_llists, lid, &pos));
564 
565         if (gid1 >= my0 && gid1 < Iend) flid = gid1 - my0;
566         else {
567           PetscCall(PCGAMGHashTableFind(&fgid_flid, gid1, &flid));
568           PetscCheck(flid >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Cannot find gid1 in table");
569         }
570         /* copy in B_i matrix - column-oriented */
571         data = &data_in[flid * bs];
572         for (ii = 0; ii < bs; ii++) {
573           for (jj = 0; jj < N; jj++) {
574             PetscReal d = data[jj * data_stride + ii];
575 
576             qqc[jj * Mdata + aggID * bs + ii] = d;
577           }
578         }
579         /* set fine IDs */
580         for (kk = 0; kk < bs; kk++) fids[aggID * bs + kk] = flid_fgid[flid] * bs + kk;
581         aggID++;
582       }
583 
584       /* pad with zeros */
585       for (ii = asz * bs; ii < Mdata; ii++) {
586         for (jj = 0; jj < N; jj++, kk++) qqc[jj * Mdata + ii] = .0;
587       }
588 
589       /* QR */
590       PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
591       PetscCallBLAS("LAPACKgeqrf", LAPACKgeqrf_(&Mdata, &N, qqc, &LDA, TAU, WORK, &LWORK, &INFO));
592       PetscCall(PetscFPTrapPop());
593       PetscCheck(INFO == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "xGEQRF error");
594       /* get R - column-oriented - output B_{i+1} */
595       {
596         PetscReal *data = &out_data[clid * nSAvec];
597 
598         for (jj = 0; jj < nSAvec; jj++) {
599           for (ii = 0; ii < nSAvec; ii++) {
600             PetscCheck(data[jj * out_data_stride + ii] == PETSC_MAX_REAL, PETSC_COMM_SELF, PETSC_ERR_PLIB, "data[jj*out_data_stride + ii] != %e", (double)PETSC_MAX_REAL);
601             if (ii <= jj) data[jj * out_data_stride + ii] = PetscRealPart(qqc[jj * Mdata + ii]);
602             else data[jj * out_data_stride + ii] = 0.;
603           }
604         }
605       }
606 
607       /* get Q - row-oriented */
608       PetscCallBLAS("LAPACKorgqr", LAPACKorgqr_(&Mdata, &N, &N, qqc, &LDA, TAU, WORK, &LWORK, &INFO));
609       PetscCheck(INFO == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "xORGQR error arg %" PetscBLASInt_FMT, -INFO);
610 
611       for (ii = 0; ii < M; ii++) {
612         for (jj = 0; jj < N; jj++) qqr[N * ii + jj] = qqc[jj * Mdata + ii];
613       }
614 
615       /* add diagonal block of P0 */
616       for (kk = 0; kk < N; kk++) { cids[kk] = N * cgid + kk; /* global col IDs in P0 */ }
617       PetscCall(MatSetValues(a_Prol, M, fids, N, cids, qqr, INSERT_VALUES));
618       PetscCall(PetscFree5(qqc, qqr, TAU, WORK, fids));
619       clid++;
620     } /* coarse agg */
621   } /* for all fine nodes */
622   PetscCall(MatAssemblyBegin(a_Prol, MAT_FINAL_ASSEMBLY));
623   PetscCall(MatAssemblyEnd(a_Prol, MAT_FINAL_ASSEMBLY));
624   PetscCall(PCGAMGHashTableDestroy(&fgid_flid));
625   PetscFunctionReturn(PETSC_SUCCESS);
626 }
627 
628 static PetscErrorCode PCView_GAMG_AGG(PC pc, PetscViewer viewer)
629 {
630   PC_MG       *mg          = (PC_MG *)pc->data;
631   PC_GAMG     *pc_gamg     = (PC_GAMG *)mg->innerctx;
632   PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG *)pc_gamg->subctx;
633 
634   PetscFunctionBegin;
635   PetscCall(PetscViewerASCIIPrintf(viewer, "      AGG specific options\n"));
636   PetscCall(PetscViewerASCIIPrintf(viewer, "        Number of levels of aggressive coarsening %d\n", (int)pc_gamg_agg->aggressive_coarsening_levels));
637   if (pc_gamg_agg->aggressive_coarsening_levels > 0) {
638     PetscCall(PetscViewerASCIIPrintf(viewer, "        %s aggressive coarsening\n", !pc_gamg_agg->use_aggressive_square_graph ? "MIS-k" : "Square graph"));
639     if (!pc_gamg_agg->use_aggressive_square_graph) PetscCall(PetscViewerASCIIPrintf(viewer, "        MIS-%d coarsening on aggressive levels\n", (int)pc_gamg_agg->aggressive_mis_k));
640   }
641   PetscCall(PetscViewerASCIIPushTab(viewer));
642   PetscCall(PetscViewerASCIIPushTab(viewer));
643   PetscCall(PetscViewerASCIIPushTab(viewer));
644   PetscCall(PetscViewerASCIIPushTab(viewer));
645   if (pc_gamg_agg->crs) PetscCall(MatCoarsenView(pc_gamg_agg->crs, viewer));
646   else PetscCall(PetscViewerASCIIPrintf(viewer, "Coarsening algorithm not yet selected\n"));
647   PetscCall(PetscViewerASCIIPopTab(viewer));
648   PetscCall(PetscViewerASCIIPopTab(viewer));
649   PetscCall(PetscViewerASCIIPopTab(viewer));
650   PetscCall(PetscViewerASCIIPopTab(viewer));
651   PetscCall(PetscViewerASCIIPrintf(viewer, "        Number smoothing steps to construct prolongation %d\n", (int)pc_gamg_agg->nsmooths));
652   PetscFunctionReturn(PETSC_SUCCESS);
653 }
654 
655 static PetscErrorCode PCGAMGCreateGraph_AGG(PC pc, Mat Amat, Mat *a_Gmat)
656 {
657   PC_MG          *mg          = (PC_MG *)pc->data;
658   PC_GAMG        *pc_gamg     = (PC_GAMG *)mg->innerctx;
659   PC_GAMG_AGG    *pc_gamg_agg = (PC_GAMG_AGG *)pc_gamg->subctx;
660   const PetscReal vfilter     = pc_gamg->threshold[pc_gamg->current_level];
661   PetscBool       ishem, ismis;
662   const char     *prefix;
663   MatInfo         info0, info1;
664   PetscInt        bs;
665 
666   PetscFunctionBegin;
667   PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[GAMG_COARSEN], 0, 0, 0, 0));
668   /* Note: depending on the algorithm that will be used for computing the coarse grid points this should pass PETSC_TRUE or PETSC_FALSE as the first argument */
669   /* MATCOARSENHEM requires numerical weights for edges so ensure they are computed */
670   PetscCall(MatCoarsenDestroy(&pc_gamg_agg->crs));
671   PetscCall(MatCoarsenCreate(PetscObjectComm((PetscObject)pc), &pc_gamg_agg->crs));
672   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)pc, &prefix));
673   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)pc_gamg_agg->crs, prefix));
674   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)pc_gamg_agg->crs, "pc_gamg_"));
675   PetscCall(MatCoarsenSetFromOptions(pc_gamg_agg->crs));
676   PetscCall(MatGetBlockSize(Amat, &bs));
677   // check for valid indices wrt bs
678   for (int ii = 0; ii < pc_gamg_agg->crs->strength_index_size; ii++) {
679     PetscCheck(pc_gamg_agg->crs->strength_index[ii] >= 0 && pc_gamg_agg->crs->strength_index[ii] < bs, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Indices (%d) must be non-negative and < block size (%d), NB, can not use -mat_coarsen_strength_index with -mat_coarsen_strength_index",
680                (int)pc_gamg_agg->crs->strength_index[ii], (int)bs);
681   }
682   PetscCall(PetscObjectTypeCompare((PetscObject)pc_gamg_agg->crs, MATCOARSENHEM, &ishem));
683   if (ishem) {
684     if (pc_gamg_agg->aggressive_coarsening_levels) PetscCall(PetscInfo(pc, "HEM and aggressive coarsening ignored: HEM using %d iterations\n", (int)pc_gamg_agg->crs->max_it));
685     pc_gamg_agg->aggressive_coarsening_levels = 0;                                         // aggressive and HEM does not make sense
686     PetscCall(MatCoarsenSetMaximumIterations(pc_gamg_agg->crs, pc_gamg_agg->crs->max_it)); // for code coverage
687     PetscCall(MatCoarsenSetThreshold(pc_gamg_agg->crs, vfilter));                          // for code coverage
688   } else {
689     PetscCall(PetscObjectTypeCompare((PetscObject)pc_gamg_agg->crs, MATCOARSENMIS, &ismis));
690     if (ismis && pc_gamg_agg->aggressive_coarsening_levels && !pc_gamg_agg->use_aggressive_square_graph) {
691       PetscCall(PetscInfo(pc, "MIS and aggressive coarsening and no square graph: force square graph\n"));
692       pc_gamg_agg->use_aggressive_square_graph = PETSC_TRUE;
693     }
694   }
695   PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_COARSEN], 0, 0, 0, 0));
696   PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[GAMG_GRAPH], 0, 0, 0, 0));
697   PetscCall(MatGetInfo(Amat, MAT_LOCAL, &info0)); /* global reduction */
698 
699   if (ishem || pc_gamg_agg->use_low_mem_filter) {
700     PetscCall(MatCreateGraph(Amat, pc_gamg_agg->graph_symmetrize, (vfilter >= 0 || ishem) ? PETSC_TRUE : PETSC_FALSE, vfilter, pc_gamg_agg->crs->strength_index_size, pc_gamg_agg->crs->strength_index, a_Gmat));
701   } else {
702     // make scalar graph, symmetrize if not known to be symmetric, scale, but do not filter (expensive)
703     PetscCall(MatCreateGraph(Amat, pc_gamg_agg->graph_symmetrize, PETSC_TRUE, -1, pc_gamg_agg->crs->strength_index_size, pc_gamg_agg->crs->strength_index, a_Gmat));
704     if (vfilter >= 0) {
705       PetscInt           Istart, Iend, ncols, nnz0, nnz1, NN, MM, nloc;
706       Mat                tGmat, Gmat = *a_Gmat;
707       MPI_Comm           comm;
708       const PetscScalar *vals;
709       const PetscInt    *idx;
710       PetscInt          *d_nnz, *o_nnz, kk, *garray = NULL, *AJ, maxcols = 0;
711       MatScalar         *AA; // this is checked in graph
712       PetscBool          isseqaij;
713       Mat                a, b, c;
714       MatType            jtype;
715 
716       PetscCall(PetscObjectGetComm((PetscObject)Gmat, &comm));
717       PetscCall(PetscObjectBaseTypeCompare((PetscObject)Gmat, MATSEQAIJ, &isseqaij));
718       PetscCall(MatGetType(Gmat, &jtype));
719       PetscCall(MatCreate(comm, &tGmat));
720       PetscCall(MatSetType(tGmat, jtype));
721 
722       /* TODO GPU: this can be called when filter = 0 -> Probably provide MatAIJThresholdCompress that compresses the entries below a threshold?
723         Also, if the matrix is symmetric, can we skip this
724         operation? It can be very expensive on large matrices. */
725 
726       // global sizes
727       PetscCall(MatGetSize(Gmat, &MM, &NN));
728       PetscCall(MatGetOwnershipRange(Gmat, &Istart, &Iend));
729       nloc = Iend - Istart;
730       PetscCall(PetscMalloc2(nloc, &d_nnz, nloc, &o_nnz));
731       if (isseqaij) {
732         a = Gmat;
733         b = NULL;
734       } else {
735         Mat_MPIAIJ *d = (Mat_MPIAIJ *)Gmat->data;
736 
737         a      = d->A;
738         b      = d->B;
739         garray = d->garray;
740       }
741       /* Determine upper bound on non-zeros needed in new filtered matrix */
742       for (PetscInt row = 0; row < nloc; row++) {
743         PetscCall(MatGetRow(a, row, &ncols, NULL, NULL));
744         d_nnz[row] = ncols;
745         if (ncols > maxcols) maxcols = ncols;
746         PetscCall(MatRestoreRow(a, row, &ncols, NULL, NULL));
747       }
748       if (b) {
749         for (PetscInt row = 0; row < nloc; row++) {
750           PetscCall(MatGetRow(b, row, &ncols, NULL, NULL));
751           o_nnz[row] = ncols;
752           if (ncols > maxcols) maxcols = ncols;
753           PetscCall(MatRestoreRow(b, row, &ncols, NULL, NULL));
754         }
755       }
756       PetscCall(MatSetSizes(tGmat, nloc, nloc, MM, MM));
757       PetscCall(MatSetBlockSizes(tGmat, 1, 1));
758       PetscCall(MatSeqAIJSetPreallocation(tGmat, 0, d_nnz));
759       PetscCall(MatMPIAIJSetPreallocation(tGmat, 0, d_nnz, 0, o_nnz));
760       PetscCall(MatSetOption(tGmat, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
761       PetscCall(PetscFree2(d_nnz, o_nnz));
762       PetscCall(PetscMalloc2(maxcols, &AA, maxcols, &AJ));
763       nnz0 = nnz1 = 0;
764       for (c = a, kk = 0; c && kk < 2; c = b, kk++) {
765         for (PetscInt row = 0, grow = Istart, ncol_row, jj; row < nloc; row++, grow++) {
766           PetscCall(MatGetRow(c, row, &ncols, &idx, &vals));
767           for (ncol_row = jj = 0; jj < ncols; jj++, nnz0++) {
768             PetscScalar sv = PetscAbs(PetscRealPart(vals[jj]));
769             if (PetscRealPart(sv) > vfilter) {
770               PetscInt cid = idx[jj] + Istart; //diag
771 
772               nnz1++;
773               if (c != a) cid = garray[idx[jj]];
774               AA[ncol_row] = vals[jj];
775               AJ[ncol_row] = cid;
776               ncol_row++;
777             }
778           }
779           PetscCall(MatRestoreRow(c, row, &ncols, &idx, &vals));
780           PetscCall(MatSetValues(tGmat, 1, &grow, ncol_row, AJ, AA, INSERT_VALUES));
781         }
782       }
783       PetscCall(PetscFree2(AA, AJ));
784       PetscCall(MatAssemblyBegin(tGmat, MAT_FINAL_ASSEMBLY));
785       PetscCall(MatAssemblyEnd(tGmat, MAT_FINAL_ASSEMBLY));
786       PetscCall(MatPropagateSymmetryOptions(Gmat, tGmat)); /* Normal Mat options are not relevant ? */
787       PetscCall(PetscInfo(pc, "\t %g%% nnz after filtering, with threshold %g, %g nnz ave. (N=%" PetscInt_FMT ", max row size %" PetscInt_FMT "\n", (!nnz0) ? 1. : 100. * (double)nnz1 / (double)nnz0, (double)vfilter, (!nloc) ? 1. : (double)nnz0 / (double)nloc, MM, maxcols));
788       PetscCall(MatViewFromOptions(tGmat, NULL, "-mat_filter_graph_view"));
789       PetscCall(MatDestroy(&Gmat));
790       *a_Gmat = tGmat;
791     }
792   }
793 
794   PetscCall(MatGetInfo(*a_Gmat, MAT_LOCAL, &info1)); /* global reduction */
795   if (info0.nz_used > 0) PetscCall(PetscInfo(pc, "Filtering left %g %% edges in graph (%e %e)\n", 100.0 * info1.nz_used * (double)(bs * bs) / info0.nz_used, info0.nz_used, info1.nz_used));
796   PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_GRAPH], 0, 0, 0, 0));
797   PetscFunctionReturn(PETSC_SUCCESS);
798 }
799 
800 typedef PetscInt    NState;
801 static const NState NOT_DONE = -2;
802 static const NState DELETED  = -1;
803 static const NState REMOVED  = -3;
804 #define IS_SELECTED(s) (s != DELETED && s != NOT_DONE && s != REMOVED)
805 
806 /*
807    fixAggregatesWithSquare - greedy grab of with G1 (unsquared graph) -- AIJ specific -- change to fixAggregatesWithSquare -- TODD
808      - AGG-MG specific: clears singletons out of 'selected_2'
809 
810    Input Parameter:
811    . Gmat_2 - global matrix of squared graph (data not defined)
812    . Gmat_1 - base graph to grab with base graph
813    Input/Output Parameter:
814    . aggs_2 - linked list of aggs with gids)
815 */
816 static PetscErrorCode fixAggregatesWithSquare(PC pc, Mat Gmat_2, Mat Gmat_1, PetscCoarsenData *aggs_2)
817 {
818   PetscBool      isMPI;
819   Mat_SeqAIJ    *matA_1, *matB_1 = NULL;
820   MPI_Comm       comm;
821   PetscInt       lid, *ii, *idx, ix, Iend, my0, kk, n, j;
822   Mat_MPIAIJ    *mpimat_2 = NULL, *mpimat_1 = NULL;
823   const PetscInt nloc = Gmat_2->rmap->n;
824   PetscScalar   *cpcol_1_state, *cpcol_2_state, *cpcol_2_par_orig, *lid_parent_gid;
825   PetscInt      *lid_cprowID_1 = NULL;
826   NState        *lid_state;
827   Vec            ghost_par_orig2;
828   PetscMPIInt    rank;
829 
830   PetscFunctionBegin;
831   PetscCall(PetscObjectGetComm((PetscObject)Gmat_2, &comm));
832   PetscCallMPI(MPI_Comm_rank(comm, &rank));
833   PetscCall(MatGetOwnershipRange(Gmat_1, &my0, &Iend));
834 
835   /* get submatrices */
836   PetscCall(PetscStrbeginswith(((PetscObject)Gmat_1)->type_name, MATMPIAIJ, &isMPI));
837   PetscCall(PetscInfo(pc, "isMPI = %s\n", isMPI ? "yes" : "no"));
838   PetscCall(PetscMalloc3(nloc, &lid_state, nloc, &lid_parent_gid, nloc, &lid_cprowID_1));
839   for (lid = 0; lid < nloc; lid++) lid_cprowID_1[lid] = -1;
840   if (isMPI) {
841     /* grab matrix objects */
842     mpimat_2 = (Mat_MPIAIJ *)Gmat_2->data;
843     mpimat_1 = (Mat_MPIAIJ *)Gmat_1->data;
844     matA_1   = (Mat_SeqAIJ *)mpimat_1->A->data;
845     matB_1   = (Mat_SeqAIJ *)mpimat_1->B->data;
846 
847     /* force compressed row storage for B matrix in AuxMat */
848     PetscCall(MatCheckCompressedRow(mpimat_1->B, matB_1->nonzerorowcnt, &matB_1->compressedrow, matB_1->i, Gmat_1->rmap->n, -1.0));
849     for (ix = 0; ix < matB_1->compressedrow.nrows; ix++) {
850       PetscInt lid = matB_1->compressedrow.rindex[ix];
851 
852       PetscCheck(lid <= nloc && lid >= -1, PETSC_COMM_SELF, PETSC_ERR_USER, "lid %d out of range. nloc = %d", (int)lid, (int)nloc);
853       if (lid != -1) lid_cprowID_1[lid] = ix;
854     }
855   } else {
856     PetscBool isAIJ;
857 
858     PetscCall(PetscStrbeginswith(((PetscObject)Gmat_1)->type_name, MATSEQAIJ, &isAIJ));
859     PetscCheck(isAIJ, PETSC_COMM_SELF, PETSC_ERR_USER, "Require AIJ matrix.");
860     matA_1 = (Mat_SeqAIJ *)Gmat_1->data;
861   }
862   if (nloc > 0) { PetscCheck(!matB_1 || matB_1->compressedrow.use, PETSC_COMM_SELF, PETSC_ERR_PLIB, "matB_1 && !matB_1->compressedrow.use: PETSc bug???"); }
863   /* get state of locals and selected gid for deleted */
864   for (lid = 0; lid < nloc; lid++) {
865     lid_parent_gid[lid] = -1.0;
866     lid_state[lid]      = DELETED;
867   }
868 
869   /* set lid_state */
870   for (lid = 0; lid < nloc; lid++) {
871     PetscCDIntNd *pos;
872 
873     PetscCall(PetscCDGetHeadPos(aggs_2, lid, &pos));
874     if (pos) {
875       PetscInt gid1;
876 
877       PetscCall(PetscCDIntNdGetID(pos, &gid1));
878       PetscCheck(gid1 == lid + my0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "gid1 %d != lid %d + my0 %d", (int)gid1, (int)lid, (int)my0);
879       lid_state[lid] = gid1;
880     }
881   }
882 
883   /* map local to selected local, DELETED means a ghost owns it */
884   for (lid = kk = 0; lid < nloc; lid++) {
885     NState state = lid_state[lid];
886     if (IS_SELECTED(state)) {
887       PetscCDIntNd *pos;
888 
889       PetscCall(PetscCDGetHeadPos(aggs_2, lid, &pos));
890       while (pos) {
891         PetscInt gid1;
892 
893         PetscCall(PetscCDIntNdGetID(pos, &gid1));
894         PetscCall(PetscCDGetNextPos(aggs_2, lid, &pos));
895         if (gid1 >= my0 && gid1 < Iend) lid_parent_gid[gid1 - my0] = (PetscScalar)(lid + my0);
896       }
897     }
898   }
899   /* get 'cpcol_1/2_state' & cpcol_2_par_orig - uses mpimat_1/2->lvec for temp space */
900   if (isMPI) {
901     Vec tempVec;
902     /* get 'cpcol_1_state' */
903     PetscCall(MatCreateVecs(Gmat_1, &tempVec, NULL));
904     for (kk = 0, j = my0; kk < nloc; kk++, j++) {
905       PetscScalar v = (PetscScalar)lid_state[kk];
906 
907       PetscCall(VecSetValues(tempVec, 1, &j, &v, INSERT_VALUES));
908     }
909     PetscCall(VecAssemblyBegin(tempVec));
910     PetscCall(VecAssemblyEnd(tempVec));
911     PetscCall(VecScatterBegin(mpimat_1->Mvctx, tempVec, mpimat_1->lvec, INSERT_VALUES, SCATTER_FORWARD));
912     PetscCall(VecScatterEnd(mpimat_1->Mvctx, tempVec, mpimat_1->lvec, INSERT_VALUES, SCATTER_FORWARD));
913     PetscCall(VecGetArray(mpimat_1->lvec, &cpcol_1_state));
914     /* get 'cpcol_2_state' */
915     PetscCall(VecScatterBegin(mpimat_2->Mvctx, tempVec, mpimat_2->lvec, INSERT_VALUES, SCATTER_FORWARD));
916     PetscCall(VecScatterEnd(mpimat_2->Mvctx, tempVec, mpimat_2->lvec, INSERT_VALUES, SCATTER_FORWARD));
917     PetscCall(VecGetArray(mpimat_2->lvec, &cpcol_2_state));
918     /* get 'cpcol_2_par_orig' */
919     for (kk = 0, j = my0; kk < nloc; kk++, j++) {
920       PetscScalar v = (PetscScalar)lid_parent_gid[kk];
921 
922       PetscCall(VecSetValues(tempVec, 1, &j, &v, INSERT_VALUES));
923     }
924     PetscCall(VecAssemblyBegin(tempVec));
925     PetscCall(VecAssemblyEnd(tempVec));
926     PetscCall(VecDuplicate(mpimat_2->lvec, &ghost_par_orig2));
927     PetscCall(VecScatterBegin(mpimat_2->Mvctx, tempVec, ghost_par_orig2, INSERT_VALUES, SCATTER_FORWARD));
928     PetscCall(VecScatterEnd(mpimat_2->Mvctx, tempVec, ghost_par_orig2, INSERT_VALUES, SCATTER_FORWARD));
929     PetscCall(VecGetArray(ghost_par_orig2, &cpcol_2_par_orig));
930 
931     PetscCall(VecDestroy(&tempVec));
932   } /* ismpi */
933   for (lid = 0; lid < nloc; lid++) {
934     NState state = lid_state[lid];
935 
936     if (IS_SELECTED(state)) {
937       /* steal locals */
938       ii  = matA_1->i;
939       n   = ii[lid + 1] - ii[lid];
940       idx = matA_1->j + ii[lid];
941       for (j = 0; j < n; j++) {
942         PetscInt lidj   = idx[j], sgid;
943         NState   statej = lid_state[lidj];
944 
945         if (statej == DELETED && (sgid = (PetscInt)PetscRealPart(lid_parent_gid[lidj])) != lid + my0) { /* steal local */
946           lid_parent_gid[lidj] = (PetscScalar)(lid + my0);                                              /* send this if sgid is not local */
947           if (sgid >= my0 && sgid < Iend) {                                                             /* I'm stealing this local from a local sgid */
948             PetscInt      hav = 0, slid = sgid - my0, gidj = lidj + my0;
949             PetscCDIntNd *pos, *last = NULL;
950 
951             /* looking for local from local so id_llist_2 works */
952             PetscCall(PetscCDGetHeadPos(aggs_2, slid, &pos));
953             while (pos) {
954               PetscInt gid;
955               PetscCall(PetscCDIntNdGetID(pos, &gid));
956               if (gid == gidj) {
957                 PetscCheck(last, PETSC_COMM_SELF, PETSC_ERR_PLIB, "last cannot be null");
958                 PetscCall(PetscCDRemoveNextNode(aggs_2, slid, last));
959                 PetscCall(PetscCDAppendNode(aggs_2, lid, pos));
960                 hav = 1;
961                 break;
962               } else last = pos;
963               PetscCall(PetscCDGetNextPos(aggs_2, slid, &pos));
964             }
965             if (hav != 1) {
966               PetscCheck(hav, PETSC_COMM_SELF, PETSC_ERR_PLIB, "failed to find adj in 'selected' lists - structurally unsymmetric matrix");
967               SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "found node %d times???", (int)hav);
968             }
969           } else { /* I'm stealing this local, owned by a ghost */
970             PetscCheck(sgid == -1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mat has an un-symmetric graph. Use '-%spc_gamg_sym_graph true' to symmetrize the graph or '-%spc_gamg_threshold -1' if the matrix is structurally symmetric.",
971                        ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "");
972             PetscCall(PetscCDAppendID(aggs_2, lid, lidj + my0));
973           }
974         }
975       } /* local neighbors */
976     } else if (state == DELETED /* && lid_cprowID_1 */) {
977       PetscInt sgidold = (PetscInt)PetscRealPart(lid_parent_gid[lid]);
978 
979       /* see if I have a selected ghost neighbor that will steal me */
980       if ((ix = lid_cprowID_1[lid]) != -1) {
981         ii  = matB_1->compressedrow.i;
982         n   = ii[ix + 1] - ii[ix];
983         idx = matB_1->j + ii[ix];
984         for (j = 0; j < n; j++) {
985           PetscInt cpid   = idx[j];
986           NState   statej = (NState)PetscRealPart(cpcol_1_state[cpid]);
987 
988           if (IS_SELECTED(statej) && sgidold != (PetscInt)statej) { /* ghost will steal this, remove from my list */
989             lid_parent_gid[lid] = (PetscScalar)statej;              /* send who selected */
990             if (sgidold >= my0 && sgidold < Iend) {                 /* this was mine */
991               PetscInt      hav = 0, oldslidj = sgidold - my0;
992               PetscCDIntNd *pos, *last        = NULL;
993 
994               /* remove from 'oldslidj' list */
995               PetscCall(PetscCDGetHeadPos(aggs_2, oldslidj, &pos));
996               while (pos) {
997                 PetscInt gid;
998 
999                 PetscCall(PetscCDIntNdGetID(pos, &gid));
1000                 if (lid + my0 == gid) {
1001                   /* id_llist_2[lastid] = id_llist_2[flid];   /\* remove lid from oldslidj list *\/ */
1002                   PetscCheck(last, PETSC_COMM_SELF, PETSC_ERR_PLIB, "last cannot be null");
1003                   PetscCall(PetscCDRemoveNextNode(aggs_2, oldslidj, last));
1004                   /* ghost (PetscScalar)statej will add this later */
1005                   hav = 1;
1006                   break;
1007                 } else last = pos;
1008                 PetscCall(PetscCDGetNextPos(aggs_2, oldslidj, &pos));
1009               }
1010               if (hav != 1) {
1011                 PetscCheck(hav, PETSC_COMM_SELF, PETSC_ERR_PLIB, "failed to find (hav=%d) adj in 'selected' lists - structurally unsymmetric matrix", (int)hav);
1012                 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "found node %d times???", (int)hav);
1013               }
1014             } else {
1015               /* TODO: ghosts remove this later */
1016             }
1017           }
1018         }
1019       }
1020     } /* selected/deleted */
1021   } /* node loop */
1022 
1023   if (isMPI) {
1024     PetscScalar    *cpcol_2_parent, *cpcol_2_gid;
1025     Vec             tempVec, ghostgids2, ghostparents2;
1026     PetscInt        cpid, nghost_2;
1027     PCGAMGHashTable gid_cpid;
1028 
1029     PetscCall(VecGetSize(mpimat_2->lvec, &nghost_2));
1030     PetscCall(MatCreateVecs(Gmat_2, &tempVec, NULL));
1031 
1032     /* get 'cpcol_2_parent' */
1033     for (kk = 0, j = my0; kk < nloc; kk++, j++) { PetscCall(VecSetValues(tempVec, 1, &j, &lid_parent_gid[kk], INSERT_VALUES)); }
1034     PetscCall(VecAssemblyBegin(tempVec));
1035     PetscCall(VecAssemblyEnd(tempVec));
1036     PetscCall(VecDuplicate(mpimat_2->lvec, &ghostparents2));
1037     PetscCall(VecScatterBegin(mpimat_2->Mvctx, tempVec, ghostparents2, INSERT_VALUES, SCATTER_FORWARD));
1038     PetscCall(VecScatterEnd(mpimat_2->Mvctx, tempVec, ghostparents2, INSERT_VALUES, SCATTER_FORWARD));
1039     PetscCall(VecGetArray(ghostparents2, &cpcol_2_parent));
1040 
1041     /* get 'cpcol_2_gid' */
1042     for (kk = 0, j = my0; kk < nloc; kk++, j++) {
1043       PetscScalar v = (PetscScalar)j;
1044 
1045       PetscCall(VecSetValues(tempVec, 1, &j, &v, INSERT_VALUES));
1046     }
1047     PetscCall(VecAssemblyBegin(tempVec));
1048     PetscCall(VecAssemblyEnd(tempVec));
1049     PetscCall(VecDuplicate(mpimat_2->lvec, &ghostgids2));
1050     PetscCall(VecScatterBegin(mpimat_2->Mvctx, tempVec, ghostgids2, INSERT_VALUES, SCATTER_FORWARD));
1051     PetscCall(VecScatterEnd(mpimat_2->Mvctx, tempVec, ghostgids2, INSERT_VALUES, SCATTER_FORWARD));
1052     PetscCall(VecGetArray(ghostgids2, &cpcol_2_gid));
1053     PetscCall(VecDestroy(&tempVec));
1054 
1055     /* look for deleted ghosts and add to table */
1056     PetscCall(PCGAMGHashTableCreate(2 * nghost_2 + 1, &gid_cpid));
1057     for (cpid = 0; cpid < nghost_2; cpid++) {
1058       NState state = (NState)PetscRealPart(cpcol_2_state[cpid]);
1059 
1060       if (state == DELETED) {
1061         PetscInt sgid_new = (PetscInt)PetscRealPart(cpcol_2_parent[cpid]);
1062         PetscInt sgid_old = (PetscInt)PetscRealPart(cpcol_2_par_orig[cpid]);
1063 
1064         if (sgid_old == -1 && sgid_new != -1) {
1065           PetscInt gid = (PetscInt)PetscRealPart(cpcol_2_gid[cpid]);
1066 
1067           PetscCall(PCGAMGHashTableAdd(&gid_cpid, gid, cpid));
1068         }
1069       }
1070     }
1071 
1072     /* look for deleted ghosts and see if they moved - remove it */
1073     for (lid = 0; lid < nloc; lid++) {
1074       NState state = lid_state[lid];
1075       if (IS_SELECTED(state)) {
1076         PetscCDIntNd *pos, *last = NULL;
1077         /* look for deleted ghosts and see if they moved */
1078         PetscCall(PetscCDGetHeadPos(aggs_2, lid, &pos));
1079         while (pos) {
1080           PetscInt gid;
1081           PetscCall(PetscCDIntNdGetID(pos, &gid));
1082 
1083           if (gid < my0 || gid >= Iend) {
1084             PetscCall(PCGAMGHashTableFind(&gid_cpid, gid, &cpid));
1085             if (cpid != -1) {
1086               /* a moved ghost - */
1087               /* id_llist_2[lastid] = id_llist_2[flid];    /\* remove 'flid' from list *\/ */
1088               PetscCall(PetscCDRemoveNextNode(aggs_2, lid, last));
1089             } else last = pos;
1090           } else last = pos;
1091 
1092           PetscCall(PetscCDGetNextPos(aggs_2, lid, &pos));
1093         } /* loop over list of deleted */
1094       } /* selected */
1095     }
1096     PetscCall(PCGAMGHashTableDestroy(&gid_cpid));
1097 
1098     /* look at ghosts, see if they changed - and it */
1099     for (cpid = 0; cpid < nghost_2; cpid++) {
1100       PetscInt sgid_new = (PetscInt)PetscRealPart(cpcol_2_parent[cpid]);
1101 
1102       if (sgid_new >= my0 && sgid_new < Iend) { /* this is mine */
1103         PetscInt      gid      = (PetscInt)PetscRealPart(cpcol_2_gid[cpid]);
1104         PetscInt      slid_new = sgid_new - my0, hav = 0;
1105         PetscCDIntNd *pos;
1106 
1107         /* search for this gid to see if I have it */
1108         PetscCall(PetscCDGetHeadPos(aggs_2, slid_new, &pos));
1109         while (pos) {
1110           PetscInt gidj;
1111 
1112           PetscCall(PetscCDIntNdGetID(pos, &gidj));
1113           PetscCall(PetscCDGetNextPos(aggs_2, slid_new, &pos));
1114 
1115           if (gidj == gid) {
1116             hav = 1;
1117             break;
1118           }
1119         }
1120         if (hav != 1) {
1121           /* insert 'flidj' into head of llist */
1122           PetscCall(PetscCDAppendID(aggs_2, slid_new, gid));
1123         }
1124       }
1125     }
1126     PetscCall(VecRestoreArray(mpimat_1->lvec, &cpcol_1_state));
1127     PetscCall(VecRestoreArray(mpimat_2->lvec, &cpcol_2_state));
1128     PetscCall(VecRestoreArray(ghostparents2, &cpcol_2_parent));
1129     PetscCall(VecRestoreArray(ghostgids2, &cpcol_2_gid));
1130     PetscCall(VecDestroy(&ghostgids2));
1131     PetscCall(VecDestroy(&ghostparents2));
1132     PetscCall(VecDestroy(&ghost_par_orig2));
1133   }
1134   PetscCall(PetscFree3(lid_state, lid_parent_gid, lid_cprowID_1));
1135   PetscFunctionReturn(PETSC_SUCCESS);
1136 }
1137 
1138 /*
1139    PCGAMGCoarsen_AGG - supports squaring the graph (deprecated) and new graph for
1140      communication of QR data used with HEM and MISk coarsening
1141 
1142   Input Parameter:
1143    . a_pc - this
1144 
1145   Input/Output Parameter:
1146    . a_Gmat1 - graph to coarsen (in), graph off processor edges for QR gather scatter (out)
1147 
1148   Output Parameter:
1149    . agg_lists - list of aggregates
1150 
1151 */
1152 static PetscErrorCode PCGAMGCoarsen_AGG(PC a_pc, Mat *a_Gmat1, PetscCoarsenData **agg_lists)
1153 {
1154   PC_MG       *mg          = (PC_MG *)a_pc->data;
1155   PC_GAMG     *pc_gamg     = (PC_GAMG *)mg->innerctx;
1156   PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG *)pc_gamg->subctx;
1157   Mat          Gmat2, Gmat1 = *a_Gmat1; /* aggressive graph */
1158   IS           perm;
1159   PetscInt     Istart, Iend, Ii, nloc, bs, nn;
1160   PetscInt    *permute, *degree;
1161   PetscBool   *bIndexSet;
1162   PetscReal    hashfact;
1163   PetscInt     iSwapIndex;
1164   PetscRandom  random;
1165   MPI_Comm     comm;
1166 
1167   PetscFunctionBegin;
1168   PetscCall(PetscObjectGetComm((PetscObject)Gmat1, &comm));
1169   PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[GAMG_COARSEN], 0, 0, 0, 0));
1170   PetscCall(MatGetLocalSize(Gmat1, &nn, NULL));
1171   PetscCall(MatGetBlockSize(Gmat1, &bs));
1172   PetscCheck(bs == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "bs %" PetscInt_FMT " must be 1", bs);
1173   nloc = nn / bs;
1174   /* get MIS aggs - randomize */
1175   PetscCall(PetscMalloc2(nloc, &permute, nloc, &degree));
1176   PetscCall(PetscCalloc1(nloc, &bIndexSet));
1177   for (Ii = 0; Ii < nloc; Ii++) permute[Ii] = Ii;
1178   PetscCall(PetscRandomCreate(PETSC_COMM_SELF, &random));
1179   PetscCall(MatGetOwnershipRange(Gmat1, &Istart, &Iend));
1180   for (Ii = 0; Ii < nloc; Ii++) {
1181     PetscInt nc;
1182 
1183     PetscCall(MatGetRow(Gmat1, Istart + Ii, &nc, NULL, NULL));
1184     degree[Ii] = nc;
1185     PetscCall(MatRestoreRow(Gmat1, Istart + Ii, &nc, NULL, NULL));
1186   }
1187   for (Ii = 0; Ii < nloc; Ii++) {
1188     PetscCall(PetscRandomGetValueReal(random, &hashfact));
1189     iSwapIndex = (PetscInt)(hashfact * nloc) % nloc;
1190     if (!bIndexSet[iSwapIndex] && iSwapIndex != Ii) {
1191       PetscInt iTemp = permute[iSwapIndex];
1192 
1193       permute[iSwapIndex]   = permute[Ii];
1194       permute[Ii]           = iTemp;
1195       iTemp                 = degree[iSwapIndex];
1196       degree[iSwapIndex]    = degree[Ii];
1197       degree[Ii]            = iTemp;
1198       bIndexSet[iSwapIndex] = PETSC_TRUE;
1199     }
1200   }
1201   // apply minimum degree ordering -- NEW
1202   if (pc_gamg_agg->use_minimum_degree_ordering) { PetscCall(PetscSortIntWithArray(nloc, degree, permute)); }
1203   PetscCall(PetscFree(bIndexSet));
1204   PetscCall(PetscRandomDestroy(&random));
1205   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nloc, permute, PETSC_USE_POINTER, &perm));
1206   PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[GAMG_MIS], 0, 0, 0, 0));
1207   // square graph
1208   if (pc_gamg->current_level < pc_gamg_agg->aggressive_coarsening_levels && pc_gamg_agg->use_aggressive_square_graph) {
1209     PetscCall(PCGAMGSquareGraph_GAMG(a_pc, Gmat1, &Gmat2));
1210   } else Gmat2 = Gmat1;
1211   // switch to old MIS-1 for square graph
1212   if (pc_gamg->current_level < pc_gamg_agg->aggressive_coarsening_levels) {
1213     if (!pc_gamg_agg->use_aggressive_square_graph) PetscCall(MatCoarsenMISKSetDistance(pc_gamg_agg->crs, pc_gamg_agg->aggressive_mis_k)); // hardwire to MIS-2
1214     else PetscCall(MatCoarsenSetType(pc_gamg_agg->crs, MATCOARSENMIS));                                                                   // old MIS -- side effect
1215   } else if (pc_gamg_agg->use_aggressive_square_graph && pc_gamg_agg->aggressive_coarsening_levels > 0) {                                 // we reset the MIS
1216     const char *prefix;
1217 
1218     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)a_pc, &prefix));
1219     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)pc_gamg_agg->crs, prefix));
1220     PetscCall(MatCoarsenSetFromOptions(pc_gamg_agg->crs)); // get the default back on non-aggressive levels when square graph switched to old MIS
1221   }
1222   PetscCall(MatCoarsenSetAdjacency(pc_gamg_agg->crs, Gmat2));
1223   PetscCall(MatCoarsenSetStrictAggs(pc_gamg_agg->crs, PETSC_TRUE));
1224   PetscCall(MatCoarsenSetGreedyOrdering(pc_gamg_agg->crs, perm));
1225   PetscCall(MatCoarsenApply(pc_gamg_agg->crs));
1226   PetscCall(MatCoarsenGetData(pc_gamg_agg->crs, agg_lists)); /* output */
1227 
1228   PetscCall(ISDestroy(&perm));
1229   PetscCall(PetscFree2(permute, degree));
1230   PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_MIS], 0, 0, 0, 0));
1231 
1232   if (Gmat2 != Gmat1) { // square graph, we need ghosts for selected
1233     PetscCoarsenData *llist = *agg_lists;
1234 
1235     PetscCall(fixAggregatesWithSquare(a_pc, Gmat2, Gmat1, *agg_lists));
1236     PetscCall(MatDestroy(&Gmat1));
1237     *a_Gmat1 = Gmat2;                          /* output */
1238     PetscCall(PetscCDSetMat(llist, *a_Gmat1)); /* Need a graph with ghosts here */
1239   }
1240   PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_COARSEN], 0, 0, 0, 0));
1241   PetscFunctionReturn(PETSC_SUCCESS);
1242 }
1243 
1244 /*
1245  PCGAMGConstructProlongator_AGG
1246 
1247  Input Parameter:
1248  . pc - this
1249  . Amat - matrix on this fine level
1250  . Graph - used to get ghost data for nodes in
1251  . agg_lists - list of aggregates
1252  Output Parameter:
1253  . a_P_out - prolongation operator to the next level
1254  */
1255 static PetscErrorCode PCGAMGConstructProlongator_AGG(PC pc, Mat Amat, PetscCoarsenData *agg_lists, Mat *a_P_out)
1256 {
1257   PC_MG         *mg      = (PC_MG *)pc->data;
1258   PC_GAMG       *pc_gamg = (PC_GAMG *)mg->innerctx;
1259   const PetscInt col_bs  = pc_gamg->data_cell_cols;
1260   PetscInt       Istart, Iend, nloc, ii, jj, kk, my0, nLocalSelected, bs;
1261   Mat            Gmat, Prol;
1262   PetscMPIInt    size;
1263   MPI_Comm       comm;
1264   PetscReal     *data_w_ghost;
1265   PetscInt       myCrs0, nbnodes = 0, *flid_fgid;
1266   MatType        mtype;
1267 
1268   PetscFunctionBegin;
1269   PetscCall(PetscObjectGetComm((PetscObject)Amat, &comm));
1270   PetscCheck(col_bs >= 1, comm, PETSC_ERR_PLIB, "Column bs cannot be less than 1");
1271   PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[GAMG_PROL], 0, 0, 0, 0));
1272   PetscCallMPI(MPI_Comm_size(comm, &size));
1273   PetscCall(MatGetOwnershipRange(Amat, &Istart, &Iend));
1274   PetscCall(MatGetBlockSize(Amat, &bs));
1275   nloc = (Iend - Istart) / bs;
1276   my0  = Istart / bs;
1277   PetscCheck((Iend - Istart) % bs == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "(Iend %" PetscInt_FMT " - Istart %" PetscInt_FMT ") not divisible by bs %" PetscInt_FMT, Iend, Istart, bs);
1278   PetscCall(PetscCDGetMat(agg_lists, &Gmat)); // get auxiliary matrix for ghost edges for size > 1
1279 
1280   /* get 'nLocalSelected' */
1281   for (ii = 0, nLocalSelected = 0; ii < nloc; ii++) {
1282     PetscBool ise;
1283 
1284     /* filter out singletons 0 or 1? */
1285     PetscCall(PetscCDIsEmptyAt(agg_lists, ii, &ise));
1286     if (!ise) nLocalSelected++;
1287   }
1288 
1289   /* create prolongator, create P matrix */
1290   PetscCall(MatGetType(Amat, &mtype));
1291   PetscCall(MatCreate(comm, &Prol));
1292   PetscCall(MatSetSizes(Prol, nloc * bs, nLocalSelected * col_bs, PETSC_DETERMINE, PETSC_DETERMINE));
1293   PetscCall(MatSetBlockSizes(Prol, bs, col_bs)); // should this be before MatSetSizes?
1294   PetscCall(MatSetType(Prol, mtype));
1295 #if PetscDefined(HAVE_DEVICE)
1296   PetscBool flg;
1297   PetscCall(MatBoundToCPU(Amat, &flg));
1298   PetscCall(MatBindToCPU(Prol, flg));
1299   if (flg) PetscCall(MatSetBindingPropagates(Prol, PETSC_TRUE));
1300 #endif
1301   PetscCall(MatSeqAIJSetPreallocation(Prol, col_bs, NULL));
1302   PetscCall(MatMPIAIJSetPreallocation(Prol, col_bs, NULL, col_bs, NULL));
1303 
1304   /* can get all points "removed" */
1305   PetscCall(MatGetSize(Prol, &kk, &ii));
1306   if (!ii) {
1307     PetscCall(PetscInfo(pc, "%s: No selected points on coarse grid\n", ((PetscObject)pc)->prefix));
1308     PetscCall(MatDestroy(&Prol));
1309     *a_P_out = NULL; /* out */
1310     PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_PROL], 0, 0, 0, 0));
1311     PetscFunctionReturn(PETSC_SUCCESS);
1312   }
1313   PetscCall(PetscInfo(pc, "%s: New grid %" PetscInt_FMT " nodes\n", ((PetscObject)pc)->prefix, ii / col_bs));
1314   PetscCall(MatGetOwnershipRangeColumn(Prol, &myCrs0, &kk));
1315 
1316   PetscCheck((kk - myCrs0) % col_bs == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "(kk %" PetscInt_FMT " -myCrs0 %" PetscInt_FMT ") not divisible by col_bs %" PetscInt_FMT, kk, myCrs0, col_bs);
1317   myCrs0 = myCrs0 / col_bs;
1318   PetscCheck((kk / col_bs - myCrs0) == nLocalSelected, PETSC_COMM_SELF, PETSC_ERR_PLIB, "(kk %" PetscInt_FMT "/col_bs %" PetscInt_FMT " - myCrs0 %" PetscInt_FMT ") != nLocalSelected %" PetscInt_FMT ")", kk, col_bs, myCrs0, nLocalSelected);
1319 
1320   /* create global vector of data in 'data_w_ghost' */
1321   PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[GAMG_PROLA], 0, 0, 0, 0));
1322   if (size > 1) { /* get ghost null space data */
1323     PetscReal *tmp_gdata, *tmp_ldata, *tp2;
1324 
1325     PetscCall(PetscMalloc1(nloc, &tmp_ldata));
1326     for (jj = 0; jj < col_bs; jj++) {
1327       for (kk = 0; kk < bs; kk++) {
1328         PetscInt         ii, stride;
1329         const PetscReal *tp = PetscSafePointerPlusOffset(pc_gamg->data, jj * bs * nloc + kk);
1330 
1331         for (ii = 0; ii < nloc; ii++, tp += bs) tmp_ldata[ii] = *tp;
1332 
1333         PetscCall(PCGAMGGetDataWithGhosts(Gmat, 1, tmp_ldata, &stride, &tmp_gdata));
1334 
1335         if (!jj && !kk) { /* now I know how many total nodes - allocate TODO: move below and do in one 'col_bs' call */
1336           PetscCall(PetscMalloc1(stride * bs * col_bs, &data_w_ghost));
1337           nbnodes = bs * stride;
1338         }
1339         tp2 = PetscSafePointerPlusOffset(data_w_ghost, jj * bs * stride + kk);
1340         for (ii = 0; ii < stride; ii++, tp2 += bs) *tp2 = tmp_gdata[ii];
1341         PetscCall(PetscFree(tmp_gdata));
1342       }
1343     }
1344     PetscCall(PetscFree(tmp_ldata));
1345   } else {
1346     nbnodes      = bs * nloc;
1347     data_w_ghost = (PetscReal *)pc_gamg->data;
1348   }
1349 
1350   /* get 'flid_fgid' TODO - move up to get 'stride' and do get null space data above in one step (jj loop) */
1351   if (size > 1) {
1352     PetscReal *fid_glid_loc, *fiddata;
1353     PetscInt   stride;
1354 
1355     PetscCall(PetscMalloc1(nloc, &fid_glid_loc));
1356     for (kk = 0; kk < nloc; kk++) fid_glid_loc[kk] = (PetscReal)(my0 + kk);
1357     PetscCall(PCGAMGGetDataWithGhosts(Gmat, 1, fid_glid_loc, &stride, &fiddata));
1358     PetscCall(PetscMalloc1(stride, &flid_fgid)); /* copy real data to in */
1359     for (kk = 0; kk < stride; kk++) flid_fgid[kk] = (PetscInt)fiddata[kk];
1360     PetscCall(PetscFree(fiddata));
1361 
1362     PetscCheck(stride == nbnodes / bs, PETSC_COMM_SELF, PETSC_ERR_PLIB, "stride %" PetscInt_FMT " != nbnodes %" PetscInt_FMT "/bs %" PetscInt_FMT, stride, nbnodes, bs);
1363     PetscCall(PetscFree(fid_glid_loc));
1364   } else {
1365     PetscCall(PetscMalloc1(nloc, &flid_fgid));
1366     for (kk = 0; kk < nloc; kk++) flid_fgid[kk] = my0 + kk;
1367   }
1368   PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_PROLA], 0, 0, 0, 0));
1369   /* get P0 */
1370   PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[GAMG_PROLB], 0, 0, 0, 0));
1371   {
1372     PetscReal *data_out = NULL;
1373 
1374     PetscCall(formProl0(agg_lists, bs, col_bs, myCrs0, nbnodes, data_w_ghost, flid_fgid, &data_out, Prol));
1375     PetscCall(PetscFree(pc_gamg->data));
1376 
1377     pc_gamg->data           = data_out;
1378     pc_gamg->data_cell_rows = col_bs;
1379     pc_gamg->data_sz        = col_bs * col_bs * nLocalSelected;
1380   }
1381   PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_PROLB], 0, 0, 0, 0));
1382   if (size > 1) PetscCall(PetscFree(data_w_ghost));
1383   PetscCall(PetscFree(flid_fgid));
1384 
1385   *a_P_out = Prol; /* out */
1386   PetscCall(MatViewFromOptions(Prol, NULL, "-pc_gamg_agg_view_initial_prolongation"));
1387 
1388   PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_PROL], 0, 0, 0, 0));
1389   PetscFunctionReturn(PETSC_SUCCESS);
1390 }
1391 
1392 /*
1393    PCGAMGOptimizeProlongator_AGG - given the initial prolongator optimizes it by smoothed aggregation pc_gamg_agg->nsmooths times
1394 
1395   Input Parameter:
1396    . pc - this
1397    . Amat - matrix on this fine level
1398  In/Output Parameter:
1399    . a_P - prolongation operator to the next level
1400 */
1401 static PetscErrorCode PCGAMGOptimizeProlongator_AGG(PC pc, Mat Amat, Mat *a_P)
1402 {
1403   PC_MG       *mg          = (PC_MG *)pc->data;
1404   PC_GAMG     *pc_gamg     = (PC_GAMG *)mg->innerctx;
1405   PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG *)pc_gamg->subctx;
1406   PetscInt     jj;
1407   Mat          Prol = *a_P;
1408   MPI_Comm     comm;
1409   KSP          eksp;
1410   Vec          bb, xx;
1411   PC           epc;
1412   PetscReal    alpha, emax, emin;
1413 
1414   PetscFunctionBegin;
1415   PetscCall(PetscObjectGetComm((PetscObject)Amat, &comm));
1416   PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[GAMG_OPT], 0, 0, 0, 0));
1417 
1418   /* compute maximum singular value of operator to be used in smoother */
1419   if (0 < pc_gamg_agg->nsmooths) {
1420     /* get eigen estimates */
1421     if (pc_gamg->emax > 0) {
1422       emin = pc_gamg->emin;
1423       emax = pc_gamg->emax;
1424     } else {
1425       const char *prefix;
1426 
1427       PetscCall(MatCreateVecs(Amat, &bb, NULL));
1428       PetscCall(MatCreateVecs(Amat, &xx, NULL));
1429       PetscCall(KSPSetNoisy_Private(bb));
1430 
1431       PetscCall(KSPCreate(comm, &eksp));
1432       PetscCall(KSPSetNestLevel(eksp, pc->kspnestlevel));
1433       PetscCall(PCGetOptionsPrefix(pc, &prefix));
1434       PetscCall(KSPSetOptionsPrefix(eksp, prefix));
1435       PetscCall(KSPAppendOptionsPrefix(eksp, "pc_gamg_esteig_"));
1436       {
1437         PetscBool isset, sflg;
1438 
1439         PetscCall(MatIsSPDKnown(Amat, &isset, &sflg));
1440         if (isset && sflg) PetscCall(KSPSetType(eksp, KSPCG));
1441       }
1442       PetscCall(KSPSetErrorIfNotConverged(eksp, pc->erroriffailure));
1443       PetscCall(KSPSetNormType(eksp, KSP_NORM_NONE));
1444 
1445       PetscCall(KSPSetInitialGuessNonzero(eksp, PETSC_FALSE));
1446       PetscCall(KSPSetOperators(eksp, Amat, Amat));
1447 
1448       PetscCall(KSPGetPC(eksp, &epc));
1449       PetscCall(PCSetType(epc, PCJACOBI)); /* smoother in smoothed agg. */
1450 
1451       PetscCall(KSPSetTolerances(eksp, PETSC_CURRENT, PETSC_CURRENT, PETSC_CURRENT, 10)); // 10 is safer, but 5 is often fine, can override with -pc_gamg_esteig_ksp_max_it -mg_levels_ksp_chebyshev_esteig 0,0.25,0,1.2
1452 
1453       PetscCall(KSPSetFromOptions(eksp));
1454       PetscCall(KSPSetComputeSingularValues(eksp, PETSC_TRUE));
1455       PetscCall(KSPSolve(eksp, bb, xx));
1456       PetscCall(KSPCheckSolve(eksp, pc, xx));
1457 
1458       PetscCall(KSPComputeExtremeSingularValues(eksp, &emax, &emin));
1459       PetscCall(PetscInfo(pc, "%s: Smooth P0: max eigen=%e min=%e PC=%s\n", ((PetscObject)pc)->prefix, (double)emax, (double)emin, PCJACOBI));
1460       PetscCall(VecDestroy(&xx));
1461       PetscCall(VecDestroy(&bb));
1462       PetscCall(KSPDestroy(&eksp));
1463     }
1464     if (pc_gamg->use_sa_esteig) {
1465       mg->min_eigen_DinvA[pc_gamg->current_level] = emin;
1466       mg->max_eigen_DinvA[pc_gamg->current_level] = emax;
1467       PetscCall(PetscInfo(pc, "%s: Smooth P0: level %" PetscInt_FMT ", cache spectra %g %g\n", ((PetscObject)pc)->prefix, pc_gamg->current_level, (double)emin, (double)emax));
1468     } else {
1469       mg->min_eigen_DinvA[pc_gamg->current_level] = 0;
1470       mg->max_eigen_DinvA[pc_gamg->current_level] = 0;
1471     }
1472   } else {
1473     mg->min_eigen_DinvA[pc_gamg->current_level] = 0;
1474     mg->max_eigen_DinvA[pc_gamg->current_level] = 0;
1475   }
1476 
1477   /* smooth P0 */
1478   if (pc_gamg_agg->nsmooths > 0) {
1479     Vec diag;
1480 
1481     /* TODO: Set a PCFailedReason and exit the building of the AMG preconditioner */
1482     PetscCheck(emax != 0.0, PetscObjectComm((PetscObject)pc), PETSC_ERR_PLIB, "Computed maximum singular value as zero");
1483 
1484     PetscCall(MatCreateVecs(Amat, &diag, NULL));
1485     PetscCall(MatGetDiagonal(Amat, diag)); /* effectively PCJACOBI */
1486     PetscCall(VecReciprocal(diag));
1487 
1488     for (jj = 0; jj < pc_gamg_agg->nsmooths; jj++) {
1489       Mat tMat;
1490 
1491       PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[GAMG_OPTSM], 0, 0, 0, 0));
1492       /*
1493         Smooth aggregation on the prolongator
1494 
1495         P_{i} := (I - 1.4/emax D^{-1}A) P_i\{i-1}
1496       */
1497       PetscCall(PetscLogEventBegin(petsc_gamg_setup_matmat_events[pc_gamg->current_level][2], 0, 0, 0, 0));
1498       PetscCall(MatMatMult(Amat, Prol, MAT_INITIAL_MATRIX, PETSC_CURRENT, &tMat));
1499       PetscCall(PetscLogEventEnd(petsc_gamg_setup_matmat_events[pc_gamg->current_level][2], 0, 0, 0, 0));
1500       PetscCall(MatProductClear(tMat));
1501       PetscCall(MatDiagonalScale(tMat, diag, NULL));
1502 
1503       /* TODO: Document the 1.4 and don't hardwire it in this routine */
1504       alpha = -1.4 / emax;
1505       PetscCall(MatAYPX(tMat, alpha, Prol, SUBSET_NONZERO_PATTERN));
1506       PetscCall(MatDestroy(&Prol));
1507       Prol = tMat;
1508       PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_OPTSM], 0, 0, 0, 0));
1509     }
1510     PetscCall(VecDestroy(&diag));
1511   }
1512   PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_OPT], 0, 0, 0, 0));
1513   PetscCall(MatViewFromOptions(Prol, NULL, "-pc_gamg_agg_view_prolongation"));
1514   *a_P = Prol;
1515   PetscFunctionReturn(PETSC_SUCCESS);
1516 }
1517 
1518 /*MC
1519   PCGAMGAGG - Smooth aggregation, {cite}`vanek1996algebraic`, {cite}`vanek2001convergence`, variant of PETSc's algebraic multigrid (`PCGAMG`) preconditioner
1520 
1521   Options Database Keys:
1522 + -pc_gamg_agg_nsmooths <nsmooth, default=1> - number of smoothing steps to use with smooth aggregation to construct prolongation
1523 . -pc_gamg_aggressive_coarsening <n,default=1> - number of aggressive coarsening (MIS-2) levels from finest.
1524 . -pc_gamg_aggressive_square_graph <bool,default=false> - Use square graph (A'A) or MIS-k (k=2) for aggressive coarsening
1525 . -pc_gamg_mis_k_minimum_degree_ordering <bool,default=true> - Use minimum degree ordering in greedy MIS algorithm
1526 . -pc_gamg_pc_gamg_asm_hem_aggs <n,default=0> - Number of HEM aggregation steps for ASM smoother
1527 - -pc_gamg_aggressive_mis_k <n,default=2> - Number (k) distance in MIS coarsening (>2 is 'aggressive')
1528 
1529   Level: intermediate
1530 
1531   Notes:
1532   To obtain good performance for `PCGAMG` for vector valued problems you must
1533   call `MatSetBlockSize()` to indicate the number of degrees of freedom per grid point.
1534   Call `MatSetNearNullSpace()` (or `PCSetCoordinates()` if solving the equations of elasticity) to indicate the near null space of the operator
1535 
1536   The many options for `PCMG` and `PCGAMG` such as controlling the smoothers on each level etc. also work for `PCGAMGAGG`
1537 
1538 .seealso: `PCGAMG`, [the Users Manual section on PCGAMG](sec_amg), [the Users Manual section on PCMG](sec_mg), [](ch_ksp), `PCCreate()`, `PCSetType()`,
1539           `MatSetBlockSize()`, `PCMGType`, `PCSetCoordinates()`, `MatSetNearNullSpace()`, `PCGAMGSetType()`,
1540           `PCGAMGAGG`, `PCGAMGGEO`, `PCGAMGCLASSICAL`, `PCGAMGSetProcEqLim()`, `PCGAMGSetCoarseEqLim()`, `PCGAMGSetRepartition()`, `PCGAMGRegister()`,
1541           `PCGAMGSetReuseInterpolation()`, `PCGAMGASMSetUseAggs()`, `PCGAMGSetParallelCoarseGridSolve()`, `PCGAMGSetNlevels()`, `PCGAMGSetThreshold()`,
1542           `PCGAMGGetType()`, `PCGAMGSetUseSAEstEig()`
1543 M*/
1544 PetscErrorCode PCCreateGAMG_AGG(PC pc)
1545 {
1546   PC_MG       *mg      = (PC_MG *)pc->data;
1547   PC_GAMG     *pc_gamg = (PC_GAMG *)mg->innerctx;
1548   PC_GAMG_AGG *pc_gamg_agg;
1549 
1550   PetscFunctionBegin;
1551   /* create sub context for SA */
1552   PetscCall(PetscNew(&pc_gamg_agg));
1553   pc_gamg->subctx = pc_gamg_agg;
1554 
1555   pc_gamg->ops->setfromoptions = PCSetFromOptions_GAMG_AGG;
1556   pc_gamg->ops->destroy        = PCDestroy_GAMG_AGG;
1557   /* reset does not do anything; setup not virtual */
1558 
1559   /* set internal function pointers */
1560   pc_gamg->ops->creategraph       = PCGAMGCreateGraph_AGG;
1561   pc_gamg->ops->coarsen           = PCGAMGCoarsen_AGG;
1562   pc_gamg->ops->prolongator       = PCGAMGConstructProlongator_AGG;
1563   pc_gamg->ops->optprolongator    = PCGAMGOptimizeProlongator_AGG;
1564   pc_gamg->ops->createdefaultdata = PCSetData_AGG;
1565   pc_gamg->ops->view              = PCView_GAMG_AGG;
1566 
1567   pc_gamg_agg->nsmooths                     = 1;
1568   pc_gamg_agg->aggressive_coarsening_levels = 1;
1569   pc_gamg_agg->use_aggressive_square_graph  = PETSC_TRUE;
1570   pc_gamg_agg->use_minimum_degree_ordering  = PETSC_FALSE;
1571   pc_gamg_agg->use_low_mem_filter           = PETSC_FALSE;
1572   pc_gamg_agg->aggressive_mis_k             = 2;
1573   pc_gamg_agg->graph_symmetrize             = PETSC_TRUE;
1574 
1575   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetNSmooths_C", PCGAMGSetNSmooths_AGG));
1576   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetAggressiveLevels_C", PCGAMGSetAggressiveLevels_AGG));
1577   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetAggressiveSquareGraph_C", PCGAMGSetAggressiveSquareGraph_AGG));
1578   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGMISkSetMinDegreeOrdering_C", PCGAMGMISkSetMinDegreeOrdering_AGG));
1579   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetLowMemoryFilter_C", PCGAMGSetLowMemoryFilter_AGG));
1580   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGMISkSetAggressive_C", PCGAMGMISkSetAggressive_AGG));
1581   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetGraphSymmetrize_C", PCGAMGSetGraphSymmetrize_AGG));
1582   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCSetCoordinates_C", PCSetCoordinates_AGG));
1583   PetscFunctionReturn(PETSC_SUCCESS);
1584 }
1585