#include "petsc.h" #include "petscvec.h" #include "petscda.h" static char help[] = "Test on vecscattertozero\n\n"; int main(int argc,char **argv) { VecScatter ctx; DA da; int nx, ny, i, j, ij; Vec coords, scoords; int rank; PetscInitialize(&argc,&argv,(char *)0,help); nx = 5; ny = 5; DACreate2d( PETSC_COMM_WORLD , // MPI Communicator DA_NONPERIODIC , // The grid is non periodic DA_STENCIL_STAR , // choose the appropriate stencil nx ,ny , // global dimension in each direction of the array PETSC_DECIDE,PETSC_DECIDE , // let petsc decide what are the LOCAL dimensions on each processors 3 , // number of degree of freedom per node 1 , // stencil width (for ghost points) PETSC_NULL, PETSC_NULL , // let it null &da); // the DA to be created. DASetUniformCoordinates(da , // the DA 0.0 , // x0 1.0, // xmax 0.0, // y0 1.0, // ymax 0.0, 0.0); // z0 and zmax are null because 2D DAGetCoordinates(da, &coords); // put these coord. in a global vector VecCreateSeq(PETSC_COMM_SELF, nx*ny*2, &scoords); VecScatterCreateToZero(coords, &ctx, &scoords); VecScatterBegin(ctx,coords,scoords,INSERT_VALUES,SCATTER_FORWARD); VecScatterEnd(ctx,coords,scoords,INSERT_VALUES,SCATTER_FORWARD); MPI_Comm_rank(PETSC_COMM_WORLD, &rank); VecView(coords, PETSC_VIEWER_STDOUT_WORLD); PetscPrintf(PETSC_COMM_WORLD, "\n\n"); VecView(scoords, PETSC_VIEWER_STDOUT_SELF); DADestroy(da); VecDestroy(coords); PetscFinalize(); return 0; }