Actual source code: matcoloring.c

petsc-3.6.4 2016-04-12
Report Typos and Errors
  1: #include <petsc/private/matimpl.h>      /*I "petscmat.h"  I*/

  3: PetscFunctionList MatColoringList              = 0;
  4: PetscBool         MatColoringRegisterAllCalled = PETSC_FALSE;
  5: const char *const MatColoringWeightTypes[] = {"RANDOM","LEXICAL","LF","SL","MatColoringWeightType","MAT_COLORING_WEIGHT_",0};

  7: PETSC_EXTERN PetscErrorCode MatColoringTestValid(MatColoring,ISColoring);

 11: /*@C
 12:    MatColoringRegister - Adds a new sparse matrix coloring to the  matrix package.

 14:    Not Collective

 16:    Input Parameters:
 17: +  sname - name of Coloring (for example MATCOLORINGSL)
 18: -  function - function pointer that creates the coloring

 20:    Level: developer

 22:    Sample usage:
 23: .vb
 24:    MatColoringRegister("my_color",MyColor);
 25: .ve

 27:    Then, your partitioner can be chosen with the procedural interface via
 28: $     MatColoringSetType(part,"my_color")
 29:    or at runtime via the option
 30: $     -mat_coloring_type my_color

 32: .keywords: matrix, Coloring, register

 34: .seealso: MatColoringRegisterDestroy(), MatColoringRegisterAll()
 35: @*/
 36: PetscErrorCode  MatColoringRegister(const char sname[],PetscErrorCode (*function)(MatColoring))
 37: {

 41:   PetscFunctionListAdd(&MatColoringList,sname,function);
 42:   return(0);
 43: }

 47: /*@
 48:    MatColoringCreate - Creates a matrix coloring context.

 50:    Collective on MatColoring

 52:    Input Parameters:
 53: .  comm - MPI communicator

 55:    Output Parameter:
 56: .  mcptr - the new MatColoring context

 58:    Options Database Keys:
 59: +   -mat_coloring_type - the type of coloring algorithm used
 60: .   -mat_coloring_maxcolors - the maximum number of relevant colors, all nodes not in a color are in maxcolors+1
 61: .   -mat_coloring_distance - compute a distance 1,2,... coloring.
 62: .   -mat_coloring_view - print information about the coloring and the produced index sets
 63: -   -mat_coloring_valid - debugging option that prints all coloring incompatibilities

 65:    Level: beginner

 67: .keywords: Coloring, Matrix

 69: .seealso: MatColoring, MatColoringApply()
 70: @*/
 71: PetscErrorCode MatColoringCreate(Mat m,MatColoring *mcptr)
 72: {
 73:   MatColoring    mc;

 79:   *mcptr = NULL;

 81: #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
 82:   MatInitializePackage();
 83: #endif
 84:   PetscHeaderCreate(mc, MAT_COLORING_CLASSID,"MatColoring","Matrix coloring", "MatColoring",PetscObjectComm((PetscObject)m),MatColoringDestroy, MatColoringView);
 85:   PetscObjectReference((PetscObject)m);
 86:   mc->mat       = m;
 87:   mc->dist      = 2; /* default to Jacobian computation case */
 88:   mc->maxcolors = IS_COLORING_MAX;
 89:   *mcptr        = mc;
 90:   mc->valid     = PETSC_FALSE;
 91:   mc->weight_type = MAT_COLORING_WEIGHT_RANDOM;
 92:   mc->user_weights = NULL;
 93:   mc->user_lperm = NULL;
 94:   return(0);
 95: }


100: /*@
101:    MatColoringDestroy - Destroys the matrix coloring context

103:    Collective on MatColoring

105:    Input Parameter:
106: .  mc - the MatColoring context

108:    Level: beginner

110: .keywords: Coloring, destroy

112: .seealso: MatColoringCreate(), MatColoringApply()
113: @*/
114: PetscErrorCode MatColoringDestroy(MatColoring *mc)
115: {

119:   if (--((PetscObject)(*mc))->refct > 0) {*mc = 0; return(0);}
120:   MatDestroy(&(*mc)->mat);
121:   if ((*mc)->ops->destroy) {(*((*mc)->ops->destroy))(*mc);}
122:   if ((*mc)->user_weights) {PetscFree((*mc)->user_weights);}
123:   if ((*mc)->user_lperm) {PetscFree((*mc)->user_lperm);}
124:   PetscHeaderDestroy(mc);
125:   return(0);
126: }

130: /*@C
131:    MatColoringSetType - Sets the type of coloring algorithm used

133:    Collective on MatColoring

135:    Input Parameter:
136: +  mc - the MatColoring context
137: -  type - the type of coloring

139:    Level: beginner

141:    Notes:  Possible types include the sequential types MATCOLORINGLF,
142:    MATCOLORINGSL, and MATCOLORINGID from the MINPACK package as well
143:    as a parallel MATCOLORINGMIS algorithm.

145: .keywords: Coloring, type

147: .seealso: MatColoringCreate(), MatColoringApply()
148: @*/
149: PetscErrorCode MatColoringSetType(MatColoring mc,MatColoringType type)
150: {
151:   PetscBool      match;
152:   PetscErrorCode ierr,(*r)(MatColoring);

157:   PetscObjectTypeCompare((PetscObject)mc,type,&match);
158:   if (match) return(0);
159:    PetscFunctionListFind(MatColoringList,type,&r);
160:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested MatColoring type %s",type);
161:   if (mc->ops->destroy) {
162:     (*(mc)->ops->destroy)(mc);
163:     mc->ops->destroy = NULL;
164:   }
165:   mc->ops->apply            = 0;
166:   mc->ops->view             = 0;
167:   mc->ops->setfromoptions   = 0;
168:   mc->ops->destroy          = 0;

170:   PetscObjectChangeTypeName((PetscObject)mc,type);
171:   (*r)(mc);
172:   return(0);
173: }

177: /*@
178:    MatColoringSetFromOptions - Sets MatColoring options from user parameters

180:    Collective on MatColoring

182:    Input Parameters:
183: .  mc - MatColoring context

185:    Options Database Keys:
186: +   -mat_coloring_type - the type of coloring algorithm used
187: .   -mat_coloring_maxcolors - the maximum number of relevant colors, all nodes not in a color are in maxcolors+1
188: .   -mat_coloring_distance - compute a distance 1,2,... coloring.
189: .   -mat_coloring_view - print information about the coloring and the produced index sets

191:    Level: beginner

193: .keywords: Coloring, Matrix

195: .seealso: MatColoring, MatColoringApply()
196: @*/
197: PetscErrorCode MatColoringSetFromOptions(MatColoring mc)
198: {
199:   PetscBool      flg;
200:   MatColoringType deft        = MATCOLORINGSL;
201:   char           type[256];
203:   PetscInt       dist,maxcolors;

207:   MatColoringGetDistance(mc,&dist);
208:   MatColoringGetMaxColors(mc,&maxcolors);
209:   MatColoringRegisterAll();
210:   PetscObjectOptionsBegin((PetscObject)mc);
211:   if (((PetscObject)mc)->type_name) deft = ((PetscObject)mc)->type_name;
212:   PetscOptionsFList("-mat_coloring_type","The coloring method used","MatColoringSetType",MatColoringList,deft,type,256,&flg);
213:   if (flg) {
214:     MatColoringSetType(mc,type);
215:   } else if (!((PetscObject)mc)->type_name) {
216:     MatColoringSetType(mc,deft);
217:   }
218:   PetscOptionsInt("-mat_coloring_distance","Distance of the coloring","MatColoringSetDistance",dist,&dist,&flg);
219:   if (flg) {MatColoringSetDistance(mc,dist);}
220:   PetscOptionsInt("-mat_coloring_maxcolors","Maximum colors returned at the end. 1 returns an independent set","MatColoringSetMaxColors",maxcolors,&maxcolors,&flg);
221:   if (flg) {MatColoringSetMaxColors(mc,maxcolors);}
222:   if (mc->ops->setfromoptions) {
223:     (*mc->ops->setfromoptions)(PetscOptionsObject,mc);
224:   }
225:   PetscOptionsBool("-mat_coloring_valid","Check that a valid coloring has been produced","",mc->valid,&mc->valid,NULL);
226:   PetscOptionsEnum("-mat_coloring_weight_type","Sets the type of vertex weighting used","MatColoringSetWeightType",MatColoringWeightTypes,(PetscEnum)mc->weight_type,(PetscEnum*)&mc->weight_type,NULL);
227:   PetscObjectProcessOptionsHandlers((PetscObject)mc);
228:   PetscOptionsEnd();
229:   return(0);
230: }

234: /*@
235:    MatColoringSetDistance - Sets the distance of the coloring

237:    Logically Collective on MatColoring

239:    Input Parameter:
240: .  mc - the MatColoring context
241: .  dist - the distance the coloring should compute

243:    Level: beginner

245:    Notes: The distance of the coloring denotes the minimum number
246:    of edges in the graph induced by the matrix any two vertices
247:    of the same color are.  Distance-1 colorings are the classical
248:    coloring, where no two vertices of the same color are adjacent.
249:    distance-2 colorings are useful for the computation of Jacobians.

251: .keywords: Coloring, distance, Jacobian

253: .seealso: MatColoringGetDistance(), MatColoringApply()
254: @*/
255: PetscErrorCode MatColoringSetDistance(MatColoring mc,PetscInt dist)
256: {
259:   mc->dist = dist;
260:   return(0);
261: }

265: /*@
266:    MatColoringGetDistance - Gets the distance of the coloring

268:    Logically Collective on MatColoring

270:    Input Parameter:
271: .  mc - the MatColoring context

273:    Output Paramter:
274: .  dist - the current distance being used for the coloring.

276:    Level: beginner

278: .keywords: Coloring, distance

280: .seealso: MatColoringSetDistance(), MatColoringApply()
281: @*/
282: PetscErrorCode MatColoringGetDistance(MatColoring mc,PetscInt *dist)
283: {
286:   if (dist) *dist = mc->dist;
287:   return(0);
288: }

292: /*@
293:    MatColoringSetMaxColors - Sets the maximum number of colors

295:    Logically Collective on MatColoring

297:    Input Parameter:
298: +  mc - the MatColoring context
299: -  maxcolors - the maximum number of colors to produce

301:    Level: beginner

303:    Notes:  This may be used to compute a certain number of
304:    independent sets from the graph.  For instance, while using
305:    MATCOLORINGMIS and maxcolors = 1, one gets out an MIS.  Vertices
306:    not in a color are set to have color maxcolors+1, which is not
307:    a valid color as they may be adjacent.

309: .keywords: Coloring

311: .seealso: MatColoringGetMaxColors(), MatColoringApply()
312: @*/
313: PetscErrorCode MatColoringSetMaxColors(MatColoring mc,PetscInt maxcolors)
314: {
317:   mc->maxcolors = maxcolors;
318:   return(0);
319: }

323: /*@
324:    MatColoringGetMaxColors - Gets the maximum number of colors

326:    Logically Collective on MatColoring

328:    Input Parameter:
329: .  mc - the MatColoring context

331:    Output Paramter:
332: .  maxcolors - the current maximum number of colors to produce

334:    Level: beginner

336: .keywords: Coloring

338: .seealso: MatColoringSetMaxColors(), MatColoringApply()
339: @*/
340: PetscErrorCode MatColoringGetMaxColors(MatColoring mc,PetscInt *maxcolors)
341: {
344:   if (maxcolors) *maxcolors = mc->maxcolors;
345:   return(0);
346: }

350: /*@
351:    MatColoringApply - Apply the coloring to the matrix, producing index
352:    sets corresponding to a number of independent sets in the induced
353:    graph.

355:    Collective on MatColoring

357:    Input Parameters:
358: .  mc - the MatColoring context

360:    Output Parameter:
361: .  coloring - the ISColoring instance containing the coloring

363:    Level: beginner

365: .keywords: Coloring, Apply

367: .seealso: MatColoring, MatColoringCreate()
368: @*/
369: PetscErrorCode MatColoringApply(MatColoring mc,ISColoring *coloring)
370: {
371:   PetscErrorCode    ierr;
372:   PetscBool         flg;
373:   PetscViewerFormat format;
374:   PetscViewer       viewer;
375:   PetscInt          nc,ncolors;

379:   PetscLogEventBegin(Mat_Coloring_Apply,mc,0,0,0);
380:   (*mc->ops->apply)(mc,coloring);
381:   PetscLogEventEnd(Mat_Coloring_Apply,mc,0,0,0);
382:   /* valid */
383:   if (mc->valid) {
384:     MatColoringTestValid(mc,*coloring);
385:   }
386:   /* view */
387:   PetscOptionsGetViewer(PetscObjectComm((PetscObject)mc),((PetscObject)mc)->prefix,"-mat_coloring_view",&viewer,&format,&flg);
388:   if (flg && !PetscPreLoadingOn) {
389:     PetscViewerPushFormat(viewer,format);
390:     MatColoringView(mc,viewer);
391:     MatGetSize(mc->mat,NULL,&nc);
392:     ISColoringGetIS(*coloring,&ncolors,NULL);
393:     PetscViewerASCIIPrintf(viewer,"  Number of colors %d\n",ncolors);
394:     PetscViewerASCIIPrintf(viewer,"  Number of total columns %d\n",nc);
395:     if (nc <= 1000) {ISColoringView(*coloring,viewer);}
396:     PetscViewerPopFormat(viewer);
397:     PetscViewerDestroy(&viewer);
398:   }
399:   return(0);
400: }

404: /*@
405:    MatColoringView - Output details about the MatColoring.

407:    Collective on MatColoring

409:    Input Parameters:
410: -  mc - the MatColoring context
411: +  viewer - the Viewer context

413:    Level: beginner

415: .keywords: Coloring, view

417: .seealso: MatColoring, MatColoringApply()
418: @*/
419: PetscErrorCode MatColoringView(MatColoring mc,PetscViewer viewer)
420: {
422:   PetscBool      iascii;

426:   if (!viewer) {
427:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)mc),&viewer);
428:   }

432:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
433:   if (iascii) {
434:     PetscObjectPrintClassNamePrefixType((PetscObject)mc,viewer);
435:     PetscViewerASCIIPrintf(viewer,"  Weight type: %s\n",MatColoringWeightTypes[mc->weight_type]);
436:     if (mc->maxcolors > 0) {
437:       PetscViewerASCIIPrintf(viewer,"  Distance %d, Max. Colors %d\n",mc->dist,mc->maxcolors);
438:     } else {
439:       PetscViewerASCIIPrintf(viewer,"  Distance %d\n",mc->dist);
440:     }
441:   }
442:   return(0);
443: }

445: /*@
446:    MatColoringSetWeightType - Set the type of weight computation used.

448:    Logically collective on MatColoring

450:    Input Parameters:
451: -  mc - the MatColoring context
452: +  wt - the weight type

454:    Level: beginner

456: .keywords: Coloring, view

458: .seealso: MatColoring, MatColoringWeightType
459: @*/
462: PetscErrorCode MatColoringSetWeightType(MatColoring mc,MatColoringWeightType wt)
463: {
465:   mc->weight_type = wt;
466:   return(0);

468: }