Actual source code: classical.c

  1: #include <../src/ksp/pc/impls/gamg/gamg.h>
  2: #include <petscsf.h>

  4: static PetscFunctionList PCGAMGClassicalProlongatorList    = NULL;
  5: static PetscBool         PCGAMGClassicalPackageInitialized = PETSC_FALSE;

  7: typedef struct {
  8:   PetscReal interp_threshold; /* interpolation threshold */
  9:   char      prolongtype[256];
 10:   PetscInt  nsmooths; /* number of jacobi smoothings on the prolongator */
 11: } PC_GAMG_Classical;

 13: /*@
 14:   PCGAMGClassicalSetType - Sets the type of classical interpolation to use with `PCGAMG`

 16:   Collective

 18:   Input Parameters:
 19: + pc   - the preconditioner context
 20: - type - the interpolation to use, see `PCGAMGClassicalType()`

 22:   Options Database Key:
 23: . -pc_gamg_classical_type (direct|standard) - set type of classical AMG prolongation

 25:   Level: intermediate

 27: .seealso: [](ch_ksp), `PCGAMG`, `PCGAMGClassicalType`, `PCGAMGClassicalGetType()`
 28: @*/
 29: PetscErrorCode PCGAMGClassicalSetType(PC pc, PCGAMGClassicalType type)
 30: {
 31:   PetscFunctionBegin;
 33:   PetscTryMethod(pc, "PCGAMGClassicalSetType_C", (PC, PCGAMGClassicalType), (pc, type));
 34:   PetscFunctionReturn(PETSC_SUCCESS);
 35: }

 37: /*@
 38:   PCGAMGClassicalGetType - Gets the type of classical interpolation to use with `PCGAMG`

 40:   Collective

 42:   Input Parameter:
 43: . pc - the preconditioner context

 45:   Output Parameter:
 46: . type - the type used, see `PCGAMGClassicalType()`

 48:   Level: intermediate

 50: .seealso: [](ch_ksp), `PCGAMG`, `PCGAMGClassicalType`, `PCGAMGClassicalSetType()`
 51: @*/
 52: PetscErrorCode PCGAMGClassicalGetType(PC pc, PCGAMGClassicalType *type)
 53: {
 54:   PetscFunctionBegin;
 56:   PetscUseMethod(pc, "PCGAMGClassicalGetType_C", (PC, PCGAMGClassicalType *), (pc, type));
 57:   PetscFunctionReturn(PETSC_SUCCESS);
 58: }

 60: static PetscErrorCode PCGAMGClassicalSetType_GAMG(PC pc, PCGAMGClassicalType type)
 61: {
 62:   PC_MG             *mg      = (PC_MG *)pc->data;
 63:   PC_GAMG           *pc_gamg = (PC_GAMG *)mg->innerctx;
 64:   PC_GAMG_Classical *cls     = (PC_GAMG_Classical *)pc_gamg->subctx;

 66:   PetscFunctionBegin;
 67:   PetscCall(PetscStrncpy(cls->prolongtype, type, sizeof(cls->prolongtype)));
 68:   PetscFunctionReturn(PETSC_SUCCESS);
 69: }

 71: static PetscErrorCode PCGAMGClassicalGetType_GAMG(PC pc, PCGAMGClassicalType *type)
 72: {
 73:   PC_MG             *mg      = (PC_MG *)pc->data;
 74:   PC_GAMG           *pc_gamg = (PC_GAMG *)mg->innerctx;
 75:   PC_GAMG_Classical *cls     = (PC_GAMG_Classical *)pc_gamg->subctx;

 77:   PetscFunctionBegin;
 78:   *type = cls->prolongtype;
 79:   PetscFunctionReturn(PETSC_SUCCESS);
 80: }

 82: static PetscErrorCode PCGAMGCreateGraph_Classical(PC pc, Mat A, Mat *G)
 83: {
 84:   PetscInt           s, f, n, idx, lidx, gidx;
 85:   PetscInt           r, c, ncols;
 86:   const PetscInt    *rcol;
 87:   const PetscScalar *rval;
 88:   PetscInt          *gcol;
 89:   PetscScalar       *gval;
 90:   PetscReal          rmax;
 91:   PetscInt           cmax = 0;
 92:   PC_MG             *mg   = (PC_MG *)pc->data;
 93:   PC_GAMG           *gamg = (PC_GAMG *)mg->innerctx;
 94:   PetscInt          *gsparse, *lsparse;
 95:   PetscScalar       *Amax;
 96:   MatType            mtype;

 98:   PetscFunctionBegin;
 99:   PetscCall(MatGetOwnershipRange(A, &s, &f));
100:   n = f - s;
101:   PetscCall(PetscMalloc3(n, &lsparse, n, &gsparse, n, &Amax));

103:   for (r = 0; r < n; r++) {
104:     lsparse[r] = 0;
105:     gsparse[r] = 0;
106:   }

108:   for (r = s; r < f; r++) {
109:     /* determine the maximum off-diagonal in each row */
110:     rmax = 0.;
111:     PetscCall(MatGetRow(A, r, &ncols, &rcol, &rval));
112:     for (c = 0; c < ncols; c++) {
113:       if (PetscRealPart(-rval[c]) > rmax && rcol[c] != r) rmax = PetscRealPart(-rval[c]);
114:     }
115:     Amax[r - s] = rmax;
116:     if (ncols > cmax) cmax = ncols;
117:     lidx = 0;
118:     gidx = 0;
119:     /* create the local and global sparsity patterns */
120:     for (c = 0; c < ncols; c++) {
121:       if (PetscRealPart(-rval[c]) > gamg->threshold[0] * PetscRealPart(Amax[r - s]) || rcol[c] == r) {
122:         if (rcol[c] < f && rcol[c] >= s) {
123:           lidx++;
124:         } else {
125:           gidx++;
126:         }
127:       }
128:     }
129:     PetscCall(MatRestoreRow(A, r, &ncols, &rcol, &rval));
130:     lsparse[r - s] = lidx;
131:     gsparse[r - s] = gidx;
132:   }
133:   PetscCall(PetscMalloc2(cmax, &gval, cmax, &gcol));

135:   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), G));
136:   PetscCall(MatGetType(A, &mtype));
137:   PetscCall(MatSetType(*G, mtype));
138:   PetscCall(MatSetSizes(*G, n, n, PETSC_DETERMINE, PETSC_DETERMINE));
139:   PetscCall(MatMPIAIJSetPreallocation(*G, 0, lsparse, 0, gsparse));
140:   PetscCall(MatSeqAIJSetPreallocation(*G, 0, lsparse));
141:   for (r = s; r < f; r++) {
142:     PetscCall(MatGetRow(A, r, &ncols, &rcol, &rval));
143:     idx = 0;
144:     for (c = 0; c < ncols; c++) {
145:       /* classical strength of connection */
146:       if (PetscRealPart(-rval[c]) > gamg->threshold[0] * PetscRealPart(Amax[r - s]) || rcol[c] == r) {
147:         gcol[idx] = rcol[c];
148:         gval[idx] = rval[c];
149:         idx++;
150:       }
151:     }
152:     PetscCall(MatSetValues(*G, 1, &r, idx, gcol, gval, INSERT_VALUES));
153:     PetscCall(MatRestoreRow(A, r, &ncols, &rcol, &rval));
154:   }
155:   PetscCall(MatAssemblyBegin(*G, MAT_FINAL_ASSEMBLY));
156:   PetscCall(MatAssemblyEnd(*G, MAT_FINAL_ASSEMBLY));

158:   PetscCall(PetscFree2(gval, gcol));
159:   PetscCall(PetscFree3(lsparse, gsparse, Amax));
160:   PetscFunctionReturn(PETSC_SUCCESS);
161: }

163: static PetscErrorCode PCGAMGCoarsen_Classical(PC pc, Mat *G, PetscCoarsenData **agg_lists)
164: {
165:   MatCoarsen  crs;
166:   MPI_Comm    fcomm = ((PetscObject)pc)->comm;
167:   const char *prefix;

169:   PetscFunctionBegin;
170:   PetscCheck(G, fcomm, PETSC_ERR_ARG_WRONGSTATE, "Must set Graph in PC in PCGAMG before coarsening");

172:   PetscCall(MatCoarsenCreate(fcomm, &crs));
173:   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)pc, &prefix));
174:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)crs, prefix));
175:   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)crs, "pc_gamg_"));
176:   PetscCall(MatCoarsenSetFromOptions(crs));
177:   PetscCall(MatCoarsenSetAdjacency(crs, *G));
178:   PetscCall(MatCoarsenSetStrictAggs(crs, PETSC_TRUE));
179:   PetscCall(MatCoarsenApply(crs));
180:   PetscCall(MatCoarsenGetData(crs, agg_lists));
181:   PetscCall(MatCoarsenDestroy(&crs));
182:   PetscFunctionReturn(PETSC_SUCCESS);
183: }

185: static PetscErrorCode PCGAMGProlongator_Classical_Direct(PC pc, Mat A, PetscCoarsenData *agg_lists, Mat *P)
186: {
187:   PC_MG             *mg   = (PC_MG *)pc->data;
188:   PC_GAMG           *gamg = (PC_GAMG *)mg->innerctx;
189:   PetscBool          iscoarse, isMPIAIJ, isSEQAIJ;
190:   PetscInt           fn, cn, fs, fe, cs, ce, i, j, ncols, col, row_f, row_c, cmax = 0, idx, noff;
191:   PetscInt          *lcid, *gcid, *lsparse, *gsparse, *pcols;
192:   const PetscInt    *rcol;
193:   PetscReal         *Amax_pos, *Amax_neg;
194:   PetscScalar        g_pos, g_neg, a_pos, a_neg, diag, invdiag, alpha, beta, pij;
195:   PetscScalar       *pvals;
196:   const PetscScalar *rval;
197:   Mat                lA, gA = NULL;
198:   MatType            mtype;
199:   Vec                C, lvec;
200:   PetscSF            sf;
201:   Mat_MPIAIJ        *mpiaij;

203:   PetscFunctionBegin;
204:   PetscCall(MatGetOwnershipRange(A, &fs, &fe));
205:   fn = fe - fs;
206:   PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMPIAIJ, &isMPIAIJ));
207:   PetscCall(PetscObjectTypeCompare((PetscObject)A, MATSEQAIJ, &isSEQAIJ));
208:   PetscCheck(isMPIAIJ || isSEQAIJ, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Classical AMG requires MPIAIJ matrix");
209:   if (isMPIAIJ) {
210:     mpiaij = (Mat_MPIAIJ *)A->data;
211:     lA     = mpiaij->A;
212:     gA     = mpiaij->B;
213:     lvec   = mpiaij->lvec;
214:     PetscCall(VecGetSize(lvec, &noff));
215:     PetscCall(MatGetMultPetscSF(A, &sf));
216:     PetscCall(PetscMalloc1(noff, &gcid));
217:   } else {
218:     lA = A;
219:   }
220:   PetscCall(PetscMalloc5(fn, &lsparse, fn, &gsparse, fn, &lcid, fn, &Amax_pos, fn, &Amax_neg));

222:   /* count the number of coarse unknowns */
223:   cn = 0;
224:   for (i = 0; i < fn; i++) {
225:     /* filter out singletons */
226:     PetscCall(PetscCDIsEmptyAt(agg_lists, i, &iscoarse));
227:     lcid[i] = -1;
228:     if (!iscoarse) cn++;
229:   }

231:   /* create the coarse vector */
232:   PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)A), cn, PETSC_DECIDE, &C));
233:   PetscCall(VecGetOwnershipRange(C, &cs, &ce));

235:   cn = 0;
236:   for (i = 0; i < fn; i++) {
237:     PetscCall(PetscCDIsEmptyAt(agg_lists, i, &iscoarse));
238:     if (!iscoarse) {
239:       lcid[i] = cs + cn;
240:       cn++;
241:     } else {
242:       lcid[i] = -1;
243:     }
244:   }

246:   if (gA) {
247:     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, lcid, gcid, MPI_REPLACE));
248:     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, lcid, gcid, MPI_REPLACE));
249:   }

251:   /* determine the largest off-diagonal entries in each row */
252:   for (i = fs; i < fe; i++) {
253:     Amax_pos[i - fs] = 0.;
254:     Amax_neg[i - fs] = 0.;
255:     PetscCall(MatGetRow(A, i, &ncols, &rcol, &rval));
256:     for (j = 0; j < ncols; j++) {
257:       if ((PetscRealPart(-rval[j]) > Amax_neg[i - fs]) && i != rcol[j]) Amax_neg[i - fs] = PetscAbsScalar(rval[j]);
258:       if ((PetscRealPart(rval[j]) > Amax_pos[i - fs]) && i != rcol[j]) Amax_pos[i - fs] = PetscAbsScalar(rval[j]);
259:     }
260:     if (ncols > cmax) cmax = ncols;
261:     PetscCall(MatRestoreRow(A, i, &ncols, &rcol, &rval));
262:   }
263:   PetscCall(PetscMalloc2(cmax, &pcols, cmax, &pvals));
264:   PetscCall(VecDestroy(&C));

266:   /* count the on and off processor sparsity patterns for the prolongator */
267:   for (i = 0; i < fn; i++) {
268:     /* on */
269:     lsparse[i] = 0;
270:     gsparse[i] = 0;
271:     if (lcid[i] >= 0) {
272:       lsparse[i] = 1;
273:       gsparse[i] = 0;
274:     } else {
275:       PetscCall(MatGetRow(lA, i, &ncols, &rcol, &rval));
276:       for (j = 0; j < ncols; j++) {
277:         col = rcol[j];
278:         if (lcid[col] >= 0 && (PetscRealPart(rval[j]) > gamg->threshold[0] * Amax_pos[i] || PetscRealPart(-rval[j]) > gamg->threshold[0] * Amax_neg[i])) lsparse[i] += 1;
279:       }
280:       PetscCall(MatRestoreRow(lA, i, &ncols, &rcol, &rval));
281:       /* off */
282:       if (gA) {
283:         PetscCall(MatGetRow(gA, i, &ncols, &rcol, &rval));
284:         for (j = 0; j < ncols; j++) {
285:           col = rcol[j];
286:           if (gcid[col] >= 0 && (PetscRealPart(rval[j]) > gamg->threshold[0] * Amax_pos[i] || PetscRealPart(-rval[j]) > gamg->threshold[0] * Amax_neg[i])) gsparse[i] += 1;
287:         }
288:         PetscCall(MatRestoreRow(gA, i, &ncols, &rcol, &rval));
289:       }
290:     }
291:   }

293:   /* preallocate and create the prolongator */
294:   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), P));
295:   PetscCall(MatGetType(A, &mtype));
296:   PetscCall(MatSetType(*P, mtype));
297:   PetscCall(MatSetSizes(*P, fn, cn, PETSC_DETERMINE, PETSC_DETERMINE));
298:   PetscCall(MatMPIAIJSetPreallocation(*P, 0, lsparse, 0, gsparse));
299:   PetscCall(MatSeqAIJSetPreallocation(*P, 0, lsparse));

301:   /* loop over local fine nodes -- get the diagonal, the sum of positive and negative strong and weak weights, and set up the row */
302:   for (i = 0; i < fn; i++) {
303:     /* determine on or off */
304:     row_f = i + fs;
305:     row_c = lcid[i];
306:     if (row_c >= 0) {
307:       pij = 1.;
308:       PetscCall(MatSetValues(*P, 1, &row_f, 1, &row_c, &pij, INSERT_VALUES));
309:     } else {
310:       g_pos = 0.;
311:       g_neg = 0.;
312:       a_pos = 0.;
313:       a_neg = 0.;
314:       diag  = 0.;

316:       /* local connections */
317:       PetscCall(MatGetRow(lA, i, &ncols, &rcol, &rval));
318:       for (j = 0; j < ncols; j++) {
319:         col = rcol[j];
320:         if (lcid[col] >= 0 && (PetscRealPart(rval[j]) > gamg->threshold[0] * Amax_pos[i] || PetscRealPart(-rval[j]) > gamg->threshold[0] * Amax_neg[i])) {
321:           if (PetscRealPart(rval[j]) > 0.) {
322:             g_pos += rval[j];
323:           } else {
324:             g_neg += rval[j];
325:           }
326:         }
327:         if (col != i) {
328:           if (PetscRealPart(rval[j]) > 0.) {
329:             a_pos += rval[j];
330:           } else {
331:             a_neg += rval[j];
332:           }
333:         } else {
334:           diag = rval[j];
335:         }
336:       }
337:       PetscCall(MatRestoreRow(lA, i, &ncols, &rcol, &rval));

339:       /* ghosted connections */
340:       if (gA) {
341:         PetscCall(MatGetRow(gA, i, &ncols, &rcol, &rval));
342:         for (j = 0; j < ncols; j++) {
343:           col = rcol[j];
344:           if (gcid[col] >= 0 && (PetscRealPart(rval[j]) > gamg->threshold[0] * Amax_pos[i] || PetscRealPart(-rval[j]) > gamg->threshold[0] * Amax_neg[i])) {
345:             if (PetscRealPart(rval[j]) > 0.) {
346:               g_pos += rval[j];
347:             } else {
348:               g_neg += rval[j];
349:             }
350:           }
351:           if (PetscRealPart(rval[j]) > 0.) {
352:             a_pos += rval[j];
353:           } else {
354:             a_neg += rval[j];
355:           }
356:         }
357:         PetscCall(MatRestoreRow(gA, i, &ncols, &rcol, &rval));
358:       }

360:       if (g_neg == 0.) {
361:         alpha = 0.;
362:       } else {
363:         alpha = -a_neg / g_neg;
364:       }

366:       if (g_pos == 0.) {
367:         diag += a_pos;
368:         beta = 0.;
369:       } else {
370:         beta = -a_pos / g_pos;
371:       }
372:       if (diag == 0.) {
373:         invdiag = 0.;
374:       } else invdiag = 1. / diag;
375:       /* on */
376:       PetscCall(MatGetRow(lA, i, &ncols, &rcol, &rval));
377:       idx = 0;
378:       for (j = 0; j < ncols; j++) {
379:         col = rcol[j];
380:         if (lcid[col] >= 0 && (PetscRealPart(rval[j]) > gamg->threshold[0] * Amax_pos[i] || PetscRealPart(-rval[j]) > gamg->threshold[0] * Amax_neg[i])) {
381:           row_f = i + fs;
382:           row_c = lcid[col];
383:           /* set the values for on-processor ones */
384:           if (PetscRealPart(rval[j]) < 0.) {
385:             pij = rval[j] * alpha * invdiag;
386:           } else {
387:             pij = rval[j] * beta * invdiag;
388:           }
389:           if (PetscAbsScalar(pij) != 0.) {
390:             pvals[idx] = pij;
391:             pcols[idx] = row_c;
392:             idx++;
393:           }
394:         }
395:       }
396:       PetscCall(MatRestoreRow(lA, i, &ncols, &rcol, &rval));
397:       /* off */
398:       if (gA) {
399:         PetscCall(MatGetRow(gA, i, &ncols, &rcol, &rval));
400:         for (j = 0; j < ncols; j++) {
401:           col = rcol[j];
402:           if (gcid[col] >= 0 && (PetscRealPart(rval[j]) > gamg->threshold[0] * Amax_pos[i] || PetscRealPart(-rval[j]) > gamg->threshold[0] * Amax_neg[i])) {
403:             row_f = i + fs;
404:             row_c = gcid[col];
405:             /* set the values for on-processor ones */
406:             if (PetscRealPart(rval[j]) < 0.) {
407:               pij = rval[j] * alpha * invdiag;
408:             } else {
409:               pij = rval[j] * beta * invdiag;
410:             }
411:             if (PetscAbsScalar(pij) != 0.) {
412:               pvals[idx] = pij;
413:               pcols[idx] = row_c;
414:               idx++;
415:             }
416:           }
417:         }
418:         PetscCall(MatRestoreRow(gA, i, &ncols, &rcol, &rval));
419:       }
420:       PetscCall(MatSetValues(*P, 1, &row_f, idx, pcols, pvals, INSERT_VALUES));
421:     }
422:   }

424:   PetscCall(MatAssemblyBegin(*P, MAT_FINAL_ASSEMBLY));
425:   PetscCall(MatAssemblyEnd(*P, MAT_FINAL_ASSEMBLY));

427:   PetscCall(PetscFree5(lsparse, gsparse, lcid, Amax_pos, Amax_neg));

429:   PetscCall(PetscFree2(pcols, pvals));
430:   if (gA) PetscCall(PetscFree(gcid));
431:   PetscFunctionReturn(PETSC_SUCCESS);
432: }

434: static PetscErrorCode PCGAMGTruncateProlongator_Private(PC pc, Mat *P)
435: {
436:   PetscInt           j, i, ps, pf, pn, pcs, pcf, pcn, idx, cmax;
437:   const PetscScalar *pval;
438:   const PetscInt    *pcol;
439:   PetscScalar       *pnval;
440:   PetscInt          *pncol;
441:   PetscInt           ncols;
442:   Mat                Pnew;
443:   PetscInt          *lsparse, *gsparse;
444:   PetscReal          pmax_pos, pmax_neg, ptot_pos, ptot_neg, pthresh_pos, pthresh_neg;
445:   PC_MG             *mg      = (PC_MG *)pc->data;
446:   PC_GAMG           *pc_gamg = (PC_GAMG *)mg->innerctx;
447:   PC_GAMG_Classical *cls     = (PC_GAMG_Classical *)pc_gamg->subctx;
448:   MatType            mtype;

450:   PetscFunctionBegin;
451:   /* trim and rescale with reallocation */
452:   PetscCall(MatGetOwnershipRange(*P, &ps, &pf));
453:   PetscCall(MatGetOwnershipRangeColumn(*P, &pcs, &pcf));
454:   pn  = pf - ps;
455:   pcn = pcf - pcs;
456:   PetscCall(PetscMalloc2(pn, &lsparse, pn, &gsparse));
457:   /* allocate */
458:   cmax = 0;
459:   for (i = ps; i < pf; i++) {
460:     lsparse[i - ps] = 0;
461:     gsparse[i - ps] = 0;
462:     PetscCall(MatGetRow(*P, i, &ncols, &pcol, &pval));
463:     if (ncols > cmax) cmax = ncols;
464:     pmax_pos = 0.;
465:     pmax_neg = 0.;
466:     for (j = 0; j < ncols; j++) {
467:       if (PetscRealPart(pval[j]) > pmax_pos) {
468:         pmax_pos = PetscRealPart(pval[j]);
469:       } else if (PetscRealPart(pval[j]) < pmax_neg) {
470:         pmax_neg = PetscRealPart(pval[j]);
471:       }
472:     }
473:     for (j = 0; j < ncols; j++) {
474:       if (PetscRealPart(pval[j]) >= pmax_pos * cls->interp_threshold || PetscRealPart(pval[j]) <= pmax_neg * cls->interp_threshold) {
475:         if (pcol[j] >= pcs && pcol[j] < pcf) {
476:           lsparse[i - ps]++;
477:         } else {
478:           gsparse[i - ps]++;
479:         }
480:       }
481:     }
482:     PetscCall(MatRestoreRow(*P, i, &ncols, &pcol, &pval));
483:   }

485:   PetscCall(PetscMalloc2(cmax, &pnval, cmax, &pncol));

487:   PetscCall(MatGetType(*P, &mtype));
488:   PetscCall(MatCreate(PetscObjectComm((PetscObject)*P), &Pnew));
489:   PetscCall(MatSetType(Pnew, mtype));
490:   PetscCall(MatSetSizes(Pnew, pn, pcn, PETSC_DETERMINE, PETSC_DETERMINE));
491:   PetscCall(MatSeqAIJSetPreallocation(Pnew, 0, lsparse));
492:   PetscCall(MatMPIAIJSetPreallocation(Pnew, 0, lsparse, 0, gsparse));

494:   for (i = ps; i < pf; i++) {
495:     PetscCall(MatGetRow(*P, i, &ncols, &pcol, &pval));
496:     pmax_pos = 0.;
497:     pmax_neg = 0.;
498:     for (j = 0; j < ncols; j++) {
499:       if (PetscRealPart(pval[j]) > pmax_pos) {
500:         pmax_pos = PetscRealPart(pval[j]);
501:       } else if (PetscRealPart(pval[j]) < pmax_neg) {
502:         pmax_neg = PetscRealPart(pval[j]);
503:       }
504:     }
505:     pthresh_pos = 0.;
506:     pthresh_neg = 0.;
507:     ptot_pos    = 0.;
508:     ptot_neg    = 0.;
509:     for (j = 0; j < ncols; j++) {
510:       if (PetscRealPart(pval[j]) >= cls->interp_threshold * pmax_pos) {
511:         pthresh_pos += PetscRealPart(pval[j]);
512:       } else if (PetscRealPart(pval[j]) <= cls->interp_threshold * pmax_neg) {
513:         pthresh_neg += PetscRealPart(pval[j]);
514:       }
515:       if (PetscRealPart(pval[j]) > 0.) {
516:         ptot_pos += PetscRealPart(pval[j]);
517:       } else {
518:         ptot_neg += PetscRealPart(pval[j]);
519:       }
520:     }
521:     if (PetscAbsReal(pthresh_pos) > 0.) ptot_pos /= pthresh_pos;
522:     if (PetscAbsReal(pthresh_neg) > 0.) ptot_neg /= pthresh_neg;
523:     idx = 0;
524:     for (j = 0; j < ncols; j++) {
525:       if (PetscRealPart(pval[j]) >= pmax_pos * cls->interp_threshold) {
526:         pnval[idx] = ptot_pos * pval[j];
527:         pncol[idx] = pcol[j];
528:         idx++;
529:       } else if (PetscRealPart(pval[j]) <= pmax_neg * cls->interp_threshold) {
530:         pnval[idx] = ptot_neg * pval[j];
531:         pncol[idx] = pcol[j];
532:         idx++;
533:       }
534:     }
535:     PetscCall(MatRestoreRow(*P, i, &ncols, &pcol, &pval));
536:     PetscCall(MatSetValues(Pnew, 1, &i, idx, pncol, pnval, INSERT_VALUES));
537:   }

539:   PetscCall(MatAssemblyBegin(Pnew, MAT_FINAL_ASSEMBLY));
540:   PetscCall(MatAssemblyEnd(Pnew, MAT_FINAL_ASSEMBLY));
541:   PetscCall(MatDestroy(P));

543:   *P = Pnew;
544:   PetscCall(PetscFree2(lsparse, gsparse));
545:   PetscCall(PetscFree2(pnval, pncol));
546:   PetscFunctionReturn(PETSC_SUCCESS);
547: }

549: static PetscErrorCode PCGAMGProlongator_Classical_Standard(PC pc, Mat A, PetscCoarsenData *agg_lists, Mat *P)
550: {
551:   Mat                lA, *lAs;
552:   MatType            mtype;
553:   Vec                cv;
554:   PetscInt          *gcid, *lcid, *lsparse, *gsparse, *picol;
555:   PetscInt           fs, fe, cs, ce, nl, i, j, k, li, lni, ci, ncols, maxcols, fn, cn, cid;
556:   PetscMPIInt        size;
557:   const PetscInt    *lidx, *icol, *gidx;
558:   PetscBool          iscoarse;
559:   PetscScalar        vi, pentry, pjentry;
560:   PetscScalar       *pcontrib, *pvcol;
561:   const PetscScalar *vcol;
562:   PetscReal          diag, jdiag, jwttotal;
563:   PetscInt           pncols;
564:   PetscSF            sf;
565:   PetscLayout        clayout;
566:   IS                 lis;

568:   PetscFunctionBegin;
569:   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
570:   PetscCall(MatGetOwnershipRange(A, &fs, &fe));
571:   fn = fe - fs;
572:   PetscCall(ISCreateStride(PETSC_COMM_SELF, fe - fs, fs, 1, &lis));
573:   if (size > 1) {
574:     PetscCall(MatGetLayouts(A, NULL, &clayout));
575:     /* increase the overlap by two to get neighbors of neighbors */
576:     PetscCall(MatIncreaseOverlap(A, 1, &lis, 2));
577:     PetscCall(ISSort(lis));
578:     /* get the local part of A */
579:     PetscCall(MatCreateSubMatrices(A, 1, &lis, &lis, MAT_INITIAL_MATRIX, &lAs));
580:     lA = lAs[0];
581:     /* build an SF out of it */
582:     PetscCall(ISGetLocalSize(lis, &nl));
583:     PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)A), &sf));
584:     PetscCall(ISGetIndices(lis, &lidx));
585:     PetscCall(PetscSFSetGraphLayout(sf, clayout, nl, NULL, PETSC_COPY_VALUES, lidx));
586:     PetscCall(ISRestoreIndices(lis, &lidx));
587:   } else {
588:     lA = A;
589:     nl = fn;
590:   }
591:   /* create a communication structure for the overlapped portion and transmit coarse indices */
592:   PetscCall(PetscMalloc3(fn, &lsparse, fn, &gsparse, nl, &pcontrib));
593:   /* create coarse vector */
594:   cn = 0;
595:   for (i = 0; i < fn; i++) {
596:     PetscCall(PetscCDIsEmptyAt(agg_lists, i, &iscoarse));
597:     if (!iscoarse) cn++;
598:   }
599:   PetscCall(PetscMalloc1(fn, &gcid));
600:   PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)A), cn, PETSC_DECIDE, &cv));
601:   PetscCall(VecGetOwnershipRange(cv, &cs, &ce));
602:   cn = 0;
603:   for (i = 0; i < fn; i++) {
604:     PetscCall(PetscCDIsEmptyAt(agg_lists, i, &iscoarse));
605:     if (!iscoarse) {
606:       gcid[i] = cs + cn;
607:       cn++;
608:     } else {
609:       gcid[i] = -1;
610:     }
611:   }
612:   if (size > 1) {
613:     PetscCall(PetscMalloc1(nl, &lcid));
614:     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, gcid, lcid, MPI_REPLACE));
615:     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, gcid, lcid, MPI_REPLACE));
616:   } else {
617:     lcid = gcid;
618:   }
619:   /* count to preallocate the prolongator */
620:   PetscCall(ISGetIndices(lis, &gidx));
621:   maxcols = 0;
622:   /* count the number of unique contributing coarse cells for each fine */
623:   for (i = 0; i < nl; i++) {
624:     pcontrib[i] = 0.;
625:     PetscCall(MatGetRow(lA, i, &ncols, &icol, NULL));
626:     if (gidx[i] >= fs && gidx[i] < fe) {
627:       li          = gidx[i] - fs;
628:       lsparse[li] = 0;
629:       gsparse[li] = 0;
630:       cid         = lcid[i];
631:       if (cid >= 0) {
632:         lsparse[li] = 1;
633:       } else {
634:         for (j = 0; j < ncols; j++) {
635:           if (lcid[icol[j]] >= 0) {
636:             pcontrib[icol[j]] = 1.;
637:           } else {
638:             ci = icol[j];
639:             PetscCall(MatRestoreRow(lA, i, &ncols, &icol, NULL));
640:             PetscCall(MatGetRow(lA, ci, &ncols, &icol, NULL));
641:             for (k = 0; k < ncols; k++) {
642:               if (lcid[icol[k]] >= 0) pcontrib[icol[k]] = 1.;
643:             }
644:             PetscCall(MatRestoreRow(lA, ci, &ncols, &icol, NULL));
645:             PetscCall(MatGetRow(lA, i, &ncols, &icol, NULL));
646:           }
647:         }
648:         for (j = 0; j < ncols; j++) {
649:           if (lcid[icol[j]] >= 0 && pcontrib[icol[j]] != 0.) {
650:             lni = lcid[icol[j]];
651:             if (lni >= cs && lni < ce) {
652:               lsparse[li]++;
653:             } else {
654:               gsparse[li]++;
655:             }
656:             pcontrib[icol[j]] = 0.;
657:           } else {
658:             ci = icol[j];
659:             PetscCall(MatRestoreRow(lA, i, &ncols, &icol, NULL));
660:             PetscCall(MatGetRow(lA, ci, &ncols, &icol, NULL));
661:             for (k = 0; k < ncols; k++) {
662:               if (lcid[icol[k]] >= 0 && pcontrib[icol[k]] != 0.) {
663:                 lni = lcid[icol[k]];
664:                 if (lni >= cs && lni < ce) {
665:                   lsparse[li]++;
666:                 } else {
667:                   gsparse[li]++;
668:                 }
669:                 pcontrib[icol[k]] = 0.;
670:               }
671:             }
672:             PetscCall(MatRestoreRow(lA, ci, &ncols, &icol, NULL));
673:             PetscCall(MatGetRow(lA, i, &ncols, &icol, NULL));
674:           }
675:         }
676:       }
677:       if (lsparse[li] + gsparse[li] > maxcols) maxcols = lsparse[li] + gsparse[li];
678:     }
679:     PetscCall(MatRestoreRow(lA, i, &ncols, &icol, &vcol));
680:   }
681:   PetscCall(PetscMalloc2(maxcols, &picol, maxcols, &pvcol));
682:   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), P));
683:   PetscCall(MatGetType(A, &mtype));
684:   PetscCall(MatSetType(*P, mtype));
685:   PetscCall(MatSetSizes(*P, fn, cn, PETSC_DETERMINE, PETSC_DETERMINE));
686:   PetscCall(MatMPIAIJSetPreallocation(*P, 0, lsparse, 0, gsparse));
687:   PetscCall(MatSeqAIJSetPreallocation(*P, 0, lsparse));
688:   for (i = 0; i < nl; i++) {
689:     diag = 0.;
690:     if (gidx[i] >= fs && gidx[i] < fe) {
691:       pncols = 0;
692:       cid    = lcid[i];
693:       if (cid >= 0) {
694:         pncols   = 1;
695:         picol[0] = cid;
696:         pvcol[0] = 1.;
697:       } else {
698:         PetscCall(MatGetRow(lA, i, &ncols, &icol, &vcol));
699:         for (j = 0; j < ncols; j++) {
700:           pentry = vcol[j];
701:           if (lcid[icol[j]] >= 0) {
702:             /* coarse neighbor */
703:             pcontrib[icol[j]] += pentry;
704:           } else if (icol[j] != i) {
705:             /* the neighbor is a strongly connected fine node */
706:             ci = icol[j];
707:             vi = vcol[j];
708:             PetscCall(MatRestoreRow(lA, i, &ncols, &icol, &vcol));
709:             PetscCall(MatGetRow(lA, ci, &ncols, &icol, &vcol));
710:             jwttotal = 0.;
711:             jdiag    = 0.;
712:             for (k = 0; k < ncols; k++) {
713:               if (ci == icol[k]) jdiag = PetscRealPart(vcol[k]);
714:             }
715:             for (k = 0; k < ncols; k++) {
716:               if (lcid[icol[k]] >= 0 && jdiag * PetscRealPart(vcol[k]) < 0.) {
717:                 pjentry = vcol[k];
718:                 jwttotal += PetscRealPart(pjentry);
719:               }
720:             }
721:             if (jwttotal != 0.) {
722:               jwttotal = PetscRealPart(vi) / jwttotal;
723:               for (k = 0; k < ncols; k++) {
724:                 if (lcid[icol[k]] >= 0 && jdiag * PetscRealPart(vcol[k]) < 0.) {
725:                   pjentry = vcol[k] * jwttotal;
726:                   pcontrib[icol[k]] += pjentry;
727:                 }
728:               }
729:             } else {
730:               diag += PetscRealPart(vi);
731:             }
732:             PetscCall(MatRestoreRow(lA, ci, &ncols, &icol, &vcol));
733:             PetscCall(MatGetRow(lA, i, &ncols, &icol, &vcol));
734:           } else {
735:             diag += PetscRealPart(vcol[j]);
736:           }
737:         }
738:         if (diag != 0.) {
739:           diag = 1. / diag;
740:           for (j = 0; j < ncols; j++) {
741:             if (lcid[icol[j]] >= 0 && pcontrib[icol[j]] != 0.) {
742:               /* the neighbor is a coarse node */
743:               if (PetscAbsScalar(pcontrib[icol[j]]) > 0.0) {
744:                 lni           = lcid[icol[j]];
745:                 pvcol[pncols] = -pcontrib[icol[j]] * diag;
746:                 picol[pncols] = lni;
747:                 pncols++;
748:               }
749:               pcontrib[icol[j]] = 0.;
750:             } else {
751:               /* the neighbor is a strongly connected fine node */
752:               ci = icol[j];
753:               PetscCall(MatRestoreRow(lA, i, &ncols, &icol, &vcol));
754:               PetscCall(MatGetRow(lA, ci, &ncols, &icol, &vcol));
755:               for (k = 0; k < ncols; k++) {
756:                 if (lcid[icol[k]] >= 0 && pcontrib[icol[k]] != 0.) {
757:                   if (PetscAbsScalar(pcontrib[icol[k]]) > 0.0) {
758:                     lni           = lcid[icol[k]];
759:                     pvcol[pncols] = -pcontrib[icol[k]] * diag;
760:                     picol[pncols] = lni;
761:                     pncols++;
762:                   }
763:                   pcontrib[icol[k]] = 0.;
764:                 }
765:               }
766:               PetscCall(MatRestoreRow(lA, ci, &ncols, &icol, &vcol));
767:               PetscCall(MatGetRow(lA, i, &ncols, &icol, &vcol));
768:             }
769:             pcontrib[icol[j]] = 0.;
770:           }
771:           PetscCall(MatRestoreRow(lA, i, &ncols, &icol, &vcol));
772:         }
773:       }
774:       ci = gidx[i];
775:       if (pncols > 0) PetscCall(MatSetValues(*P, 1, &ci, pncols, picol, pvcol, INSERT_VALUES));
776:     }
777:   }
778:   PetscCall(ISRestoreIndices(lis, &gidx));
779:   PetscCall(PetscFree2(picol, pvcol));
780:   PetscCall(PetscFree3(lsparse, gsparse, pcontrib));
781:   PetscCall(ISDestroy(&lis));
782:   PetscCall(PetscFree(gcid));
783:   if (size > 1) {
784:     PetscCall(PetscFree(lcid));
785:     PetscCall(MatDestroyMatrices(1, &lAs));
786:     PetscCall(PetscSFDestroy(&sf));
787:   }
788:   PetscCall(VecDestroy(&cv));
789:   PetscCall(MatAssemblyBegin(*P, MAT_FINAL_ASSEMBLY));
790:   PetscCall(MatAssemblyEnd(*P, MAT_FINAL_ASSEMBLY));
791:   PetscFunctionReturn(PETSC_SUCCESS);
792: }

794: static PetscErrorCode PCGAMGOptProlongator_Classical_Jacobi(PC pc, Mat A, Mat *P)
795: {
796:   PetscInt           f, s, n, cf, cs, i, idx;
797:   PetscInt          *coarserows;
798:   PetscInt           ncols;
799:   const PetscInt    *pcols;
800:   const PetscScalar *pvals;
801:   Mat                Pnew;
802:   Vec                diag;
803:   PC_MG             *mg      = (PC_MG *)pc->data;
804:   PC_GAMG           *pc_gamg = (PC_GAMG *)mg->innerctx;
805:   PC_GAMG_Classical *cls     = (PC_GAMG_Classical *)pc_gamg->subctx;

807:   PetscFunctionBegin;
808:   if (cls->nsmooths == 0) {
809:     PetscCall(PCGAMGTruncateProlongator_Private(pc, P));
810:     PetscFunctionReturn(PETSC_SUCCESS);
811:   }
812:   PetscCall(MatGetOwnershipRange(*P, &s, &f));
813:   n = f - s;
814:   PetscCall(MatGetOwnershipRangeColumn(*P, &cs, &cf));
815:   PetscCall(PetscMalloc1(n, &coarserows));
816:   /* identify the rows corresponding to coarse unknowns */
817:   idx = 0;
818:   for (i = s; i < f; i++) {
819:     PetscCall(MatGetRow(*P, i, &ncols, &pcols, &pvals));
820:     /* assume, for now, that it's a coarse unknown if it has a single unit entry */
821:     if (ncols == 1) {
822:       if (pvals[0] == 1.) {
823:         coarserows[idx] = i;
824:         idx++;
825:       }
826:     }
827:     PetscCall(MatRestoreRow(*P, i, &ncols, &pcols, &pvals));
828:   }
829:   PetscCall(MatCreateVecs(A, &diag, NULL));
830:   PetscCall(MatGetDiagonal(A, diag));
831:   PetscCall(VecReciprocal(diag));
832:   for (i = 0; i < cls->nsmooths; i++) {
833:     PetscCall(MatMatMult(A, *P, MAT_INITIAL_MATRIX, PETSC_CURRENT, &Pnew));
834:     PetscCall(MatZeroRows(Pnew, idx, coarserows, 0., NULL, NULL));
835:     PetscCall(MatDiagonalScale(Pnew, diag, NULL));
836:     PetscCall(MatAYPX(Pnew, -1.0, *P, DIFFERENT_NONZERO_PATTERN));
837:     PetscCall(MatDestroy(P));
838:     *P   = Pnew;
839:     Pnew = NULL;
840:   }
841:   PetscCall(VecDestroy(&diag));
842:   PetscCall(PetscFree(coarserows));
843:   PetscCall(PCGAMGTruncateProlongator_Private(pc, P));
844:   PetscFunctionReturn(PETSC_SUCCESS);
845: }

847: static PetscErrorCode PCGAMGProlongator_Classical(PC pc, Mat A, PetscCoarsenData *agg_lists, Mat *P)
848: {
849:   PetscErrorCode (*f)(PC, Mat, PetscCoarsenData *, Mat *);
850:   PC_MG             *mg      = (PC_MG *)pc->data;
851:   PC_GAMG           *pc_gamg = (PC_GAMG *)mg->innerctx;
852:   PC_GAMG_Classical *cls     = (PC_GAMG_Classical *)pc_gamg->subctx;

854:   PetscFunctionBegin;
855:   PetscCall(PetscFunctionListFind(PCGAMGClassicalProlongatorList, cls->prolongtype, &f));
856:   PetscCheck(f, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Cannot find PCGAMG Classical prolongator type");
857:   PetscCall((*f)(pc, A, agg_lists, P));
858:   PetscFunctionReturn(PETSC_SUCCESS);
859: }

861: static PetscErrorCode PCGAMGDestroy_Classical(PC pc)
862: {
863:   PC_MG   *mg      = (PC_MG *)pc->data;
864:   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;

866:   PetscFunctionBegin;
867:   PetscCall(PetscFree(pc_gamg->subctx));
868:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGClassicalSetType_C", NULL));
869:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGClassicalGetType_C", NULL));
870:   PetscFunctionReturn(PETSC_SUCCESS);
871: }

873: static PetscErrorCode PCGAMGSetFromOptions_Classical(PC pc, PetscOptionItems PetscOptionsObject)
874: {
875:   PC_MG             *mg      = (PC_MG *)pc->data;
876:   PC_GAMG           *pc_gamg = (PC_GAMG *)mg->innerctx;
877:   PC_GAMG_Classical *cls     = (PC_GAMG_Classical *)pc_gamg->subctx;
878:   char               tname[256];
879:   PetscBool          flg;

881:   PetscFunctionBegin;
882:   PetscOptionsHeadBegin(PetscOptionsObject, "GAMG-Classical options");
883:   PetscCall(PetscOptionsFList("-pc_gamg_classical_type", "Type of Classical AMG prolongation", "PCGAMGClassicalSetType", PCGAMGClassicalProlongatorList, cls->prolongtype, tname, sizeof(tname), &flg));
884:   if (flg) PetscCall(PCGAMGClassicalSetType(pc, tname));
885:   PetscCall(PetscOptionsReal("-pc_gamg_classical_interp_threshold", "Threshold for classical interpolator entries", "", cls->interp_threshold, &cls->interp_threshold, NULL));
886:   PetscCall(PetscOptionsInt("-pc_gamg_classical_nsmooths", "Threshold for classical interpolator entries", "", cls->nsmooths, &cls->nsmooths, NULL));
887:   PetscOptionsHeadEnd();
888:   PetscFunctionReturn(PETSC_SUCCESS);
889: }

891: static PetscErrorCode PCGAMGSetData_Classical(PC pc, Mat A)
892: {
893:   PC_MG   *mg      = (PC_MG *)pc->data;
894:   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;

896:   PetscFunctionBegin;
897:   /* no data for classical AMG */
898:   pc_gamg->data           = NULL;
899:   pc_gamg->data_cell_cols = 0;
900:   pc_gamg->data_cell_rows = 0;
901:   pc_gamg->data_sz        = 0;
902:   PetscFunctionReturn(PETSC_SUCCESS);
903: }

905: static PetscErrorCode PCGAMGClassicalFinalizePackage(void)
906: {
907:   PetscFunctionBegin;
908:   PCGAMGClassicalPackageInitialized = PETSC_FALSE;
909:   PetscCall(PetscFunctionListDestroy(&PCGAMGClassicalProlongatorList));
910:   PetscFunctionReturn(PETSC_SUCCESS);
911: }

913: static PetscErrorCode PCGAMGClassicalInitializePackage(void)
914: {
915:   PetscFunctionBegin;
916:   if (PCGAMGClassicalPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
917:   PetscCall(PetscFunctionListAdd(&PCGAMGClassicalProlongatorList, PCGAMGCLASSICALDIRECT, PCGAMGProlongator_Classical_Direct));
918:   PetscCall(PetscFunctionListAdd(&PCGAMGClassicalProlongatorList, PCGAMGCLASSICALSTANDARD, PCGAMGProlongator_Classical_Standard));
919:   PetscCall(PetscRegisterFinalize(PCGAMGClassicalFinalizePackage));
920:   PetscFunctionReturn(PETSC_SUCCESS);
921: }

923: PetscErrorCode PCCreateGAMG_Classical(PC pc)
924: {
925:   PC_MG             *mg      = (PC_MG *)pc->data;
926:   PC_GAMG           *pc_gamg = (PC_GAMG *)mg->innerctx;
927:   PC_GAMG_Classical *pc_gamg_classical;

929:   PetscFunctionBegin;
930:   PetscCall(PCGAMGClassicalInitializePackage());
931:   if (pc_gamg->subctx) {
932:     /* call base class */
933:     PetscCall(PCDestroy_GAMG(pc));
934:   }

936:   /* create sub context for SA */
937:   PetscCall(PetscNew(&pc_gamg_classical));
938:   pc_gamg->subctx         = pc_gamg_classical;
939:   pc->ops->setfromoptions = PCGAMGSetFromOptions_Classical;
940:   /* reset does not do anything; setup not virtual */

942:   /* set internal function pointers */
943:   pc_gamg->ops->destroy        = PCGAMGDestroy_Classical;
944:   pc_gamg->ops->creategraph    = PCGAMGCreateGraph_Classical;
945:   pc_gamg->ops->coarsen        = PCGAMGCoarsen_Classical;
946:   pc_gamg->ops->prolongator    = PCGAMGProlongator_Classical;
947:   pc_gamg->ops->optprolongator = PCGAMGOptProlongator_Classical_Jacobi;
948:   pc_gamg->ops->setfromoptions = PCGAMGSetFromOptions_Classical;

950:   pc_gamg->ops->createdefaultdata     = PCGAMGSetData_Classical;
951:   pc_gamg_classical->interp_threshold = 0.2;
952:   pc_gamg_classical->nsmooths         = 0;
953:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGClassicalSetType_C", PCGAMGClassicalSetType_GAMG));
954:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGClassicalGetType_C", PCGAMGClassicalGetType_GAMG));
955:   PetscCall(PCGAMGClassicalSetType(pc, PCGAMGCLASSICALSTANDARD));
956:   PetscFunctionReturn(PETSC_SUCCESS);
957: }