Actual source code: ex17.c

petsc-3.5.4 2015-05-23
Report Typos and Errors
  2: static char help[] = "Scatters from a parallel vector to a sequential vector.  In\n\
  3: this case each local vector is as long as the entire parallel vector.\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:   VecCreateSeq(PETSC_COMM_SELF,N,&x);

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

 34:   VecSet(x,zero);
 35:   VecGetOwnershipRange(y,&low,&high);
 36:   for (i=0; i<n; i++) {
 37:     iglobal = i + low; value = (PetscScalar) (i + 10*rank);
 38:     VecSetValues(y,1,&iglobal,&value,INSERT_VALUES);
 39:   }
 40:   VecAssemblyBegin(y);
 41:   VecAssemblyEnd(y);
 42:   VecView(y,PETSC_VIEWER_STDOUT_WORLD);

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

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

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

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