Actual source code: ex25.c

  2: static char help[] = "Scatters from a parallel vector to a sequential vector.  In\n\
  3: this case processor zero is as long as the entire parallel vector; rest are zero length.\n\n";

 5:  #include petscvec.h

  9: int main(int argc,char **argv)
 10: {
 12:   PetscInt       n = 5,N,low,high,iglobal,i;
 13:   PetscMPIInt    size,rank;
 14:   PetscScalar    value,zero = 0.0;
 15:   Vec            x,y;
 16:   IS             is1,is2;
 17:   VecScatter     ctx;

 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:   N = size*n;
 25:   VecCreate(PETSC_COMM_WORLD,&y);
 26:   VecSetSizes(y,PETSC_DECIDE,N);
 27:   VecSetFromOptions(y);
 28:   if (!rank) {
 29:     VecCreateSeq(PETSC_COMM_SELF,N,&x);
 30:   } else {
 31:     VecCreateSeq(PETSC_COMM_SELF,0,&x);
 32:   }

 34:   /* create two index sets */
 35:   if (!rank) {
 36:     ISCreateStride(PETSC_COMM_SELF,N,0,1,&is1);
 37:     ISCreateStride(PETSC_COMM_SELF,N,0,1,&is2);
 38:   } else {
 39:     ISCreateStride(PETSC_COMM_SELF,0,0,1,&is1);
 40:     ISCreateStride(PETSC_COMM_SELF,0,0,1,&is2);
 41:   }

 43:   VecSet(x,zero);
 44:   VecGetOwnershipRange(y,&low,&high);
 45:   for (i=0; i<n; i++) {
 46:     iglobal = i + low; value = (PetscScalar) (i + 10*rank);
 47:     VecSetValues(y,1,&iglobal,&value,INSERT_VALUES);
 48:   }
 49:   VecAssemblyBegin(y);
 50:   VecAssemblyEnd(y);
 51:   VecView(y,PETSC_VIEWER_STDOUT_WORLD);

 53:   VecScatterCreate(y,is2,x,is1,&ctx);
 54:   VecScatterBegin(ctx,y,x,ADD_VALUES,SCATTER_FORWARD);
 55:   VecScatterEnd(ctx,y,x,ADD_VALUES,SCATTER_FORWARD);
 56:   VecScatterDestroy(ctx);
 57: 
 58:   if (!rank)
 59:     {printf("----\n"); VecView(x,PETSC_VIEWER_STDOUT_SELF);}

 61:   VecDestroy(x);
 62:   VecDestroy(y);
 63:   ISDestroy(is1);
 64:   ISDestroy(is2);

 66:   PetscFinalize();
 67:   return 0;
 68: }
 69: