Actual source code: ex2.c


  2: static char help[] = "Tests vector scatter-gather operations.  Input arguments are\n\
  3:   -n <length> : vector length\n\n";

  5: #include <petscvec.h>

  7: int main(int argc,char **argv)
  8: {
  9:   PetscInt       n   = 5,idx1[2] = {0,3},idx2[2] = {1,4};
 10:   PetscScalar    one = 1.0,two = 2.0;
 11:   Vec            x,y;
 12:   IS             is1,is2;
 13:   VecScatter     ctx = 0;

 15:   PetscInitialize(&argc,&argv,(char*)0,help);
 16:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);

 18:   /* create two vector */
 19:   VecCreateSeq(PETSC_COMM_SELF,n,&x);
 20:   VecDuplicate(x,&y);

 22:   /* create two index sets */
 23:   ISCreateGeneral(PETSC_COMM_SELF,2,idx1,PETSC_COPY_VALUES,&is1);
 24:   ISCreateGeneral(PETSC_COMM_SELF,2,idx2,PETSC_COPY_VALUES,&is2);

 26:   VecSet(x,one);
 27:   VecSet(y,two);
 28:   VecScatterCreate(x,is1,y,is2,&ctx);
 29:   VecScatterBegin(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);
 30:   VecScatterEnd(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);

 32:   VecView(y,PETSC_VIEWER_STDOUT_SELF);

 34:   VecScatterBegin(ctx,y,x,INSERT_VALUES,SCATTER_FORWARD);
 35:   VecScatterEnd(ctx,y,x,INSERT_VALUES,SCATTER_FORWARD);
 36:   VecScatterDestroy(&ctx);

 38:   PetscPrintf(PETSC_COMM_SELF,"-------\n");
 39:   VecView(x,PETSC_VIEWER_STDOUT_SELF);

 41:   ISDestroy(&is1);
 42:   ISDestroy(&is2);

 44:   VecDestroy(&x);
 45:   VecDestroy(&y);

 47:   PetscFinalize();
 48:   return 0;
 49: }

 51: /*TEST

 53:      test:

 55: TEST*/