#include <petscvec.h>
int main(int argc, char **argv)
{
PetscInt i, j, rstart, rend, n, N, *indices;
PetscMPIInt size, rank;
IS ix;
VecScatter vscat;
Vec x, y;
PetscFunctionBeginUser;
PetscCall(PetscInitialize(&argc, &argv, (char *)0, NULL));
PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
PetscCall(VecSetFromOptions(x));
PetscCall(PetscObjectSetName((PetscObject)x, "Vec X"));
n = (rank < 4) ? 9 : 0;
PetscCall(VecSetSizes(x, n, PETSC_DECIDE));
PetscCall(VecGetOwnershipRange(x, &rstart, &rend));
for (i = rstart; i < rend; i++) PetscCall(VecSetValue(x, i, (PetscScalar)i, INSERT_VALUES));
PetscCall(VecAssemblyBegin(x));
PetscCall(VecAssemblyEnd(x));
PetscCall(VecGetSize(x, &N));
PetscCall(VecCreate(PETSC_COMM_WORLD, &y));
PetscCall(VecSetFromOptions(y));
PetscCall(PetscObjectSetName((PetscObject)y, "Vec Y"));
PetscCall(VecSetSizes(y, PETSC_DECIDE, N));
PetscCall(VecGetOwnershipRange(y, &rstart, &rend));
PetscCall(PetscMalloc1(rend - rstart, &indices));
for (i = rstart, j = 0; i < rend; i++, j++) indices[j] = rank + size * j;
PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, rend - rstart, indices, PETSC_OWN_POINTER, &ix));
PetscCall(VecScatterCreate(x, ix, y, NULL, &vscat));
PetscCall(VecScatterBegin(vscat, x, y, INSERT_VALUES, SCATTER_FORWARD));
PetscCall(VecScatterEnd(vscat, x, y, INSERT_VALUES, SCATTER_FORWARD));
PetscCall(ISView(ix, PETSC_VIEWER_STDOUT_WORLD));
PetscCall(VecView(x, PETSC_VIEWER_STDERR_WORLD));
PetscCall(VecView(y, PETSC_VIEWER_STDERR_WORLD));
PetscCall(VecScatterDestroy(&vscat));
PetscCall(ISDestroy(&ix));
PetscCall(VecDestroy(&x));
PetscCall(VecDestroy(&y));
PetscCall(PetscFinalize());
return 0;
}
$ mpirun -n 12 ./ex100
IS Object: 12 MPI processes
type: general
[0] Number of indices in set 3
[0] 0 0
[0] 1 12
[0] 2 24
[1] Number of indices in set 3
[1] 0 1
[1] 1 13
[1] 2 25
[2] Number of indices in set 3
[2] 0 2
[2] 1 14
[2] 2 26
[3] Number of indices in set 3
[3] 0 3
[3] 1 15
[3] 2 27
[4] Number of indices in set 3
[4] 0 4
[4] 1 16
[4] 2 28
[5] Number of indices in set 3
[5] 0 5
[5] 1 17
[5] 2 29
[6] Number of indices in set 3
[6] 0 6
[6] 1 18
[6] 2 30
[7] Number of indices in set 3
[7] 0 7
[7] 1 19
[7] 2 31
[8] Number of indices in set 3
[8] 0 8
[8] 1 20
[8] 2 32
[9] Number of indices in set 3
[9] 0 9
[9] 1 21
[9] 2 33
[10] Number of indices in set 3
[10] 0 10
[10] 1 22
[10] 2 34
[11] Number of indices in set 3
[11] 0 11
[11] 1 23
[11] 2 35
Vec Object: Vec X 12 MPI processes
type: mpi
Process [0]
0.
1.
2.
3.
4.
5.
6.
7.
8.
Process [1]
9.
10.
11.
12.
13.
14.
15.
16.
17.
Process [2]
18.
19.
20.
21.
22.
23.
24.
25.
26.
Process [3]
27.
28.
29.
30.
31.
32.
33.
34.
35.
Process [4]
Process [5]
Process [6]
Process [7]
Process [8]
Process [9]
Process [10]
Process [11]
Vec Object: Vec Y 12 MPI processes
type: mpi
Process [0]
0.
12.
24.
Process [1]
1.
13.
25.
Process [2]
2.
14.
26.
Process [3]
3.
15.
27.
Process [4]
4.
16.
28.
Process [5]
5.
17.
29.
Process [6]
6.
18.
30.
Process [7]
7.
19.
31.
Process [8]
8.
20.
32.
Process [9]
9.
21.
33.
Process [10]
10.
22.
34.
Process [11]
11.
23.
35.