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:   PetscFunctionBeginUser;
 16:   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
 17:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));

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

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

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

 33:   PetscCall(VecView(y, PETSC_VIEWER_STDOUT_SELF));

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

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

 42:   PetscCall(ISDestroy(&is1));
 43:   PetscCall(ISDestroy(&is2));

 45:   PetscCall(VecDestroy(&x));
 46:   PetscCall(VecDestroy(&y));

 48:   PetscCall(PetscFinalize());
 49:   return 0;
 50: }

 52: /*TEST

 54:      test:

 56: TEST*/