Actual source code: ex4.c
1: static char help[] = "Scatters from a parallel vector into sequential vectors.\n\n";
3: #include <petscvec.h>
5: int main(int argc, char **argv)
6: {
7: PetscMPIInt rank;
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));
17: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
19: /* create two vectors */
20: PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
21: PetscCall(VecSetSizes(x, n, PETSC_DECIDE));
22: PetscCall(VecSetFromOptions(x));
23: PetscCall(VecCreate(PETSC_COMM_SELF, &y));
24: PetscCall(VecSetSizes(y, n, PETSC_DECIDE));
25: PetscCall(VecSetFromOptions(y));
27: /* create two index sets */
28: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 2, idx1, PETSC_COPY_VALUES, &is1));
29: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 2, idx2, PETSC_COPY_VALUES, &is2));
31: PetscCall(VecSet(x, one));
32: PetscCall(VecSet(y, two));
33: PetscCall(VecScatterCreate(x, is1, y, is2, &ctx));
34: PetscCall(VecScatterBegin(ctx, x, y, INSERT_VALUES, SCATTER_FORWARD));
35: PetscCall(VecScatterEnd(ctx, x, y, INSERT_VALUES, SCATTER_FORWARD));
36: PetscCall(VecScatterDestroy(&ctx));
38: if (rank == 0) PetscCall(VecView(y, PETSC_VIEWER_STDOUT_SELF));
40: PetscCall(ISDestroy(&is1));
41: PetscCall(ISDestroy(&is2));
43: PetscCall(VecDestroy(&x));
44: PetscCall(VecDestroy(&y));
45: PetscCall(PetscFinalize());
46: return 0;
47: }
49: /*TEST
51: test:
52: nsize: 2
53: filter: grep -v type
54: diff_args: -j
56: test:
57: diff_args: -j
58: suffix: cuda
59: args: -vec_type cuda
60: output_file: output/ex4_1.out
61: filter: grep -v type
62: requires: cuda
64: test:
65: diff_args: -j
66: suffix: cuda2
67: nsize: 2
68: args: -vec_type cuda
69: output_file: output/ex4_1.out
70: filter: grep -v type
71: requires: cuda
73: test:
74: diff_args: -j
75: suffix: kokkos
76: args: -vec_type kokkos
77: output_file: output/ex4_1.out
78: filter: grep -v type
79: requires: kokkos_kernels
81: test:
82: diff_args: -j
83: suffix: kokkos2
84: nsize: 2
85: args: -vec_type kokkos
86: output_file: output/ex4_1.out
87: filter: grep -v type
88: requires: kokkos_kernels
90: testset:
91: diff_args: -j
92: requires: hip
93: filter: grep -v type
94: args: -vec_type hip
95: output_file: output/ex4_1.out
96: test:
97: suffix: hip
98: test:
99: suffix: hip2
100: nsize: 2
101: TEST*/