Actual source code: ex24.c

  1: static char help[]     = "Test that MatPartitioning and PetscPartitioner interfaces are equivalent when using PETSCPARTITIONERMATPARTITIONING\n\n";
  2: static char FILENAME[] = "ex24.c";

  4: #include <petscdmplex.h>
  5: #include <petscviewerhdf5.h>

  7: #if PetscDefined(HAVE_PTSCOTCH)
  8: EXTERN_C_BEGIN
  9:   #include <ptscotch.h>
 10: EXTERN_C_END
 11: #endif

 13: typedef struct {
 14:   PetscBool compare_is; /* Compare ISs and PetscSections */
 15:   PetscBool compare_dm; /* Compare DM */
 16:   PetscBool tpw;        /* Use target partition weights */
 17:   char      partitioning[64];
 18:   char      repartitioning[64];
 19: } AppCtx;

 21: static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
 22: {
 23:   PetscBool repartition = PETSC_TRUE;

 25:   PetscFunctionBegin;
 26:   options->compare_is = PETSC_FALSE;
 27:   options->compare_dm = PETSC_FALSE;

 29:   PetscOptionsBegin(comm, "", "Meshing Interpolation Test Options", "DMPLEX");
 30:   PetscCall(PetscOptionsBool("-compare_is", "Compare ISs and PetscSections?", FILENAME, options->compare_is, &options->compare_is, NULL));
 31:   PetscCall(PetscOptionsBool("-compare_dm", "Compare DMs?", FILENAME, options->compare_dm, &options->compare_dm, NULL));
 32:   PetscCall(PetscStrncpy(options->partitioning, MATPARTITIONINGPARMETIS, sizeof(options->partitioning)));
 33:   PetscCall(PetscOptionsString("-partitioning", "The mat partitioning type to test", "None", options->partitioning, options->partitioning, sizeof(options->partitioning), NULL));
 34:   PetscCall(PetscOptionsBool("-repartition", "Partition again after the first partition?", FILENAME, repartition, &repartition, NULL));
 35:   if (repartition) {
 36:     PetscCall(PetscStrncpy(options->repartitioning, MATPARTITIONINGPARMETIS, 64));
 37:     PetscCall(PetscOptionsString("-repartitioning", "The mat partitioning type to test (second partitioning)", "None", options->repartitioning, options->repartitioning, sizeof(options->repartitioning), NULL));
 38:   } else {
 39:     options->repartitioning[0] = '\0';
 40:   }
 41:   PetscCall(PetscOptionsBool("-tpweight", "Use target partition weights", FILENAME, options->tpw, &options->tpw, NULL));
 42:   PetscOptionsEnd();
 43:   PetscFunctionReturn(PETSC_SUCCESS);
 44: }

 46: static PetscErrorCode ScotchResetRandomSeed()
 47: {
 48:   PetscFunctionBegin;
 49: #if PetscDefined(HAVE_PTSCOTCH)
 50:   SCOTCH_randomReset();
 51: #endif
 52:   PetscFunctionReturn(PETSC_SUCCESS);
 53: }

 55: static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm)
 56: {
 57:   PetscFunctionBegin;
 58:   PetscCall(DMCreate(comm, dm));
 59:   PetscCall(DMSetType(*dm, DMPLEX));
 60:   PetscCall(DMPlexDistributeSetDefault(*dm, PETSC_FALSE));
 61:   PetscCall(DMSetFromOptions(*dm));
 62:   PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view"));
 63:   PetscFunctionReturn(PETSC_SUCCESS);
 64: }

 66: int main(int argc, char **argv)
 67: {
 68:   MPI_Comm               comm;
 69:   DM                     dm1, dm2, dmdist1, dmdist2;
 70:   DMPlexInterpolatedFlag interp;
 71:   MatPartitioning        mp;
 72:   PetscPartitioner       part1, part2;
 73:   AppCtx                 user;
 74:   IS                     is1 = NULL, is2 = NULL;
 75:   IS                     is1g, is2g;
 76:   PetscSection           s1 = NULL, s2 = NULL, tpws = NULL;
 77:   PetscBool              flg;
 78:   PetscMPIInt            size;

 80:   PetscFunctionBeginUser;
 81:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 82:   comm = PETSC_COMM_WORLD;
 83:   PetscCallMPI(MPI_Comm_size(comm, &size));
 84:   PetscCall(ProcessOptions(comm, &user));
 85:   PetscCall(CreateMesh(comm, &user, &dm1));
 86:   PetscCall(CreateMesh(comm, &user, &dm2));

 88:   if (user.tpw) {
 89:     PetscBool isscotch;

 91:     PetscCall(PetscSectionCreate(comm, &tpws));
 92:     PetscCall(PetscSectionSetChart(tpws, 0, size));
 93:     for (PetscInt i = 0; i < size; i++) {
 94:       PetscInt tdof = i % 2 ? 2 * i - 1 : i + 2;
 95:       PetscCall(PetscSectionSetDof(tpws, i, tdof));
 96:     }
 97:     // PTScotch cannot have a zero partition weight
 98:     PetscCall(PetscStrncmp(user.partitioning, PETSCPARTITIONERPTSCOTCH, sizeof(PETSCPARTITIONERPTSCOTCH), &isscotch));
 99:     if (!isscotch && size > 1) { /* test zero tpw entry */
100:       PetscCall(PetscSectionSetDof(tpws, 0, 0));
101:     }
102:     PetscCall(PetscSectionSetUp(tpws));
103:   }

105:   /* partition dm1 using PETSCPARTITIONERPARMETIS */
106:   PetscCall(ScotchResetRandomSeed());
107:   PetscCall(DMPlexGetPartitioner(dm1, &part1));
108:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)part1, "p1_"));
109:   PetscCall(PetscPartitionerSetType(part1, user.partitioning));
110:   PetscCall(PetscPartitionerSetFromOptions(part1));
111:   PetscCall(PetscSectionCreate(comm, &s1));
112:   PetscCall(PetscPartitionerDMPlexPartition(part1, dm1, tpws, s1, &is1));

114:   /* partition dm2 using PETSCPARTITIONERMATPARTITIONING with MATPARTITIONINGPARMETIS */
115:   PetscCall(ScotchResetRandomSeed());
116:   PetscCall(DMPlexGetPartitioner(dm2, &part2));
117:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)part2, "p2_"));
118:   PetscCall(PetscPartitionerSetType(part2, PETSCPARTITIONERMATPARTITIONING));
119:   PetscCall(PetscPartitionerMatPartitioningGetMatPartitioning(part2, &mp));
120:   PetscCall(MatPartitioningSetType(mp, user.partitioning));
121:   PetscCall(PetscPartitionerSetFromOptions(part2));
122:   PetscCall(PetscSectionCreate(comm, &s2));
123:   PetscCall(PetscPartitionerDMPlexPartition(part2, dm2, tpws, s2, &is2));

125:   PetscCall(ISOnComm(is1, comm, PETSC_USE_POINTER, &is1g));
126:   PetscCall(ISOnComm(is2, comm, PETSC_USE_POINTER, &is2g));
127:   PetscCall(ISViewFromOptions(is1g, NULL, "-seq_is1_view"));
128:   PetscCall(ISViewFromOptions(is2g, NULL, "-seq_is2_view"));
129:   /* compare the two ISs */
130:   if (user.compare_is) {
131:     PetscCall(ISEqualUnsorted(is1g, is2g, &flg));
132:     if (!flg) PetscCall(PetscPrintf(comm, "ISs are not equal with type %s with size %d.\n", user.partitioning, size));
133:   }
134:   PetscCall(ISDestroy(&is1g));
135:   PetscCall(ISDestroy(&is2g));

137:   /* compare the two PetscSections */
138:   PetscCall(PetscSectionViewFromOptions(s1, NULL, "-seq_s1_view"));
139:   PetscCall(PetscSectionViewFromOptions(s2, NULL, "-seq_s2_view"));
140:   if (user.compare_is) {
141:     PetscCall(PetscSectionCompare(s1, s2, &flg));
142:     if (!flg) PetscCall(PetscPrintf(comm, "PetscSections are not equal with %s with size %d.\n", user.partitioning, size));
143:   }

145:   /* distribute both DMs */
146:   PetscCall(ScotchResetRandomSeed());
147:   PetscCall(DMPlexDistribute(dm1, 0, NULL, &dmdist1));
148:   PetscCall(ScotchResetRandomSeed());
149:   PetscCall(DMPlexDistribute(dm2, 0, NULL, &dmdist2));

151:   /* cleanup */
152:   PetscCall(PetscSectionDestroy(&tpws));
153:   PetscCall(PetscSectionDestroy(&s1));
154:   PetscCall(PetscSectionDestroy(&s2));
155:   PetscCall(ISDestroy(&is1));
156:   PetscCall(ISDestroy(&is2));
157:   PetscCall(DMDestroy(&dm1));
158:   PetscCall(DMDestroy(&dm2));

160:   /* if distributed DMs are NULL (sequential case), then quit */
161:   if (!dmdist1 && !dmdist2) return 0;

163:   PetscCall(DMViewFromOptions(dmdist1, NULL, "-dm_dist1_view"));
164:   PetscCall(DMViewFromOptions(dmdist2, NULL, "-dm_dist2_view"));

166:   /* compare the two distributed DMs */
167:   if (user.compare_dm) {
168:     PetscCall(DMPlexEqual(dmdist1, dmdist2, &flg));
169:     if (!flg) PetscCall(PetscPrintf(comm, "Distributed DMs are not equal %s with size %d.\n", user.partitioning, size));
170:   }

172:   /* if repartitioning is disabled, then quit */
173:   if (user.repartitioning[0] == '\0') return 0;

175:   if (user.tpw) {
176:     PetscCall(PetscSectionCreate(comm, &tpws));
177:     PetscCall(PetscSectionSetChart(tpws, 0, size));
178:     for (PetscInt i = 0; i < size; i++) {
179:       PetscInt tdof = i % 2 ? i + 1 : size - i;
180:       PetscCall(PetscSectionSetDof(tpws, i, tdof));
181:     }
182:     PetscCall(PetscSectionSetUp(tpws));
183:   }

185:   /* repartition distributed DM dmdist1 */
186:   PetscCall(ScotchResetRandomSeed());
187:   PetscCall(DMPlexGetPartitioner(dmdist1, &part1));
188:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)part1, "dp1_"));
189:   PetscCall(PetscPartitionerSetType(part1, user.repartitioning));
190:   PetscCall(PetscPartitionerSetFromOptions(part1));
191:   PetscCall(PetscSectionCreate(comm, &s1));
192:   PetscCall(PetscPartitionerDMPlexPartition(part1, dmdist1, tpws, s1, &is1));

194:   /* repartition distributed DM dmdist2 */
195:   PetscCall(ScotchResetRandomSeed());
196:   PetscCall(DMPlexGetPartitioner(dmdist2, &part2));
197:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)part2, "dp2_"));
198:   PetscCall(PetscPartitionerSetType(part2, PETSCPARTITIONERMATPARTITIONING));
199:   PetscCall(PetscPartitionerMatPartitioningGetMatPartitioning(part2, &mp));
200:   PetscCall(MatPartitioningSetType(mp, user.repartitioning));
201:   PetscCall(PetscPartitionerSetFromOptions(part2));
202:   PetscCall(PetscSectionCreate(comm, &s2));
203:   PetscCall(PetscPartitionerDMPlexPartition(part2, dmdist2, tpws, s2, &is2));

205:   /* compare the two ISs */
206:   PetscCall(ISOnComm(is1, comm, PETSC_USE_POINTER, &is1g));
207:   PetscCall(ISOnComm(is2, comm, PETSC_USE_POINTER, &is2g));
208:   PetscCall(ISViewFromOptions(is1g, NULL, "-dist_is1_view"));
209:   PetscCall(ISViewFromOptions(is2g, NULL, "-dist_is2_view"));
210:   if (user.compare_is) {
211:     PetscCall(ISEqualUnsorted(is1g, is2g, &flg));
212:     if (!flg) PetscCall(PetscPrintf(comm, "Distributed ISs are not equal, with %s with size %d.\n", user.repartitioning, size));
213:   }
214:   PetscCall(ISDestroy(&is1g));
215:   PetscCall(ISDestroy(&is2g));

217:   /* compare the two PetscSections */
218:   PetscCall(PetscSectionViewFromOptions(s1, NULL, "-dist_s1_view"));
219:   PetscCall(PetscSectionViewFromOptions(s2, NULL, "-dist_s2_view"));
220:   if (user.compare_is) {
221:     PetscCall(PetscSectionCompare(s1, s2, &flg));
222:     if (!flg) PetscCall(PetscPrintf(comm, "Distributed PetscSections are not equal, with %s with size %d.\n", user.repartitioning, size));
223:   }

225:   /* redistribute both distributed DMs */
226:   PetscCall(ScotchResetRandomSeed());
227:   PetscCall(DMPlexDistribute(dmdist1, 0, NULL, &dm1));
228:   PetscCall(ScotchResetRandomSeed());
229:   PetscCall(DMPlexDistribute(dmdist2, 0, NULL, &dm2));

231:   /* compare the two distributed DMs */
232:   PetscCall(DMPlexIsInterpolated(dm1, &interp));
233:   if (interp == DMPLEX_INTERPOLATED_NONE) {
234:     PetscCall(DMPlexEqual(dm1, dm2, &flg));
235:     if (!flg) PetscCall(PetscPrintf(comm, "Redistributed DMs are not equal, with %s with size %d.\n", user.repartitioning, size));
236:   }

238:   /* cleanup */
239:   PetscCall(PetscSectionDestroy(&tpws));
240:   PetscCall(PetscSectionDestroy(&s1));
241:   PetscCall(PetscSectionDestroy(&s2));
242:   PetscCall(ISDestroy(&is1));
243:   PetscCall(ISDestroy(&is2));
244:   PetscCall(DMDestroy(&dm1));
245:   PetscCall(DMDestroy(&dm2));
246:   PetscCall(DMDestroy(&dmdist1));
247:   PetscCall(DMDestroy(&dmdist2));
248:   PetscCall(PetscFinalize());
249:   return 0;
250: }

252: /*TEST

254:   test:
255:     # partition sequential mesh loaded from Exodus file
256:     suffix: 0
257:     nsize: {{1 2 3 4 8}}
258:     requires: chaco parmetis ptscotch exodusii
259:     args: -dm_plex_filename ${PETSC_DIR}/share/petsc/datafiles/meshes/blockcylinder-50.exo
260:     args: -partitioning {{chaco parmetis ptscotch}} -repartitioning {{parmetis ptscotch}} -tpweight {{0 1}}
261:     output_file: output/empty.out
262:   test:
263:     # partition mesh generated by ctetgen using scotch, then repartition with scotch, diff view
264:     suffix: 3
265:     nsize: 4
266:     requires: ptscotch ctetgen
267:     args: -dm_plex_dim 3 -dm_plex_box_faces 2,3,2 -partitioning ptscotch -repartitioning ptscotch
268:     args: -p1_petscpartitioner_view -p2_petscpartitioner_view -dp1_petscpartitioner_view -dp2_petscpartitioner_view -tpweight {{0 1}}
269:   test:
270:     # partition mesh generated by ctetgen using partitioners supported both by MatPartitioning and PetscPartitioner
271:     suffix: 4
272:     nsize: {{1 2 3 4 8}}
273:     requires: chaco parmetis ptscotch ctetgen
274:     args: -dm_plex_dim 3 -dm_plex_box_faces {{2,3,4 5,4,3 7,11,5}} -partitioning {{chaco parmetis ptscotch}} -repartitioning {{parmetis ptscotch}} -tpweight {{0 1}}
275:     output_file: output/empty.out

277: TEST*/