Actual source code: ex12.c
2: static char help[] = "Demonstrates VecStrideScatter() and VecStrideGather().\n\n";
4: /*
5: Include "petscvec.h" so that we can use vectors. Note that this file
6: automatically includes:
7: petscsys.h - base PETSc routines petscis.h - index sets
8: petscviewer.h - viewers
9: */
11: #include <petscvec.h>
13: int main(int argc, char **argv)
14: {
15: Vec v, s; /* vectors */
16: PetscInt n = 20;
17: PetscScalar one = 1.0;
19: PetscFunctionBeginUser;
20: PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
21: PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
23: /*
24: Create multi-component vector with 2 components
25: */
26: PetscCall(VecCreate(PETSC_COMM_WORLD, &v));
27: PetscCall(VecSetSizes(v, PETSC_DECIDE, n));
28: PetscCall(VecSetBlockSize(v, 2));
29: PetscCall(VecSetFromOptions(v));
31: /*
32: Create single-component vector
33: */
34: PetscCall(VecCreate(PETSC_COMM_WORLD, &s));
35: PetscCall(VecSetSizes(s, PETSC_DECIDE, n / 2));
36: PetscCall(VecSetFromOptions(s));
38: /*
39: Set the vectors to entries to a constant value.
40: */
41: PetscCall(VecSet(v, one));
43: /*
44: Get the first component from the multi-component vector to the single vector
45: */
46: PetscCall(VecStrideGather(v, 0, s, INSERT_VALUES));
48: PetscCall(VecView(s, PETSC_VIEWER_STDOUT_WORLD));
50: /*
51: Put the values back into the second component
52: */
53: PetscCall(VecStrideScatter(s, 1, v, ADD_VALUES));
55: PetscCall(VecView(v, PETSC_VIEWER_STDOUT_WORLD));
57: /*
58: Free work space. All PETSc objects should be destroyed when they
59: are no longer needed.
60: */
61: PetscCall(VecDestroy(&v));
62: PetscCall(VecDestroy(&s));
63: PetscCall(PetscFinalize());
64: return 0;
65: }
67: /*TEST
69: test:
70: nsize: 2
72: TEST*/