Actual source code: ex3.c
petsc-3.9.4 2018-09-11
2: static char help[] = "Parallel vector layout.\n\n";
4: /*T
5: Concepts: vectors^setting values
6: Concepts: vectors^local access to
7: Concepts: vectors^drawing vectors;
8: Processors: n
9: T*/
11: /*
12: Include "petscvec.h" so that we can use vectors. Note that this file
13: automatically includes:
14: petscsys.h - base PETSc routines petscis.h - index sets
15: petscviewer.h - viewers
16: */
17: #include <petscvec.h>
19: int main(int argc,char **argv)
20: {
22: PetscMPIInt rank;
23: PetscInt i,istart,iend,n = 6,nlocal;
24: PetscScalar v,*array;
25: Vec x;
26: PetscViewer viewer;
28: PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
29: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
31: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
33: /*
34: Create a vector, specifying only its global dimension.
35: When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
36: the vector format (currently parallel or sequential) is
37: determined at runtime. Also, the parallel partitioning of
38: the vector is determined by PETSc at runtime.
39: */
40: VecCreate(PETSC_COMM_WORLD,&x);
41: VecSetSizes(x,PETSC_DECIDE,n);
42: VecSetFromOptions(x);
44: /*
45: PETSc parallel vectors are partitioned by
46: contiguous chunks of rows across the processors. Determine
47: which vector are locally owned.
48: */
49: VecGetOwnershipRange(x,&istart,&iend);
51: /* --------------------------------------------------------------------
52: Set the vector elements.
53: - Always specify global locations of vector entries.
54: - Each processor can insert into any location, even ones it does not own
55: - In this case each processor adds values to all the entries,
56: this is not practical, but is merely done as an example
57: */
58: for (i=0; i<n; i++) {
59: v = (PetscReal)(rank*i);
60: VecSetValues(x,1,&i,&v,ADD_VALUES);
61: }
63: /*
64: Assemble vector, using the 2-step process:
65: VecAssemblyBegin(), VecAssemblyEnd()
66: Computations can be done while messages are in transition
67: by placing code between these two statements.
68: */
69: VecAssemblyBegin(x);
70: VecAssemblyEnd(x);
72: /*
73: Open an X-window viewer. Note that we specify the same communicator
74: for the viewer as we used for the distributed vector (PETSC_COMM_WORLD).
75: - Helpful runtime option:
76: -draw_pause <pause> : sets time (in seconds) that the
77: program pauses after PetscDrawPause() has been called
78: (0 is default, -1 implies until user input).
80: */
81: PetscViewerDrawOpen(PETSC_COMM_WORLD,NULL,NULL,0,0,300,300,&viewer);
82: PetscObjectSetName((PetscObject)viewer,"Line graph Plot");
83: PetscViewerPushFormat(viewer,PETSC_VIEWER_DRAW_LG);
84: /*
85: View the vector
86: */
87: VecView(x,viewer);
89: /* --------------------------------------------------------------------
90: Access the vector values directly. Each processor has access only
91: to its portion of the vector. For default PETSc vectors VecGetArray()
92: does NOT involve a copy
93: */
94: VecGetLocalSize(x,&nlocal);
95: VecGetArray(x,&array);
96: for (i=0; i<nlocal; i++) array[i] = rank + 1;
97: VecRestoreArray(x,&array);
99: /*
100: View the vector
101: */
102: VecView(x,viewer);
104: /*
105: Free work space. All PETSc objects should be destroyed when they
106: are no longer needed.
107: */
108: PetscViewerPopFormat(viewer);
109: PetscViewerDestroy(&viewer);
110: VecDestroy(&x);
112: PetscFinalize();
113: return ierr;
114: }
116: /*TEST
118: test:
119: nsize: 2
121: TEST*/