Actual source code: ex1.c

petsc-3.11.4 2019-09-28
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;

 70:   PetscInitialize(&argc,&argv,NULL,help);if (ierr) return ierr;

 72:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
 73:   CheckGraphNotSet(sf);
 74:   PetscSFDestroy(&sf);

 76:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
 77:   CheckGraphNotSet(sf);
 78:   PetscSFReset(sf);
 79:   CheckGraphNotSet(sf);
 80:   PetscSFDestroy(&sf);

 82:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
 83:   CheckGraphNotSet(sf);
 84:   PetscSFSetType(sf,PETSCSFBASIC);
 85:   CheckGraphNotSet(sf);
 86:   PetscSFDestroy(&sf);

 88:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
 89:   CheckGraphNotSet(sf);
 90:   PetscSFSetType(sf,PETSCSFBASIC);
 91:   CheckGraphNotSet(sf);
 92:   PetscSFReset(sf);
 93:   CheckGraphNotSet(sf);
 94:   PetscSFDestroy(&sf);

 96:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
 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:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
104:   PetscSFSetType(sf,PETSCSFBASIC);
105:   PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES);
106:   CheckGraphEmpty(sf);
107:   PetscSFReset(sf);
108:   CheckGraphNotSet(sf);
109:   PetscSFDestroy(&sf);

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

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

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

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

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

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

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

161:   /* Test Bcast */
162:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
163:   PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES);
164:   PetscSFBcastBegin(sf,MPI_INT,NULL,NULL);
165:   PetscSFBcastEnd  (sf,MPI_INT,NULL,NULL);
166:   PetscSFDestroy(&sf);

168:   /* Test Reduce */
169:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
170:   PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES);
171:   PetscSFReduceBegin(sf,MPI_INT,NULL,NULL,MPIU_REPLACE);
172:   PetscSFReduceEnd  (sf,MPI_INT,NULL,NULL,MPIU_REPLACE);
173:   PetscSFReduceBegin(sf,MPI_INT,NULL,NULL,MPI_SUM);
174:   PetscSFReduceEnd  (sf,MPI_INT,NULL,NULL,MPI_SUM);
175:   PetscSFDestroy(&sf);

177:   /* Test FetchAndOp */
178:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
179:   PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES);
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:   PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES);
187:   PetscSFComputeDegreeBegin(sf,&degree);
188:   PetscSFComputeDegreeEnd(sf,&degree);
189:   PetscSFDestroy(&sf);

191:   /* Test PetscSFDuplicate() */
192:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
193:   PetscSFSetGraph(sf,0,0,NULL,PETSC_USE_POINTER,NULL,PETSC_USE_POINTER);
194:   PetscSFDuplicate(sf,PETSCSF_DUPLICATE_GRAPH,&sfDup);
195:   CheckGraphEmpty(sfDup);
196:   PetscSFDestroy(&sfDup);
197:   PetscSFDestroy(&sf);

199:   /* Test PetscSFCreateInverseSF() */
200:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
201:   PetscSFSetGraph(sf,0,0,NULL,PETSC_USE_POINTER,NULL,PETSC_USE_POINTER);
202:   PetscSFCreateInverseSF(sf,&sfInv);
203:   CheckGraphEmpty(sfInv);
204:   PetscSFDestroy(&sfInv);
205:   PetscSFDestroy(&sf);

207:   /* Test PetscSFCreateEmbeddedSF() */
208:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
209:   PetscSFSetGraph(sf,0,0,NULL,PETSC_USE_POINTER,NULL,PETSC_USE_POINTER);
210:   PetscSFCreateEmbeddedSF(sf,0,NULL,&sfEmbed);
211:   CheckGraphEmpty(sfEmbed);
212:   PetscSFDestroy(&sfEmbed);
213:   PetscSFDestroy(&sf);

215:   /* Test PetscSFCreateEmbeddedLeafSF() */
216:   PetscSFCreate(PETSC_COMM_WORLD,&sf);
217:   PetscSFSetGraph(sf,0,0,NULL,PETSC_USE_POINTER,NULL,PETSC_USE_POINTER);
218:   PetscSFCreateEmbeddedLeafSF(sf,0,NULL,&sfEmbed);
219:   CheckGraphEmpty(sfEmbed);
220:   PetscSFDestroy(&sfEmbed);
221:   PetscSFDestroy(&sf);

223:   /* Test PetscSFCompose() */
224:   PetscSFCreate(PETSC_COMM_WORLD,&sfA);
225:   PetscSFSetGraph(sfA,0,0,NULL,PETSC_USE_POINTER,NULL,PETSC_USE_POINTER);
226:   PetscSFCreate(PETSC_COMM_WORLD,&sfB);
227:   PetscSFSetGraph(sfB,0,0,NULL,PETSC_USE_POINTER,NULL,PETSC_USE_POINTER);
228:   PetscSFCompose(sfA,sfB,&sfBA);
229:   CheckGraphEmpty(sfBA);
230:   PetscSFDestroy(&sfBA);
231:   PetscSFDestroy(&sfA);
232:   PetscSFDestroy(&sfB);

234:   PetscFinalize();
235:   return ierr;
236: }

238: /*TEST

240:    test:
241:       suffix: basic_1
242:       nsize: 1

244:    test:
245:       suffix: basic_2
246:       nsize: 2

248:    test:
249:       suffix: basic_3
250:       nsize: 3

252: TEST*/