Actual source code: ex6.c
1: /*$Id: ex6.c,v 1.48 2001/08/07 03:04:42 balay Exp $*/
2:
3: static char help[] = "Tests various 3-dimensional DA routines.nn";
5: #include petscda.h
6: #include petscsys.h
7: #include petscao.h
9: int main(int argc,char **argv)
10: {
11: int rank,M = 3,N = 5,P=3,s=1,w=2,nloc,l,i,j,k,kk;
12: int m = PETSC_DECIDE,n = PETSC_DECIDE,p = PETSC_DECIDE,ierr;
13: int Xs,Xm,Ys,Ym,Zs,Zm,iloc,*ltog,*iglobal;
14: int *lx = PETSC_NULL,*ly = PETSC_NULL,*lz = PETSC_NULL;
15: PetscTruth test_order;
16: DA da;
17: PetscViewer viewer;
18: Vec local,global;
19: PetscScalar value;
20: DAPeriodicType wrap = DA_XYPERIODIC;
21: DAStencilType stencil_type = DA_STENCIL_BOX;
22: AO ao;
23: PetscTruth flg;
25: PetscInitialize(&argc,&argv,(char*)0,help);
26: PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",300,0,400,300,&viewer);
28: /* Read options */
29: PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);
30: PetscOptionsGetInt(PETSC_NULL,"-N",&N,PETSC_NULL);
31: PetscOptionsGetInt(PETSC_NULL,"-P",&P,PETSC_NULL);
32: PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
33: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
34: PetscOptionsGetInt(PETSC_NULL,"-p",&p,PETSC_NULL);
35: PetscOptionsGetInt(PETSC_NULL,"-s",&s,PETSC_NULL);
36: PetscOptionsGetInt(PETSC_NULL,"-w",&w,PETSC_NULL);
37: PetscOptionsHasName(PETSC_NULL,"-star",&flg);
38: if (flg) stencil_type = DA_STENCIL_STAR;
39: PetscOptionsHasName(PETSC_NULL,"-test_order",&test_order);
41: PetscOptionsHasName(PETSC_NULL,"-distribute",&flg);
42: if (flg) {
43: if (m == PETSC_DECIDE) SETERRQ(1,"Must set -m option with -distribute option");
44: PetscMalloc(m*sizeof(int),&lx);
45: for (i=0; i<m-1; i++) { lx[i] = 4;}
46: lx[m-1] = M - 4*(m-1);
47: if (n == PETSC_DECIDE) SETERRQ(1,"Must set -n option with -distribute option");
48: PetscMalloc(n*sizeof(int),&ly);
49: for (i=0; i<n-1; i++) { ly[i] = 2;}
50: ly[n-1] = N - 2*(n-1);
51: if (p == PETSC_DECIDE) SETERRQ(1,"Must set -p option with -distribute option");
52: PetscMalloc(p*sizeof(int),&lz);
53: for (i=0; i<p-1; i++) { lz[i] = 2;}
54: lz[p-1] = P - 2*(p-1);
55: }
57: /* Create distributed array and get vectors */
58: DACreate3d(PETSC_COMM_WORLD,wrap,stencil_type,M,N,P,m,n,p,w,s,
59: lx,ly,lz,&da);
60: if (lx) {
61: PetscFree(lx);
62: PetscFree(ly);
63: PetscFree(lz);
64: }
65: DAView(da,viewer);
66: DACreateGlobalVector(da,&global);
67: DACreateLocalVector(da,&local);
69: /* Set global vector; send ghost points to local vectors */
70: value = 1;
71: VecSet(&value,global);
72: DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);
73: DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);
75: /* Scale local vectors according to processor rank; pass to global vector */
76: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
77: value = rank;
78: VecScale(&value,local);
79: DALocalToGlobal(da,local,INSERT_VALUES,global);
81: if (!test_order) { /* turn off printing when testing ordering mappings */
82: if (M*N*P<40) {
83: PetscPrintf(PETSC_COMM_WORLD,"nGlobal Vector:n");
84: VecView(global,PETSC_VIEWER_STDOUT_WORLD);
85: PetscPrintf(PETSC_COMM_WORLD,"n");
86: }
87: }
89: /* Send ghost points to local vectors */
90: DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);
91: DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);
93: PetscOptionsHasName(PETSC_NULL,"-local_print",&flg);
94: if (flg) {
95: PetscViewer sviewer;
96: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"nLocal Vector: processor %dn",rank);
97: PetscViewerGetSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
98: VecView(local,sviewer);
99: PetscViewerRestoreSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
100: PetscSynchronizedFlush(PETSC_COMM_WORLD);
101: }
103: /* Tests mappings betweeen application/PETSc orderings */
104: if (test_order) {
105: DAGetGhostCorners(da,&Xs,&Ys,&Zs,&Xm,&Ym,&Zm);
106: DAGetGlobalIndices(da,&nloc,<og);
107: DAGetAO(da,&ao);
108: /* AOView(ao,PETSC_VIEWER_STDOUT_WORLD); */
109: PetscMalloc(nloc*sizeof(int),&iglobal);
111: /* Set iglobal to be global indices for each processor's local and ghost nodes,
112: using the DA ordering of grid points */
113: kk = 0;
114: for (k=Zs; k<Zs+Zm; k++) {
115: for (j=Ys; j<Ys+Ym; j++) {
116: for (i=Xs; i<Xs+Xm; i++) {
117: iloc = w*((k-Zs)*Xm*Ym + (j-Ys)*Xm + i-Xs);
118: for (l=0; l<w; l++) {
119: iglobal[kk++] = ltog[iloc+l];
120: }
121: }
122: }
123: }
125: /* Map this to the application ordering (which for DAs is just the natural ordering
126: that would be used for 1 processor, numbering most rapidly by x, then y, then z) */
127: AOPetscToApplication(ao,nloc,iglobal);
129: /* Then map the application ordering back to the PETSc DA ordering */
130: AOApplicationToPetsc(ao,nloc,iglobal);
132: /* Verify the mappings */
133: kk=0;
134: for (k=Zs; k<Zs+Zm; k++) {
135: for (j=Ys; j<Ys+Ym; j++) {
136: for (i=Xs; i<Xs+Xm; i++) {
137: iloc = w*((k-Zs)*Xm*Ym + (j-Ys)*Xm + i-Xs);
138: for (l=0; l<w; l++) {
139: if (iglobal[kk] != ltog[iloc+l]) {
140: fprintf(stdout,"[%d] Problem with mapping: z=%d, j=%d, i=%d, l=%d, petsc1=%d, petsc2=%dn",
141: rank,k,j,i,l,ltog[iloc+l],iglobal[kk]);
142: }
143: kk++;
144: }
145: }
146: }
147: }
148: PetscFree(iglobal);
149: }
151: /* Free memory */
152: PetscViewerDestroy(viewer);
153: VecDestroy(local);
154: VecDestroy(global);
155: DADestroy(da);
156: PetscFinalize();
157: return 0;
158: }
159: