Actual source code: ex11.c

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

 4:  #include petscvec.h
 5:  #include petscsys.h

  9: int main(int argc,char **argv)
 10: {
 12:   PetscMPIInt    size,rank;
 13:   PetscInt       i,N;
 14:   PetscScalar    mone = -1.0,value;
 15:   Vec            x,y;
 16:   IS             is1,is2;
 17:   VecScatter     ctx = 0;

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

 23:   /* create two vectors */
 24:   VecCreate(PETSC_COMM_WORLD,&x);
 25:   VecSetSizes(x,rank+1,PETSC_DECIDE);
 26:   VecSetFromOptions(x);
 27:   VecGetSize(x,&N);
 28:   VecCreateSeq(PETSC_COMM_SELF,N-rank,&y);

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

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

 43:   VecView(x,PETSC_VIEWER_STDOUT_WORLD);

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

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

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

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