Actual source code: ex2.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: static char help[] = "Tests DMDAGlobalToNaturalAllCreate() using contour plotting for 2d DMDAs.\n\n";

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

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


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

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

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

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

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

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

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

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

 73:   VecView(global,viewer);

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