Actual source code: ex11.c
2: static char help[] = "Scatters from a parallel vector to a sequential vector.\n\n";
4: #include <petscvec.h>
6: int main(int argc, char **argv)
7: {
8: PetscMPIInt size, rank;
9: PetscInt i, N;
10: PetscScalar value;
11: Vec x, y;
12: IS is1, is2;
13: VecScatter ctx = 0;
16: PetscInitialize(&argc, &argv, (char *)0, help);
17: MPI_Comm_size(PETSC_COMM_WORLD, &size);
18: MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
20: /* create two vectors */
21: VecCreate(PETSC_COMM_WORLD, &x);
22: VecSetSizes(x, rank + 1, PETSC_DECIDE);
23: VecSetFromOptions(x);
24: VecGetSize(x, &N);
25: VecCreateSeq(PETSC_COMM_SELF, N - rank, &y);
27: /* create two index sets */
28: ISCreateStride(PETSC_COMM_SELF, N - rank, rank, 1, &is1);
29: ISCreateStride(PETSC_COMM_SELF, N - rank, 0, 1, &is2);
31: /* fill parallel vector: note this is not efficient way*/
32: for (i = 0; i < N; i++) {
33: value = (PetscScalar)i;
34: VecSetValues(x, 1, &i, &value, INSERT_VALUES);
35: }
36: VecAssemblyBegin(x);
37: VecAssemblyEnd(x);
38: VecSet(y, -1.0);
40: VecView(x, PETSC_VIEWER_STDOUT_WORLD);
42: VecScatterCreate(x, is1, y, is2, &ctx);
43: VecScatterBegin(ctx, x, y, INSERT_VALUES, SCATTER_FORWARD);
44: VecScatterEnd(ctx, x, y, INSERT_VALUES, SCATTER_FORWARD);
45: VecScatterDestroy(&ctx);
47: if (rank == 0) {
48: PetscPrintf(PETSC_COMM_SELF, "----\n");
49: VecView(y, PETSC_VIEWER_STDOUT_SELF);
50: }
52: ISDestroy(&is1);
53: ISDestroy(&is2);
54: VecDestroy(&x);
55: VecDestroy(&y);
57: PetscFinalize();
58: return 0;
59: }
61: /*TEST
63: test:
64: nsize: 2
66: test:
67: suffix: bts
68: nsize: 2
69: args: -vec_assembly_legacy
70: output_file: output/ex11_1.out
72: TEST*/