Actual source code: ex1.c

petsc-3.13.4 2020-08-01
Report Typos and Errors
  1: static const char help[] = "Test star forest communication (PetscSF)\n\n";

  3: /*T
  4:     Description: This example creates empty star forests to test the API.
  5: T*/

  7:  #include <petscsf.h>
  8:  #include <petsc/private/sfimpl.h>

 10: static PetscErrorCode CheckGraphNotSet(PetscSF sf)
 11: {
 12:   PetscInt          nroots,nleaves;
 13:   const PetscInt    *ilocal;
 14:   const PetscSFNode *iremote;
 15:   PetscErrorCode    ierr;

 18:   if (sf->graphset) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is set");
 19:   PetscSFGetGraph(sf,&nroots,&nleaves,&ilocal,&iremote);
 20:   if (nroots  >= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is set");
 21:   if (nleaves >= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is set");
 22:   if (ilocal)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is set");
 23:   if (iremote) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is set");
 24:   if (sf->minleaf != PETSC_MAX_INT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF minimum leaf is not PETSC_MAX_INT");
 25:   if (sf->maxleaf != PETSC_MIN_INT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF minimum leaf is not PETSC_MIN_INT");
 26:   return(0);
 27: }

 29: static PetscErrorCode CheckGraphEmpty(PetscSF sf)
 30: {
 31:   PetscInt          nroots,nleaves;
 32:   const PetscInt    *ilocal;
 33:   const PetscSFNode *iremote;
 34:   PetscInt          minleaf,maxleaf;
 35:   PetscErrorCode    ierr;

 38:   PetscSFGetGraph(sf,&nroots,&nleaves,&ilocal,&iremote);
 39:   if (nroots)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is not empty");
 40:   if (nleaves) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is not empty");
 41:   if (ilocal)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is not empty");
 42:   if (iremote) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is not empty");
 43:   PetscSFGetLeafRange(sf,&minleaf,&maxleaf);
 44:   if (minleaf !=  0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF minimum leaf is not 0");
 45:   if (maxleaf != -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF maximum leaf is not -1");
 46:   return(0);
 47: }

 49: static PetscErrorCode CheckRanksNotSet(PetscSF sf)
 50: {
 52:   if (sf->nranks != -1)   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF ranks are set");
 53:   if (sf->ranks  != NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF ranks are set");
 54:   return(0);
 55: }

 57: static PetscErrorCode CheckRanksEmpty(PetscSF sf)
 58: {
 60:   if (sf->nranks != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF ranks not empty");
 61:   return(0);
 62: }

 64: int main(int argc,char **argv)
 65: {
 66:   PetscSF        sf,sfDup,sfInv,sfEmbed,sfA,sfB,sfBA;
 67:   const PetscInt *degree;
 69:   char           sftype[64] = PETSCSFBASIC;

 71:   PetscInitialize(&argc,&argv,NULL,help);if (ierr) return ierr;
 72:   PetscOptionsGetString(NULL,NULL,"-user_sf_type",sftype,sizeof(sftype),NULL);

 74:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
 75:   CheckGraphNotSet(sf);
 76:   PetscSFDestroy(&sf);

 78:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
 79:   CheckGraphNotSet(sf);
 80:   PetscSFReset(sf);
 81:   CheckGraphNotSet(sf);
 82:   PetscSFDestroy(&sf);

 84:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
 85:   CheckGraphNotSet(sf);
 86:   PetscSFSetType(sf,sftype);
 87:   CheckGraphNotSet(sf);
 88:   PetscSFDestroy(&sf);

 90:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
 91:   CheckGraphNotSet(sf);
 92:   PetscSFSetType(sf,sftype);
 93:   CheckGraphNotSet(sf);
 94:   PetscSFReset(sf);
 95:   CheckGraphNotSet(sf);
 96:   PetscSFDestroy(&sf);

 98:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
 99:   PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES);
100:   CheckGraphEmpty(sf);
101:   PetscSFReset(sf);
102:   CheckGraphNotSet(sf);
103:   PetscSFDestroy(&sf);

105:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
106:   PetscSFSetType(sf,sftype);
107:   PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES);
108:   CheckGraphEmpty(sf);
109:   PetscSFReset(sf);
110:   CheckGraphNotSet(sf);
111:   PetscSFDestroy(&sf);

113:   /* Test setup */
114:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
115:   CheckRanksNotSet(sf);
116:   PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES);
117:   CheckRanksNotSet(sf);
118:   PetscSFSetUp(sf);
119:   CheckRanksEmpty(sf);
120:   PetscSFDestroy(&sf);

122:   /* Test setup then reset */
123:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
124:   PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES);
125:   PetscSFSetUp(sf);
126:   PetscSFReset(sf);
127:   CheckRanksNotSet(sf);
128:   PetscSFDestroy(&sf);

130:   /* Test view (no graph set, no type set) */
131:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
132:   PetscSFView(sf,NULL);
133:   PetscSFDestroy(&sf);

135:   /* Test set graph then view (no type set) */
136:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
137:   PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES);
138:   PetscSFView(sf,NULL);
139:   PetscSFDestroy(&sf);

141:   /* Test set type then view (no graph set) */
142:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
143:   PetscSFSetType(sf,sftype);
144:   PetscSFView(sf,NULL);
145:   PetscSFDestroy(&sf);

147:   /* Test set type then graph then view */
148:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
149:   PetscSFSetType(sf,sftype);
150:   PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES);
151:   PetscSFView(sf,NULL);
152:   PetscSFDestroy(&sf);

154:   /* Test set graph then type */
155:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
156:   PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES);
157:   PetscSFSetType(sf,sftype);
158:   CheckGraphEmpty(sf);
159:   PetscSFReset(sf);
160:   CheckGraphNotSet(sf);
161:   PetscSFDestroy(&sf);

163:   /* Test Bcast (we call setfromoptions) */
164:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
165:   PetscSFSetType(sf,sftype);
166:   PetscSFSetFromOptions(sf);
167:   PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES);
168:   PetscSFBcastBegin(sf,MPI_INT,NULL,NULL);
169:   PetscSFBcastEnd  (sf,MPI_INT,NULL,NULL);
170:   PetscSFDestroy(&sf);

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

174:   /* Test Reduce */
175:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
176:   PetscSFSetType(sf,sftype);
177:   PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES);
178:   PetscSFSetFromOptions(sf);
179:   PetscSFReduceBegin(sf,MPI_INT,NULL,NULL,MPIU_REPLACE);
180:   PetscSFReduceEnd  (sf,MPI_INT,NULL,NULL,MPIU_REPLACE);
181:   PetscSFReduceBegin(sf,MPI_INT,NULL,NULL,MPI_SUM);
182:   PetscSFReduceEnd  (sf,MPI_INT,NULL,NULL,MPI_SUM);
183:   PetscSFDestroy(&sf);

185:   /* Test FetchAndOp */
186:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
187:   PetscSFSetType(sf,sftype);
188:   PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES);
189:   PetscSFSetFromOptions(sf);
190:   PetscSFFetchAndOpBegin(sf,MPI_INT,NULL,NULL,NULL,MPI_SUM);
191:   PetscSFFetchAndOpEnd  (sf,MPI_INT,NULL,NULL,NULL,MPI_SUM);
192:   PetscSFDestroy(&sf);

194:   /* Test ComputeDegree */
195:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
196:   PetscSFSetType(sf,sftype);
197:   PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES);
198:   PetscSFSetFromOptions(sf);
199:   PetscSFComputeDegreeBegin(sf,&degree);
200:   PetscSFComputeDegreeEnd(sf,&degree);
201:   PetscSFDestroy(&sf);

203:   /* Test PetscSFDuplicate() */
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:   PetscSFDuplicate(sf,PETSCSF_DUPLICATE_GRAPH,&sfDup);
209:   CheckGraphEmpty(sfDup);
210:   PetscSFDestroy(&sfDup);
211:   PetscSFDestroy(&sf);

213:   /* Test PetscSFCreateInverseSF() */
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:   PetscSFCreateInverseSF(sf,&sfInv);
219:   CheckGraphEmpty(sfInv);
220:   PetscSFDestroy(&sfInv);
221:   PetscSFDestroy(&sf);

223:   /* Test PetscSFCreateEmbeddedSF() */
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:   PetscSFCreateEmbeddedSF(sf,0,NULL,&sfEmbed);
229:   CheckGraphEmpty(sfEmbed);
230:   PetscSFDestroy(&sfEmbed);
231:   PetscSFDestroy(&sf);

233:   /* Test PetscSFCreateEmbeddedLeafSF() */
234:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
235:   PetscSFSetType(sf,sftype);
236:   PetscSFSetGraph(sf,0,0,NULL,PETSC_USE_POINTER,NULL,PETSC_USE_POINTER);
237:   PetscSFSetFromOptions(sf);
238:   PetscSFCreateEmbeddedLeafSF(sf,0,NULL,&sfEmbed);
239:   CheckGraphEmpty(sfEmbed);
240:   PetscSFDestroy(&sfEmbed);
241:   PetscSFDestroy(&sf);

243:   /* Test PetscSFCompose() */
244:   PetscSFCreate(PETSC_COMM_WORLD,&sfA);
245:   PetscSFSetType(sfA,sftype);
246:   PetscSFSetGraph(sfA,0,0,NULL,PETSC_USE_POINTER,NULL,PETSC_USE_POINTER);
247:   PetscSFCreate(PETSC_COMM_WORLD,&sfB);
248:   PetscSFSetType(sfB,sftype);
249:   PetscSFSetGraph(sfB,0,0,NULL,PETSC_USE_POINTER,NULL,PETSC_USE_POINTER);
250:   PetscSFCompose(sfA,sfB,&sfBA);
251:   CheckGraphEmpty(sfBA);
252:   PetscSFDestroy(&sfBA);
253:   PetscSFDestroy(&sfA);
254:   PetscSFDestroy(&sfB);

256:   PetscFinalize();
257:   return ierr;
258: }

260: /*TEST

262:    test:
263:       suffix: basic_1
264:       nsize: 1

266:    test:
267:       suffix: basic_2
268:       nsize: 2

270:    test:
271:       suffix: basic_3
272:       nsize: 3

274:    test:
275:       suffix: window
276:       args: -user_sf_type window -sf_type window -sf_window_flavor {{create dynamic allocate}} -sf_window_sync {{fence active lock}}
277:       nsize: {{1 2 3}separate output}
278:       requires: define(PETSC_HAVE_MPI_ONE_SIDED)

280:    # The nightly test suite with MPICH uses ch3:sock, which is broken when winsize == 0 in some of the processes
281:    test:
282:       suffix: window_shared
283:       args: -user_sf_type window -sf_type window -sf_window_flavor shared -sf_window_sync {{fence active lock}}
284:       nsize: {{1 2 3}separate output}
285:       requires: define(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY) !define(PETSC_HAVE_MPICH_NUMVERSION) define(PETSC_HAVE_MPI_ONE_SIDED)

287: TEST*/