Actual source code: ex6.c
petsc-dev 2014-02-02
1: static char help[] = "Tests various 3-dimensional DMDA routines.\n\n";
3: #include <petscdmda.h>
4: #include <petscao.h>
8: int main(int argc,char **argv)
9: {
10: PetscMPIInt rank;
11: PetscInt M = 3,N = 5,P=3,s=1,w=2,nloc,l,i,j,k,kk,m = PETSC_DECIDE,n = PETSC_DECIDE,p = PETSC_DECIDE;
12: PetscErrorCode ierr;
13: PetscInt Xs,Xm,Ys,Ym,Zs,Zm,iloc,*iglobal;
14: const PetscInt *ltog;
15: PetscInt *lx = NULL,*ly = NULL,*lz = NULL;
16: PetscBool test_order = PETSC_FALSE;
17: DM da;
18: PetscViewer viewer;
19: Vec local,global;
20: PetscScalar value;
21: DMDABoundaryType bx = DMDA_BOUNDARY_NONE,by = DMDA_BOUNDARY_NONE,bz = DMDA_BOUNDARY_NONE;
22: DMDAStencilType stencil_type = DMDA_STENCIL_BOX;
23: AO ao;
24: PetscBool flg = PETSC_FALSE;
26: PetscInitialize(&argc,&argv,(char*)0,help);
27: PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",300,0,400,300,&viewer);
29: /* Read options */
30: PetscOptionsGetInt(NULL,"-NX",&M,NULL);
31: PetscOptionsGetInt(NULL,"-NY",&N,NULL);
32: PetscOptionsGetInt(NULL,"-NZ",&P,NULL);
33: PetscOptionsGetInt(NULL,"-m",&m,NULL);
34: PetscOptionsGetInt(NULL,"-n",&n,NULL);
35: PetscOptionsGetInt(NULL,"-p",&p,NULL);
36: PetscOptionsGetInt(NULL,"-s",&s,NULL);
37: PetscOptionsGetInt(NULL,"-w",&w,NULL);
38: flg = PETSC_FALSE;
39: PetscOptionsGetBool(NULL,"-star",&flg,NULL);
40: if (flg) stencil_type = DMDA_STENCIL_STAR;
41: flg = PETSC_FALSE;
42: PetscOptionsGetBool(NULL,"-box",&flg,NULL);
43: if (flg) stencil_type = DMDA_STENCIL_BOX;
45: flg = PETSC_FALSE;
46: PetscOptionsGetBool(NULL,"-xperiodic",&flg,NULL);
47: if (flg) bx = DMDA_BOUNDARY_PERIODIC;
48: flg = PETSC_FALSE;
49: PetscOptionsGetBool(NULL,"-xghosted",&flg,NULL);
50: if (flg) bx = DMDA_BOUNDARY_GHOSTED;
51: flg = PETSC_FALSE;
52: PetscOptionsGetBool(NULL,"-xnonghosted",&flg,NULL);
54: flg = PETSC_FALSE;
55: PetscOptionsGetBool(NULL,"-yperiodic",&flg,NULL);
56: if (flg) by = DMDA_BOUNDARY_PERIODIC;
57: flg = PETSC_FALSE;
58: PetscOptionsGetBool(NULL,"-yghosted",&flg,NULL);
59: if (flg) by = DMDA_BOUNDARY_GHOSTED;
60: flg = PETSC_FALSE;
61: PetscOptionsGetBool(NULL,"-ynonghosted",&flg,NULL);
63: flg = PETSC_FALSE;
64: PetscOptionsGetBool(NULL,"-zperiodic",&flg,NULL);
65: if (flg) bz = DMDA_BOUNDARY_PERIODIC;
66: flg = PETSC_FALSE;
67: PetscOptionsGetBool(NULL,"-zghosted",&flg,NULL);
68: if (flg) bz = DMDA_BOUNDARY_GHOSTED;
69: flg = PETSC_FALSE;
70: PetscOptionsGetBool(NULL,"-znonghosted",&flg,NULL);
72: PetscOptionsGetBool(NULL,"-testorder",&test_order,NULL);
74: flg = PETSC_FALSE;
75: PetscOptionsGetBool(NULL,"-distribute",&flg,NULL);
76: if (flg) {
77: if (m == PETSC_DECIDE) SETERRQ(PETSC_COMM_WORLD,1,"Must set -m option with -distribute option");
78: PetscMalloc1(m,&lx);
79: for (i=0; i<m-1; i++) lx[i] = 4;
80: lx[m-1] = M - 4*(m-1);
81: if (n == PETSC_DECIDE) SETERRQ(PETSC_COMM_WORLD,1,"Must set -n option with -distribute option");
82: PetscMalloc1(n,&ly);
83: for (i=0; i<n-1; i++) ly[i] = 2;
84: ly[n-1] = N - 2*(n-1);
85: if (p == PETSC_DECIDE) SETERRQ(PETSC_COMM_WORLD,1,"Must set -p option with -distribute option");
86: PetscMalloc1(p,&lz);
87: for (i=0; i<p-1; i++) lz[i] = 2;
88: lz[p-1] = P - 2*(p-1);
89: }
91: /* Create distributed array and get vectors */
92: DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,stencil_type,M,N,P,m,n,p,w,s,lx,ly,lz,&da);
93: PetscFree(lx);
94: PetscFree(ly);
95: PetscFree(lz);
96: DMView(da,viewer);
97: DMCreateGlobalVector(da,&global);
98: DMCreateLocalVector(da,&local);
100: /* Set global vector; send ghost points to local vectors */
101: value = 1;
102: VecSet(global,value);
103: DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
104: DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);
106: /* Scale local vectors according to processor rank; pass to global vector */
107: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
108: value = rank;
109: VecScale(local,value);
110: DMLocalToGlobalBegin(da,local,INSERT_VALUES,global);
111: DMLocalToGlobalEnd(da,local,INSERT_VALUES,global);
113: if (!test_order) { /* turn off printing when testing ordering mappings */
114: if (M*N*P<40) {
115: PetscPrintf(PETSC_COMM_WORLD,"\nGlobal Vector:\n");
116: VecView(global,PETSC_VIEWER_STDOUT_WORLD);
117: PetscPrintf(PETSC_COMM_WORLD,"\n");
118: }
119: }
121: /* Send ghost points to local vectors */
122: DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
123: DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);
125: flg = PETSC_FALSE;
126: PetscOptionsGetBool(NULL,"-local_print",&flg,NULL);
127: if (flg) {
128: PetscViewer sviewer;
129: PetscViewerASCIISynchronizedAllow(PETSC_VIEWER_STDOUT_WORLD,PETSC_TRUE);
130: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"\nLocal Vector: processor %d\n",rank);
131: PetscViewerGetSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
132: VecView(local,sviewer);
133: PetscViewerRestoreSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
134: PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);
135: }
137: /* Tests mappings betweeen application/PETSc orderings */
138: if (test_order) {
139: DMDAGetGhostCorners(da,&Xs,&Ys,&Zs,&Xm,&Ym,&Zm);
140: DMDAGetGlobalIndices(da,&nloc,<og);
141: DMDAGetAO(da,&ao);
142: /* AOView(ao,PETSC_VIEWER_STDOUT_WORLD); */
143: PetscMalloc1(nloc,&iglobal);
145: /* Set iglobal to be global indices for each processor's local and ghost nodes,
146: using the DMDA ordering of grid points */
147: kk = 0;
148: for (k=Zs; k<Zs+Zm; k++) {
149: for (j=Ys; j<Ys+Ym; j++) {
150: for (i=Xs; i<Xs+Xm; i++) {
151: iloc = w*((k-Zs)*Xm*Ym + (j-Ys)*Xm + i-Xs);
152: for (l=0; l<w; l++) {
153: iglobal[kk++] = ltog[iloc+l];
154: }
155: }
156: }
157: }
159: /* Map this to the application ordering (which for DMDAs is just the natural ordering
160: that would be used for 1 processor, numbering most rapidly by x, then y, then z) */
161: AOPetscToApplication(ao,nloc,iglobal);
163: /* Then map the application ordering back to the PETSc DMDA ordering */
164: AOApplicationToPetsc(ao,nloc,iglobal);
166: /* Verify the mappings */
167: kk=0;
168: for (k=Zs; k<Zs+Zm; k++) {
169: for (j=Ys; j<Ys+Ym; j++) {
170: for (i=Xs; i<Xs+Xm; i++) {
171: iloc = w*((k-Zs)*Xm*Ym + (j-Ys)*Xm + i-Xs);
172: for (l=0; l<w; l++) {
173: if (iglobal[kk] != ltog[iloc+l]) {
174: PetscPrintf(MPI_COMM_WORLD,"[%D] Problem with mapping: z=%D, j=%D, i=%D, l=%D, petsc1=%D, petsc2=%D\n",
175: rank,k,j,i,l,ltog[iloc+l],iglobal[kk]);
176: }
177: kk++;
178: }
179: }
180: }
181: }
182: PetscFree(iglobal);
183: DMDARestoreGlobalIndices(da,&nloc,<og);
184: }
186: /* Free memory */
187: PetscViewerDestroy(&viewer);
188: VecDestroy(&local);
189: VecDestroy(&global);
190: DMDestroy(&da);
191: PetscFinalize();
192: return 0;
193: }