Actual source code: ex9.c

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

  3: static char help[]= "Scatters from a parallel vector to a sequential vector.nn";

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

  8: int main(int argc,char **argv)
  9: {
 10:   int           n = 5,ierr,idx2[3] = {0,2,3},idx1[3] = {0,1,2};
 11:   int           size,rank,i;
 12:   Scalar        mone = -1.0,value;
 13:   Vec           x,y;
 14:   IS            is1,is2;
 15:   VecScatter    ctx = 0;

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

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

 25:   /* create two index sets */
 26:   ISCreateGeneral(PETSC_COMM_SELF,3,idx1,&is1);
 27:   ISCreateGeneral(PETSC_COMM_SELF,3,idx2,&is2);

 29:   /* fill local part of parallel vector */
 30:   for (i=n*rank; i<n*(rank+1); i++) {
 31:     value = (Scalar) i;
 32:     VecSetValues(x,1,&i,&value,INSERT_VALUES);
 33:   }
 34:   VecAssemblyBegin(x);
 35:   VecAssemblyEnd(x);

 37:   VecView(x,PETSC_VIEWER_STDOUT_WORLD);

 39:   VecSet(&mone,y);

 41:   VecScatterCreate(x,is1,y,is2,&ctx);
 42:   VecScatterBegin(x,y,INSERT_VALUES,SCATTER_FORWARD,ctx);
 43:   VecScatterEnd(x,y,INSERT_VALUES,SCATTER_FORWARD,ctx);
 44:   VecScatterDestroy(ctx);

 46:   if (!rank) {
 47:     PetscPrintf(PETSC_COMM_SELF,"scattered vectorn");
 48:     VecView(y,PETSC_VIEWER_STDOUT_SELF);
 49:   }
 50:   ISDestroy(is1);
 51:   ISDestroy(is2);
 52:   VecDestroy(x);
 53:   VecDestroy(y);

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