Actual source code: ex12.c
1: static char help[] = "Scatters from a parallel vector to a sequential vector. \n\
2: uses block index sets\n\n";
4: #include <petscvec.h>
6: int main(int argc, char **argv)
7: {
8: PetscInt bs = 1, n = 5, i, low;
9: PetscInt ix0[3] = {5, 7, 9}, iy0[3] = {1, 2, 4}, ix1[3] = {2, 3, 4}, iy1[3] = {0, 1, 3};
10: PetscMPIInt size, rank;
11: PetscScalar *array;
12: Vec x, y;
13: IS isx, isy;
14: VecScatter ctx;
16: PetscFunctionBeginUser;
17: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
18: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
19: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
21: PetscCheck(size >= 2, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Must run more than one processor");
23: PetscCall(PetscOptionsGetInt(NULL, NULL, "-bs", &bs, NULL));
24: n = bs * n;
26: /* Create vector x over shared memory */
27: PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
28: PetscCall(VecSetSizes(x, n, PETSC_DECIDE));
29: PetscCall(VecSetFromOptions(x));
31: PetscCall(VecGetOwnershipRange(x, &low, NULL));
32: PetscCall(VecGetArray(x, &array));
33: for (i = 0; i < n; i++) array[i] = (PetscScalar)(i + low);
34: PetscCall(VecRestoreArray(x, &array));
36: /* Create a sequential vector y */
37: PetscCall(VecCreateSeq(PETSC_COMM_SELF, n, &y));
39: /* Create two index sets */
40: if (rank == 0) {
41: PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, 3, ix0, PETSC_COPY_VALUES, &isx));
42: PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, 3, iy0, PETSC_COPY_VALUES, &isy));
43: } else {
44: PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, 3, ix1, PETSC_COPY_VALUES, &isx));
45: PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, 3, iy1, PETSC_COPY_VALUES, &isy));
46: }
48: if (rank == 10) {
49: PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n[%d] isx:\n", rank));
50: PetscCall(ISView(isx, PETSC_VIEWER_STDOUT_SELF));
51: }
53: PetscCall(VecScatterCreate(x, isx, y, isy, &ctx));
54: PetscCall(VecScatterSetFromOptions(ctx));
56: /* Test forward vecscatter */
57: PetscCall(VecScatterBegin(ctx, x, y, ADD_VALUES, SCATTER_FORWARD));
58: PetscCall(VecScatterEnd(ctx, x, y, ADD_VALUES, SCATTER_FORWARD));
59: if (rank == 0) {
60: PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] y:\n", rank));
61: PetscCall(VecView(y, PETSC_VIEWER_STDOUT_SELF));
62: }
64: /* Test reverse vecscatter */
65: PetscCall(VecScale(y, -1.0));
66: if (rank) PetscCall(VecScale(y, 1.0 / (size - 1)));
68: PetscCall(VecScatterBegin(ctx, y, x, ADD_VALUES, SCATTER_REVERSE));
69: PetscCall(VecScatterEnd(ctx, y, x, ADD_VALUES, SCATTER_REVERSE));
70: PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
72: /* Free spaces */
73: PetscCall(VecScatterDestroy(&ctx));
74: PetscCall(ISDestroy(&isx));
75: PetscCall(ISDestroy(&isy));
76: PetscCall(VecDestroy(&x));
77: PetscCall(VecDestroy(&y));
78: PetscCall(PetscFinalize());
79: return 0;
80: }
82: /*TEST
84: test:
85: nsize: 3
87: TEST*/