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

      2: static char help[] = "Scatters from a parallel vector to a parallel vector.\n\n";

      4: #include <petscvec.h>

      8: int main(int argc,char **argv)
      9: {
     11:   PetscInt       n = 5,N,i;
     12:   PetscMPIInt    size,rank;
     13:   PetscScalar    value,zero = 0.0;
     14:   Vec            x,y;
     15:   IS             is1,is2;
     16:   VecScatter     ctx = 0;

     18:   PetscInitialize(&argc,&argv,(char*)0,help);
     19:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
     20:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

     22:   /* create two vectors */
     23:   N    = size*n;
     24:   VecCreate(PETSC_COMM_WORLD,&y);
     25:   VecSetSizes(y,n,PETSC_DECIDE);
     26:   VecSetFromOptions(y);

     28:   VecCreate(PETSC_COMM_WORLD,&x);
     29:   VecSetSizes(x,n,PETSC_DECIDE);
     30:   VecSetFromOptions(x);

     32:   /* create two index sets */
     33:   ISCreateStride(PETSC_COMM_WORLD,n,n*rank,1,&is1);
     34:   ISCreateStride(PETSC_COMM_WORLD,n,(n*(rank+1))%N,1,&is2);

     36:   /* fill local part of parallel vector x */
     37:   value = (PetscScalar)(rank+1);
     38:   for (i=n*rank; i<n*(rank+1); i++) {
     39:     VecSetValues(x,1,&i,&value,INSERT_VALUES);
     40:   }
     41:   VecAssemblyBegin(x);
     42:   VecAssemblyEnd(x);

     44:   VecSet(y,zero);

     46:   VecScatterCreate(x,is1,y,is2,&ctx);
     47:   VecScatterBegin(ctx,x,y,ADD_VALUES,SCATTER_FORWARD);
     48:   VecScatterEnd(ctx,x,y,ADD_VALUES,SCATTER_FORWARD);
     49:   VecScatterDestroy(&ctx);

     51:   VecView(y,PETSC_VIEWER_STDOUT_WORLD);

     53:   VecDestroy(&x);
     54:   VecDestroy(&y);
     55:   ISDestroy(&is1);
     56:   ISDestroy(&is2);

     58:   PetscFinalize();
     59:   return 0;
     60: }