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