Actual source code: ex1.c

  1: static const char help[] = "Test star forest communication (PetscSF)\n\n";

  3: #include <petscsf.h>
  4: #include <petsc/private/sfimpl.h>

  6: static PetscErrorCode CheckGraphNotSet(PetscSF sf)
  7: {
  8:   PetscInt           nroots, nleaves;
  9:   const PetscInt    *ilocal;
 10:   const PetscSFNode *iremote;

 13:   PetscSFGetGraph(sf, &nroots, &nleaves, &ilocal, &iremote);
 20:   return 0;
 21: }

 23: static PetscErrorCode CheckGraphEmpty(PetscSF sf)
 24: {
 25:   PetscInt           nroots, nleaves;
 26:   const PetscInt    *ilocal;
 27:   const PetscSFNode *iremote;
 28:   PetscInt           minleaf, maxleaf;

 30:   PetscSFGetGraph(sf, &nroots, &nleaves, &ilocal, &iremote);
 35:   PetscSFGetLeafRange(sf, &minleaf, &maxleaf);
 38:   return 0;
 39: }

 41: static PetscErrorCode CheckRanksNotSet(PetscSF sf)
 42: {
 45:   return 0;
 46: }

 48: static PetscErrorCode CheckRanksEmpty(PetscSF sf)
 49: {
 51:   return 0;
 52: }

 54: int main(int argc, char **argv)
 55: {
 56:   PetscSF         sf, sfDup, sfInv, sfEmbed, sfA, sfB, sfBA;
 57:   const PetscInt *degree;
 58:   char            sftype[64] = PETSCSFBASIC;

 61:   PetscInitialize(&argc, &argv, NULL, help);
 62:   PetscOptionsGetString(NULL, NULL, "-user_sf_type", sftype, sizeof(sftype), NULL);

 64:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
 65:   CheckGraphNotSet(sf);
 66:   PetscSFDestroy(&sf);

 68:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
 69:   CheckGraphNotSet(sf);
 70:   PetscSFReset(sf);
 71:   CheckGraphNotSet(sf);
 72:   PetscSFDestroy(&sf);

 74:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
 75:   CheckGraphNotSet(sf);
 76:   PetscSFSetType(sf, sftype);
 77:   CheckGraphNotSet(sf);
 78:   PetscSFDestroy(&sf);

 80:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
 81:   CheckGraphNotSet(sf);
 82:   PetscSFSetType(sf, sftype);
 83:   CheckGraphNotSet(sf);
 84:   PetscSFReset(sf);
 85:   CheckGraphNotSet(sf);
 86:   PetscSFDestroy(&sf);

 88:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
 89:   PetscSFSetGraph(sf, 0, 0, NULL, PETSC_COPY_VALUES, NULL, PETSC_COPY_VALUES);
 90:   CheckGraphEmpty(sf);
 91:   PetscSFReset(sf);
 92:   CheckGraphNotSet(sf);
 93:   PetscSFDestroy(&sf);

 95:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
 96:   PetscSFSetType(sf, sftype);
 97:   PetscSFSetGraph(sf, 0, 0, NULL, PETSC_COPY_VALUES, NULL, PETSC_COPY_VALUES);
 98:   CheckGraphEmpty(sf);
 99:   PetscSFReset(sf);
100:   CheckGraphNotSet(sf);
101:   PetscSFDestroy(&sf);

103:   /* Test setup */
104:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
105:   CheckRanksNotSet(sf);
106:   PetscSFSetGraph(sf, 0, 0, NULL, PETSC_COPY_VALUES, NULL, PETSC_COPY_VALUES);
107:   CheckRanksNotSet(sf);
108:   PetscSFSetUp(sf);
109:   CheckRanksEmpty(sf);
110:   PetscSFDestroy(&sf);

112:   /* Test setup then reset */
113:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
114:   PetscSFSetGraph(sf, 0, 0, NULL, PETSC_COPY_VALUES, NULL, PETSC_COPY_VALUES);
115:   PetscSFSetUp(sf);
116:   PetscSFReset(sf);
117:   CheckRanksNotSet(sf);
118:   PetscSFDestroy(&sf);

120:   /* Test view (no graph set, no type set) */
121:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
122:   PetscSFView(sf, NULL);
123:   PetscSFDestroy(&sf);

125:   /* Test set graph then view (no type set) */
126:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
127:   PetscSFSetGraph(sf, 0, 0, NULL, PETSC_COPY_VALUES, NULL, PETSC_COPY_VALUES);
128:   PetscSFView(sf, NULL);
129:   PetscSFDestroy(&sf);

131:   /* Test set type then view (no graph set) */
132:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
133:   PetscSFSetType(sf, sftype);
134:   PetscSFView(sf, NULL);
135:   PetscSFDestroy(&sf);

137:   /* Test set type then graph then view */
138:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
139:   PetscSFSetType(sf, sftype);
140:   PetscSFSetGraph(sf, 0, 0, NULL, PETSC_COPY_VALUES, NULL, PETSC_COPY_VALUES);
141:   PetscSFView(sf, NULL);
142:   PetscSFDestroy(&sf);

144:   /* Test set graph then type */
145:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
146:   PetscSFSetGraph(sf, 0, 0, NULL, PETSC_COPY_VALUES, NULL, PETSC_COPY_VALUES);
147:   PetscSFSetType(sf, sftype);
148:   CheckGraphEmpty(sf);
149:   PetscSFReset(sf);
150:   CheckGraphNotSet(sf);
151:   PetscSFDestroy(&sf);

153:   /* Test Bcast (we call setfromoptions) */
154:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
155:   PetscSFSetType(sf, sftype);
156:   PetscSFSetFromOptions(sf);
157:   PetscSFSetGraph(sf, 0, 0, NULL, PETSC_COPY_VALUES, NULL, PETSC_COPY_VALUES);
158:   PetscSFBcastBegin(sf, MPI_INT, NULL, NULL, MPI_REPLACE);
159:   PetscSFBcastEnd(sf, MPI_INT, NULL, NULL, MPI_REPLACE);
160:   PetscSFDestroy(&sf);

162:   /* From now on we also call SetFromOptions */

164:   /* Test Reduce */
165:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
166:   PetscSFSetType(sf, sftype);
167:   PetscSFSetGraph(sf, 0, 0, NULL, PETSC_COPY_VALUES, NULL, PETSC_COPY_VALUES);
168:   PetscSFSetFromOptions(sf);
169:   PetscSFReduceBegin(sf, MPI_INT, NULL, NULL, MPI_REPLACE);
170:   PetscSFReduceEnd(sf, MPI_INT, NULL, NULL, MPI_REPLACE);
171:   PetscSFReduceBegin(sf, MPI_INT, NULL, NULL, MPI_SUM);
172:   PetscSFReduceEnd(sf, MPI_INT, NULL, NULL, MPI_SUM);
173:   PetscSFDestroy(&sf);

175:   /* Test FetchAndOp */
176:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
177:   PetscSFSetType(sf, sftype);
178:   PetscSFSetGraph(sf, 0, 0, NULL, PETSC_COPY_VALUES, NULL, PETSC_COPY_VALUES);
179:   PetscSFSetFromOptions(sf);
180:   PetscSFFetchAndOpBegin(sf, MPI_INT, NULL, NULL, NULL, MPI_SUM);
181:   PetscSFFetchAndOpEnd(sf, MPI_INT, NULL, NULL, NULL, MPI_SUM);
182:   PetscSFDestroy(&sf);

184:   /* Test ComputeDegree */
185:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
186:   PetscSFSetType(sf, sftype);
187:   PetscSFSetGraph(sf, 0, 0, NULL, PETSC_COPY_VALUES, NULL, PETSC_COPY_VALUES);
188:   PetscSFSetFromOptions(sf);
189:   PetscSFComputeDegreeBegin(sf, &degree);
190:   PetscSFComputeDegreeEnd(sf, &degree);
191:   PetscSFDestroy(&sf);

193:   /* Test PetscSFDuplicate() */
194:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
195:   PetscSFSetType(sf, sftype);
196:   PetscSFSetGraph(sf, 0, 0, NULL, PETSC_USE_POINTER, NULL, PETSC_USE_POINTER);
197:   PetscSFSetFromOptions(sf);
198:   PetscSFDuplicate(sf, PETSCSF_DUPLICATE_GRAPH, &sfDup);
199:   CheckGraphEmpty(sfDup);
200:   PetscSFDestroy(&sfDup);
201:   PetscSFDestroy(&sf);

203:   /* Test PetscSFCreateInverseSF() */
204:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
205:   PetscSFSetType(sf, sftype);
206:   PetscSFSetGraph(sf, 0, 0, NULL, PETSC_USE_POINTER, NULL, PETSC_USE_POINTER);
207:   PetscSFSetFromOptions(sf);
208:   PetscSFCreateInverseSF(sf, &sfInv);
209:   CheckGraphEmpty(sfInv);
210:   PetscSFDestroy(&sfInv);
211:   PetscSFDestroy(&sf);

213:   /* Test PetscSFCreateEmbeddedRootSF() */
214:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
215:   PetscSFSetType(sf, sftype);
216:   PetscSFSetGraph(sf, 0, 0, NULL, PETSC_USE_POINTER, NULL, PETSC_USE_POINTER);
217:   PetscSFSetFromOptions(sf);
218:   PetscSFCreateEmbeddedRootSF(sf, 0, NULL, &sfEmbed);
219:   CheckGraphEmpty(sfEmbed);
220:   PetscSFDestroy(&sfEmbed);
221:   PetscSFDestroy(&sf);

223:   /* Test PetscSFCreateEmbeddedLeafSF() */
224:   PetscSFCreate(PETSC_COMM_WORLD, &sf);
225:   PetscSFSetType(sf, sftype);
226:   PetscSFSetGraph(sf, 0, 0, NULL, PETSC_USE_POINTER, NULL, PETSC_USE_POINTER);
227:   PetscSFSetFromOptions(sf);
228:   PetscSFCreateEmbeddedLeafSF(sf, 0, NULL, &sfEmbed);
229:   CheckGraphEmpty(sfEmbed);
230:   PetscSFDestroy(&sfEmbed);
231:   PetscSFDestroy(&sf);

233:   /* Test PetscSFCompose() */
234:   PetscSFCreate(PETSC_COMM_WORLD, &sfA);
235:   PetscSFSetType(sfA, sftype);
236:   PetscSFSetGraph(sfA, 0, 0, NULL, PETSC_USE_POINTER, NULL, PETSC_USE_POINTER);
237:   PetscSFCreate(PETSC_COMM_WORLD, &sfB);
238:   PetscSFSetType(sfB, sftype);
239:   PetscSFSetGraph(sfB, 0, 0, NULL, PETSC_USE_POINTER, NULL, PETSC_USE_POINTER);
240:   PetscSFCompose(sfA, sfB, &sfBA);
241:   CheckGraphEmpty(sfBA);
242:   PetscSFDestroy(&sfBA);
243:   PetscSFDestroy(&sfA);
244:   PetscSFDestroy(&sfB);

246:   PetscFinalize();
247:   return 0;
248: }

250: /*TEST

252:    test:
253:       suffix: basic_1
254:       nsize: 1

256:    test:
257:       suffix: basic_2
258:       nsize: 2

260:    test:
261:       suffix: basic_3
262:       nsize: 3

264:    test:
265:       suffix: window
266:       args: -user_sf_type window -sf_type window -sf_window_flavor {{create dynamic allocate}} -sf_window_sync {{fence active lock}}
267:       nsize: {{1 2 3}separate output}
268:       requires: defined(PETSC_HAVE_MPI_ONE_SIDED) defined(PETSC_HAVE_MPI_FEATURE_DYNAMIC_WINDOW)

270:    # The nightly test suite with MPICH uses ch3:sock, which is broken when winsize == 0 in some of the processes
271:    test:
272:       suffix: window_shared
273:       args: -user_sf_type window -sf_type window -sf_window_flavor shared -sf_window_sync {{fence active lock}}
274:       nsize: {{1 2 3}separate output}
275:       requires: defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY) !defined(PETSC_HAVE_MPICH_NUMVERSION) defined(PETSC_HAVE_MPI_ONE_SIDED) defined(PETSC_HAVE_MPI_FEATURE_DYNAMIC_WINDOW)

277: TEST*/