Actual source code: misk.c

  1: #include <petsc/private/matimpl.h>
  2: #include <../src/mat/impls/aij/seq/aij.h>
  3: #include <../src/mat/impls/aij/mpi/mpiaij.h>
  4: #include <petscsf.h>

  6: #define MIS_NOT_DONE       -2
  7: #define MIS_DELETED        -1
  8: #define MIS_REMOVED        -3
  9: #define MIS_IS_SELECTED(s) (s >= 0)

 11: /* edge for priority queue */
 12: typedef struct edge_tag {
 13:   PetscReal weight;
 14:   PetscInt  lid0, gid1, cpid1;
 15: } Edge;

 17: static PetscErrorCode PetscCoarsenDataView_private(PetscCoarsenData *agg_lists, PetscViewer viewer)
 18: {
 19:   PetscCDIntNd *pos, *pos2;

 21:   PetscFunctionBegin;
 22:   for (PetscInt kk = 0; kk < agg_lists->size; kk++) {
 23:     PetscCall(PetscCDGetHeadPos(agg_lists, kk, &pos));
 24:     if ((pos2 = pos)) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "selected local %d: ", (int)kk));
 25:     while (pos) {
 26:       PetscInt gid1;
 27:       PetscCall(PetscCDIntNdGetID(pos, &gid1));
 28:       PetscCall(PetscCDGetNextPos(agg_lists, kk, &pos));
 29:       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %d ", (int)gid1));
 30:     }
 31:     if (pos2) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
 32:   }
 33:   PetscFunctionReturn(PETSC_SUCCESS);
 34: }

 36: /*
 37:   MatCoarsenApply_MISK_private - parallel heavy edge matching

 39:   Input Parameter:
 40:    . perm - permutation
 41:    . Gmat - global matrix of graph (data not defined)

 43:   Output Parameter:
 44:    . a_locals_llist - array of list of local nodes rooted at local node
 45: */
 46: static PetscErrorCode MatCoarsenApply_MISK_private(IS perm, const PetscInt misk, Mat Gmat, PetscCoarsenData **a_locals_llist)
 47: {
 48:   PetscBool   isMPI;
 49:   MPI_Comm    comm;
 50:   PetscMPIInt rank, size;
 51:   Mat         cMat, Prols[5], Rtot;
 52:   PetscScalar one = 1;

 54:   PetscFunctionBegin;
 57:   PetscAssertPointer(a_locals_llist, 4);
 58:   PetscCheck(misk < 5 && misk > 0, PETSC_COMM_SELF, PETSC_ERR_SUP, "too many/few levels: %d", (int)misk);
 59:   PetscCall(PetscObjectBaseTypeCompare((PetscObject)Gmat, MATMPIAIJ, &isMPI));
 60:   PetscCall(PetscObjectGetComm((PetscObject)Gmat, &comm));
 61:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
 62:   PetscCallMPI(MPI_Comm_size(comm, &size));
 63:   PetscCall(PetscInfo(Gmat, "misk %d\n", (int)misk));
 64:   /* make a copy of the graph, this gets destroyed in iterates */
 65:   if (misk > 1) PetscCall(MatDuplicate(Gmat, MAT_COPY_VALUES, &cMat));
 66:   else cMat = Gmat;
 67:   for (PetscInt iterIdx = 0; iterIdx < misk; iterIdx++) {
 68:     Mat_SeqAIJ       *matA, *matB = NULL;
 69:     Mat_MPIAIJ       *mpimat = NULL;
 70:     const PetscInt   *perm_ix;
 71:     const PetscInt    nloc = cMat->rmap->n;
 72:     PetscCoarsenData *agg_lists;
 73:     PetscInt         *cpcol_gid = NULL, *cpcol_state, *lid_cprowID, *lid_state, *lid_parent_gid = NULL;
 74:     PetscInt          num_fine_ghosts, kk, n, ix, j, *idx, *ai, Iend, my0, nremoved, gid, lid, cpid, lidj, sgid, t1, t2, slid, nDone, nselected = 0, state;
 75:     PetscBool        *lid_removed, isOK;
 76:     PetscLayout       layout;
 77:     PetscSF           sf;

 79:     if (isMPI) {
 80:       mpimat = (Mat_MPIAIJ *)cMat->data;
 81:       matA   = (Mat_SeqAIJ *)mpimat->A->data;
 82:       matB   = (Mat_SeqAIJ *)mpimat->B->data;
 83:       /* force compressed storage of B */
 84:       PetscCall(MatCheckCompressedRow(mpimat->B, matB->nonzerorowcnt, &matB->compressedrow, matB->i, cMat->rmap->n, -1.0));
 85:     } else {
 86:       PetscBool isAIJ;
 87:       PetscCall(PetscObjectBaseTypeCompare((PetscObject)cMat, MATSEQAIJ, &isAIJ));
 88:       PetscCheck(isAIJ, PETSC_COMM_SELF, PETSC_ERR_USER, "Require AIJ matrix.");
 89:       matA = (Mat_SeqAIJ *)cMat->data;
 90:     }
 91:     PetscCall(MatGetOwnershipRange(cMat, &my0, &Iend));
 92:     if (mpimat) {
 93:       PetscInt *lid_gid;
 94:       PetscCall(PetscMalloc1(nloc, &lid_gid)); /* explicit array needed */
 95:       for (kk = 0, gid = my0; kk < nloc; kk++, gid++) lid_gid[kk] = gid;
 96:       PetscCall(VecGetLocalSize(mpimat->lvec, &num_fine_ghosts));
 97:       PetscCall(PetscMalloc1(num_fine_ghosts, &cpcol_gid));
 98:       PetscCall(PetscMalloc1(num_fine_ghosts, &cpcol_state));
 99:       PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)cMat), &sf));
100:       PetscCall(MatGetLayouts(cMat, &layout, NULL));
101:       PetscCall(PetscSFSetGraphLayout(sf, layout, num_fine_ghosts, NULL, PETSC_COPY_VALUES, mpimat->garray));
102:       PetscCall(PetscSFBcastBegin(sf, MPIU_INT, lid_gid, cpcol_gid, MPI_REPLACE));
103:       PetscCall(PetscSFBcastEnd(sf, MPIU_INT, lid_gid, cpcol_gid, MPI_REPLACE));
104:       for (kk = 0; kk < num_fine_ghosts; kk++) cpcol_state[kk] = MIS_NOT_DONE;
105:       PetscCall(PetscFree(lid_gid));
106:     } else num_fine_ghosts = 0;

108:     PetscCall(PetscMalloc1(nloc, &lid_cprowID));
109:     PetscCall(PetscMalloc1(nloc, &lid_removed)); /* explicit array needed */
110:     PetscCall(PetscMalloc1(nloc, &lid_parent_gid));
111:     PetscCall(PetscMalloc1(nloc, &lid_state));

113:     /* the data structure */
114:     PetscCall(PetscCDCreate(nloc, &agg_lists));
115:     /* need an inverse map - locals */
116:     for (kk = 0; kk < nloc; kk++) {
117:       lid_cprowID[kk]    = -1;
118:       lid_removed[kk]    = PETSC_FALSE;
119:       lid_parent_gid[kk] = -1.0;
120:       lid_state[kk]      = MIS_NOT_DONE;
121:     }
122:     /* set index into cmpressed row 'lid_cprowID' */
123:     if (matB) {
124:       for (ix = 0; ix < matB->compressedrow.nrows; ix++) {
125:         lid = matB->compressedrow.rindex[ix];
126:         if (lid >= 0) lid_cprowID[lid] = ix;
127:       }
128:     }
129:     /* MIS */
130:     nremoved = nDone = 0;
131:     if (!iterIdx) PetscCall(ISGetIndices(perm, &perm_ix)); // use permutation on first MIS
132:     else perm_ix = NULL;
133:     while (nDone < nloc || PETSC_TRUE) { /* asynchronous not implemented */
134:       /* check all vertices */
135:       for (kk = 0; kk < nloc; kk++) {
136:         lid   = perm_ix ? perm_ix[kk] : kk;
137:         state = lid_state[lid];
138:         if (lid_removed[lid]) continue;
139:         if (state == MIS_NOT_DONE) {
140:           /* parallel test, delete if selected ghost */
141:           isOK = PETSC_TRUE;
142:           /* parallel test */
143:           if ((ix = lid_cprowID[lid]) != -1) { /* if I have any ghost neighbors */
144:             ai  = matB->compressedrow.i;
145:             n   = ai[ix + 1] - ai[ix];
146:             idx = matB->j + ai[ix];
147:             for (j = 0; j < n; j++) {
148:               cpid = idx[j]; /* compressed row ID in B mat */
149:               gid  = cpcol_gid[cpid];
150:               if (cpcol_state[cpid] == MIS_NOT_DONE && gid >= Iend) { /* or pe>rank */
151:                 isOK = PETSC_FALSE;                                   /* can not delete */
152:                 break;
153:               }
154:             }
155:           }
156:           if (isOK) { /* select or remove this vertex if it is a true singleton like a BC */
157:             nDone++;
158:             /* check for singleton */
159:             ai = matA->i;
160:             n  = ai[lid + 1] - ai[lid];
161:             if (n < 2) {
162:               /* if I have any ghost adj then not a singleton */
163:               ix = lid_cprowID[lid];
164:               if (ix == -1 || !(matB->compressedrow.i[ix + 1] - matB->compressedrow.i[ix])) {
165:                 nremoved++;
166:                 lid_removed[lid] = PETSC_TRUE;
167:                 /* should select this because it is technically in the MIS but lets not */
168:                 continue; /* one local adj (me) and no ghost - singleton */
169:               }
170:             }
171:             /* SELECTED state encoded with global index */
172:             lid_state[lid] = nselected; // >= 0  is selected, cache for ordering coarse grid
173:             nselected++;
174:             PetscCall(PetscCDAppendID(agg_lists, lid, lid + my0));
175:             /* delete local adj */
176:             idx = matA->j + ai[lid];
177:             for (j = 0; j < n; j++) {
178:               lidj = idx[j];
179:               if (lid_state[lidj] == MIS_NOT_DONE) {
180:                 nDone++;
181:                 PetscCall(PetscCDAppendID(agg_lists, lid, lidj + my0));
182:                 lid_state[lidj] = MIS_DELETED; /* delete this */
183:               }
184:             }
185:           } /* selected */
186:         }   /* not done vertex */
187:       }     /* vertex loop */

189:       /* update ghost states and count todos */
190:       if (mpimat) {
191:         /* scatter states, check for done */
192:         PetscCall(PetscSFBcastBegin(sf, MPIU_INT, lid_state, cpcol_state, MPI_REPLACE));
193:         PetscCall(PetscSFBcastEnd(sf, MPIU_INT, lid_state, cpcol_state, MPI_REPLACE));
194:         ai = matB->compressedrow.i;
195:         for (ix = 0; ix < matB->compressedrow.nrows; ix++) {
196:           lid   = matB->compressedrow.rindex[ix]; /* local boundary node */
197:           state = lid_state[lid];
198:           if (state == MIS_NOT_DONE) {
199:             /* look at ghosts */
200:             n   = ai[ix + 1] - ai[ix];
201:             idx = matB->j + ai[ix];
202:             for (j = 0; j < n; j++) {
203:               cpid = idx[j];                            /* compressed row ID in B mat */
204:               if (MIS_IS_SELECTED(cpcol_state[cpid])) { /* lid is now deleted by ghost */
205:                 nDone++;
206:                 lid_state[lid]      = MIS_DELETED; /* delete this */
207:                 sgid                = cpcol_gid[cpid];
208:                 lid_parent_gid[lid] = sgid; /* keep track of proc that I belong to */
209:                 break;
210:               }
211:             }
212:           }
213:         }
214:         /* all done? */
215:         t1 = nloc - nDone;
216:         PetscCall(MPIU_Allreduce(&t1, &t2, 1, MPIU_INT, MPI_SUM, comm)); /* synchronous version */
217:         if (!t2) break;
218:       } else break; /* no mpi - all done */
219:     }               /* outer parallel MIS loop */
220:     if (!iterIdx) PetscCall(ISRestoreIndices(perm, &perm_ix));
221:     PetscCall(PetscInfo(Gmat, "\t removed %" PetscInt_FMT " of %" PetscInt_FMT " vertices.  %" PetscInt_FMT " selected.\n", nremoved, nloc, nselected));

223:     /* tell adj who my lid_parent_gid vertices belong to - fill in agg_lists selected ghost lists */
224:     if (matB) {
225:       PetscInt *cpcol_sel_gid, *icpcol_gid;
226:       /* need to copy this to free buffer -- should do this globally */
227:       PetscCall(PetscMalloc1(num_fine_ghosts, &cpcol_sel_gid));
228:       PetscCall(PetscMalloc1(num_fine_ghosts, &icpcol_gid));
229:       for (cpid = 0; cpid < num_fine_ghosts; cpid++) icpcol_gid[cpid] = cpcol_gid[cpid];
230:       /* get proc of deleted ghost */
231:       PetscCall(PetscSFBcastBegin(sf, MPIU_INT, lid_parent_gid, cpcol_sel_gid, MPI_REPLACE));
232:       PetscCall(PetscSFBcastEnd(sf, MPIU_INT, lid_parent_gid, cpcol_sel_gid, MPI_REPLACE));
233:       for (cpid = 0; cpid < num_fine_ghosts; cpid++) {
234:         sgid = cpcol_sel_gid[cpid];
235:         gid  = icpcol_gid[cpid];
236:         if (sgid >= my0 && sgid < Iend) { /* I own this deleted */
237:           slid = sgid - my0;
238:           PetscCall(PetscCDAppendID(agg_lists, slid, gid));
239:         }
240:       }
241:       // done - cleanup
242:       PetscCall(PetscFree(icpcol_gid));
243:       PetscCall(PetscFree(cpcol_sel_gid));
244:       PetscCall(PetscSFDestroy(&sf));
245:       PetscCall(PetscFree(cpcol_gid));
246:       PetscCall(PetscFree(cpcol_state));
247:     }
248:     PetscCall(PetscFree(lid_cprowID));
249:     PetscCall(PetscFree(lid_removed));
250:     PetscCall(PetscFree(lid_parent_gid));
251:     PetscCall(PetscFree(lid_state));

253:     /* MIS done - make projection matrix - P */
254:     MatType jtype;
255:     PetscCall(MatGetType(Gmat, &jtype));
256:     PetscCall(MatCreate(comm, &Prols[iterIdx]));
257:     PetscCall(MatSetType(Prols[iterIdx], jtype));
258:     PetscCall(MatSetSizes(Prols[iterIdx], nloc, nselected, PETSC_DETERMINE, PETSC_DETERMINE));
259:     PetscCall(MatSeqAIJSetPreallocation(Prols[iterIdx], 1, NULL));
260:     PetscCall(MatMPIAIJSetPreallocation(Prols[iterIdx], 1, NULL, 1, NULL));
261:     {
262:       PetscCDIntNd *pos, *pos2;
263:       PetscInt      colIndex, Iend, fgid;
264:       PetscCall(MatGetOwnershipRangeColumn(Prols[iterIdx], &colIndex, &Iend));
265:       // TODO - order with permutation in lid_selected (reversed)
266:       for (PetscInt lid = 0; lid < agg_lists->size; lid++) {
267:         PetscCall(PetscCDGetHeadPos(agg_lists, lid, &pos));
268:         pos2 = pos;
269:         while (pos) {
270:           PetscCall(PetscCDIntNdGetID(pos, &fgid));
271:           PetscCall(PetscCDGetNextPos(agg_lists, lid, &pos));
272:           PetscCall(MatSetValues(Prols[iterIdx], 1, &fgid, 1, &colIndex, &one, INSERT_VALUES));
273:         }
274:         if (pos2) colIndex++;
275:       }
276:       PetscCheck(Iend == colIndex, PETSC_COMM_SELF, PETSC_ERR_SUP, "Iend!=colIndex: %d %d", (int)Iend, (int)colIndex);
277:     }
278:     PetscCall(MatAssemblyBegin(Prols[iterIdx], MAT_FINAL_ASSEMBLY));
279:     PetscCall(MatAssemblyEnd(Prols[iterIdx], MAT_FINAL_ASSEMBLY));
280:     /* project to make new graph for next MIS, skip if last */
281:     if (iterIdx < misk - 1) {
282:       Mat new_mat;
283:       PetscCall(MatPtAP(cMat, Prols[iterIdx], MAT_INITIAL_MATRIX, PETSC_DEFAULT, &new_mat));
284:       PetscCall(MatDestroy(&cMat));
285:       cMat = new_mat; // next iter
286:     } else if (cMat != Gmat) PetscCall(MatDestroy(&cMat));
287:     // cleanup
288:     PetscCall(PetscCDDestroy(agg_lists));
289:   } /* MIS-k iteration */
290:   /* make total prolongator Rtot = P_0 * P_1 * ... */
291:   Rtot = Prols[misk - 1]; // compose P then transpose to get R
292:   for (PetscInt iterIdx = misk - 1; iterIdx > 0; iterIdx--) {
293:     Mat P;
294:     PetscCall(MatMatMult(Prols[iterIdx - 1], Rtot, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &P));
295:     PetscCall(MatDestroy(&Prols[iterIdx - 1]));
296:     PetscCall(MatDestroy(&Rtot));
297:     Rtot = P;
298:   }
299:   PetscCall(MatTranspose(Rtot, MAT_INPLACE_MATRIX, &Rtot)); // R now
300:   PetscCall(MatViewFromOptions(Rtot, NULL, "-misk_aggregation_view"));
301:   /* make aggregates with Rtot - could use Rtot directly in theory but have to go through the aggregate list data structure */
302:   {
303:     PetscInt          Istart, Iend, ncols, NN, MM, jj = 0, max_osz = 0;
304:     const PetscInt    nloc = Gmat->rmap->n;
305:     PetscCoarsenData *agg_lists;
306:     Mat               mat;
307:     PetscCall(PetscCDCreate(nloc, &agg_lists));
308:     *a_locals_llist = agg_lists; // return
309:     PetscCall(MatGetOwnershipRange(Rtot, &Istart, &Iend));
310:     for (int grow = Istart, lid = 0; grow < Iend; grow++, lid++) {
311:       const PetscInt *idx;
312:       PetscCall(MatGetRow(Rtot, grow, &ncols, &idx, NULL));
313:       for (int jj = 0; jj < ncols; jj++) {
314:         PetscInt gcol = idx[jj];
315:         PetscCall(PetscCDAppendID(agg_lists, lid, gcol)); // local row, global column
316:       }
317:       PetscCall(MatRestoreRow(Rtot, grow, &ncols, &idx, NULL));
318:     }
319:     PetscCall(MatDestroy(&Rtot));

321:     /* make fake matrix, get largest */
322:     for (int lid = 0; lid < nloc; lid++) {
323:       PetscCall(PetscCDSizeAt(agg_lists, lid, &jj));
324:       if (jj > max_osz) max_osz = jj;
325:     }
326:     PetscCall(MatGetSize(Gmat, &MM, &NN));
327:     if (max_osz > MM - nloc) max_osz = MM - nloc;
328:     PetscCall(MatGetOwnershipRange(Gmat, &Istart, NULL));
329:     PetscCall(MatCreateAIJ(comm, nloc, nloc, PETSC_DETERMINE, PETSC_DETERMINE, 0, NULL, max_osz, NULL, &mat));
330:     for (PetscInt lid = 0, gidi = Istart; lid < nloc; lid++, gidi++) {
331:       PetscCDIntNd *pos;
332:       PetscCall(PetscCDGetHeadPos(agg_lists, lid, &pos));
333:       while (pos) {
334:         PetscInt gidj;
335:         PetscCall(PetscCDIntNdGetID(pos, &gidj));
336:         PetscCall(PetscCDGetNextPos(agg_lists, lid, &pos));
337:         if (gidj < Istart || gidj >= Istart + nloc) PetscCall(MatSetValues(mat, 1, &gidi, 1, &gidj, &one, ADD_VALUES));
338:       }
339:     }
340:     PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
341:     PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
342:     PetscCall(PetscCDSetMat(agg_lists, mat));
343:   }

345:   PetscFunctionReturn(PETSC_SUCCESS);
346: }

348: /*
349:    Distance k MIS. k is in 'subctx'
350: */
351: static PetscErrorCode MatCoarsenApply_MISK(MatCoarsen coarse)
352: {
353:   Mat      mat = coarse->graph;
354:   PetscInt k;

356:   PetscFunctionBegin;
357:   PetscCall(MatCoarsenMISKGetDistance(coarse, &k));
358:   PetscCheck(k > 0, PETSC_COMM_SELF, PETSC_ERR_SUP, "too few levels: %d", (int)k);
359:   if (!coarse->perm) {
360:     IS       perm;
361:     PetscInt n, m;

363:     PetscCall(MatGetLocalSize(mat, &m, &n));
364:     PetscCall(ISCreateStride(PetscObjectComm((PetscObject)mat), m, 0, 1, &perm));
365:     PetscCall(MatCoarsenApply_MISK_private(perm, (PetscInt)k, mat, &coarse->agg_lists));
366:     PetscCall(ISDestroy(&perm));
367:   } else {
368:     PetscCall(MatCoarsenApply_MISK_private(coarse->perm, (PetscInt)k, mat, &coarse->agg_lists));
369:   }
370:   PetscFunctionReturn(PETSC_SUCCESS);
371: }

373: static PetscErrorCode MatCoarsenView_MISK(MatCoarsen coarse, PetscViewer viewer)
374: {
375:   PetscMPIInt rank;
376:   PetscBool   iascii;

378:   PetscFunctionBegin;
379:   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)coarse), &rank));
380:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
381:   if (iascii) {
382:     PetscCall(PetscViewerASCIIPushSynchronized(viewer));
383:     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "  [%d] MISK aggregator\n", rank));
384:     if (!rank) PetscCall(PetscCoarsenDataView_private(coarse->agg_lists, viewer));
385:     PetscCall(PetscViewerFlush(viewer));
386:     PetscCall(PetscViewerASCIIPopSynchronized(viewer));
387:   }
388:   PetscFunctionReturn(PETSC_SUCCESS);
389: }

391: static PetscErrorCode MatCoarsenSetFromOptions_MISK(MatCoarsen coarse, PetscOptionItems *PetscOptionsObject)
392: {
393:   PetscInt  k = 1;
394:   PetscBool flg;
395:   PetscFunctionBegin;
396:   PetscOptionsHeadBegin(PetscOptionsObject, "MatCoarsen-MISk options");
397:   PetscCall(PetscOptionsInt("-mat_coarsen_misk_distance", "k distance for MIS", "", k, &k, &flg));
398:   if (flg) coarse->subctx = (void *)(size_t)k;
399:   PetscOptionsHeadEnd();
400:   PetscFunctionReturn(PETSC_SUCCESS);
401: }

403: /*MC
404:    MATCOARSENMISK - A coarsener that uses MISK, a simple greedy coarsener

406:    Level: beginner

408:    Options Database Key:
409: .   -mat_coarsen_misk_distance <k> - distance for MIS

411: .seealso: `MatCoarsen`, `MatCoarsenMISKSetDistance()`, `MatCoarsenApply()`, `MatCoarsenSetType()`, `MatCoarsenType`, `MatCoarsenCreate()`
412: M*/

414: PETSC_EXTERN PetscErrorCode MatCoarsenCreate_MISK(MatCoarsen coarse)
415: {
416:   PetscFunctionBegin;
417:   coarse->ops->apply          = MatCoarsenApply_MISK;
418:   coarse->ops->view           = MatCoarsenView_MISK;
419:   coarse->subctx              = (void *)(size_t)1;
420:   coarse->ops->setfromoptions = MatCoarsenSetFromOptions_MISK;
421:   PetscFunctionReturn(PETSC_SUCCESS);
422: }

424: /*@
425:   MatCoarsenMISKSetDistance - the distance to be used by MISK

427:   Collective

429:   Input Parameters:
430: + crs - the coarsen
431: - k   - the distance

433:   Options Database Key:
434: . -mat_coarsen_misk_distance <k> - distance for MIS

436:   Level: advanced

438: .seealso: `MATCOARSENMISK`, `MatCoarsen`, `MatCoarseSetFromOptions()`, `MatCoarsenSetType()`, `MatCoarsenRegister()`, `MatCoarsenCreate()`,
439:           `MatCoarsenDestroy()`, `MatCoarsenSetAdjacency()`, `MatCoarsenMISKGetDistance()`
440:           `MatCoarsenGetData()`
441: @*/
442: PetscErrorCode MatCoarsenMISKSetDistance(MatCoarsen crs, PetscInt k)
443: {
444:   PetscFunctionBegin;
445:   crs->subctx = (void *)(size_t)k;
446:   PetscFunctionReturn(PETSC_SUCCESS);
447: }

449: /*@
450:   MatCoarsenMISKGetDistance - gets the distance to be used by MISK

452:   Collective

454:   Input Parameter:
455: . crs - the coarsen

457:   Output Parameter:
458: . k - the distance

460:   Level: advanced

462: .seealso: `MATCOARSENMISK`, `MatCoarsen`, `MatCoarseSetFromOptions()`, `MatCoarsenSetType()`,
463: `MatCoarsenRegister()`, `MatCoarsenCreate()`, `MatCoarsenDestroy()`,
464: `MatCoarsenSetAdjacency()`, `MatCoarsenGetData()`
465: @*/
466: PetscErrorCode MatCoarsenMISKGetDistance(MatCoarsen crs, PetscInt *k)
467: {
468:   PetscFunctionBegin;
469:   *k = (PetscInt)(size_t)crs->subctx;
470:   PetscFunctionReturn(PETSC_SUCCESS);
471: }