Actual source code: ex3.c
2: static char help[] = "Tests parallel vector assembly. Input arguments are\n\
3: -n <length> : local vector length\n\n";
5: #include <petscvec.h>
7: int main(int argc, char **argv)
8: {
9: PetscMPIInt size, rank;
10: PetscInt n = 5, idx;
11: PetscScalar one = 1.0, two = 2.0, three = 3.0;
12: Vec x, y;
14: PetscFunctionBeginUser;
15: PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
16: PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
17: if (n < 5) n = 5;
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 be run with at least two processors");
23: /* create two vector */
24: PetscCall(VecCreateSeq(PETSC_COMM_SELF, n, &x));
25: PetscCall(VecCreate(PETSC_COMM_WORLD, &y));
26: PetscCall(VecSetSizes(y, n, PETSC_DECIDE));
27: PetscCall(VecSetFromOptions(y));
28: PetscCall(VecSet(x, one));
29: PetscCall(VecSet(y, two));
31: if (rank == 1) {
32: idx = 2;
33: PetscCall(VecSetValues(y, 1, &idx, &three, INSERT_VALUES));
34: idx = 0;
35: PetscCall(VecSetValues(y, 1, &idx, &two, INSERT_VALUES));
36: idx = 0;
37: PetscCall(VecSetValues(y, 1, &idx, &one, INSERT_VALUES));
38: } else {
39: idx = 7;
40: PetscCall(VecSetValues(y, 1, &idx, &three, INSERT_VALUES));
41: }
42: PetscCall(VecAssemblyBegin(y));
43: PetscCall(VecAssemblyEnd(y));
45: PetscCall(VecView(y, PETSC_VIEWER_STDOUT_WORLD));
47: PetscCall(VecDestroy(&x));
48: PetscCall(VecDestroy(&y));
50: PetscCall(PetscFinalize());
51: return 0;
52: }
54: /*TEST
56: test:
57: nsize: 2
59: test:
60: suffix: 2
61: nsize: 2
62: args: -vec_view ascii::ascii_info
64: TEST*/