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