Actual source code: ex2.c
2: static char help[] = "Builds a parallel vector with 1 component on the first processor, 2 on the second, etc.\n\
3: Then each processor adds one to all elements except the last rank.\n\n";
5: /*
6: Include "petscvec.h" so that we can use vectors. Note that this file
7: automatically includes:
8: petscsys.h - base PETSc routines petscis.h - index sets
9: petscviewer.h - viewers
10: */
11: #include <petscvec.h>
13: int main(int argc, char **argv)
14: {
15: PetscMPIInt rank;
16: PetscInt i, N;
17: PetscScalar one = 1.0;
18: Vec x;
20: PetscFunctionBeginUser;
21: PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
22: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
24: /*
25: Create a parallel vector.
26: - In this case, we specify the size of each processor's local
27: portion, and PETSc computes the global size. Alternatively,
28: if we pass the global size and use PETSC_DECIDE for the
29: local size PETSc will choose a reasonable partition trying
30: to put nearly an equal number of elements on each processor.
31: */
32: PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
33: PetscCall(VecSetSizes(x, rank + 1, PETSC_DECIDE));
34: PetscCall(VecSetFromOptions(x));
35: PetscCall(VecGetSize(x, &N));
36: PetscCall(VecSet(x, one));
38: /*
39: Set the vector elements.
40: - Always specify global locations of vector entries.
41: - Each processor can contribute any vector entries,
42: regardless of which processor "owns" them; any nonlocal
43: contributions will be transferred to the appropriate processor
44: during the assembly process.
45: - In this example, the flag ADD_VALUES indicates that all
46: contributions will be added together.
47: */
48: for (i = 0; i < N - rank; i++) PetscCall(VecSetValues(x, 1, &i, &one, ADD_VALUES));
50: /*
51: Assemble vector, using the 2-step process:
52: VecAssemblyBegin(), VecAssemblyEnd()
53: Computations can be done while messages are in transition
54: by placing code between these two statements.
55: */
56: PetscCall(VecAssemblyBegin(x));
57: PetscCall(VecAssemblyEnd(x));
59: /*
60: View the vector; then destroy it.
61: */
62: PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
63: PetscCall(VecDestroy(&x));
65: PetscCall(PetscFinalize());
66: return 0;
67: }
69: /*TEST
71: test:
72: nsize: 2
74: TEST*/