Actual source code: ex12.c

  2: static char help[] = "Scatters from a sequential vector to a parallel vector.\n\
  3: This does case when we are merely selecting the local part of the\n\
  4: parallel vector.\n";

 6:  #include petscvec.h
 7:  #include petscsys.h

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

 21:   PetscInitialize(&argc,&argv,(char*)0,help);

 23:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 24:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 26:   /* create two vectors */
 27:   VecCreate(PETSC_COMM_WORLD,&x);
 28:   VecSetSizes(x,PETSC_DECIDE,size*n);
 29:   VecSetFromOptions(x);
 30:   VecCreateSeq(PETSC_COMM_SELF,n,&y);

 32:   /* create two index sets */
 33:   ISCreateStride(PETSC_COMM_SELF,n,n*rank,1,&is1);
 34:   ISCreateStride(PETSC_COMM_SELF,n,0,1,&is2);

 36:   /* each processor inserts the entire vector */
 37:   /* this is redundant but tests assembly */
 38:   for (i=0; i<n; i++) {
 39:     value = (PetscScalar) (i + 10*rank);
 40:     VecSetValues(y,1,&i,&value,INSERT_VALUES);
 41:   }
 42:   VecAssemblyBegin(y);
 43:   VecAssemblyEnd(y);

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

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

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