Actual source code: ex17.c

  1: /*$Id: ex17.c,v 1.38 2001/09/11 16:32:10 bsmith Exp $*/

  3: static char help[] = "Scatters from a parallel vector to a sequential vector.  In\n\
  4: this case each local vector is as long as the entire parallel vector.\n\n";

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

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

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

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

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

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

 45:   VecScatterCreate(y,is2,x,is1,&ctx);
 46:   VecScatterBegin(y,x,ADD_VALUES,SCATTER_FORWARD,ctx);
 47:   VecScatterEnd(y,x,ADD_VALUES,SCATTER_FORWARD,ctx);
 48:   VecScatterDestroy(ctx);
 49: 
 50:   if (!rank)
 51:     {printf("----\n"); VecView(x,PETSC_VIEWER_STDOUT_SELF);}

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

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