Actual source code: ex2.c

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

  4: #include <petscvec.h>

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

 14:   PetscFunctionBeginUser;
 15:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 16:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));

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

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

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

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

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

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

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

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

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

 51: /*TEST

 53:      test:

 55: TEST*/