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

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

      4: #include <petscvec.h>

      8: int main(int argc,char **argv)
      9: {
     11:   PetscMPIInt    size,rank;
     12:   PetscInt       i,N;
     13:   PetscScalar    value;
     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:   VecCreate(PETSC_COMM_WORLD,&x);
     24:   VecSetSizes(x,rank+1,PETSC_DECIDE);
     25:   VecSetFromOptions(x);
     26:   VecGetSize(x,&N);
     27:   VecCreateSeq(PETSC_COMM_SELF,N-rank,&y);

     29:   /* create two index sets */
     30:   ISCreateStride(PETSC_COMM_SELF,N-rank,rank,1,&is1);
     31:   ISCreateStride(PETSC_COMM_SELF,N-rank,0,1,&is2);

     33:   /* fill parallel vector: note this is not efficient way*/
     34:   for (i=0; i<N; i++) {
     35:     value = (PetscScalar) i;
     36:     VecSetValues(x,1,&i,&value,INSERT_VALUES);
     37:   }
     38:   VecAssemblyBegin(x);
     39:   VecAssemblyEnd(x);
     40:   VecSet(y,-1.0);

     42:   VecView(x,PETSC_VIEWER_STDOUT_WORLD);

     44:   VecScatterCreate(x,is1,y,is2,&ctx);
     45:   VecScatterBegin(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);
     46:   VecScatterEnd(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);
     47:   VecScatterDestroy(&ctx);

     49:   if (!rank) {
     50:     printf("----\n");
     51:     VecView(y,PETSC_VIEWER_STDOUT_SELF);
     52:   }

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

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