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

      2: static char help[] = "Tests DMDAGlobalToNaturalAllCreate() using contour plotting for 2d DMDAs.\n\n";

      4: #include <petscdmda.h>
      5: #include <petscdraw.h>

      9: int main(int argc,char **argv)
     10: {
     11:   PetscInt         i,j,M = 10,N = 8,m = PETSC_DECIDE,n = PETSC_DECIDE;
     12:   PetscMPIInt      rank;
     13:   PetscErrorCode   ierr;
     14:   PetscBool        flg = PETSC_FALSE;
     15:   DM               da;
     16:   PetscViewer      viewer;
     17:   Vec              localall,global;
     18:   PetscScalar      value,*vlocal;
     19:   DMDABoundaryType bx    = DMDA_BOUNDARY_NONE,by = DMDA_BOUNDARY_NONE;
     20:   DMDAStencilType  stype = DMDA_STENCIL_BOX;
     21:   VecScatter       tolocalall,fromlocalall;
     22:   PetscInt         start,end;

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

     28:   /* Read options */
     29:   PetscOptionsGetInt(NULL,"-M",&M,NULL);
     30:   PetscOptionsGetInt(NULL,"-N",&N,NULL);
     31:   PetscOptionsGetInt(NULL,"-m",&m,NULL);
     32:   PetscOptionsGetInt(NULL,"-n",&n,NULL);
     33:   PetscOptionsGetBool(NULL,"-star_stencil",&flg,NULL);
     34:   if (flg) stype = DMDA_STENCIL_STAR;

     36:   /* Create distributed array and get vectors */
     37:   DMDACreate2d(PETSC_COMM_WORLD,bx,by,stype,
     38:                       M,N,m,n,1,1,NULL,NULL,&da);
     39:   DMCreateGlobalVector(da,&global);
     40:   VecCreateSeq(PETSC_COMM_SELF,M*N,&localall);

     42:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
     43:   VecGetOwnershipRange(global,&start,&end);
     44:   for (i=start; i<end; i++) {
     45:     value = 5.0*rank;
     46:     VecSetValues(global,1,&i,&value,INSERT_VALUES);
     47:   }
     48:   VecView(global,viewer);

     50:   /*
     51:      Create Scatter from global DMDA parallel vector to local vector that
     52:    contains all entries
     53:   */
     54:   DMDAGlobalToNaturalAllCreate(da,&tolocalall);
     55:   DMDANaturalAllToGlobalCreate(da,&fromlocalall);

     57:   VecScatterBegin(tolocalall,global,localall,INSERT_VALUES,SCATTER_FORWARD);
     58:   VecScatterEnd(tolocalall,global,localall,INSERT_VALUES,SCATTER_FORWARD);

     60:   VecGetArray(localall,&vlocal);
     61:   for (j=0; j<N; j++) {
     62:     for (i=0; i<M; i++) {
     63:       *vlocal++ += i + j*M;
     64:     }
     65:   }
     66:   VecRestoreArray(localall,&vlocal);

     68:   /* scatter back to global vector */
     69:   VecScatterBegin(fromlocalall,localall,global,INSERT_VALUES,SCATTER_FORWARD);
     70:   VecScatterEnd(fromlocalall,localall,global,INSERT_VALUES,SCATTER_FORWARD);

     72:   VecView(global,viewer);

     74:   /* Free memory */
     75:   VecScatterDestroy(&tolocalall);
     76:   VecScatterDestroy(&fromlocalall);
     77:   PetscViewerDestroy(&viewer);
     78:   VecDestroy(&localall);
     79:   VecDestroy(&global);
     80:   DMDestroy(&da);
     81:   PetscFinalize();
     82:   return 0;
     83: }