Actual source code: ex2.c

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

 4:  #include petscda.h
 5:  #include petscsys.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;
 14:   PetscTruth     flg;
 15:   DA             da;
 16:   PetscViewer    viewer;
 17:   Vec            localall,global;
 18:   PetscScalar    value,*vlocal;
 19:   DAPeriodicType ptype = DA_NONPERIODIC;
 20:   DAStencilType  stype = DA_STENCIL_BOX;
 21:   VecScatter     tolocalall,fromlocalall;

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

 26:   /* Read options */
 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:   PetscOptionsHasName(PETSC_NULL,"-star_stencil",&flg);
 32:   if (flg) stype = DA_STENCIL_STAR;

 34:   /* Create distributed array and get vectors */
 35:   DACreate2d(PETSC_COMM_WORLD,ptype,stype,
 36:                     M,N,m,n,1,1,PETSC_NULL,PETSC_NULL,&da);
 37:   DACreateGlobalVector(da,&global);
 38:   VecCreateSeq(PETSC_COMM_SELF,M*N,&localall);

 40:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 41:   value = 5.0*rank;
 42:   VecSet(&value,global);

 44:   VecView(global,viewer);

 46:   /*
 47:      Create Scatter from global DA parallel vector to local vector that
 48:    contains all entries
 49:   */
 50:   DAGlobalToNaturalAllCreate(da,&tolocalall);
 51:   DANaturalAllToGlobalCreate(da,&fromlocalall);

 53:   VecScatterBegin(global,localall,INSERT_VALUES,SCATTER_FORWARD,tolocalall);
 54:   VecScatterEnd(global,localall,INSERT_VALUES,SCATTER_FORWARD,tolocalall);

 56:   VecGetArray(localall,&vlocal);
 57:   for (j=0; j<N; j++) {
 58:     for (i=0; i<M; i++) {
 59:       *vlocal++ += i + j*M;
 60:     }
 61:   }
 62:   VecRestoreArray(localall,&vlocal);

 64:   /* scatter back to global vector */
 65:   VecScatterBegin(localall,global,INSERT_VALUES,SCATTER_FORWARD,fromlocalall);
 66:   VecScatterEnd(localall,global,INSERT_VALUES,SCATTER_FORWARD,fromlocalall);

 68:   VecView(global,viewer);

 70:   /* Free memory */
 71:   VecScatterDestroy(tolocalall);
 72:   VecScatterDestroy(fromlocalall);
 73:   PetscViewerDestroy(viewer);
 74:   VecDestroy(localall);
 75:   VecDestroy(global);
 76:   DADestroy(da);
 77:   PetscFinalize();
 78:   return 0;
 79: }
 80: