1 static char help[] = "Tests DMGetCompatibility() with a 3D DMDA.\n\n"; 2 3 #include <petscdm.h> 4 #include <petscdmda.h> 5 #include <petscdmstag.h> 6 7 int main(int argc,char **argv) { 8 PetscInt M = 3,N = 5,P=3,s=1,w=2,i,m = PETSC_DECIDE,n = PETSC_DECIDE,p = PETSC_DECIDE; 9 PetscErrorCode ierr; 10 PetscInt *lx = NULL,*ly = NULL,*lz = NULL; 11 PetscBool test_order = PETSC_FALSE; 12 DM da; 13 DMBoundaryType bx = DM_BOUNDARY_NONE,by = DM_BOUNDARY_NONE,bz = DM_BOUNDARY_NONE; 14 DMDAStencilType stencil_type = DMDA_STENCIL_BOX; 15 PetscBool flg = PETSC_FALSE,distribute = PETSC_FALSE; 16 17 ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; 18 19 /* Read options */ 20 CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-NX",&M,NULL)); 21 CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-NY",&N,NULL)); 22 CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-NZ",&P,NULL)); 23 CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL)); 24 CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL)); 25 CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-p",&p,NULL)); 26 CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-s",&s,NULL)); 27 CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-w",&w,NULL)); 28 flg = PETSC_FALSE; 29 CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-star",&flg,NULL)); 30 if (flg) stencil_type = DMDA_STENCIL_STAR; 31 flg = PETSC_FALSE; 32 CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-box",&flg,NULL)); 33 if (flg) stencil_type = DMDA_STENCIL_BOX; 34 35 flg = PETSC_FALSE; 36 CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-xperiodic",&flg,NULL)); 37 if (flg) bx = DM_BOUNDARY_PERIODIC; 38 flg = PETSC_FALSE; 39 CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-xghosted",&flg,NULL)); 40 if (flg) bx = DM_BOUNDARY_GHOSTED; 41 flg = PETSC_FALSE; 42 CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-xnonghosted",&flg,NULL)); 43 44 flg = PETSC_FALSE; 45 CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-yperiodic",&flg,NULL)); 46 if (flg) by = DM_BOUNDARY_PERIODIC; 47 flg = PETSC_FALSE; 48 CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-yghosted",&flg,NULL)); 49 if (flg) by = DM_BOUNDARY_GHOSTED; 50 flg = PETSC_FALSE; 51 CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-ynonghosted",&flg,NULL)); 52 53 flg = PETSC_FALSE; 54 CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-zperiodic",&flg,NULL)); 55 if (flg) bz = DM_BOUNDARY_PERIODIC; 56 flg = PETSC_FALSE; 57 CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-zghosted",&flg,NULL)); 58 if (flg) bz = DM_BOUNDARY_GHOSTED; 59 flg = PETSC_FALSE; 60 CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-znonghosted",&flg,NULL)); 61 62 CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-testorder",&test_order,NULL)); 63 64 flg = PETSC_FALSE; 65 CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-distribute",&distribute,NULL)); 66 if (distribute) { 67 PetscCheckFalse(m == PETSC_DECIDE,PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Must set -m option with -distribute option"); 68 CHKERRQ(PetscMalloc1(m,&lx)); 69 for (i=0; i<m-1; i++) lx[i] = 4; 70 lx[m-1] = M - 4*(m-1); 71 PetscCheckFalse(n == PETSC_DECIDE,PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Must set -n option with -distribute option"); 72 CHKERRQ(PetscMalloc1(n,&ly)); 73 for (i=0; i<n-1; i++) ly[i] = 2; 74 ly[n-1] = N - 2*(n-1); 75 PetscCheckFalse(p == PETSC_DECIDE,PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Must set -p option with -distribute option"); 76 CHKERRQ(PetscMalloc1(p,&lz)); 77 for (i=0; i<p-1; i++) lz[i] = 2; 78 lz[p-1] = P - 2*(p-1); 79 } 80 81 CHKERRQ(DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,stencil_type,M,N,P,m,n,p,w,s,lx,ly,lz,&da)); 82 CHKERRQ(DMSetFromOptions(da)); 83 CHKERRQ(DMSetUp(da)); 84 85 /* Check self-compatibility */ 86 { 87 PetscBool compatible,set; 88 CHKERRQ(DMGetCompatibility(da,da,&compatible,&set)); 89 if (!set || !compatible) { 90 CHKERRQ(PetscPrintf(PetscObjectComm((PetscObject)da),"Error: DM not compatible with itself\n")); 91 } 92 } 93 94 /* Check compatibility with the same DM on a dup'd communicator */ 95 { 96 DM da2; 97 PetscBool compatible,set; 98 MPI_Comm comm2; 99 CHKERRMPI(MPI_Comm_dup(PETSC_COMM_WORLD,&comm2)); 100 CHKERRQ(DMDACreate3d(comm2,bx,by,bz,stencil_type,M,N,P,m,n,p,w,s,lx,ly,lz,&da2)); 101 CHKERRQ(DMSetFromOptions(da2)); 102 CHKERRQ(DMSetUp(da2)); 103 CHKERRQ(DMGetCompatibility(da,da2,&compatible,&set)); 104 if (!set || !compatible) { 105 CHKERRQ(PetscPrintf(PetscObjectComm((PetscObject)da),"Error: DM not compatible with DMDA on dup'd comm\n")); 106 } 107 CHKERRQ(DMDestroy(&da2)); 108 CHKERRMPI(MPI_Comm_free(&comm2)); 109 } 110 111 /* Check compatibility with a derived DMDA */ 112 { 113 DM da2; 114 PetscBool compatible,set; 115 CHKERRQ(DMDACreateCompatibleDMDA(da,w*2,&da2)); 116 CHKERRQ(DMGetCompatibility(da,da2,&compatible,&set)); 117 if (!set || !compatible) { 118 CHKERRQ(PetscPrintf(PetscObjectComm((PetscObject)da),"Error: DM not compatible with DMDA created with DMDACreateCompatibleDMDA()\n")); 119 } 120 CHKERRQ(DMDestroy(&da2)); 121 } 122 123 /* Confirm incompatibility with different stencil width */ 124 { 125 DM da2; 126 PetscBool compatible,set; 127 CHKERRQ(DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,stencil_type,M,N,P,m,n,p,w,0,lx,ly,lz,&da2)); 128 CHKERRQ(DMSetUp(da2)); 129 CHKERRQ(DMGetCompatibility(da,da2,&compatible,&set)); 130 if (!set || compatible) { 131 CHKERRQ(PetscPrintf(PetscObjectComm((PetscObject)da),"Error: DM not determined incompatible with known-incompatible DMDA (different stencil width)\n")); 132 } 133 CHKERRQ(DMDestroy(&da2)); 134 } 135 136 /* Confirm incompatibility with different boundary types */ 137 { 138 DM da2; 139 PetscBool compatible,set; 140 DMBoundaryType bz2; 141 bz2 = bz == DM_BOUNDARY_NONE ? DM_BOUNDARY_GHOSTED : DM_BOUNDARY_NONE; 142 CHKERRQ(DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz2,stencil_type,M,N,P,m,n,p,w,s,lx,ly,lz,&da2)); 143 CHKERRQ(DMSetUp(da2)); 144 CHKERRQ(DMGetCompatibility(da,da2,&compatible,&set)); 145 if (!set || compatible) { 146 CHKERRQ(PetscPrintf(PetscObjectComm((PetscObject)da),"Error: DM not determined incompatible with known-incompatible DMDA (different boundary type)\n")); 147 } 148 CHKERRQ(DMDestroy(&da2)); 149 } 150 151 if (!distribute) { 152 /* Confirm incompatibility with different global sizes */ 153 { 154 DM da2; 155 PetscBool compatible,set; 156 CHKERRQ(DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,stencil_type,M,N,P*2,m,n,p,w,s,lx,ly,lz,&da2)); 157 CHKERRQ(DMSetUp(da2)); 158 CHKERRQ(DMGetCompatibility(da,da2,&compatible,&set)); 159 if (!set || compatible) { 160 CHKERRQ(PetscPrintf(PetscObjectComm((PetscObject)da),"Error: DM not determined incompatible with known-incompatible DMDA (different global sizes)\n")); 161 } 162 CHKERRQ(DMDestroy(&da2)); 163 } 164 } 165 166 if (distribute && p > 1) { 167 /* Confirm incompatibility with different local size */ 168 { 169 DM da2; 170 PetscBool compatible,set; 171 PetscMPIInt rank; 172 PetscInt *lz2; 173 CHKERRQ(PetscMalloc1(p,&lz2)); 174 for (i=0; i<p-1; i++) lz2[i] = 1; /* One point per rank instead of 2 */ 175 lz2[p-1] = P - (p-1); 176 CHKERRMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 177 CHKERRQ(DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,stencil_type,M,N,P,m,n,p,w,s,lx,ly,lz2,&da2)); 178 CHKERRQ(DMSetUp(da2)); 179 CHKERRQ(DMGetCompatibility(da,da2,&compatible,&set)); 180 if (!set || compatible) { 181 CHKERRQ(PetscPrintf(PetscObjectComm((PetscObject)da),"Error: DM not determined incompatible with known-incompatible DMDA (different local sizes) \n")); 182 } 183 CHKERRQ(DMDestroy(&da2)); 184 CHKERRQ(PetscFree(lz2)); 185 } 186 } 187 188 /* Check compatibility with a DM of different type (DMStag) */ 189 { 190 DM dm2; 191 PetscBool compatible,set; 192 CHKERRQ(DMStagCreate3d(PETSC_COMM_WORLD,bx,by,bz,M,N,P,m,n,p,1,1,1,1,DMSTAG_STENCIL_STAR,w,lx,ly,lz,&dm2)); 193 CHKERRQ(DMSetUp(dm2)); 194 CHKERRQ(DMGetCompatibility(da,dm2,&compatible,&set)); 195 /* Don't interpret the result, but note that one can run with -info */ 196 CHKERRQ(DMDestroy(&dm2)); 197 } 198 199 /* Free memory */ 200 CHKERRQ(PetscFree(lx)); 201 CHKERRQ(PetscFree(ly)); 202 CHKERRQ(PetscFree(lz)); 203 CHKERRQ(DMDestroy(&da)); 204 ierr = PetscFinalize(); 205 return ierr; 206 } 207 208 /*TEST 209 210 test: 211 suffix: 1 212 213 test: 214 suffix: 2 215 nsize: 3 216 args: distribute -m 1 -n 1 -p 3 -NZ 20 217 218 TEST*/ 219