Actual source code: partshell.c
petsc-master 2020-08-25
1: #include <petsc/private/partitionerimpl.h>
3: typedef struct {
4: PetscSection section; /* Sizes for each partition */
5: IS partition; /* Points in each partition */
6: PetscBool random; /* Flag for a random partition */
7: } PetscPartitioner_Shell;
9: static PetscErrorCode PetscPartitionerReset_Shell(PetscPartitioner part)
10: {
11: PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
12: PetscErrorCode ierr;
15: PetscSectionDestroy(&p->section);
16: ISDestroy(&p->partition);
17: return(0);
18: }
20: static PetscErrorCode PetscPartitionerDestroy_Shell(PetscPartitioner part)
21: {
25: PetscPartitionerReset_Shell(part);
26: PetscFree(part->data);
27: return(0);
28: }
30: static PetscErrorCode PetscPartitionerView_Shell_ASCII(PetscPartitioner part, PetscViewer viewer)
31: {
32: PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
33: PetscErrorCode ierr;
36: if (p->random) {
37: PetscViewerASCIIPushTab(viewer);
38: PetscViewerASCIIPrintf(viewer, "using random partition\n");
39: PetscViewerASCIIPopTab(viewer);
40: }
41: return(0);
42: }
44: static PetscErrorCode PetscPartitionerView_Shell(PetscPartitioner part, PetscViewer viewer)
45: {
46: PetscBool iascii;
52: PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);
53: if (iascii) {PetscPartitionerView_Shell_ASCII(part, viewer);}
54: return(0);
55: }
57: static PetscErrorCode PetscPartitionerSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject, PetscPartitioner part)
58: {
59: PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
60: PetscErrorCode ierr;
63: PetscOptionsHead(PetscOptionsObject, "PetscPartitioner Shell Options");
64: PetscOptionsBool("-petscpartitioner_shell_random", "Use a random partition", "PetscPartitionerView", PETSC_FALSE, &p->random, NULL);
65: PetscOptionsTail();
66: return(0);
67: }
69: static PetscErrorCode PetscPartitionerPartition_Shell(PetscPartitioner part, PetscInt nparts, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection vertSection, PetscSection targetSection, PetscSection partSection, IS *partition)
70: {
71: PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
72: PetscInt np;
73: PetscErrorCode ierr;
76: if (p->random) {
77: PetscRandom r;
78: PetscInt *sizes, *points, v, p;
79: PetscMPIInt rank;
81: MPI_Comm_rank(PetscObjectComm((PetscObject) part), &rank);
82: PetscRandomCreate(PETSC_COMM_SELF, &r);
83: PetscRandomSetInterval(r, 0.0, (PetscScalar) nparts);
84: PetscRandomSetFromOptions(r);
85: PetscCalloc2(nparts, &sizes, numVertices, &points);
86: for (v = 0; v < numVertices; ++v) {points[v] = v;}
87: for (p = 0; p < nparts; ++p) {sizes[p] = numVertices/nparts + (PetscInt) (p < numVertices % nparts);}
88: for (v = numVertices-1; v > 0; --v) {
89: PetscReal val;
90: PetscInt w, tmp;
92: PetscRandomSetInterval(r, 0.0, (PetscScalar) (v+1));
93: PetscRandomGetValueReal(r, &val);
94: w = PetscFloorReal(val);
95: tmp = points[v];
96: points[v] = points[w];
97: points[w] = tmp;
98: }
99: PetscRandomDestroy(&r);
100: PetscPartitionerShellSetPartition(part, nparts, sizes, points);
101: PetscFree2(sizes, points);
102: }
103: if (!p->section) SETERRQ(PetscObjectComm((PetscObject) part), PETSC_ERR_ARG_WRONG, "Shell partitioner information not provided. Please call PetscPartitionerShellSetPartition()");
104: PetscSectionGetChart(p->section, NULL, &np);
105: if (nparts != np) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of requested partitions %d != configured partitions %d", nparts, np);
106: ISGetLocalSize(p->partition, &np);
107: if (numVertices != np) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of input vertices %d != configured vertices %d", numVertices, np);
108: PetscSectionCopy(p->section, partSection);
109: *partition = p->partition;
110: PetscObjectReference((PetscObject) p->partition);
111: return(0);
112: }
114: static PetscErrorCode PetscPartitionerInitialize_Shell(PetscPartitioner part)
115: {
117: part->noGraph = PETSC_TRUE; /* PetscPartitionerShell cannot overload the partition call, so it is safe for now */
118: part->ops->view = PetscPartitionerView_Shell;
119: part->ops->setfromoptions = PetscPartitionerSetFromOptions_Shell;
120: part->ops->reset = PetscPartitionerReset_Shell;
121: part->ops->destroy = PetscPartitionerDestroy_Shell;
122: part->ops->partition = PetscPartitionerPartition_Shell;
123: return(0);
124: }
126: /*MC
127: PETSCPARTITIONERSHELL = "shell" - A PetscPartitioner object
129: Level: intermediate
131: Options Database Keys:
132: . -petscpartitioner_shell_random - Use a random partition
134: .seealso: PetscPartitionerType, PetscPartitionerCreate(), PetscPartitionerSetType()
135: M*/
137: PETSC_EXTERN PetscErrorCode PetscPartitionerCreate_Shell(PetscPartitioner part)
138: {
139: PetscPartitioner_Shell *p;
140: PetscErrorCode ierr;
144: PetscNewLog(part, &p);
145: part->data = p;
147: PetscPartitionerInitialize_Shell(part);
148: p->random = PETSC_FALSE;
149: return(0);
150: }
152: /*@C
153: PetscPartitionerShellSetPartition - Set an artifical partition for a mesh
155: Collective on PetscPartitioner
157: Input Parameters:
158: + part - The PetscPartitioner
159: . size - The number of partitions
160: . sizes - array of length size (or NULL) providing the number of points in each partition
161: - points - array of length sum(sizes) (may be NULL iff sizes is NULL), a permutation of the points that groups those assigned to each partition in order (i.e., partition 0 first, partition 1 next, etc.)
163: Level: developer
165: Notes:
166: It is safe to free the sizes and points arrays after use in this routine.
168: .seealso DMPlexDistribute(), PetscPartitionerCreate()
169: @*/
170: PetscErrorCode PetscPartitionerShellSetPartition(PetscPartitioner part, PetscInt size, const PetscInt sizes[], const PetscInt points[])
171: {
172: PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
173: PetscInt proc, numPoints;
174: PetscErrorCode ierr;
180: PetscSectionDestroy(&p->section);
181: ISDestroy(&p->partition);
182: PetscSectionCreate(PetscObjectComm((PetscObject) part), &p->section);
183: PetscSectionSetChart(p->section, 0, size);
184: if (sizes) {
185: for (proc = 0; proc < size; ++proc) {
186: PetscSectionSetDof(p->section, proc, sizes[proc]);
187: }
188: }
189: PetscSectionSetUp(p->section);
190: PetscSectionGetStorageSize(p->section, &numPoints);
191: ISCreateGeneral(PetscObjectComm((PetscObject) part), numPoints, points, PETSC_COPY_VALUES, &p->partition);
192: return(0);
193: }
195: /*@
196: PetscPartitionerShellSetRandom - Set the flag to use a random partition
198: Collective on PetscPartitioner
200: Input Parameters:
201: + part - The PetscPartitioner
202: - random - The flag to use a random partition
204: Level: intermediate
206: .seealso PetscPartitionerShellGetRandom(), PetscPartitionerCreate()
207: @*/
208: PetscErrorCode PetscPartitionerShellSetRandom(PetscPartitioner part, PetscBool random)
209: {
210: PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
214: p->random = random;
215: return(0);
216: }
218: /*@
219: PetscPartitionerShellGetRandom - get the flag to use a random partition
221: Collective on PetscPartitioner
223: Input Parameter:
224: . part - The PetscPartitioner
226: Output Parameter:
227: . random - The flag to use a random partition
229: Level: intermediate
231: .seealso PetscPartitionerShellSetRandom(), PetscPartitionerCreate()
232: @*/
233: PetscErrorCode PetscPartitionerShellGetRandom(PetscPartitioner part, PetscBool *random)
234: {
235: PetscPartitioner_Shell *p = (PetscPartitioner_Shell *) part->data;
240: *random = p->random;
241: return(0);
242: }