Actual source code: geo.c
1: /*
2: GAMG geometric-algebraic multigrid PC - Mark Adams 2011
3: */
5: #include <../src/ksp/pc/impls/gamg/gamg.h>
7: #if defined(PETSC_HAVE_TRIANGLE)
8: #if !defined(ANSI_DECLARATORS)
9: #define ANSI_DECLARATORS
10: #endif
11: #include <triangle.h>
12: #endif
14: #include <petscblaslapack.h>
16: /* Private context for the GAMG preconditioner */
17: typedef struct {
18: PetscInt lid; /* local vertex index */
19: PetscInt degree; /* vertex degree */
20: } GAMGNode;
22: static inline int petsc_geo_mg_compare(const void *a, const void *b)
23: {
24: return (int)(((GAMGNode *)a)->degree - ((GAMGNode *)b)->degree);
25: }
27: // PetscClangLinter pragma disable: -fdoc-sowing-chars
28: /*
29: PCSetCoordinates_GEO
31: Input Parameter:
32: . pc - the preconditioner context
33: */
34: static PetscErrorCode PCSetCoordinates_GEO(PC pc, PetscInt ndm, PetscInt a_nloc, PetscReal *coords)
35: {
36: PC_MG *mg = (PC_MG *)pc->data;
37: PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
38: PetscInt arrsz, bs, my0, kk, ii, nloc, Iend, aloc;
39: Mat Amat = pc->pmat;
41: PetscFunctionBegin;
43: PetscCall(MatGetBlockSize(Amat, &bs));
44: PetscCall(MatGetOwnershipRange(Amat, &my0, &Iend));
45: aloc = (Iend - my0);
46: nloc = (Iend - my0) / bs;
48: PetscCheck(nloc == a_nloc || aloc == a_nloc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of local blocks %" PetscInt_FMT " must be %" PetscInt_FMT " or %" PetscInt_FMT ".", a_nloc, nloc, aloc);
50: pc_gamg->data_cell_rows = 1;
51: PetscCheck(coords || (nloc <= 0), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Need coordinates for pc_gamg_type 'geo'.");
52: pc_gamg->data_cell_cols = ndm; /* coordinates */
54: arrsz = nloc * pc_gamg->data_cell_rows * pc_gamg->data_cell_cols;
56: /* create data - syntactic sugar that should be refactored at some point */
57: if (!pc_gamg->data || (pc_gamg->data_sz != arrsz)) {
58: PetscCall(PetscFree(pc_gamg->data));
59: PetscCall(PetscMalloc1(arrsz + 1, &pc_gamg->data));
60: }
61: for (kk = 0; kk < arrsz; kk++) pc_gamg->data[kk] = -999.;
62: pc_gamg->data[arrsz] = -99.;
63: /* copy data in - column-oriented */
64: if (nloc == a_nloc) {
65: for (kk = 0; kk < nloc; kk++) {
66: for (ii = 0; ii < ndm; ii++) pc_gamg->data[ii * nloc + kk] = coords[kk * ndm + ii];
67: }
68: } else { /* assumes the coordinates are blocked */
69: for (kk = 0; kk < nloc; kk++) {
70: for (ii = 0; ii < ndm; ii++) pc_gamg->data[ii * nloc + kk] = coords[bs * kk * ndm + ii];
71: }
72: }
73: PetscCheck(pc_gamg->data[arrsz] == -99., PETSC_COMM_SELF, PETSC_ERR_PLIB, "pc_gamg->data[arrsz %" PetscInt_FMT "] %g != -99.", arrsz, (double)pc_gamg->data[arrsz]);
74: pc_gamg->data_sz = arrsz;
75: PetscFunctionReturn(PETSC_SUCCESS);
76: }
78: // PetscClangLinter pragma disable: -fdoc-sowing-chars
79: /*
80: PCSetData_GEO
82: Input Parameter:
83: . pc -
84: */
85: static PetscErrorCode PCSetData_GEO(PC pc, Mat m)
86: {
87: PetscFunctionBegin;
88: SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_PLIB, "GEO MG needs coordinates");
89: }
91: static PetscErrorCode PCSetFromOptions_GEO(PC pc, PetscOptionItems PetscOptionsObject)
92: {
93: PetscFunctionBegin;
94: PetscOptionsHeadBegin(PetscOptionsObject, "GAMG-GEO options");
95: {
96: /* -pc_gamg_sa_nsmooths */
97: /* pc_gamg_sa->smooths = 0; */
98: /* ierr = PetscOptionsInt("-pc_gamg_agg_nsmooths", */
99: /* "smoothing steps for smoothed aggregation, usually 1 (0)", */
100: /* "PCGAMGSetNSmooths_AGG", */
101: /* pc_gamg_sa->smooths, */
102: /* &pc_gamg_sa->smooths, */
103: /* &flag); */
104: }
105: PetscOptionsHeadEnd();
106: PetscFunctionReturn(PETSC_SUCCESS);
107: }
109: // PetscClangLinter pragma disable: -fdoc-sowing-chars
110: /*
111: triangulateAndFormProl
113: Input Parameter:
114: . selected_2 - list of selected local ID, includes selected ghosts
115: . data_stride -
116: . coords[2*data_stride] - column vector of local coordinates w/ ghosts
117: . nselected_1 - selected IDs that go with base (1) graph includes selected ghosts
118: . clid_lid_1[nselected_1] - lids of selected (c) nodes ???????????
119: . agg_lists_1 - list of aggregates selected_1 vertices of aggregate unselected vertices
120: . crsGID[selected.size()] - global index for prolongation operator
121: . bs - block size
122: Output Parameter:
123: . a_Prol - prolongation operator
124: . a_worst_best - measure of worst missed fine vertex, 0 is no misses
125: */
126: static PetscErrorCode triangulateAndFormProl(IS selected_2, PetscInt data_stride, PetscReal coords[], PetscInt nselected_1, const PetscInt clid_lid_1[], const PetscCoarsenData *agg_lists_1, const PetscInt crsGID[], PetscInt bs, Mat a_Prol, PetscReal *a_worst_best)
127: {
128: #if defined(PETSC_HAVE_TRIANGLE)
129: PetscInt jj, tid, tt, idx, nselected_2;
130: struct triangulateio in, mid;
131: const PetscInt *selected_idx_2;
132: PetscMPIInt rank;
133: PetscInt Istart, Iend, nFineLoc, myFine0;
134: int kk, nPlotPts, sid;
135: MPI_Comm comm;
136: PetscReal tm;
138: PetscFunctionBegin;
139: PetscCall(PetscObjectGetComm((PetscObject)a_Prol, &comm));
140: PetscCallMPI(MPI_Comm_rank(comm, &rank));
141: PetscCall(ISGetSize(selected_2, &nselected_2));
142: if (nselected_2 == 1 || nselected_2 == 2) { /* 0 happens on idle processors */
143: *a_worst_best = 100.0; /* this will cause a stop, but not globalized (should not happen) */
144: } else *a_worst_best = 0.0;
145: PetscCallMPI(MPIU_Allreduce(a_worst_best, &tm, 1, MPIU_REAL, MPIU_MAX, comm));
146: if (tm > 0.0) {
147: *a_worst_best = 100.0;
148: PetscFunctionReturn(PETSC_SUCCESS);
149: }
150: PetscCall(MatGetOwnershipRange(a_Prol, &Istart, &Iend));
151: nFineLoc = (Iend - Istart) / bs;
152: myFine0 = Istart / bs;
153: PetscCall(PetscCIntCast(nFineLoc, &nPlotPts)); /* locals */
154: /* triangle */
155: /* Define input points - in */
156: PetscCall(PetscCIntCast(nselected_2, &in.numberofpoints));
157: in.numberofpointattributes = 0;
158: /* get nselected points */
159: PetscCall(PetscMalloc1(2 * nselected_2, &in.pointlist));
160: PetscCall(ISGetIndices(selected_2, &selected_idx_2));
162: for (kk = 0, sid = 0; kk < nselected_2; kk++, sid += 2) {
163: PetscInt lid = selected_idx_2[kk];
164: in.pointlist[sid] = coords[lid];
165: in.pointlist[sid + 1] = coords[data_stride + lid];
166: if (lid >= nFineLoc) nPlotPts++;
167: }
168: PetscCheck(sid == 2 * nselected_2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "sid %d != 2*nselected_2 %" PetscInt_FMT, sid, nselected_2);
170: in.numberofsegments = 0;
171: in.numberofedges = 0;
172: in.numberofholes = 0;
173: in.numberofregions = 0;
174: in.trianglelist = NULL;
175: in.segmentmarkerlist = NULL;
176: in.pointattributelist = NULL;
177: in.pointmarkerlist = NULL;
178: in.triangleattributelist = NULL;
179: in.trianglearealist = NULL;
180: in.segmentlist = NULL;
181: in.holelist = NULL;
182: in.regionlist = NULL;
183: in.edgelist = NULL;
184: in.edgemarkerlist = NULL;
185: in.normlist = NULL;
187: /* triangulate */
188: mid.pointlist = NULL; /* Not needed if -N switch used. */
189: /* Not needed if -N switch used or number of point attributes is zero: */
190: mid.pointattributelist = NULL;
191: mid.pointmarkerlist = NULL; /* Not needed if -N or -B switch used. */
192: mid.trianglelist = NULL; /* Not needed if -E switch used. */
193: /* Not needed if -E switch used or number of triangle attributes is zero: */
194: mid.triangleattributelist = NULL;
195: mid.neighborlist = NULL; /* Needed only if -n switch used. */
196: /* Needed only if segments are output (-p or -c) and -P not used: */
197: mid.segmentlist = NULL;
198: /* Needed only if segments are output (-p or -c) and -P and -B not used: */
199: mid.segmentmarkerlist = NULL;
200: mid.edgelist = NULL; /* Needed only if -e switch used. */
201: mid.edgemarkerlist = NULL; /* Needed if -e used and -B not used. */
202: mid.numberoftriangles = 0;
204: /* Triangulate the points. Switches are chosen to read and write a */
205: /* PSLG (p), preserve the convex hull (c), number everything from */
206: /* zero (z), assign a regional attribute to each element (A), and */
207: /* produce an edge list (e), a Voronoi diagram (v), and a triangle */
208: /* neighbor list (n). */
209: if (nselected_2 != 0) { /* inactive processor */
210: char args[] = "npczQ"; /* c is needed ? */
211: triangulate(args, &in, &mid, (struct triangulateio *)NULL);
212: /* output .poly files for 'showme' */
213: if (!PETSC_TRUE) {
214: static int level = 1;
215: FILE *file;
216: char fname[32];
218: PetscCall(PetscSNPrintf(fname, PETSC_STATIC_ARRAY_LENGTH(fname), "C%d_%d.poly", level, rank));
219: file = fopen(fname, "w");
220: /* First line: <# of vertices> <dimension (must be 2)> <# of attributes> <# of boundary markers (0 or 1)> */
221: fprintf(file, "%d %d %d %d\n", in.numberofpoints, 2, 0, 0);
222: /* Following lines: <vertex #> <x> <y> */
223: for (kk = 0, sid = 0; kk < in.numberofpoints; kk++, sid += 2) fprintf(file, "%d %e %e\n", kk, in.pointlist[sid], in.pointlist[sid + 1]);
224: /* One line: <# of segments> <# of boundary markers (0 or 1)> */
225: fprintf(file, "%d %d\n", 0, 0);
226: /* Following lines: <segment #> <endpoint> <endpoint> [boundary marker] */
227: /* One line: <# of holes> */
228: fprintf(file, "%d\n", 0);
229: /* Following lines: <hole #> <x> <y> */
230: /* Optional line: <# of regional attributes and/or area constraints> */
231: /* Optional following lines: <region #> <x> <y> <attribute> <maximum area> */
232: fclose(file);
234: /* elems */
235: PetscCall(PetscSNPrintf(fname, PETSC_STATIC_ARRAY_LENGTH(fname), "C%d_%d.ele", level, rank));
236: file = fopen(fname, "w");
237: /* First line: <# of triangles> <nodes per triangle> <# of attributes> */
238: fprintf(file, "%d %d %d\n", mid.numberoftriangles, 3, 0);
239: /* Remaining lines: <triangle #> <node> <node> <node> ... [attributes] */
240: for (kk = 0, sid = 0; kk < mid.numberoftriangles; kk++, sid += 3) fprintf(file, "%d %d %d %d\n", kk, mid.trianglelist[sid], mid.trianglelist[sid + 1], mid.trianglelist[sid + 2]);
241: fclose(file);
243: PetscCall(PetscSNPrintf(fname, PETSC_STATIC_ARRAY_LENGTH(fname), "C%d_%d.node", level, rank));
244: file = fopen(fname, "w");
245: /* First line: <# of vertices> <dimension (must be 2)> <# of attributes> <# of boundary markers (0 or 1)> */
246: /* fprintf(file, "%d %d %d %d\n",in.numberofpoints,2,0,0); */
247: fprintf(file, "%d %d %d %d\n", nPlotPts, 2, 0, 0);
248: /* Following lines: <vertex #> <x> <y> */
249: for (kk = 0, sid = 0; kk < in.numberofpoints; kk++, sid += 2) fprintf(file, "%d %e %e\n", kk, in.pointlist[sid], in.pointlist[sid + 1]);
251: sid /= 2;
252: for (jj = 0; jj < nFineLoc; jj++) {
253: PetscBool sel = PETSC_TRUE;
254: for (kk = 0; kk < nselected_2 && sel; kk++) {
255: PetscInt lid = selected_idx_2[kk];
256: if (lid == jj) sel = PETSC_FALSE;
257: }
258: if (sel) fprintf(file, "%d %e %e\n", sid++, (double)coords[jj], (double)coords[data_stride + jj]);
259: }
260: fclose(file);
261: PetscCheck(sid == nPlotPts, PETSC_COMM_SELF, PETSC_ERR_PLIB, "sid %d != nPlotPts %d", sid, nPlotPts);
262: level++;
263: }
264: }
265: { /* form P - setup some maps */
266: PetscInt clid, mm, *nTri, *node_tri;
268: PetscCall(PetscMalloc2(nselected_2, &node_tri, nselected_2, &nTri));
270: /* need list of triangles on node */
271: for (kk = 0; kk < nselected_2; kk++) nTri[kk] = 0;
272: for (tid = 0, kk = 0; tid < mid.numberoftriangles; tid++) {
273: for (jj = 0; jj < 3; jj++) {
274: PetscInt cid = mid.trianglelist[kk++];
275: if (nTri[cid] == 0) node_tri[cid] = tid;
276: nTri[cid]++;
277: }
278: }
279: #define EPS 1.e-12
280: /* find points and set prolongation */
281: for (mm = clid = 0; mm < nFineLoc; mm++) {
282: PetscBool ise;
283: PetscCall(PetscCDIsEmptyAt(agg_lists_1, mm, &ise));
284: if (!ise) {
285: const PetscInt lid = mm;
286: PetscScalar AA[3][3];
287: PetscBLASInt N = 3, NRHS = 1, LDA = 3, IPIV[3], LDB = 3;
288: PetscCDIntNd *pos;
290: PetscCall(PetscCDGetHeadPos(agg_lists_1, lid, &pos));
291: while (pos) {
292: PetscInt flid;
293: PetscCall(PetscCDIntNdGetID(pos, &flid));
294: PetscCall(PetscCDGetNextPos(agg_lists_1, lid, &pos));
296: if (flid < nFineLoc) { /* could be a ghost */
297: PetscInt bestTID = -1;
298: PetscReal best_alpha = 1.e10;
299: const PetscInt fgid = flid + myFine0;
300: /* compute shape function for gid */
301: const PetscReal fcoord[3] = {coords[flid], coords[data_stride + flid], 1.0};
302: PetscBool haveit = PETSC_FALSE;
303: PetscScalar alpha[3];
304: PetscInt clids[3];
306: /* look for it */
307: for (tid = node_tri[clid], jj = 0; jj < 5 && !haveit && tid != -1; jj++) {
308: for (tt = 0; tt < 3; tt++) {
309: PetscInt cid2 = mid.trianglelist[3 * tid + tt];
310: PetscInt lid2 = selected_idx_2[cid2];
311: AA[tt][0] = coords[lid2];
312: AA[tt][1] = coords[data_stride + lid2];
313: AA[tt][2] = 1.0;
314: clids[tt] = cid2; /* store for interp */
315: }
317: for (tt = 0; tt < 3; tt++) alpha[tt] = (PetscScalar)fcoord[tt];
319: PetscCallLAPACKInfo("LAPACKgesv", LAPACKgesv_(&N, &NRHS, (PetscScalar *)AA, &LDA, IPIV, alpha, &LDB, &info));
320: {
321: PetscBool have = PETSC_TRUE;
322: PetscReal lowest = 1.e10;
323: for (tt = 0, idx = 0; tt < 3; tt++) {
324: if (PetscRealPart(alpha[tt]) > (1.0 + EPS) || PetscRealPart(alpha[tt]) < -EPS) have = PETSC_FALSE;
325: if (PetscRealPart(alpha[tt]) < lowest) {
326: lowest = PetscRealPart(alpha[tt]);
327: idx = tt;
328: }
329: }
330: haveit = have;
331: }
332: tid = mid.neighborlist[3 * tid + idx];
333: }
335: if (!haveit) {
336: /* brute force */
337: for (tid = 0; tid < mid.numberoftriangles && !haveit; tid++) {
338: for (tt = 0; tt < 3; tt++) {
339: PetscInt cid2 = mid.trianglelist[3 * tid + tt];
340: PetscInt lid2 = selected_idx_2[cid2];
341: AA[tt][0] = coords[lid2];
342: AA[tt][1] = coords[data_stride + lid2];
343: AA[tt][2] = 1.0;
344: clids[tt] = cid2; /* store for interp */
345: }
346: for (tt = 0; tt < 3; tt++) alpha[tt] = fcoord[tt];
347: PetscCallLAPACKInfo("LAPACKgesv", LAPACKgesv_(&N, &NRHS, (PetscScalar *)AA, &LDA, IPIV, alpha, &LDB, &info));
348: {
349: PetscBool have = PETSC_TRUE;
350: PetscReal worst = 0.0, v;
351: for (tt = 0; tt < 3 && have; tt++) {
352: if (PetscRealPart(alpha[tt]) > 1.0 + EPS || PetscRealPart(alpha[tt]) < -EPS) have = PETSC_FALSE;
353: if ((v = PetscAbs(PetscRealPart(alpha[tt]) - 0.5)) > worst) worst = v;
354: }
355: if (worst < best_alpha) {
356: best_alpha = worst;
357: bestTID = tid;
358: }
359: haveit = have;
360: }
361: }
362: }
363: if (!haveit) {
364: if (best_alpha > *a_worst_best) *a_worst_best = best_alpha;
365: /* use best one */
366: for (tt = 0; tt < 3; tt++) {
367: PetscInt cid2 = mid.trianglelist[3 * bestTID + tt];
368: PetscInt lid2 = selected_idx_2[cid2];
369: AA[tt][0] = coords[lid2];
370: AA[tt][1] = coords[data_stride + lid2];
371: AA[tt][2] = 1.0;
372: clids[tt] = cid2; /* store for interp */
373: }
374: for (tt = 0; tt < 3; tt++) alpha[tt] = fcoord[tt];
375: PetscCallLAPACKInfo("LAPACKgesv", LAPACKgesv_(&N, &NRHS, (PetscScalar *)AA, &LDA, IPIV, alpha, &LDB, &info));
376: }
378: /* put in row of P */
379: for (idx = 0; idx < 3; idx++) {
380: PetscScalar shp = alpha[idx];
381: if (PetscAbs(PetscRealPart(shp)) > 1.e-6) {
382: PetscInt cgid = crsGID[clids[idx]];
383: PetscInt jj = cgid * bs, ii = fgid * bs; /* need to gloalize */
384: for (tt = 0; tt < bs; tt++, ii++, jj++) PetscCall(MatSetValues(a_Prol, 1, &ii, 1, &jj, &shp, INSERT_VALUES));
385: }
386: }
387: }
388: } /* aggregates iterations */
389: clid++;
390: } /* a coarse agg */
391: } /* for all fine nodes */
393: PetscCall(ISRestoreIndices(selected_2, &selected_idx_2));
394: PetscCall(MatAssemblyBegin(a_Prol, MAT_FINAL_ASSEMBLY));
395: PetscCall(MatAssemblyEnd(a_Prol, MAT_FINAL_ASSEMBLY));
397: PetscCall(PetscFree2(node_tri, nTri));
398: }
399: free(mid.trianglelist);
400: free(mid.neighborlist);
401: free(mid.segmentlist);
402: free(mid.segmentmarkerlist);
403: free(mid.pointlist);
404: free(mid.pointmarkerlist);
405: PetscCall(PetscFree(in.pointlist));
406: PetscFunctionReturn(PETSC_SUCCESS);
407: #else
408: SETERRQ(PetscObjectComm((PetscObject)a_Prol), PETSC_ERR_PLIB, "configure with TRIANGLE to use geometric MG");
409: #endif
410: }
412: // PetscClangLinter pragma disable: -fdoc-sowing-chars
413: /*
414: getGIDsOnSquareGraph - square graph, get
416: Input Parameter:
417: . nselected_1 - selected local indices (includes ghosts in input Gmat1)
418: . clid_lid_1 - [nselected_1] lids of selected nodes
419: . Gmat1 - graph that goes with 'selected_1'
420: Output Parameter:
421: . a_selected_2 - selected local indices (includes ghosts in output a_Gmat_2)
422: . a_Gmat_2 - graph that is squared of 'Gmat_1'
423: . a_crsGID[a_selected_2.size()] - map of global IDs of coarse grid nodes
424: */
425: static PetscErrorCode getGIDsOnSquareGraph(PC pc, PetscInt nselected_1, const PetscInt clid_lid_1[], const Mat Gmat1, IS *a_selected_2, Mat *a_Gmat_2, PetscInt **a_crsGID)
426: {
427: PetscMPIInt size;
428: PetscInt *crsGID, kk, my0, Iend, nloc;
429: MPI_Comm comm;
431: PetscFunctionBegin;
432: PetscCall(PetscObjectGetComm((PetscObject)Gmat1, &comm));
433: PetscCallMPI(MPI_Comm_size(comm, &size));
434: PetscCall(MatGetOwnershipRange(Gmat1, &my0, &Iend)); /* AIJ */
435: nloc = Iend - my0; /* this does not change */
437: if (size == 1) { /* not much to do in serial */
438: PetscCall(PetscMalloc1(nselected_1, &crsGID));
439: for (kk = 0; kk < nselected_1; kk++) crsGID[kk] = kk;
440: *a_Gmat_2 = NULL;
441: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nselected_1, clid_lid_1, PETSC_COPY_VALUES, a_selected_2));
442: } else {
443: PetscInt idx, num_fine_ghosts, num_crs_ghost, myCrs0;
444: Mat_MPIAIJ *mpimat2;
445: Mat Gmat2;
446: Vec locState;
447: PetscScalar *cpcol_state;
449: /* scan my coarse zero gid, set 'lid_state' with coarse GID */
450: kk = nselected_1;
451: PetscCallMPI(MPI_Scan(&kk, &myCrs0, 1, MPIU_INT, MPI_SUM, comm));
452: myCrs0 -= nselected_1;
454: if (a_Gmat_2) { /* output */
455: /* grow graph to get wider set of selected vertices to cover fine grid, invalidates 'llist' */
456: PetscCall(PCGAMGSquareGraph_GAMG(pc, Gmat1, &Gmat2));
457: *a_Gmat_2 = Gmat2; /* output */
458: } else Gmat2 = Gmat1; /* use local to get crsGIDs at least */
459: /* get coarse grid GIDS for selected (locals and ghosts) */
460: mpimat2 = (Mat_MPIAIJ *)Gmat2->data;
461: PetscCall(MatCreateVecs(Gmat2, &locState, NULL));
462: PetscCall(VecSet(locState, -1)); /* set with UNKNOWN state */
463: for (kk = 0; kk < nselected_1; kk++) {
464: PetscInt fgid = clid_lid_1[kk] + my0;
465: PetscScalar v = (PetscScalar)(kk + myCrs0);
466: PetscCall(VecSetValues(locState, 1, &fgid, &v, INSERT_VALUES)); /* set with PID */
467: }
468: PetscCall(VecAssemblyBegin(locState));
469: PetscCall(VecAssemblyEnd(locState));
470: PetscCall(VecScatterBegin(mpimat2->Mvctx, locState, mpimat2->lvec, INSERT_VALUES, SCATTER_FORWARD));
471: PetscCall(VecScatterEnd(mpimat2->Mvctx, locState, mpimat2->lvec, INSERT_VALUES, SCATTER_FORWARD));
472: PetscCall(VecGetLocalSize(mpimat2->lvec, &num_fine_ghosts));
473: PetscCall(VecGetArray(mpimat2->lvec, &cpcol_state));
474: for (kk = 0, num_crs_ghost = 0; kk < num_fine_ghosts; kk++) {
475: if ((PetscInt)PetscRealPart(cpcol_state[kk]) != -1) num_crs_ghost++;
476: }
477: PetscCall(PetscMalloc1(nselected_1 + num_crs_ghost, &crsGID)); /* output */
478: {
479: PetscInt *selected_set;
480: PetscCall(PetscMalloc1(nselected_1 + num_crs_ghost, &selected_set));
481: /* do ghost of 'crsGID' */
482: for (kk = 0, idx = nselected_1; kk < num_fine_ghosts; kk++) {
483: if ((PetscInt)PetscRealPart(cpcol_state[kk]) != -1) {
484: PetscInt cgid = (PetscInt)PetscRealPart(cpcol_state[kk]);
485: selected_set[idx] = nloc + kk;
486: crsGID[idx++] = cgid;
487: }
488: }
489: PetscCheck(idx == (nselected_1 + num_crs_ghost), PETSC_COMM_SELF, PETSC_ERR_PLIB, "idx %" PetscInt_FMT " != (nselected_1 %" PetscInt_FMT " + num_crs_ghost %" PetscInt_FMT ")", idx, nselected_1, num_crs_ghost);
490: PetscCall(VecRestoreArray(mpimat2->lvec, &cpcol_state));
491: /* do locals in 'crsGID' */
492: PetscCall(VecGetArray(locState, &cpcol_state));
493: for (kk = 0, idx = 0; kk < nloc; kk++) {
494: if ((PetscInt)PetscRealPart(cpcol_state[kk]) != -1) {
495: PetscInt cgid = (PetscInt)PetscRealPart(cpcol_state[kk]);
496: selected_set[idx] = kk;
497: crsGID[idx++] = cgid;
498: }
499: }
500: PetscCheck(idx == nselected_1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "idx %" PetscInt_FMT " != nselected_1 %" PetscInt_FMT, idx, nselected_1);
501: PetscCall(VecRestoreArray(locState, &cpcol_state));
503: if (a_selected_2 != NULL) { /* output */
504: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nselected_1 + num_crs_ghost, selected_set, PETSC_OWN_POINTER, a_selected_2));
505: } else {
506: PetscCall(PetscFree(selected_set));
507: }
508: }
509: PetscCall(VecDestroy(&locState));
510: }
511: *a_crsGID = crsGID; /* output */
512: PetscFunctionReturn(PETSC_SUCCESS);
513: }
515: static PetscErrorCode PCGAMGCreateGraph_GEO(PC pc, Mat Amat, Mat *a_Gmat)
516: {
517: PC_MG *mg = (PC_MG *)pc->data;
518: PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
519: const PetscReal vfilter = pc_gamg->threshold[0];
521: PetscFunctionBegin;
522: PetscCall(MatCreateGraph(Amat, PETSC_TRUE, PETSC_TRUE, vfilter, 0, NULL, a_Gmat));
523: PetscFunctionReturn(PETSC_SUCCESS);
524: }
526: static PetscErrorCode PCGAMGCoarsen_GEO(PC a_pc, Mat *a_Gmat, PetscCoarsenData **a_llist_parent)
527: {
528: PetscInt Istart, Iend, nloc, kk, Ii, ncols;
529: IS perm;
530: GAMGNode *gnodes;
531: PetscInt *permute;
532: Mat Gmat = *a_Gmat;
533: MPI_Comm comm;
534: MatCoarsen crs;
536: PetscFunctionBegin;
537: PetscCall(PetscObjectGetComm((PetscObject)a_pc, &comm));
539: PetscCall(MatGetOwnershipRange(Gmat, &Istart, &Iend));
540: nloc = (Iend - Istart);
542: /* create random permutation with sort for geo-mg */
543: PetscCall(PetscMalloc1(nloc, &gnodes));
544: PetscCall(PetscMalloc1(nloc, &permute));
546: for (Ii = Istart; Ii < Iend; Ii++) { /* locals only? */
547: PetscCall(MatGetRow(Gmat, Ii, &ncols, NULL, NULL));
548: {
549: PetscInt lid = Ii - Istart;
550: gnodes[lid].lid = lid;
551: gnodes[lid].degree = ncols;
552: }
553: PetscCall(MatRestoreRow(Gmat, Ii, &ncols, NULL, NULL));
554: }
555: if (PETSC_TRUE) {
556: PetscRandom rand;
557: PetscBool *bIndexSet;
558: PetscReal rr;
559: PetscInt iSwapIndex;
561: PetscCall(PetscRandomCreate(comm, &rand));
562: PetscCall(PetscCalloc1(nloc, &bIndexSet));
563: for (Ii = 0; Ii < nloc; Ii++) {
564: PetscCall(PetscRandomGetValueReal(rand, &rr));
565: iSwapIndex = (PetscInt)(rr * nloc);
566: if (!bIndexSet[iSwapIndex] && iSwapIndex != Ii) {
567: GAMGNode iTemp = gnodes[iSwapIndex];
568: gnodes[iSwapIndex] = gnodes[Ii];
569: gnodes[Ii] = iTemp;
570: bIndexSet[Ii] = PETSC_TRUE;
571: bIndexSet[iSwapIndex] = PETSC_TRUE;
572: }
573: }
574: PetscCall(PetscRandomDestroy(&rand));
575: PetscCall(PetscFree(bIndexSet));
576: }
577: /* only sort locals */
578: if (gnodes) qsort(gnodes, nloc, sizeof(GAMGNode), petsc_geo_mg_compare);
579: /* create IS of permutation */
580: for (kk = 0; kk < nloc; kk++) permute[kk] = gnodes[kk].lid; /* locals only */
581: PetscCall(PetscFree(gnodes));
582: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nloc, permute, PETSC_OWN_POINTER, &perm));
584: /* get MIS aggs */
586: PetscCall(MatCoarsenCreate(comm, &crs));
587: PetscCall(MatCoarsenSetType(crs, MATCOARSENMIS));
588: PetscCall(MatCoarsenSetGreedyOrdering(crs, perm));
589: PetscCall(MatCoarsenSetAdjacency(crs, Gmat));
590: PetscCall(MatCoarsenSetStrictAggs(crs, PETSC_FALSE));
591: PetscCall(MatCoarsenApply(crs));
592: PetscCall(MatCoarsenGetData(crs, a_llist_parent));
593: PetscCall(MatCoarsenDestroy(&crs));
595: PetscCall(ISDestroy(&perm));
596: PetscFunctionReturn(PETSC_SUCCESS);
597: }
599: static PetscErrorCode PCGAMGProlongator_GEO(PC pc, Mat Amat, PetscCoarsenData *agg_lists, Mat *a_P_out)
600: {
601: PC_MG *mg = (PC_MG *)pc->data;
602: PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
603: const PetscInt dim = pc_gamg->data_cell_cols, data_cols = pc_gamg->data_cell_cols;
604: PetscInt Istart, Iend, nloc, my0, jj, kk, ncols, nLocalSelected, bs, *clid_flid;
605: Mat Prol, Gmat;
606: PetscMPIInt rank, size;
607: MPI_Comm comm;
608: IS selected_2, selected_1;
609: const PetscInt *selected_idx;
610: MatType mtype;
612: PetscFunctionBegin;
613: PetscCall(PetscObjectGetComm((PetscObject)Amat, &comm));
615: PetscCallMPI(MPI_Comm_rank(comm, &rank));
616: PetscCallMPI(MPI_Comm_size(comm, &size));
617: PetscCall(MatGetOwnershipRange(Amat, &Istart, &Iend));
618: PetscCall(MatGetBlockSize(Amat, &bs));
619: nloc = (Iend - Istart) / bs;
620: my0 = Istart / bs;
621: PetscCheck((Iend - Istart) % bs == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "(Iend %" PetscInt_FMT " - Istart %" PetscInt_FMT ") %% bs %" PetscInt_FMT, Iend, Istart, bs);
623: /* get 'nLocalSelected' */
624: PetscCall(PetscCDGetMat(agg_lists, &Gmat)); // get auxiliary matrix for ghost edges
625: PetscCall(PetscCDGetNonemptyIS(agg_lists, &selected_1));
626: PetscCall(ISGetSize(selected_1, &jj));
627: PetscCall(PetscMalloc1(jj, &clid_flid));
628: PetscCall(ISGetIndices(selected_1, &selected_idx));
629: for (kk = 0, nLocalSelected = 0; kk < jj; kk++) {
630: PetscInt lid = selected_idx[kk];
631: if (lid < nloc) {
632: PetscCall(MatGetRow(Gmat, lid + my0, &ncols, NULL, NULL));
633: if (ncols > 1) clid_flid[nLocalSelected++] = lid; /* filter out singletons */
634: PetscCall(MatRestoreRow(Gmat, lid + my0, &ncols, NULL, NULL));
635: }
636: }
637: PetscCall(ISRestoreIndices(selected_1, &selected_idx));
638: PetscCall(ISDestroy(&selected_1)); /* this is selected_1 in serial */
640: /* create prolongator matrix */
641: PetscCall(MatGetType(Amat, &mtype));
642: PetscCall(MatCreate(comm, &Prol));
643: PetscCall(MatSetSizes(Prol, nloc * bs, nLocalSelected * bs, PETSC_DETERMINE, PETSC_DETERMINE));
644: PetscCall(MatSetBlockSizes(Prol, bs, bs));
645: PetscCall(MatSetType(Prol, mtype));
646: PetscCall(MatSeqAIJSetPreallocation(Prol, 3 * data_cols, NULL));
647: PetscCall(MatMPIAIJSetPreallocation(Prol, 3 * data_cols, NULL, 3 * data_cols, NULL));
649: /* can get all points "removed" - but not on geomg */
650: PetscCall(MatGetSize(Prol, &kk, &jj));
651: if (!jj) {
652: PetscCall(PetscInfo(pc, "ERROE: no selected points on coarse grid\n"));
653: PetscCall(PetscFree(clid_flid));
654: PetscCall(MatDestroy(&Prol));
655: *a_P_out = NULL; /* out */
656: PetscFunctionReturn(PETSC_SUCCESS);
657: }
659: {
660: PetscReal *coords;
661: PetscInt data_stride;
662: PetscInt *crsGID = NULL;
663: Mat Gmat2;
665: PetscCheck(dim == data_cols, PETSC_COMM_SELF, PETSC_ERR_PLIB, "dim %" PetscInt_FMT " != data_cols %" PetscInt_FMT, dim, data_cols);
666: /* grow ghost data for better coarse grid cover of fine grid */
667: /* messy method, squares graph and gets some data */
668: PetscCall(getGIDsOnSquareGraph(pc, nLocalSelected, clid_flid, Gmat, &selected_2, &Gmat2, &crsGID));
669: /* llist is now not valid wrt squared graph, but will work as iterator in 'triangulateAndFormProl' */
670: /* create global vector of coorindates in 'coords' */
671: if (size > 1) {
672: PetscCall(PCGAMGGetDataWithGhosts(Gmat2, dim, pc_gamg->data, &data_stride, &coords));
673: } else {
674: coords = pc_gamg->data;
675: data_stride = pc_gamg->data_sz / pc_gamg->data_cell_cols;
676: }
677: PetscCall(MatDestroy(&Gmat2));
678: /* triangulate */
679: {
680: PetscReal metric, tm;
682: PetscCheck(dim == 2, comm, PETSC_ERR_PLIB, "3D not implemented for 'geo' AMG");
683: PetscCall(triangulateAndFormProl(selected_2, data_stride, coords, nLocalSelected, clid_flid, agg_lists, crsGID, bs, Prol, &metric));
684: PetscCall(PetscFree(crsGID));
686: /* clean up and create coordinates for coarse grid (output) */
687: if (size > 1) PetscCall(PetscFree(coords));
689: PetscCallMPI(MPIU_Allreduce(&metric, &tm, 1, MPIU_REAL, MPIU_MAX, comm));
690: if (tm > 1.) { /* needs to be globalized - should not happen */
691: PetscCall(PetscInfo(pc, " failed metric for coarse grid %e\n", (double)tm));
692: PetscCall(MatDestroy(&Prol));
693: } else if (metric > .0) {
694: PetscCall(PetscInfo(pc, "worst metric for coarse grid = %e\n", (double)metric));
695: }
696: }
697: { /* create next coords - output */
698: PetscReal *crs_crds;
699: PetscCall(PetscMalloc1(dim * nLocalSelected, &crs_crds));
700: for (kk = 0; kk < nLocalSelected; kk++) { /* grab local select nodes to promote - output */
701: PetscInt lid = clid_flid[kk];
702: for (jj = 0; jj < dim; jj++) crs_crds[jj * nLocalSelected + kk] = pc_gamg->data[jj * nloc + lid];
703: }
705: PetscCall(PetscFree(pc_gamg->data));
706: pc_gamg->data = crs_crds; /* out */
707: pc_gamg->data_sz = dim * nLocalSelected;
708: }
709: PetscCall(ISDestroy(&selected_2));
710: }
712: *a_P_out = Prol; /* out */
713: PetscCall(PetscFree(clid_flid));
714: PetscFunctionReturn(PETSC_SUCCESS);
715: }
717: static PetscErrorCode PCDestroy_GAMG_GEO(PC pc)
718: {
719: PetscFunctionBegin;
720: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCSetCoordinates_C", NULL));
721: PetscFunctionReturn(PETSC_SUCCESS);
722: }
724: PetscErrorCode PCCreateGAMG_GEO(PC pc)
725: {
726: PC_MG *mg = (PC_MG *)pc->data;
727: PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
729: PetscFunctionBegin;
730: pc_gamg->ops->setfromoptions = PCSetFromOptions_GEO;
731: pc_gamg->ops->destroy = PCDestroy_GAMG_GEO;
732: /* reset does not do anything; setup not virtual */
734: /* set internal function pointers */
735: pc_gamg->ops->creategraph = PCGAMGCreateGraph_GEO;
736: pc_gamg->ops->coarsen = PCGAMGCoarsen_GEO;
737: pc_gamg->ops->prolongator = PCGAMGProlongator_GEO;
738: pc_gamg->ops->optprolongator = NULL;
739: pc_gamg->ops->createdefaultdata = PCSetData_GEO;
741: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCSetCoordinates_C", PCSetCoordinates_GEO));
742: PetscFunctionReturn(PETSC_SUCCESS);
743: }