Actual source code: partition.c
1: #define PETSCMAT_DLL
3: #include include/private/matimpl.h
5: /* Logging support */
6: PetscCookie MAT_PARTITIONING_COOKIE = 0;
8: /*
9: Simplest partitioning, keeps the current partitioning.
10: */
13: static PetscErrorCode MatPartitioningApply_Current(MatPartitioning part,IS *partitioning)
14: {
16: PetscInt m;
17: PetscMPIInt rank,size;
20: MPI_Comm_size(part->comm,&size);
21: if (part->n != size) {
22: SETERRQ(PETSC_ERR_SUP,"This is the DEFAULT NO-OP partitioner, it currently only supports one domain per processor\nuse -matpartitioning_type parmetis or chaco or scotch for more than one subdomain per processor");
23: }
24: MPI_Comm_rank(part->comm,&rank);
26: MatGetLocalSize(part->adj,&m,PETSC_NULL);
27: ISCreateStride(part->comm,m,rank,0,partitioning);
28: return(0);
29: }
33: static PetscErrorCode MatPartitioningApply_Square(MatPartitioning part,IS *partitioning)
34: {
36: PetscInt cell,n,N,p,rstart,rend,*color;
37: PetscMPIInt size;
40: MPI_Comm_size(part->comm,&size);
41: if (part->n != size) {
42: SETERRQ(PETSC_ERR_SUP,"Currently only supports one domain per processor");
43: }
44: p = (PetscInt)sqrt((double)part->n);
45: if (p*p != part->n) {
46: SETERRQ(PETSC_ERR_SUP,"Square partitioning requires \"perfect square\" number of domains");
47: }
48: MatGetSize(part->adj,&N,PETSC_NULL);
49: n = (PetscInt)sqrt((double)N);
50: if (n*n != N) { /* This condition is NECESSARY, but NOT SUFFICIENT in order to the domain be square */
51: SETERRQ(PETSC_ERR_SUP,"Square partitioning requires square domain");
52: }
53: if (n%p != 0) {
54: SETERRQ(PETSC_ERR_SUP,"Square partitioning requires p to divide n");
55: }
56: MatGetOwnershipRange(part->adj,&rstart,&rend);
57: PetscMalloc((rend-rstart)*sizeof(PetscInt),&color);
58: /* for (int cell=rstart; cell<rend; cell++) { color[cell-rstart] = ((cell%n) < (n/2)) + 2 * ((cell/n) < (n/2)); } */
59: for (cell=rstart; cell<rend; cell++) {
60: color[cell-rstart] = ((cell%n) / (n/p)) + p * ((cell/n) / (n/p));
61: }
62: ISCreateGeneral(part->comm,rend-rstart,color,partitioning);
63: PetscFree(color);
65: return(0);
66: }
71: PetscErrorCode MatPartitioningCreate_Current(MatPartitioning part)
72: {
74: part->ops->apply = MatPartitioningApply_Current;
75: part->ops->view = 0;
76: part->ops->destroy = 0;
77: return(0);
78: }
84: PetscErrorCode MatPartitioningCreate_Square(MatPartitioning part)
85: {
87: part->ops->apply = MatPartitioningApply_Square;
88: part->ops->view = 0;
89: part->ops->destroy = 0;
90: return(0);
91: }
94: /* ===========================================================================================*/
96: #include petscsys.h
98: PetscFList MatPartitioningList = 0;
99: PetscTruth MatPartitioningRegisterAllCalled = PETSC_FALSE;
104: PetscErrorCode MatPartitioningRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(MatPartitioning))
105: {
107: char fullname[PETSC_MAX_PATH_LEN];
110: PetscFListConcat(path,name,fullname);
111: PetscFListAdd(&MatPartitioningList,sname,fullname,(void (*)(void))function);
112: return(0);
113: }
117: /*@C
118: MatPartitioningRegisterDestroy - Frees the list of partitioning routines.
120: Not Collective
122: Level: developer
124: .keywords: matrix, register, destroy
126: .seealso: MatPartitioningRegisterDynamic(), MatPartitioningRegisterAll()
127: @*/
128: PetscErrorCode MatPartitioningRegisterDestroy(void)
129: {
133: if (MatPartitioningList) {
134: PetscFListDestroy(&MatPartitioningList);
135: MatPartitioningList = 0;
136: }
137: return(0);
138: }
142: /*@C
143: MatPartitioningGetType - Gets the Partitioning method type and name (as a string)
144: from the partitioning context.
146: Not collective
148: Input Parameter:
149: . partitioning - the partitioning context
151: Output Parameter:
152: . type - partitioner type
154: Level: intermediate
156: Not Collective
158: .keywords: Partitioning, get, method, name, type
159: @*/
160: PetscErrorCode MatPartitioningGetType(MatPartitioning partitioning,MatPartitioningType *type)
161: {
163: *type = partitioning->type_name;
164: return(0);
165: }
169: /*@C
170: MatPartitioningSetNParts - Set how many partitions need to be created;
171: by default this is one per processor. Certain partitioning schemes may
172: in fact only support that option.
174: Not collective
176: Input Parameter:
177: . partitioning - the partitioning context
178: . n - the number of partitions
180: Level: intermediate
182: Not Collective
184: .keywords: Partitioning, set
186: .seealso: MatPartitioningCreate(), MatPartitioningApply()
187: @*/
188: PetscErrorCode MatPartitioningSetNParts(MatPartitioning part,PetscInt n)
189: {
191: part->n = n;
192: return(0);
193: }
197: /*@
198: MatPartitioningApply - Gets a partitioning for a matrix.
200: Collective on Mat
202: Input Parameters:
203: . matp - the matrix partitioning object
205: Output Parameters:
206: . partitioning - the partitioning. For each local node this tells the processor
207: number that that node is assigned to.
209: Options Database Keys:
210: To specify the partitioning through the options database, use one of
211: the following
212: $ -mat_partitioning_type parmetis, -mat_partitioning current
213: To see the partitioning result
214: $ -mat_partitioning_view
216: Level: beginner
218: The user can define additional partitionings; see MatPartitioningRegisterDynamic().
220: .keywords: matrix, get, partitioning
222: .seealso: MatPartitioningRegisterDynamic(), MatPartitioningCreate(),
223: MatPartitioningDestroy(), MatPartitioningSetAdjacency(), ISPartitioningToNumbering(),
224: ISPartitioningCount()
225: @*/
226: PetscErrorCode MatPartitioningApply(MatPartitioning matp,IS *partitioning)
227: {
229: PetscTruth flag;
234: if (!matp->adj->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
235: if (matp->adj->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
236: if (!matp->ops->apply) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set type with MatPartitioningSetFromOptions() or MatPartitioningSetType()");
238: (*matp->ops->apply)(matp,partitioning);
241: PetscOptionsHasName(PETSC_NULL,"-mat_partitioning_view",&flag);
242: if (flag) {
243: PetscViewer viewer;
244: PetscViewerASCIIGetStdout(matp->comm,&viewer);
245: MatPartitioningView(matp,viewer);
246: ISView(*partitioning,viewer);
247: }
248: return(0);
249: }
250:
253: /*@
254: MatPartitioningSetAdjacency - Sets the adjacency graph (matrix) of the thing to be
255: partitioned.
257: Collective on MatPartitioning and Mat
259: Input Parameters:
260: + part - the partitioning context
261: - adj - the adjacency matrix
263: Level: beginner
265: .keywords: Partitioning, adjacency
267: .seealso: MatPartitioningCreate()
268: @*/
269: PetscErrorCode MatPartitioningSetAdjacency(MatPartitioning part,Mat adj)
270: {
274: part->adj = adj;
275: return(0);
276: }
280: /*@
281: MatPartitioningDestroy - Destroys the partitioning context.
283: Collective on Partitioning
285: Input Parameters:
286: . part - the partitioning context
288: Level: beginner
290: .keywords: Partitioning, destroy, context
292: .seealso: MatPartitioningCreate()
293: @*/
294: PetscErrorCode MatPartitioningDestroy(MatPartitioning part)
295: {
300: if (--part->refct > 0) return(0);
302: if (part->ops->destroy) {
303: (*part->ops->destroy)(part);
304: }
305: PetscFree(part->vertex_weights);
306: PetscFree(part->part_weights);
307: PetscHeaderDestroy(part);
308: return(0);
309: }
313: /*@C
314: MatPartitioningSetVertexWeights - Sets the weights for vertices for a partitioning.
316: Collective on Partitioning
318: Input Parameters:
319: + part - the partitioning context
320: - weights - the weights
322: Level: beginner
324: Notes:
325: The array weights is freed by PETSc so the user should not free the array. In C/C++
326: the array must be obtained with a call to PetscMalloc(), not malloc().
328: .keywords: Partitioning, destroy, context
330: .seealso: MatPartitioningCreate(), MatPartitioningSetType(), MatPartitioningSetPartitionWeights()
331: @*/
332: PetscErrorCode MatPartitioningSetVertexWeights(MatPartitioning part,const PetscInt weights[])
333: {
339: PetscFree(part->vertex_weights);
340: part->vertex_weights = (PetscInt*)weights;
341: return(0);
342: }
346: /*@C
347: MatPartitioningSetPartitionWeights - Sets the weights for each partition.
349: Collective on Partitioning
351: Input Parameters:
352: + part - the partitioning context
353: - weights - the weights
355: Level: beginner
357: Notes:
358: The array weights is freed by PETSc so the user should not free the array. In C/C++
359: the array must be obtained with a call to PetscMalloc(), not malloc().
361: .keywords: Partitioning, destroy, context
363: .seealso: MatPartitioningCreate(), MatPartitioningSetType(), MatPartitioningSetVertexWeights()
364: @*/
365: PetscErrorCode MatPartitioningSetPartitionWeights(MatPartitioning part,const PetscReal weights[])
366: {
372: PetscFree(part->part_weights);
373: part->part_weights = (PetscReal*)weights;
374: return(0);
375: }
379: /*@
380: MatPartitioningCreate - Creates a partitioning context.
382: Collective on MPI_Comm
384: Input Parameter:
385: . comm - MPI communicator
387: Output Parameter:
388: . newp - location to put the context
390: Level: beginner
392: .keywords: Partitioning, create, context
394: .seealso: MatPartitioningSetType(), MatPartitioningApply(), MatPartitioningDestroy(),
395: MatPartitioningSetAdjacency()
397: @*/
398: PetscErrorCode MatPartitioningCreate(MPI_Comm comm,MatPartitioning *newp)
399: {
400: MatPartitioning part;
401: PetscErrorCode ierr;
402: PetscMPIInt size;
405: *newp = 0;
407: PetscHeaderCreate(part,_p_MatPartitioning,struct _MatPartitioningOps,MAT_PARTITIONING_COOKIE,-1,"MatPartitioning",comm,MatPartitioningDestroy,
408: MatPartitioningView);
409: part->type = -1;
410: part->vertex_weights = PETSC_NULL;
411: part->part_weights = PETSC_NULL;
412: MPI_Comm_size(comm,&size);
413: part->n = (PetscInt)size;
415: *newp = part;
416: return(0);
417: }
421: /*@C
422: MatPartitioningView - Prints the partitioning data structure.
424: Collective on MatPartitioning
426: Input Parameters:
427: . part - the partitioning context
428: . viewer - optional visualization context
430: Level: intermediate
432: Note:
433: The available visualization contexts include
434: + PETSC_VIEWER_STDOUT_SELF - standard output (default)
435: - PETSC_VIEWER_STDOUT_WORLD - synchronized standard
436: output where only the first processor opens
437: the file. All other processors send their
438: data to the first processor to print.
440: The user can open alternative visualization contexts with
441: . PetscViewerASCIIOpen() - output to a specified file
443: .keywords: Partitioning, view
445: .seealso: PetscViewerASCIIOpen()
446: @*/
447: PetscErrorCode MatPartitioningView(MatPartitioning part,PetscViewer viewer)
448: {
449: PetscErrorCode ierr;
450: PetscTruth iascii;
451: MatPartitioningType name;
455: if (!viewer) {
456: PetscViewerASCIIGetStdout(part->comm,&viewer);
457: }
461: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
462: if (iascii) {
463: MatPartitioningGetType(part,&name);
464: PetscViewerASCIIPrintf(viewer,"MatPartitioning Object: %s\n",name);
465: if (part->vertex_weights) {
466: PetscViewerASCIIPrintf(viewer," Using vertex weights\n");
467: }
468: } else {
469: SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for this MatParitioning",((PetscObject)viewer)->type_name);
470: }
472: if (part->ops->view) {
473: PetscViewerASCIIPushTab(viewer);
474: (*part->ops->view)(part,viewer);
475: PetscViewerASCIIPopTab(viewer);
476: }
478: return(0);
479: }
483: /*@C
484: MatPartitioningSetType - Sets the type of partitioner to use
486: Collective on MatPartitioning
488: Input Parameter:
489: . part - the partitioning context.
490: . type - a known method
492: Options Database Command:
493: $ -mat_partitioning_type <type>
494: $ Use -help for a list of available methods
495: $ (for instance, parmetis)
497: Level: intermediate
499: .keywords: partitioning, set, method, type
501: .seealso: MatPartitioningCreate(), MatPartitioningApply(), MatPartitioningType
503: @*/
504: PetscErrorCode MatPartitioningSetType(MatPartitioning part,const MatPartitioningType type)
505: {
506: PetscErrorCode ierr,(*r)(MatPartitioning);
507: PetscTruth match;
513: PetscTypeCompare((PetscObject)part,type,&match);
514: if (match) return(0);
516: if (part->setupcalled) {
517: (*part->ops->destroy)(part);
518: part->data = 0;
519: part->setupcalled = 0;
520: }
522: /* Get the function pointers for the method requested */
523: if (!MatPartitioningRegisterAllCalled){ MatPartitioningRegisterAll(0);}
524: PetscFListFind(part->comm,MatPartitioningList,type,(void (**)(void)) &r);
526: if (!r) {SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown partitioning type %s",type);}
528: part->ops->destroy = (PetscErrorCode (*)(MatPartitioning)) 0;
529: part->ops->view = (PetscErrorCode (*)(MatPartitioning,PetscViewer)) 0;
530: (*r)(part);
532: PetscStrfree(part->type_name);
533: PetscStrallocpy(type,&part->type_name);
534: return(0);
535: }
539: /*@
540: MatPartitioningSetFromOptions - Sets various partitioning options from the
541: options database.
543: Collective on MatPartitioning
545: Input Parameter:
546: . part - the partitioning context.
548: Options Database Command:
549: $ -mat_partitioning_type <type>
550: $ Use -help for a list of available methods
551: $ (for instance, parmetis)
553: Level: beginner
555: .keywords: partitioning, set, method, type
556: @*/
557: PetscErrorCode MatPartitioningSetFromOptions(MatPartitioning part)
558: {
560: PetscTruth flag;
561: char type[256];
562: const char *def;
565: if (!MatPartitioningRegisterAllCalled){ MatPartitioningRegisterAll(0);}
566: PetscOptionsBegin(part->comm,part->prefix,"Partitioning options","MatOrderings");
567: if (!part->type_name) {
568: #if defined(PETSC_HAVE_PARMETIS)
569: def = MAT_PARTITIONING_PARMETIS;
570: #else
571: def = MAT_PARTITIONING_CURRENT;
572: #endif
573: } else {
574: def = part->type_name;
575: }
576: PetscOptionsList("-mat_partitioning_type","Type of partitioner","MatPartitioningSetType",MatPartitioningList,def,type,256,&flag);
577: if (flag) {
578: MatPartitioningSetType(part,type);
579: }
580: /*
581: Set the type if it was never set.
582: */
583: if (!part->type_name) {
584: MatPartitioningSetType(part,def);
585: }
587: if (part->ops->setfromoptions) {
588: (*part->ops->setfromoptions)(part);
589: }
590: PetscOptionsEnd();
591: return(0);
592: }