Actual source code: ex9.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:   PetscInt       n = 5,i,idx2[3] = {0,2,3},idx1[3] = {0,1,2};
     12:   PetscMPIInt    size,rank;
     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,PETSC_DECIDE,size*n);
     25:   VecSetFromOptions(x);
     26:   VecCreateSeq(PETSC_COMM_SELF,n,&y);

     28:   /* create two index sets */
     29:   ISCreateGeneral(PETSC_COMM_SELF,3,idx1,PETSC_COPY_VALUES,&is1);
     30:   ISCreateGeneral(PETSC_COMM_SELF,3,idx2,PETSC_COPY_VALUES,&is2);

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

     40:   VecView(x,PETSC_VIEWER_STDOUT_WORLD);

     42:   VecSet(y,-1.0);

     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:     PetscPrintf(PETSC_COMM_SELF,"scattered vector\n");
     51:     VecView(y,PETSC_VIEWER_STDOUT_SELF);
     52:   }
     53:   ISDestroy(&is1);
     54:   ISDestroy(&is2);
     55:   VecDestroy(&x);
     56:   VecDestroy(&y);

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