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