Actual source code: ex4.c


  2: static char help[] = "Scatters from a parallel vector into sequential vectors.\n\n";

  4: #include <petscvec.h>

  6: int main(int argc, char **argv)
  7: {
  8:   PetscMPIInt rank;
  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));
 18:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));

 20:   /* create two vectors */
 21:   PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
 22:   PetscCall(VecSetSizes(x, n, PETSC_DECIDE));
 23:   PetscCall(VecSetFromOptions(x));
 24:   PetscCall(VecCreate(PETSC_COMM_SELF, &y));
 25:   PetscCall(VecSetSizes(y, n, PETSC_DECIDE));
 26:   PetscCall(VecSetFromOptions(y));

 28:   /* create two index sets */
 29:   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 2, idx1, PETSC_COPY_VALUES, &is1));
 30:   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 2, idx2, PETSC_COPY_VALUES, &is2));

 32:   PetscCall(VecSet(x, one));
 33:   PetscCall(VecSet(y, two));
 34:   PetscCall(VecScatterCreate(x, is1, y, is2, &ctx));
 35:   PetscCall(VecScatterBegin(ctx, x, y, INSERT_VALUES, SCATTER_FORWARD));
 36:   PetscCall(VecScatterEnd(ctx, x, y, INSERT_VALUES, SCATTER_FORWARD));
 37:   PetscCall(VecScatterDestroy(&ctx));

 39:   if (rank == 0) PetscCall(VecView(y, PETSC_VIEWER_STDOUT_SELF));

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

 44:   PetscCall(VecDestroy(&x));
 45:   PetscCall(VecDestroy(&y));
 46:   PetscCall(PetscFinalize());
 47:   return 0;
 48: }

 50: /*TEST

 52:    test:
 53:       nsize: 2
 54:       filter: grep -v type
 55:       diff_args: -j

 57:    test:
 58:       diff_args: -j
 59:       suffix: cuda
 60:       args: -vec_type cuda
 61:       output_file: output/ex4_1.out
 62:       filter: grep -v type
 63:       requires: cuda

 65:    test:
 66:       diff_args: -j
 67:       suffix: cuda2
 68:       nsize: 2
 69:       args: -vec_type cuda
 70:       output_file: output/ex4_1.out
 71:       filter: grep -v type
 72:       requires: cuda

 74:    test:
 75:       diff_args: -j
 76:       suffix: kokkos
 77:       args: -vec_type kokkos
 78:       output_file: output/ex4_1.out
 79:       filter: grep -v type
 80:       requires: kokkos_kernels

 82:    test:
 83:       diff_args: -j
 84:       suffix: kokkos2
 85:       nsize: 2
 86:       args: -vec_type kokkos
 87:       output_file: output/ex4_1.out
 88:       filter: grep -v type
 89:       requires: kokkos_kernels

 91:    testset:
 92:       diff_args: -j
 93:       requires: hip
 94:       filter: grep -v type
 95:       args: -vec_type hip
 96:       output_file: output/ex4_1.out
 97:       test:
 98:         suffix: hip
 99:       test:
100:         suffix: hip2
101:         nsize: 2
102: TEST*/