Actual source code: geo.c
petsc-3.7.3 2016-08-01
1: /*
2: GAMG geometric-algebric multiogrid PC - Mark Adams 2011
3: */
5: #include <../src/ksp/pc/impls/gamg/gamg.h> /*I "petscpc.h" I*/
6: #include <petsc/private/kspimpl.h>
8: #if defined(PETSC_HAVE_TRIANGLE)
9: #define REAL PetscReal
10: #include <triangle.h>
11: #endif
13: #include <petscblaslapack.h>
15: /* Private context for the GAMG preconditioner */
16: typedef struct {
17: PetscInt lid; /* local vertex index */
18: PetscInt degree; /* vertex degree */
19: } GAMGNode;
21: int petsc_geo_mg_compare(const void *a, const void *b)
22: {
23: return (((GAMGNode*)a)->degree - ((GAMGNode*)b)->degree);
24: }
26: /* -------------------------------------------------------------------------- */
27: /*
28: PCSetCoordinates_GEO
30: Input Parameter:
31: . pc - the preconditioner context
32: */
35: PetscErrorCode PCSetCoordinates_GEO(PC pc, PetscInt ndm, PetscInt a_nloc, PetscReal *coords)
36: {
37: PC_MG *mg = (PC_MG*)pc->data;
38: PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx;
40: PetscInt arrsz,bs,my0,kk,ii,nloc,Iend;
41: Mat Amat = pc->pmat;
45: MatGetBlockSize(Amat, &bs);
47: MatGetOwnershipRange(Amat, &my0, &Iend);
48: nloc = (Iend-my0)/bs;
50: if (nloc!=a_nloc) SETERRQ2(PetscObjectComm((PetscObject)Amat),PETSC_ERR_ARG_WRONG, "Stokes not supported nloc = %d %d.",a_nloc,nloc);
51: if ((Iend-my0)%bs!=0) SETERRQ1(PetscObjectComm((PetscObject)Amat),PETSC_ERR_ARG_WRONG, "Bad local size %d.",nloc);
53: pc_gamg->data_cell_rows = 1;
54: if (!coords && nloc > 0) SETERRQ(PetscObjectComm((PetscObject)Amat),PETSC_ERR_ARG_WRONG, "Need coordinates for pc_gamg_type 'geo'.");
55: pc_gamg->data_cell_cols = ndm; /* coordinates */
57: arrsz = nloc*pc_gamg->data_cell_rows*pc_gamg->data_cell_cols;
59: /* create data - syntactic sugar that should be refactored at some point */
60: if (!pc_gamg->data || (pc_gamg->data_sz != arrsz)) {
61: PetscFree(pc_gamg->data);
62: PetscMalloc1(arrsz+1, &pc_gamg->data);
63: }
64: for (kk=0; kk<arrsz; kk++) pc_gamg->data[kk] = -999.;
65: pc_gamg->data[arrsz] = -99.;
66: /* copy data in - column oriented */
67: for (kk = 0; kk < nloc; kk++) {
68: for (ii = 0; ii < ndm; ii++) {
69: pc_gamg->data[ii*nloc + kk] = coords[kk*ndm + ii];
70: }
71: }
72: if (pc_gamg->data[arrsz] != -99.) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"pc_gamg->data[arrsz %D] %g != -99.",arrsz,pc_gamg->data[arrsz]);
73: pc_gamg->data_sz = arrsz;
74: return(0);
75: }
77: /* -------------------------------------------------------------------------- */
78: /*
79: PCSetData_GEO
81: Input Parameter:
82: . pc -
83: */
86: PetscErrorCode PCSetData_GEO(PC pc, Mat m)
87: {
89: SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_PLIB,"GEO MG needs coordinates");
90: }
92: /* -------------------------------------------------------------------------- */
93: /*
94: PCSetFromOptions_GEO
96: Input Parameter:
97: . pc -
98: */
101: PetscErrorCode PCSetFromOptions_GEO(PetscOptionItems *PetscOptionsObject,PC pc)
102: {
106: PetscOptionsHead(PetscOptionsObject,"GAMG-GEO options");
107: {
108: /* -pc_gamg_sa_nsmooths */
109: /* pc_gamg_sa->smooths = 0; */
110: /* PetscOptionsInt("-pc_gamg_agg_nsmooths", */
111: /* "smoothing steps for smoothed aggregation, usually 1 (0)", */
112: /* "PCGAMGSetNSmooths_AGG", */
113: /* pc_gamg_sa->smooths, */
114: /* &pc_gamg_sa->smooths, */
115: /* &flag); */
116: /* */
117: }
118: PetscOptionsTail();
119: return(0);
120: }
122: /* -------------------------------------------------------------------------- */
123: /*
124: triangulateAndFormProl
126: Input Parameter:
127: . selected_2 - list of selected local ID, includes selected ghosts
128: . data_stride -
129: . coords[2*data_stride] - column vector of local coordinates w/ ghosts
130: . nselected_1 - selected IDs that go with base (1) graph includes selected ghosts
131: . clid_lid_1[nselected_1] - lids of selected (c) nodes ???????????
132: . agg_lists_1 - list of aggregates selected_1 vertices of aggregate unselected vertices
133: . crsGID[selected.size()] - global index for prolongation operator
134: . bs - block size
135: Output Parameter:
136: . a_Prol - prolongation operator
137: . a_worst_best - measure of worst missed fine vertex, 0 is no misses
138: */
141: static PetscErrorCode triangulateAndFormProl(IS selected_2,PetscInt data_stride,PetscReal coords[],PetscInt nselected_1,const PetscInt clid_lid_1[],const PetscCoarsenData *agg_lists_1,
142: const PetscInt crsGID[],PetscInt bs,Mat a_Prol,PetscReal *a_worst_best)
143: {
144: #if defined(PETSC_HAVE_TRIANGLE)
145: PetscErrorCode ierr;
146: PetscInt jj,tid,tt,idx,nselected_2;
147: struct triangulateio in,mid;
148: const PetscInt *selected_idx_2;
149: PetscMPIInt rank;
150: PetscInt Istart,Iend,nFineLoc,myFine0;
151: int kk,nPlotPts,sid;
152: MPI_Comm comm;
153: PetscReal tm;
156: PetscObjectGetComm((PetscObject)a_Prol,&comm);
157: MPI_Comm_rank(comm,&rank);
158: ISGetSize(selected_2, &nselected_2);
159: if (nselected_2 == 1 || nselected_2 == 2) { /* 0 happens on idle processors */
160: *a_worst_best = 100.0; /* this will cause a stop, but not globalized (should not happen) */
161: } else *a_worst_best = 0.0;
162: MPIU_Allreduce(a_worst_best, &tm, 1, MPIU_REAL, MPIU_MAX, comm);
163: if (tm > 0.0) {
164: *a_worst_best = 100.0;
165: return(0);
166: }
167: MatGetOwnershipRange(a_Prol, &Istart, &Iend);
168: nFineLoc = (Iend-Istart)/bs; myFine0 = Istart/bs;
169: nPlotPts = nFineLoc; /* locals */
170: /* traingle */
171: /* Define input points - in*/
172: in.numberofpoints = nselected_2;
173: in.numberofpointattributes = 0;
174: /* get nselected points */
175: PetscMalloc1(2*nselected_2, &in.pointlist);
176: ISGetIndices(selected_2, &selected_idx_2);
178: for (kk=0,sid=0; kk<nselected_2; kk++,sid += 2) {
179: PetscInt lid = selected_idx_2[kk];
180: in.pointlist[sid] = coords[lid];
181: in.pointlist[sid+1] = coords[data_stride + lid];
182: if (lid>=nFineLoc) nPlotPts++;
183: }
184: if (sid != 2*nselected_2) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"sid %D != 2*nselected_2 %D",sid,nselected_2);
186: in.numberofsegments = 0;
187: in.numberofedges = 0;
188: in.numberofholes = 0;
189: in.numberofregions = 0;
190: in.trianglelist = 0;
191: in.segmentmarkerlist = 0;
192: in.pointattributelist = 0;
193: in.pointmarkerlist = 0;
194: in.triangleattributelist = 0;
195: in.trianglearealist = 0;
196: in.segmentlist = 0;
197: in.holelist = 0;
198: in.regionlist = 0;
199: in.edgelist = 0;
200: in.edgemarkerlist = 0;
201: in.normlist = 0;
203: /* triangulate */
204: mid.pointlist = 0; /* Not needed if -N switch used. */
205: /* Not needed if -N switch used or number of point attributes is zero: */
206: mid.pointattributelist = 0;
207: mid.pointmarkerlist = 0; /* Not needed if -N or -B switch used. */
208: mid.trianglelist = 0; /* Not needed if -E switch used. */
209: /* Not needed if -E switch used or number of triangle attributes is zero: */
210: mid.triangleattributelist = 0;
211: mid.neighborlist = 0; /* Needed only if -n switch used. */
212: /* Needed only if segments are output (-p or -c) and -P not used: */
213: mid.segmentlist = 0;
214: /* Needed only if segments are output (-p or -c) and -P and -B not used: */
215: mid.segmentmarkerlist = 0;
216: mid.edgelist = 0; /* Needed only if -e switch used. */
217: mid.edgemarkerlist = 0; /* Needed if -e used and -B not used. */
218: mid.numberoftriangles = 0;
220: /* Triangulate the points. Switches are chosen to read and write a */
221: /* PSLG (p), preserve the convex hull (c), number everything from */
222: /* zero (z), assign a regional attribute to each element (A), and */
223: /* produce an edge list (e), a Voronoi diagram (v), and a triangle */
224: /* neighbor list (n). */
225: if (nselected_2 != 0) { /* inactive processor */
226: char args[] = "npczQ"; /* c is needed ? */
227: triangulate(args, &in, &mid, (struct triangulateio*) NULL);
228: /* output .poly files for 'showme' */
229: if (!PETSC_TRUE) {
230: static int level = 1;
231: FILE *file; char fname[32];
233: sprintf(fname,"C%d_%d.poly",level,rank); file = fopen(fname, "w");
234: /*First line: <# of vertices> <dimension (must be 2)> <# of attributes> <# of boundary markers (0 or 1)>*/
235: fprintf(file, "%d %d %d %d\n",in.numberofpoints,2,0,0);
236: /*Following lines: <vertex #> <x> <y> */
237: for (kk=0,sid=0; kk<in.numberofpoints; kk++,sid += 2) {
238: fprintf(file, "%d %e %e\n",kk,in.pointlist[sid],in.pointlist[sid+1]);
239: }
240: /*One line: <# of segments> <# of boundary markers (0 or 1)> */
241: fprintf(file, "%d %d\n",0,0);
242: /*Following lines: <segment #> <endpoint> <endpoint> [boundary marker] */
243: /* One line: <# of holes> */
244: fprintf(file, "%d\n",0);
245: /* Following lines: <hole #> <x> <y> */
246: /* Optional line: <# of regional attributes and/or area constraints> */
247: /* Optional following lines: <region #> <x> <y> <attribute> <maximum area> */
248: fclose(file);
250: /* elems */
251: sprintf(fname,"C%d_%d.ele",level,rank); file = fopen(fname, "w");
252: /* First line: <# of triangles> <nodes per triangle> <# of attributes> */
253: fprintf(file, "%d %d %d\n",mid.numberoftriangles,3,0);
254: /* Remaining lines: <triangle #> <node> <node> <node> ... [attributes] */
255: for (kk=0,sid=0; kk<mid.numberoftriangles; kk++,sid += 3) {
256: fprintf(file, "%d %d %d %d\n",kk,mid.trianglelist[sid],mid.trianglelist[sid+1],mid.trianglelist[sid+2]);
257: }
258: fclose(file);
260: sprintf(fname,"C%d_%d.node",level,rank); file = fopen(fname, "w");
261: /* First line: <# of vertices> <dimension (must be 2)> <# of attributes> <# of boundary markers (0 or 1)> */
262: /* fprintf(file, "%d %d %d %d\n",in.numberofpoints,2,0,0); */
263: fprintf(file, "%d %d %d %d\n",nPlotPts,2,0,0);
264: /*Following lines: <vertex #> <x> <y> */
265: for (kk=0,sid=0; kk<in.numberofpoints; kk++,sid+=2) {
266: fprintf(file, "%d %e %e\n",kk,in.pointlist[sid],in.pointlist[sid+1]);
267: }
269: sid /= 2;
270: for (jj=0; jj<nFineLoc; jj++) {
271: PetscBool sel = PETSC_TRUE;
272: for (kk=0; kk<nselected_2 && sel; kk++) {
273: PetscInt lid = selected_idx_2[kk];
274: if (lid == jj) sel = PETSC_FALSE;
275: }
276: if (sel) fprintf(file, "%d %e %e\n",sid++,coords[jj],coords[data_stride + jj]);
277: }
278: fclose(file);
279: if (sid != nPlotPts) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"sid %D != nPlotPts %D",sid,nPlotPts);
280: level++;
281: }
282: }
283: #if defined PETSC_GAMG_USE_LOG
284: PetscLogEventBegin(petsc_gamg_setup_events[FIND_V],0,0,0,0);
285: #endif
286: { /* form P - setup some maps */
287: PetscInt clid,mm,*nTri,*node_tri;
289: PetscMalloc2(nselected_2, &node_tri,nselected_2, &nTri);
291: /* need list of triangles on node */
292: for (kk=0; kk<nselected_2; kk++) nTri[kk] = 0;
293: for (tid=0,kk=0; tid<mid.numberoftriangles; tid++) {
294: for (jj=0; jj<3; jj++) {
295: PetscInt cid = mid.trianglelist[kk++];
296: if (nTri[cid] == 0) node_tri[cid] = tid;
297: nTri[cid]++;
298: }
299: }
300: #define EPS 1.e-12
301: /* find points and set prolongation */
302: for (mm = clid = 0; mm < nFineLoc; mm++) {
303: PetscBool ise;
304: PetscCDEmptyAt(agg_lists_1,mm,&ise);
305: if (!ise) {
306: const PetscInt lid = mm;
307: /* for (clid_iterator=0;clid_iterator<nselected_1;clid_iterator++) { */
308: PetscScalar AA[3][3];
309: PetscBLASInt N=3,NRHS=1,LDA=3,IPIV[3],LDB=3,INFO;
310: PetscCDPos pos;
311: PetscCDGetHeadPos(agg_lists_1,lid,&pos);
312: while (pos) {
313: PetscInt flid;
314: PetscLLNGetID(pos, &flid);
315: PetscCDGetNextPos(agg_lists_1,lid,&pos);
317: if (flid < nFineLoc) { /* could be a ghost */
318: PetscInt bestTID = -1; PetscReal best_alpha = 1.e10;
319: const PetscInt fgid = flid + myFine0;
320: /* compute shape function for gid */
321: const PetscReal fcoord[3] = {coords[flid],coords[data_stride+flid],1.0};
322: PetscBool haveit =PETSC_FALSE; PetscScalar alpha[3]; PetscInt clids[3];
324: /* look for it */
325: for (tid = node_tri[clid], jj=0;
326: jj < 5 && !haveit && tid != -1;
327: jj++) {
328: for (tt=0; tt<3; tt++) {
329: PetscInt cid2 = mid.trianglelist[3*tid + tt];
330: PetscInt lid2 = selected_idx_2[cid2];
331: AA[tt][0] = coords[lid2]; AA[tt][1] = coords[data_stride + lid2]; AA[tt][2] = 1.0;
332: clids[tt] = cid2; /* store for interp */
333: }
335: for (tt=0; tt<3; tt++) alpha[tt] = (PetscScalar)fcoord[tt];
337: /* SUBROUTINE DGESV(N, NRHS, A, LDA, IPIV, B, LDB, INFO) */
338: PetscStackCallBLAS("LAPACKgesv",LAPACKgesv_(&N, &NRHS, (PetscScalar*)AA, &LDA, IPIV, alpha, &LDB, &INFO));
339: {
340: PetscBool have=PETSC_TRUE; PetscReal lowest=1.e10;
341: for (tt = 0, idx = 0; tt < 3; tt++) {
342: if (PetscRealPart(alpha[tt]) > (1.0+EPS) || PetscRealPart(alpha[tt]) < -EPS) have = PETSC_FALSE;
343: if (PetscRealPart(alpha[tt]) < lowest) {
344: lowest = PetscRealPart(alpha[tt]);
345: idx = tt;
346: }
347: }
348: haveit = have;
349: }
350: tid = mid.neighborlist[3*tid + idx];
351: }
353: if (!haveit) {
354: /* brute force */
355: for (tid=0; tid<mid.numberoftriangles && !haveit; tid++) {
356: for (tt=0; tt<3; tt++) {
357: PetscInt cid2 = mid.trianglelist[3*tid + tt];
358: PetscInt lid2 = selected_idx_2[cid2];
359: AA[tt][0] = coords[lid2]; AA[tt][1] = coords[data_stride + lid2]; AA[tt][2] = 1.0;
360: clids[tt] = cid2; /* store for interp */
361: }
362: for (tt=0; tt<3; tt++) alpha[tt] = fcoord[tt];
363: /* SUBROUTINE DGESV(N, NRHS, A, LDA, IPIV, B, LDB, INFO) */
364: PetscStackCallBLAS("LAPACKgesv",LAPACKgesv_(&N, &NRHS, (PetscScalar*)AA, &LDA, IPIV, alpha, &LDB, &INFO));
365: {
366: PetscBool have=PETSC_TRUE; PetscReal worst=0.0, v;
367: for (tt=0; tt<3 && have; tt++) {
368: if (PetscRealPart(alpha[tt]) > 1.0+EPS || PetscRealPart(alpha[tt]) < -EPS) have=PETSC_FALSE;
369: if ((v=PetscAbs(PetscRealPart(alpha[tt])-0.5)) > worst) worst = v;
370: }
371: if (worst < best_alpha) {
372: best_alpha = worst; bestTID = tid;
373: }
374: haveit = have;
375: }
376: }
377: }
378: if (!haveit) {
379: if (best_alpha > *a_worst_best) *a_worst_best = best_alpha;
380: /* use best one */
381: for (tt=0; tt<3; tt++) {
382: PetscInt cid2 = mid.trianglelist[3*bestTID + tt];
383: PetscInt lid2 = selected_idx_2[cid2];
384: AA[tt][0] = coords[lid2]; AA[tt][1] = coords[data_stride + lid2]; AA[tt][2] = 1.0;
385: clids[tt] = cid2; /* store for interp */
386: }
387: for (tt=0; tt<3; tt++) alpha[tt] = fcoord[tt];
388: /* SUBROUTINE DGESV(N, NRHS, A, LDA, IPIV, B, LDB, INFO) */
389: PetscStackCallBLAS("LAPACKgesv",LAPACKgesv_(&N, &NRHS, (PetscScalar*)AA, &LDA, IPIV, alpha, &LDB, &INFO));
390: }
392: /* put in row of P */
393: for (idx=0; idx<3; idx++) {
394: PetscScalar shp = alpha[idx];
395: if (PetscAbs(PetscRealPart(shp)) > 1.e-6) {
396: PetscInt cgid = crsGID[clids[idx]];
397: PetscInt jj = cgid*bs, ii = fgid*bs; /* need to gloalize */
398: for (tt=0; tt < bs; tt++, ii++, jj++) {
399: MatSetValues(a_Prol,1,&ii,1,&jj,&shp,INSERT_VALUES);
400: }
401: }
402: }
403: }
404: } /* aggregates iterations */
405: clid++;
406: } /* a coarse agg */
407: } /* for all fine nodes */
409: ISRestoreIndices(selected_2, &selected_idx_2);
410: MatAssemblyBegin(a_Prol,MAT_FINAL_ASSEMBLY);
411: MatAssemblyEnd(a_Prol,MAT_FINAL_ASSEMBLY);
413: PetscFree2(node_tri,nTri);
414: }
415: #if defined PETSC_GAMG_USE_LOG
416: PetscLogEventEnd(petsc_gamg_setup_events[FIND_V],0,0,0,0);
417: #endif
418: free(mid.trianglelist);
419: free(mid.neighborlist);
420: PetscFree(in.pointlist);
421: return(0);
422: #else
423: SETERRQ(PetscObjectComm((PetscObject)a_Prol),PETSC_ERR_PLIB,"configure with TRIANGLE to use geometric MG");
424: #endif
425: }
426: /* -------------------------------------------------------------------------- */
427: /*
428: getGIDsOnSquareGraph - square graph, get
430: Input Parameter:
431: . nselected_1 - selected local indices (includes ghosts in input Gmat1)
432: . clid_lid_1 - [nselected_1] lids of selected nodes
433: . Gmat1 - graph that goes with 'selected_1'
434: Output Parameter:
435: . a_selected_2 - selected local indices (includes ghosts in output a_Gmat_2)
436: . a_Gmat_2 - graph that is squared of 'Gmat_1'
437: . a_crsGID[a_selected_2.size()] - map of global IDs of coarse grid nodes
438: */
441: static PetscErrorCode getGIDsOnSquareGraph(PetscInt nselected_1,const PetscInt clid_lid_1[],const Mat Gmat1,IS *a_selected_2,Mat *a_Gmat_2,PetscInt **a_crsGID)
442: {
444: PetscMPIInt size;
445: PetscInt *crsGID, kk,my0,Iend,nloc;
446: MPI_Comm comm;
449: PetscObjectGetComm((PetscObject)Gmat1,&comm);
450: MPI_Comm_size(comm,&size);
451: MatGetOwnershipRange(Gmat1,&my0,&Iend); /* AIJ */
452: nloc = Iend - my0; /* this does not change */
454: if (size == 1) { /* not much to do in serial */
455: PetscMalloc1(nselected_1, &crsGID);
456: for (kk=0; kk<nselected_1; kk++) crsGID[kk] = kk;
457: *a_Gmat_2 = 0;
458: ISCreateGeneral(PETSC_COMM_SELF,nselected_1,clid_lid_1,PETSC_COPY_VALUES,a_selected_2);
459: } else {
460: PetscInt idx,num_fine_ghosts,num_crs_ghost,myCrs0;
461: Mat_MPIAIJ *mpimat2;
462: Mat Gmat2;
463: Vec locState;
464: PetscScalar *cpcol_state;
466: /* scan my coarse zero gid, set 'lid_state' with coarse GID */
467: kk = nselected_1;
468: MPI_Scan(&kk, &myCrs0, 1, MPIU_INT, MPI_SUM, comm);
469: myCrs0 -= nselected_1;
471: if (a_Gmat_2) { /* output */
472: /* grow graph to get wider set of selected vertices to cover fine grid, invalidates 'llist' */
473: MatTransposeMatMult(Gmat1, Gmat1, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &Gmat2);
474: *a_Gmat_2 = Gmat2; /* output */
475: } else Gmat2 = Gmat1; /* use local to get crsGIDs at least */
476: /* get coarse grid GIDS for selected (locals and ghosts) */
477: mpimat2 = (Mat_MPIAIJ*)Gmat2->data;
478: MatCreateVecs(Gmat2, &locState, 0);
479: VecSet(locState, (PetscScalar)(PetscReal)(-1)); /* set with UNKNOWN state */
480: for (kk=0; kk<nselected_1; kk++) {
481: PetscInt fgid = clid_lid_1[kk] + my0;
482: PetscScalar v = (PetscScalar)(kk+myCrs0);
483: VecSetValues(locState, 1, &fgid, &v, INSERT_VALUES); /* set with PID */
484: }
485: VecAssemblyBegin(locState);
486: VecAssemblyEnd(locState);
487: VecScatterBegin(mpimat2->Mvctx,locState,mpimat2->lvec,INSERT_VALUES,SCATTER_FORWARD);
488: VecScatterEnd(mpimat2->Mvctx,locState,mpimat2->lvec,INSERT_VALUES,SCATTER_FORWARD);
489: VecGetLocalSize(mpimat2->lvec, &num_fine_ghosts);
490: VecGetArray(mpimat2->lvec, &cpcol_state);
491: for (kk=0,num_crs_ghost=0; kk<num_fine_ghosts; kk++) {
492: if ((PetscInt)PetscRealPart(cpcol_state[kk]) != -1) num_crs_ghost++;
493: }
494: PetscMalloc1(nselected_1+num_crs_ghost, &crsGID); /* output */
495: {
496: PetscInt *selected_set;
497: PetscMalloc1(nselected_1+num_crs_ghost, &selected_set);
498: /* do ghost of 'crsGID' */
499: for (kk=0,idx=nselected_1; kk<num_fine_ghosts; kk++) {
500: if ((PetscInt)PetscRealPart(cpcol_state[kk]) != -1) {
501: PetscInt cgid = (PetscInt)PetscRealPart(cpcol_state[kk]);
502: selected_set[idx] = nloc + kk;
503: crsGID[idx++] = cgid;
504: }
505: }
506: if (idx != (nselected_1+num_crs_ghost)) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"idx %D != (nselected_1 %D + num_crs_ghost %D)",idx,nselected_1,num_crs_ghost);
507: VecRestoreArray(mpimat2->lvec, &cpcol_state);
508: /* do locals in 'crsGID' */
509: VecGetArray(locState, &cpcol_state);
510: for (kk=0,idx=0; kk<nloc; kk++) {
511: if ((PetscInt)PetscRealPart(cpcol_state[kk]) != -1) {
512: PetscInt cgid = (PetscInt)PetscRealPart(cpcol_state[kk]);
513: selected_set[idx] = kk;
514: crsGID[idx++] = cgid;
515: }
516: }
517: if (idx != nselected_1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"idx %D != nselected_1 %D",idx,nselected_1);
518: VecRestoreArray(locState, &cpcol_state);
520: if (a_selected_2 != 0) { /* output */
521: ISCreateGeneral(PETSC_COMM_SELF,(nselected_1+num_crs_ghost),selected_set,PETSC_OWN_POINTER,a_selected_2);
522: } else {
523: PetscFree(selected_set);
524: }
525: }
526: VecDestroy(&locState);
527: }
528: *a_crsGID = crsGID; /* output */
529: return(0);
530: }
532: /* -------------------------------------------------------------------------- */
533: /*
534: PCGAMGGraph_GEO
536: Input Parameter:
537: . pc - this
538: . Amat - matrix on this fine level
539: Output Parameter:
540: . a_Gmat
541: */
544: PetscErrorCode PCGAMGGraph_GEO(PC pc,Mat Amat,Mat *a_Gmat)
545: {
546: PetscErrorCode ierr;
547: PC_MG *mg = (PC_MG*)pc->data;
548: PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx;
549: const PetscReal vfilter = pc_gamg->threshold;
550: MPI_Comm comm;
551: Mat Gmat;
552: PetscBool set,flg,symm;
555: PetscObjectGetComm((PetscObject)Amat,&comm);
556: PetscLogEventBegin(PC_GAMGGraph_GEO,0,0,0,0);
558: MatIsSymmetricKnown(Amat, &set, &flg);
559: symm = (PetscBool)!(set && flg);
561: PCGAMGCreateGraph(Amat, &Gmat);
562: PCGAMGFilterGraph(&Gmat, vfilter, symm);
564: *a_Gmat = Gmat;
565: PetscLogEventEnd(PC_GAMGGraph_GEO,0,0,0,0);
566: return(0);
567: }
569: /* -------------------------------------------------------------------------- */
570: /*
571: PCGAMGCoarsen_GEO
573: Input Parameter:
574: . a_pc - this
575: . a_Gmat - graph
576: Output Parameter:
577: . a_llist_parent - linked list from selected indices for data locality only
578: */
581: PetscErrorCode PCGAMGCoarsen_GEO(PC a_pc,Mat *a_Gmat,PetscCoarsenData **a_llist_parent)
582: {
584: PetscInt Istart,Iend,nloc,kk,Ii,ncols;
585: IS perm;
586: GAMGNode *gnodes;
587: PetscInt *permute;
588: Mat Gmat = *a_Gmat;
589: MPI_Comm comm;
590: MatCoarsen crs;
593: PetscObjectGetComm((PetscObject)a_pc,&comm);
594: PetscLogEventBegin(PC_GAMGCoarsen_GEO,0,0,0,0);
595: MatGetOwnershipRange(Gmat, &Istart, &Iend);
596: nloc = (Iend-Istart);
598: /* create random permutation with sort for geo-mg */
599: PetscMalloc1(nloc, &gnodes);
600: PetscMalloc1(nloc, &permute);
602: for (Ii=Istart; Ii<Iend; Ii++) { /* locals only? */
603: MatGetRow(Gmat,Ii,&ncols,0,0);
604: {
605: PetscInt lid = Ii - Istart;
606: gnodes[lid].lid = lid;
607: gnodes[lid].degree = ncols;
608: }
609: MatRestoreRow(Gmat,Ii,&ncols,0,0);
610: }
611: if (PETSC_TRUE) {
612: PetscRandom rand;
613: PetscBool *bIndexSet;
614: PetscReal rr;
615: PetscInt iSwapIndex;
617: PetscRandomCreate(comm,&rand);
618: PetscCalloc1(nloc, &bIndexSet);
619: for (Ii = 0; Ii < nloc; Ii++) {
620: PetscRandomGetValueReal(rand,&rr);
621: iSwapIndex = (PetscInt) (rr*nloc);
622: if (!bIndexSet[iSwapIndex] && iSwapIndex != Ii) {
623: GAMGNode iTemp = gnodes[iSwapIndex];
624: gnodes[iSwapIndex] = gnodes[Ii];
625: gnodes[Ii] = iTemp;
626: bIndexSet[Ii] = PETSC_TRUE;
627: bIndexSet[iSwapIndex] = PETSC_TRUE;
628: }
629: }
630: PetscRandomDestroy(&rand);
631: PetscFree(bIndexSet);
632: }
633: /* only sort locals */
634: qsort(gnodes, nloc, sizeof(GAMGNode), petsc_geo_mg_compare);
635: /* create IS of permutation */
636: for (kk=0; kk<nloc; kk++) permute[kk] = gnodes[kk].lid; /* locals only */
637: ISCreateGeneral(PETSC_COMM_SELF, nloc, permute, PETSC_OWN_POINTER, &perm);
639: PetscFree(gnodes);
641: /* get MIS aggs */
643: MatCoarsenCreate(comm, &crs);
644: MatCoarsenSetType(crs, MATCOARSENMIS);
645: MatCoarsenSetGreedyOrdering(crs, perm);
646: MatCoarsenSetAdjacency(crs, Gmat);
647: MatCoarsenSetStrictAggs(crs, PETSC_FALSE);
648: MatCoarsenApply(crs);
649: MatCoarsenGetData(crs, a_llist_parent);
650: MatCoarsenDestroy(&crs);
652: ISDestroy(&perm);
653: PetscLogEventEnd(PC_GAMGCoarsen_GEO,0,0,0,0);
654: return(0);
655: }
657: /* -------------------------------------------------------------------------- */
658: /*
659: PCGAMGProlongator_GEO
661: Input Parameter:
662: . pc - this
663: . Amat - matrix on this fine level
664: . Graph - used to get ghost data for nodes in
665: . selected_1 - [nselected]
666: . agg_lists - [nselected]
667: Output Parameter:
668: . a_P_out - prolongation operator to the next level
669: */
672: PetscErrorCode PCGAMGProlongator_GEO(PC pc,Mat Amat,Mat Gmat,PetscCoarsenData *agg_lists,Mat *a_P_out)
673: {
674: PC_MG *mg = (PC_MG*)pc->data;
675: PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx;
676: const PetscInt dim = pc_gamg->data_cell_cols, data_cols = pc_gamg->data_cell_cols;
678: PetscInt Istart,Iend,nloc,my0,jj,kk,ncols,nLocalSelected,bs,*clid_flid;
679: Mat Prol;
680: PetscMPIInt rank, size;
681: MPI_Comm comm;
682: IS selected_2,selected_1;
683: const PetscInt *selected_idx;
684: MatType mtype;
687: PetscObjectGetComm((PetscObject)Amat,&comm);
688: PetscLogEventBegin(PC_GAMGProlongator_GEO,0,0,0,0);
689: MPI_Comm_rank(comm,&rank);
690: MPI_Comm_size(comm,&size);
691: MatGetOwnershipRange(Amat, &Istart, &Iend);
692: MatGetBlockSize(Amat, &bs);
693: nloc = (Iend-Istart)/bs; my0 = Istart/bs;
694: if ((Iend-Istart) % bs) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"(Iend %D - Istart %D) % bs %D",Iend,Istart,bs);
696: /* get 'nLocalSelected' */
697: PetscCDGetMIS(agg_lists, &selected_1);
698: ISGetSize(selected_1, &jj);
699: PetscMalloc1(jj, &clid_flid);
700: ISGetIndices(selected_1, &selected_idx);
701: for (kk=0,nLocalSelected=0; kk<jj; kk++) {
702: PetscInt lid = selected_idx[kk];
703: if (lid<nloc) {
704: MatGetRow(Gmat,lid+my0,&ncols,0,0);
705: if (ncols>1) clid_flid[nLocalSelected++] = lid; /* fiter out singletons */
706: MatRestoreRow(Gmat,lid+my0,&ncols,0,0);
707: }
708: }
709: ISRestoreIndices(selected_1, &selected_idx);
710: ISDestroy(&selected_1); /* this is selected_1 in serial */
712: /* create prolongator matrix */
713: MatGetType(Amat,&mtype);
714: MatCreate(comm, &Prol);
715: MatSetSizes(Prol,nloc*bs,nLocalSelected*bs,PETSC_DETERMINE,PETSC_DETERMINE);
716: MatSetBlockSizes(Prol, bs, bs);
717: MatSetType(Prol, mtype);
718: MatSeqAIJSetPreallocation(Prol,3*data_cols,NULL);
719: MatMPIAIJSetPreallocation(Prol,3*data_cols,NULL,3*data_cols,NULL);
721: /* can get all points "removed" - but not on geomg */
722: MatGetSize(Prol, &kk, &jj);
723: if (!jj) {
724: PetscInfo(pc,"ERROE: no selected points on coarse grid\n");
725: PetscFree(clid_flid);
726: MatDestroy(&Prol);
727: *a_P_out = NULL; /* out */
728: return(0);
729: }
731: {
732: PetscReal *coords;
733: PetscInt data_stride;
734: PetscInt *crsGID = NULL;
735: Mat Gmat2;
737: if (dim != data_cols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"dim %D != data_cols %D",dim,data_cols);
738: /* grow ghost data for better coarse grid cover of fine grid */
739: #if defined PETSC_GAMG_USE_LOG
740: PetscLogEventBegin(petsc_gamg_setup_events[SET5],0,0,0,0);
741: #endif
742: /* messy method, squares graph and gets some data */
743: getGIDsOnSquareGraph(nLocalSelected, clid_flid, Gmat, &selected_2, &Gmat2, &crsGID);
744: /* llist is now not valid wrt squared graph, but will work as iterator in 'triangulateAndFormProl' */
745: #if defined PETSC_GAMG_USE_LOG
746: PetscLogEventEnd(petsc_gamg_setup_events[SET5],0,0,0,0);
747: #endif
748: /* create global vector of coorindates in 'coords' */
749: if (size > 1) {
750: PCGAMGGetDataWithGhosts(Gmat2, dim, pc_gamg->data, &data_stride, &coords);
751: } else {
752: coords = (PetscReal*)pc_gamg->data;
753: data_stride = pc_gamg->data_sz/pc_gamg->data_cell_cols;
754: }
755: MatDestroy(&Gmat2);
757: /* triangulate */
758: if (dim == 2) {
759: PetscReal metric,tm;
760: #if defined PETSC_GAMG_USE_LOG
761: PetscLogEventBegin(petsc_gamg_setup_events[SET6],0,0,0,0);
762: #endif
763: triangulateAndFormProl(selected_2, data_stride, coords,nLocalSelected, clid_flid, agg_lists, crsGID, bs, Prol, &metric);
764: #if defined PETSC_GAMG_USE_LOG
765: PetscLogEventEnd(petsc_gamg_setup_events[SET6],0,0,0,0);
766: #endif
767: PetscFree(crsGID);
769: /* clean up and create coordinates for coarse grid (output) */
770: if (size > 1) PetscFree(coords);
772: MPIU_Allreduce(&metric, &tm, 1, MPIU_REAL, MPIU_MAX, comm);
773: if (tm > 1.) { /* needs to be globalized - should not happen */
774: PetscInfo1(pc," failed metric for coarse grid %e\n",(double)tm);
775: MatDestroy(&Prol);
776: } else if (metric > .0) {
777: PetscInfo1(pc,"worst metric for coarse grid = %e\n",(double)metric);
778: }
779: } else SETERRQ(comm,PETSC_ERR_PLIB,"3D not implemented for 'geo' AMG");
780: { /* create next coords - output */
781: PetscReal *crs_crds;
782: PetscMalloc1(dim*nLocalSelected, &crs_crds);
783: for (kk=0; kk<nLocalSelected; kk++) { /* grab local select nodes to promote - output */
784: PetscInt lid = clid_flid[kk];
785: for (jj=0; jj<dim; jj++) crs_crds[jj*nLocalSelected + kk] = pc_gamg->data[jj*nloc + lid];
786: }
788: PetscFree(pc_gamg->data);
789: pc_gamg->data = crs_crds; /* out */
790: pc_gamg->data_sz = dim*nLocalSelected;
791: }
792: ISDestroy(&selected_2);
793: }
795: *a_P_out = Prol; /* out */
796: PetscFree(clid_flid);
797: PetscLogEventEnd(PC_GAMGProlongator_GEO,0,0,0,0);
798: return(0);
799: }
803: static PetscErrorCode PCDestroy_GAMG_GEO(PC pc)
804: {
808: PetscObjectComposeFunction((PetscObject)pc,"PCSetCoordinates_C",NULL);
809: return(0);
810: }
812: /* -------------------------------------------------------------------------- */
813: /*
814: PCCreateGAMG_GEO
816: Input Parameter:
817: . pc -
818: */
821: PetscErrorCode PCCreateGAMG_GEO(PC pc)
822: {
824: PC_MG *mg = (PC_MG*)pc->data;
825: PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx;
828: pc_gamg->ops->setfromoptions = PCSetFromOptions_GEO;
829: pc_gamg->ops->destroy = PCDestroy_GAMG_GEO;
830: /* reset does not do anything; setup not virtual */
832: /* set internal function pointers */
833: pc_gamg->ops->graph = PCGAMGGraph_GEO;
834: pc_gamg->ops->coarsen = PCGAMGCoarsen_GEO;
835: pc_gamg->ops->prolongator = PCGAMGProlongator_GEO;
836: pc_gamg->ops->optprolongator = NULL;
837: pc_gamg->ops->createdefaultdata = PCSetData_GEO;
839: PetscObjectComposeFunction((PetscObject)pc,"PCSetCoordinates_C",PCSetCoordinates_GEO);
840: return(0);
841: }