Actual source code: ex17.c
2: static char help[] = "Scatters from a parallel vector to a sequential vector. In\n\
3: this case each local vector is as long as the entire parallel vector.\n\n";
5: #include <petscvec.h>
7: int main(int argc, char **argv)
8: {
9: PetscInt n = 5, N, low, high, iglobal, i;
10: PetscMPIInt size, rank;
11: PetscScalar value, zero = 0.0;
12: Vec x, y;
13: IS is1, is2;
14: VecScatter ctx;
17: PetscInitialize(&argc, &argv, (char *)0, help);
18: MPI_Comm_size(PETSC_COMM_WORLD, &size);
19: MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
21: /* create two vectors */
22: N = size * n;
23: VecCreate(PETSC_COMM_WORLD, &y);
24: VecSetSizes(y, PETSC_DECIDE, N);
25: VecSetFromOptions(y);
26: VecCreateSeq(PETSC_COMM_SELF, N, &x);
28: /* create two index sets */
29: ISCreateStride(PETSC_COMM_SELF, N, 0, 1, &is1);
30: ISCreateStride(PETSC_COMM_SELF, N, 0, 1, &is2);
32: VecSet(x, zero);
33: VecGetOwnershipRange(y, &low, &high);
34: for (i = 0; i < n; i++) {
35: iglobal = i + low;
36: value = (PetscScalar)(i + 10 * rank);
37: VecSetValues(y, 1, &iglobal, &value, INSERT_VALUES);
38: }
39: VecAssemblyBegin(y);
40: VecAssemblyEnd(y);
41: VecView(y, PETSC_VIEWER_STDOUT_WORLD);
43: VecScatterCreate(y, is2, x, is1, &ctx);
44: VecScatterBegin(ctx, y, x, ADD_VALUES, SCATTER_FORWARD);
45: VecScatterEnd(ctx, y, x, ADD_VALUES, SCATTER_FORWARD);
46: VecScatterDestroy(&ctx);
48: if (rank == 0) {
49: PetscPrintf(PETSC_COMM_SELF, "----\n");
50: VecView(x, PETSC_VIEWER_STDOUT_SELF);
51: }
53: VecDestroy(&x);
54: VecDestroy(&y);
55: ISDestroy(&is1);
56: ISDestroy(&is2);
58: PetscFinalize();
59: return 0;
60: }
62: /*TEST
64: test:
65: nsize: 2
67: TEST*/