Actual source code: ex4.c

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

 4:  #include petscvec.h

  8: int main(int argc,char **argv)
  9: {
 11:   PetscMPIInt    rank;
 12:   PetscInt       n = 5,idx1[2] = {0,3},idx2[2] = {1,4};
 13:   PetscScalar    one = 1.0,two = 2.0;
 14:   Vec            x,y;
 15:   IS             is1,is2;
 16:   VecScatter     ctx = 0;

 18:   PetscInitialize(&argc,&argv,(char*)0,help);
 19:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
 20:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 22:   /* create two vectors */
 23:   VecCreate(PETSC_COMM_WORLD,&x);
 24:   VecSetSizes(x,n,PETSC_DECIDE);
 25:   VecSetFromOptions(x);
 26:   VecCreateSeq(PETSC_COMM_SELF,n,&y);

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

 32:   VecSet(x,one);
 33:   VecSet(y,two);
 34:   VecScatterCreate(x,is1,y,is2,&ctx);
 35:   VecScatterBegin(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);
 36:   VecScatterEnd(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);
 37:   VecScatterDestroy(ctx);
 38: 
 39:   if (!rank) {VecView(y,PETSC_VIEWER_STDOUT_SELF);}

 41:   ISDestroy(is1);
 42:   ISDestroy(is2);

 44:   VecDestroy(x);
 45:   VecDestroy(y);
 46:   PetscFinalize();

 48:   return 0;
 49: }