Actual source code: ex17.c

  1: /*$Id: ex17.c,v 1.35 2001/03/23 23:21:30 balay Exp $*/

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

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

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

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

 22:   /* create two vectors */
 23:   N = size*n;
 24:   VecCreateMPI(PETSC_COMM_WORLD,PETSC_DECIDE,N,&y);
 25:   VecCreateSeq(PETSC_COMM_SELF,N,&x);

 27:   /* create two index sets */
 28:   ISCreateStride(PETSC_COMM_SELF,N,0,1,&is1);
 29:   ISCreateStride(PETSC_COMM_SELF,N,0,1,&is2);

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

 41:   VecScatterCreate(y,is2,x,is1,&ctx);
 42:   VecScatterBegin(y,x,ADD_VALUES,SCATTER_FORWARD,ctx);
 43:   VecScatterEnd(y,x,ADD_VALUES,SCATTER_FORWARD,ctx);
 44:   VecScatterDestroy(ctx);
 45: 
 46:   if (!rank)
 47:     {printf("----n"); VecView(x,PETSC_VIEWER_STDOUT_SELF);}

 49:   VecDestroy(x);
 50:   VecDestroy(y);
 51:   ISDestroy(is1);
 52:   ISDestroy(is2);

 54:   PetscFinalize();
 55:   return 0;
 56: }
 57: