xref: /petsc/src/vec/vec/tests/ex22.c (revision df4cd43f92eaa320656440c40edb1046daee8f75)
1 
2 static char help[] = "Scatters from a parallel vector to a parallel vector.\n\n";
3 
4 #include <petscvec.h>
5 
6 int main(int argc, char **argv)
7 {
8   PetscInt    n = 5, N, i;
9   PetscMPIInt size, rank;
10   PetscScalar value, zero = 0.0;
11   Vec         x, y;
12   IS          is1, is2;
13   VecScatter  ctx = 0;
14 
15   PetscFunctionBeginUser;
16   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
17   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
18   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
19 
20   /* create two vectors */
21   N = size * n;
22   PetscCall(VecCreate(PETSC_COMM_WORLD, &y));
23   PetscCall(VecSetSizes(y, n, PETSC_DECIDE));
24   PetscCall(VecSetFromOptions(y));
25 
26   PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
27   PetscCall(VecSetSizes(x, n, PETSC_DECIDE));
28   PetscCall(VecSetFromOptions(x));
29 
30   /* create two index sets */
31   PetscCall(ISCreateStride(PETSC_COMM_WORLD, n, n * rank, 1, &is1));
32   PetscCall(ISCreateStride(PETSC_COMM_WORLD, n, (n * (rank + 1)) % N, 1, &is2));
33 
34   /* fill local part of parallel vector x */
35   value = (PetscScalar)(rank + 1);
36   for (i = n * rank; i < n * (rank + 1); i++) PetscCall(VecSetValues(x, 1, &i, &value, INSERT_VALUES));
37   PetscCall(VecAssemblyBegin(x));
38   PetscCall(VecAssemblyEnd(x));
39 
40   PetscCall(VecSet(y, zero));
41 
42   PetscCall(VecScatterCreate(x, is1, y, is2, &ctx));
43   PetscCall(VecScatterBegin(ctx, x, y, ADD_VALUES, SCATTER_FORWARD));
44   PetscCall(VecScatterEnd(ctx, x, y, ADD_VALUES, SCATTER_FORWARD));
45   PetscCall(VecScatterDestroy(&ctx));
46 
47   PetscCall(VecView(y, PETSC_VIEWER_STDOUT_WORLD));
48 
49   PetscCall(VecDestroy(&x));
50   PetscCall(VecDestroy(&y));
51   PetscCall(ISDestroy(&is1));
52   PetscCall(ISDestroy(&is2));
53 
54   PetscCall(PetscFinalize());
55   return 0;
56 }
57 
58 /*TEST
59 
60    testset:
61       nsize: 4
62       output_file: output/ex22_1.out
63       filter: grep -v "  type:"
64       diff_args: -j
65       test:
66         suffix: standard
67         args: -vec_type standard
68       test:
69         requires: cuda
70         suffix: cuda
71         args: -vec_type cuda
72       test:
73         requires: viennacl
74         suffix:  viennacl
75         args: -vec_type viennacl
76       test:
77         requires: kokkos_kernels
78         suffix: kokkos
79         args: -vec_type kokkos
80       test:
81         requires: hip
82         suffix: hip
83         args: -vec_type hip
84 
85 TEST*/
86