Actual source code: ex4.c
petsc-3.4.2 2013-07-02

      2: static char help[] = "Tests various 2-dimensional DMDA routines.\n\n";

      4: #include <petscdmda.h>

      8: int main(int argc,char **argv)
      9: {
     10:   PetscMPIInt      rank;
     11:   PetscErrorCode   ierr;
     12:   PetscInt         M = 10,N = 8,m = PETSC_DECIDE;
     13:   PetscInt         s =2,w=2,n = PETSC_DECIDE,nloc,l,i,j,kk;
     14:   PetscInt         Xs,Xm,Ys,Ym,iloc,*iglobal,*ltog;
     15:   PetscInt         *lx       = NULL,*ly = NULL;
     16:   PetscBool        testorder = PETSC_FALSE,flg;
     17:   DMDABoundaryType bx        = DMDA_BOUNDARY_NONE,by= DMDA_BOUNDARY_NONE;
     18:   DM               da;
     19:   PetscViewer      viewer;
     20:   Vec              local,global;
     21:   PetscScalar      value;
     22:   DMDAStencilType  st = DMDA_STENCIL_BOX;
     23:   AO               ao;

     25:   PetscInitialize(&argc,&argv,(char*)0,help);
     26:   PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",300,0,400,400,&viewer);

     28:   /* Readoptions */
     29:   PetscOptionsGetInt(NULL,"-NX",&M,NULL);
     30:   PetscOptionsGetInt(NULL,"-NY",&N,NULL);
     31:   PetscOptionsGetInt(NULL,"-m",&m,NULL);
     32:   PetscOptionsGetInt(NULL,"-n",&n,NULL);
     33:   PetscOptionsGetInt(NULL,"-s",&s,NULL);
     34:   PetscOptionsGetInt(NULL,"-w",&w,NULL);

     36:   flg  = PETSC_FALSE;
     37:   PetscOptionsGetBool(NULL,"-xperiodic",&flg,NULL); if (flg) bx = DMDA_BOUNDARY_PERIODIC;
     38:   flg  = PETSC_FALSE;
     39:   PetscOptionsGetBool(NULL,"-yperiodic",&flg,NULL); if (flg) by = DMDA_BOUNDARY_PERIODIC;
     40:   flg  = PETSC_FALSE;
     41:   PetscOptionsGetBool(NULL,"-xghosted",&flg,NULL); if (flg) bx = DMDA_BOUNDARY_GHOSTED;
     42:   flg  = PETSC_FALSE;
     43:   PetscOptionsGetBool(NULL,"-yghosted",&flg,NULL); if (flg) by = DMDA_BOUNDARY_GHOSTED;
     44:   flg  = PETSC_FALSE;
     45:   PetscOptionsGetBool(NULL,"-star",&flg,NULL); if (flg) st = DMDA_STENCIL_STAR;
     46:   flg  = PETSC_FALSE;
     47:   PetscOptionsGetBool(NULL,"-box",&flg,NULL); if (flg) st = DMDA_STENCIL_BOX;
     48:   flg  = PETSC_FALSE;
     49:   PetscOptionsGetBool(NULL,"-testorder",&testorder,NULL);
     50:   /*
     51:       Test putting two nodes in x and y on each processor, exact last processor
     52:       in x and y gets the rest.
     53:   */
     54:   flg  = PETSC_FALSE;
     55:   PetscOptionsGetBool(NULL,"-distribute",&flg,NULL);
     56:   if (flg) {
     57:     if (m == PETSC_DECIDE) SETERRQ(PETSC_COMM_WORLD,1,"Must set -m option with -distribute option");
     58:     PetscMalloc(m*sizeof(PetscInt),&lx);
     59:     for (i=0; i<m-1; i++) { lx[i] = 4;}
     60:     lx[m-1] = M - 4*(m-1);
     61:     if (n == PETSC_DECIDE) SETERRQ(PETSC_COMM_WORLD,1,"Must set -n option with -distribute option");
     62:     PetscMalloc(n*sizeof(PetscInt),&ly);
     63:     for (i=0; i<n-1; i++) { ly[i] = 2;}
     64:     ly[n-1] = N - 2*(n-1);
     65:   }

     68:   /* Create distributed array and get vectors */
     69:   DMDACreate2d(PETSC_COMM_WORLD,bx,by,st,M,N,m,n,w,s,lx,ly,&da);
     70:   PetscFree(lx);
     71:   PetscFree(ly);

     73:   DMView(da,viewer);
     74:   DMCreateGlobalVector(da,&global);
     75:   DMCreateLocalVector(da,&local);

     77:   /* Set global vector; send ghost points to local vectors */
     78:   value = 1;
     79:   VecSet(global,value);
     80:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
     81:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);

     83:   /* Scale local vectors according to processor rank; pass to global vector */
     84:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
     85:   value = rank;
     86:   VecScale(local,value);
     87:   DMLocalToGlobalBegin(da,local,INSERT_VALUES,global);
     88:   DMLocalToGlobalEnd(da,local,INSERT_VALUES,global);

     90:   if (!testorder) { /* turn off printing when testing ordering mappings */
     91:     PetscPrintf (PETSC_COMM_WORLD,"\nGlobal Vectors:\n");
     92:     VecView(global,PETSC_VIEWER_STDOUT_WORLD);
     93:     PetscPrintf (PETSC_COMM_WORLD,"\n\n");
     94:   }

     96:   /* Send ghost points to local vectors */
     97:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
     98:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);

    100:   flg  = PETSC_FALSE;
    101:   PetscOptionsGetBool(NULL,"-local_print",&flg,NULL);
    102:   if (flg) {
    103:     PetscViewer sviewer;
    104:     PetscSynchronizedPrintf(PETSC_COMM_WORLD,"\nLocal Vector: processor %d\n",rank);
    105:     PetscViewerGetSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
    106:     VecView(local,sviewer);
    107:     PetscViewerRestoreSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
    108:   }

    110:   /* Tests mappings betweeen application/PETSc orderings */
    111:   if (testorder) {
    112:     DMDAGetGhostCorners(da,&Xs,&Ys,NULL,&Xm,&Ym,NULL);
    113:     DMDAGetGlobalIndices(da,&nloc,&ltog);
    114:     DMDAGetAO(da,&ao);
    115:     PetscMalloc(nloc*sizeof(PetscInt),&iglobal);

    117:     /* Set iglobal to be global indices for each processor's local and ghost nodes,
    118:        using the DMDA ordering of grid points */
    119:     kk = 0;
    120:     for (j=Ys; j<Ys+Ym; j++) {
    121:       for (i=Xs; i<Xs+Xm; i++) {
    122:         iloc = w*((j-Ys)*Xm + i-Xs);
    123:         for (l=0; l<w; l++) {
    124:           iglobal[kk++] = ltog[iloc+l];
    125:         }
    126:       }
    127:     }

    129:     /* Map this to the application ordering (which for DMDAs is just the natural ordering
    130:        that would be used for 1 processor, numbering most rapidly by x, then y) */
    131:     AOPetscToApplication(ao,nloc,iglobal);

    133:     /* Then map the application ordering back to the PETSc DMDA ordering */
    134:     AOApplicationToPetsc(ao,nloc,iglobal);

    136:     /* Verify the mappings */
    137:     kk=0;
    138:     for (j=Ys; j<Ys+Ym; j++) {
    139:       for (i=Xs; i<Xs+Xm; i++) {
    140:         iloc = w*((j-Ys)*Xm + i-Xs);
    141:         for (l=0; l<w; l++) {
    142:           if (iglobal[kk] != ltog[iloc+l]) {
    143:             PetscFPrintf(PETSC_COMM_SELF,stdout,"[%d] Problem with mapping: j=%D, i=%D, l=%D, petsc1=%D, petsc2=%D\n",
    144:                                 rank,j,i,l,ltog[iloc+l],iglobal[kk]);
    145:           }
    146:           kk++;
    147:         }
    148:       }
    149:     }
    150:     PetscFree(iglobal);
    151:   }

    153:   /* Free memory */
    154:   PetscViewerDestroy(&viewer);
    155:   VecDestroy(&local);
    156:   VecDestroy(&global);
    157:   DMDestroy(&da);

    159:   PetscFinalize();
    160:   return 0;
    161: }