1 static const char help[] = "Test star forest communication (PetscSF)\n\n"; 2 3 /*T 4 Description: This example creates empty star forests to test the API. 5 T*/ 6 7 #include <petscsf.h> 8 #include <petsc/private/sfimpl.h> 9 10 static PetscErrorCode CheckGraphNotSet(PetscSF sf) 11 { 12 PetscInt nroots,nleaves; 13 const PetscInt *ilocal; 14 const PetscSFNode *iremote; 15 16 PetscFunctionBegin; 17 PetscCheck(!sf->graphset,PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is set"); 18 CHKERRQ(PetscSFGetGraph(sf,&nroots,&nleaves,&ilocal,&iremote)); 19 PetscCheckFalse(nroots >= 0,PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is set"); 20 PetscCheckFalse(nleaves >= 0,PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is set"); 21 PetscCheck(!ilocal,PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is set"); 22 PetscCheck(!iremote,PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is set"); 23 PetscCheckFalse(sf->minleaf != PETSC_MAX_INT,PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF minimum leaf is not PETSC_MAX_INT"); 24 PetscCheckFalse(sf->maxleaf != PETSC_MIN_INT,PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF minimum leaf is not PETSC_MIN_INT"); 25 PetscFunctionReturn(0); 26 } 27 28 static PetscErrorCode CheckGraphEmpty(PetscSF sf) 29 { 30 PetscInt nroots,nleaves; 31 const PetscInt *ilocal; 32 const PetscSFNode *iremote; 33 PetscInt minleaf,maxleaf; 34 35 PetscFunctionBegin; 36 CHKERRQ(PetscSFGetGraph(sf,&nroots,&nleaves,&ilocal,&iremote)); 37 PetscCheck(!nroots,PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is not empty"); 38 PetscCheck(!nleaves,PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is not empty"); 39 PetscCheck(!ilocal,PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is not empty"); 40 PetscCheck(!iremote,PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF graph is not empty"); 41 CHKERRQ(PetscSFGetLeafRange(sf,&minleaf,&maxleaf)); 42 PetscCheckFalse(minleaf != 0,PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF minimum leaf is not 0"); 43 PetscCheckFalse(maxleaf != -1,PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF maximum leaf is not -1"); 44 PetscFunctionReturn(0); 45 } 46 47 static PetscErrorCode CheckRanksNotSet(PetscSF sf) 48 { 49 PetscFunctionBegin; 50 PetscCheckFalse(sf->nranks != -1,PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF ranks are set"); 51 PetscCheckFalse(sf->ranks != NULL,PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF ranks are set"); 52 PetscFunctionReturn(0); 53 } 54 55 static PetscErrorCode CheckRanksEmpty(PetscSF sf) 56 { 57 PetscFunctionBegin; 58 PetscCheckFalse(sf->nranks != 0,PETSC_COMM_SELF,PETSC_ERR_PLIB,"SF ranks not empty"); 59 PetscFunctionReturn(0); 60 } 61 62 int main(int argc,char **argv) 63 { 64 PetscSF sf,sfDup,sfInv,sfEmbed,sfA,sfB,sfBA; 65 const PetscInt *degree; 66 char sftype[64] = PETSCSFBASIC; 67 68 CHKERRQ(PetscInitialize(&argc,&argv,NULL,help)); 69 CHKERRQ(PetscOptionsGetString(NULL,NULL,"-user_sf_type",sftype,sizeof(sftype),NULL)); 70 71 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 72 CHKERRQ(CheckGraphNotSet(sf)); 73 CHKERRQ(PetscSFDestroy(&sf)); 74 75 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 76 CHKERRQ(CheckGraphNotSet(sf)); 77 CHKERRQ(PetscSFReset(sf)); 78 CHKERRQ(CheckGraphNotSet(sf)); 79 CHKERRQ(PetscSFDestroy(&sf)); 80 81 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 82 CHKERRQ(CheckGraphNotSet(sf)); 83 CHKERRQ(PetscSFSetType(sf,sftype)); 84 CHKERRQ(CheckGraphNotSet(sf)); 85 CHKERRQ(PetscSFDestroy(&sf)); 86 87 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 88 CHKERRQ(CheckGraphNotSet(sf)); 89 CHKERRQ(PetscSFSetType(sf,sftype)); 90 CHKERRQ(CheckGraphNotSet(sf)); 91 CHKERRQ(PetscSFReset(sf)); 92 CHKERRQ(CheckGraphNotSet(sf)); 93 CHKERRQ(PetscSFDestroy(&sf)); 94 95 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 96 CHKERRQ(PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES)); 97 CHKERRQ(CheckGraphEmpty(sf)); 98 CHKERRQ(PetscSFReset(sf)); 99 CHKERRQ(CheckGraphNotSet(sf)); 100 CHKERRQ(PetscSFDestroy(&sf)); 101 102 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 103 CHKERRQ(PetscSFSetType(sf,sftype)); 104 CHKERRQ(PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES)); 105 CHKERRQ(CheckGraphEmpty(sf)); 106 CHKERRQ(PetscSFReset(sf)); 107 CHKERRQ(CheckGraphNotSet(sf)); 108 CHKERRQ(PetscSFDestroy(&sf)); 109 110 /* Test setup */ 111 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 112 CHKERRQ(CheckRanksNotSet(sf)); 113 CHKERRQ(PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES)); 114 CHKERRQ(CheckRanksNotSet(sf)); 115 CHKERRQ(PetscSFSetUp(sf)); 116 CHKERRQ(CheckRanksEmpty(sf)); 117 CHKERRQ(PetscSFDestroy(&sf)); 118 119 /* Test setup then reset */ 120 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 121 CHKERRQ(PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES)); 122 CHKERRQ(PetscSFSetUp(sf)); 123 CHKERRQ(PetscSFReset(sf)); 124 CHKERRQ(CheckRanksNotSet(sf)); 125 CHKERRQ(PetscSFDestroy(&sf)); 126 127 /* Test view (no graph set, no type set) */ 128 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 129 CHKERRQ(PetscSFView(sf,NULL)); 130 CHKERRQ(PetscSFDestroy(&sf)); 131 132 /* Test set graph then view (no type set) */ 133 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 134 CHKERRQ(PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES)); 135 CHKERRQ(PetscSFView(sf,NULL)); 136 CHKERRQ(PetscSFDestroy(&sf)); 137 138 /* Test set type then view (no graph set) */ 139 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 140 CHKERRQ(PetscSFSetType(sf,sftype)); 141 CHKERRQ(PetscSFView(sf,NULL)); 142 CHKERRQ(PetscSFDestroy(&sf)); 143 144 /* Test set type then graph then view */ 145 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 146 CHKERRQ(PetscSFSetType(sf,sftype)); 147 CHKERRQ(PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES)); 148 CHKERRQ(PetscSFView(sf,NULL)); 149 CHKERRQ(PetscSFDestroy(&sf)); 150 151 /* Test set graph then type */ 152 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 153 CHKERRQ(PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES)); 154 CHKERRQ(PetscSFSetType(sf,sftype)); 155 CHKERRQ(CheckGraphEmpty(sf)); 156 CHKERRQ(PetscSFReset(sf)); 157 CHKERRQ(CheckGraphNotSet(sf)); 158 CHKERRQ(PetscSFDestroy(&sf)); 159 160 /* Test Bcast (we call setfromoptions) */ 161 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 162 CHKERRQ(PetscSFSetType(sf,sftype)); 163 CHKERRQ(PetscSFSetFromOptions(sf)); 164 CHKERRQ(PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES)); 165 CHKERRQ(PetscSFBcastBegin(sf,MPI_INT,NULL,NULL,MPI_REPLACE)); 166 CHKERRQ(PetscSFBcastEnd (sf,MPI_INT,NULL,NULL,MPI_REPLACE)); 167 CHKERRQ(PetscSFDestroy(&sf)); 168 169 /* From now on we also call SetFromOptions */ 170 171 /* Test Reduce */ 172 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 173 CHKERRQ(PetscSFSetType(sf,sftype)); 174 CHKERRQ(PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES)); 175 CHKERRQ(PetscSFSetFromOptions(sf)); 176 CHKERRQ(PetscSFReduceBegin(sf,MPI_INT,NULL,NULL,MPI_REPLACE)); 177 CHKERRQ(PetscSFReduceEnd (sf,MPI_INT,NULL,NULL,MPI_REPLACE)); 178 CHKERRQ(PetscSFReduceBegin(sf,MPI_INT,NULL,NULL,MPI_SUM)); 179 CHKERRQ(PetscSFReduceEnd (sf,MPI_INT,NULL,NULL,MPI_SUM)); 180 CHKERRQ(PetscSFDestroy(&sf)); 181 182 /* Test FetchAndOp */ 183 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 184 CHKERRQ(PetscSFSetType(sf,sftype)); 185 CHKERRQ(PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES)); 186 CHKERRQ(PetscSFSetFromOptions(sf)); 187 CHKERRQ(PetscSFFetchAndOpBegin(sf,MPI_INT,NULL,NULL,NULL,MPI_SUM)); 188 CHKERRQ(PetscSFFetchAndOpEnd (sf,MPI_INT,NULL,NULL,NULL,MPI_SUM)); 189 CHKERRQ(PetscSFDestroy(&sf)); 190 191 /* Test ComputeDegree */ 192 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 193 CHKERRQ(PetscSFSetType(sf,sftype)); 194 CHKERRQ(PetscSFSetGraph(sf,0,0,NULL,PETSC_COPY_VALUES,NULL,PETSC_COPY_VALUES)); 195 CHKERRQ(PetscSFSetFromOptions(sf)); 196 CHKERRQ(PetscSFComputeDegreeBegin(sf,°ree)); 197 CHKERRQ(PetscSFComputeDegreeEnd(sf,°ree)); 198 CHKERRQ(PetscSFDestroy(&sf)); 199 200 /* Test PetscSFDuplicate() */ 201 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 202 CHKERRQ(PetscSFSetType(sf,sftype)); 203 CHKERRQ(PetscSFSetGraph(sf,0,0,NULL,PETSC_USE_POINTER,NULL,PETSC_USE_POINTER)); 204 CHKERRQ(PetscSFSetFromOptions(sf)); 205 CHKERRQ(PetscSFDuplicate(sf,PETSCSF_DUPLICATE_GRAPH,&sfDup)); 206 CHKERRQ(CheckGraphEmpty(sfDup)); 207 CHKERRQ(PetscSFDestroy(&sfDup)); 208 CHKERRQ(PetscSFDestroy(&sf)); 209 210 /* Test PetscSFCreateInverseSF() */ 211 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 212 CHKERRQ(PetscSFSetType(sf,sftype)); 213 CHKERRQ(PetscSFSetGraph(sf,0,0,NULL,PETSC_USE_POINTER,NULL,PETSC_USE_POINTER)); 214 CHKERRQ(PetscSFSetFromOptions(sf)); 215 CHKERRQ(PetscSFCreateInverseSF(sf,&sfInv)); 216 CHKERRQ(CheckGraphEmpty(sfInv)); 217 CHKERRQ(PetscSFDestroy(&sfInv)); 218 CHKERRQ(PetscSFDestroy(&sf)); 219 220 /* Test PetscSFCreateEmbeddedRootSF() */ 221 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 222 CHKERRQ(PetscSFSetType(sf,sftype)); 223 CHKERRQ(PetscSFSetGraph(sf,0,0,NULL,PETSC_USE_POINTER,NULL,PETSC_USE_POINTER)); 224 CHKERRQ(PetscSFSetFromOptions(sf)); 225 CHKERRQ(PetscSFCreateEmbeddedRootSF(sf,0,NULL,&sfEmbed)); 226 CHKERRQ(CheckGraphEmpty(sfEmbed)); 227 CHKERRQ(PetscSFDestroy(&sfEmbed)); 228 CHKERRQ(PetscSFDestroy(&sf)); 229 230 /* Test PetscSFCreateEmbeddedLeafSF() */ 231 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sf)); 232 CHKERRQ(PetscSFSetType(sf,sftype)); 233 CHKERRQ(PetscSFSetGraph(sf,0,0,NULL,PETSC_USE_POINTER,NULL,PETSC_USE_POINTER)); 234 CHKERRQ(PetscSFSetFromOptions(sf)); 235 CHKERRQ(PetscSFCreateEmbeddedLeafSF(sf,0,NULL,&sfEmbed)); 236 CHKERRQ(CheckGraphEmpty(sfEmbed)); 237 CHKERRQ(PetscSFDestroy(&sfEmbed)); 238 CHKERRQ(PetscSFDestroy(&sf)); 239 240 /* Test PetscSFCompose() */ 241 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sfA)); 242 CHKERRQ(PetscSFSetType(sfA,sftype)); 243 CHKERRQ(PetscSFSetGraph(sfA,0,0,NULL,PETSC_USE_POINTER,NULL,PETSC_USE_POINTER)); 244 CHKERRQ(PetscSFCreate(PETSC_COMM_WORLD,&sfB)); 245 CHKERRQ(PetscSFSetType(sfB,sftype)); 246 CHKERRQ(PetscSFSetGraph(sfB,0,0,NULL,PETSC_USE_POINTER,NULL,PETSC_USE_POINTER)); 247 CHKERRQ(PetscSFCompose(sfA,sfB,&sfBA)); 248 CHKERRQ(CheckGraphEmpty(sfBA)); 249 CHKERRQ(PetscSFDestroy(&sfBA)); 250 CHKERRQ(PetscSFDestroy(&sfA)); 251 CHKERRQ(PetscSFDestroy(&sfB)); 252 253 CHKERRQ(PetscFinalize()); 254 return 0; 255 } 256 257 /*TEST 258 259 test: 260 suffix: basic_1 261 nsize: 1 262 263 test: 264 suffix: basic_2 265 nsize: 2 266 267 test: 268 suffix: basic_3 269 nsize: 3 270 271 test: 272 suffix: window 273 args: -user_sf_type window -sf_type window -sf_window_flavor {{create dynamic allocate}} -sf_window_sync {{fence active lock}} 274 nsize: {{1 2 3}separate output} 275 requires: defined(PETSC_HAVE_MPI_ONE_SIDED) defined(PETSC_HAVE_MPI_FEATURE_DYNAMIC_WINDOW) 276 277 # The nightly test suite with MPICH uses ch3:sock, which is broken when winsize == 0 in some of the processes 278 test: 279 suffix: window_shared 280 args: -user_sf_type window -sf_type window -sf_window_flavor shared -sf_window_sync {{fence active lock}} 281 nsize: {{1 2 3}separate output} 282 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) 283 284 TEST*/ 285