Actual source code: ex3.c
petsc-3.4.5 2014-06-29
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>
21: int main(int argc,char **argv)
22: {
24: PetscMPIInt rank;
25: PetscInt i,istart,iend,n = 6,nlocal;
26: PetscScalar v,*array;
27: Vec x;
28: PetscViewer viewer;
30: PetscInitialize(&argc,&argv,(char*)0,help);
31: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
33: PetscOptionsGetInt(NULL,"-n",&n,NULL);
35: /*
36: Create a vector, specifying only its global dimension.
37: When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
38: the vector format (currently parallel or sequential) is
39: determined at runtime. Also, the parallel partitioning of
40: the vector is determined by PETSc at runtime.
41: */
42: VecCreate(PETSC_COMM_WORLD,&x);
43: VecSetSizes(x,PETSC_DECIDE,n);
44: VecSetFromOptions(x);
46: /*
47: PETSc parallel vectors are partitioned by
48: contiguous chunks of rows across the processors. Determine
49: which vector are locally owned.
50: */
51: VecGetOwnershipRange(x,&istart,&iend);
53: /* --------------------------------------------------------------------
54: Set the vector elements.
55: - Always specify global locations of vector entries.
56: - Each processor can insert into any location, even ones it does not own
57: - In this case each processor adds values to all the entries,
58: this is not practical, but is merely done as an example
59: */
60: for (i=0; i<n; i++) {
61: v = (PetscReal)(rank*i);
62: VecSetValues(x,1,&i,&v,ADD_VALUES);
63: }
65: /*
66: Assemble vector, using the 2-step process:
67: VecAssemblyBegin(), VecAssemblyEnd()
68: Computations can be done while messages are in transition
69: by placing code between these two statements.
70: */
71: VecAssemblyBegin(x);
72: VecAssemblyEnd(x);
74: /*
75: Open an X-window viewer. Note that we specify the same communicator
76: for the viewer as we used for the distributed vector (PETSC_COMM_WORLD).
77: - Helpful runtime option:
78: -draw_pause <pause> : sets time (in seconds) that the
79: program pauses after PetscDrawPause() has been called
80: (0 is default, -1 implies until user input).
82: */
83: PetscViewerDrawOpen(PETSC_COMM_WORLD,NULL,NULL,0,0,300,300,&viewer);
84: PetscObjectSetName((PetscObject)viewer,"Line graph Plot");
85: PetscViewerPushFormat(viewer,PETSC_VIEWER_DRAW_LG);
86: /*
87: View the vector
88: */
89: VecView(x,viewer);
91: /* --------------------------------------------------------------------
92: Access the vector values directly. Each processor has access only
93: to its portion of the vector. For default PETSc vectors VecGetArray()
94: does NOT involve a copy
95: */
96: VecGetLocalSize(x,&nlocal);
97: VecGetArray(x,&array);
98: for (i=0; i<nlocal; i++) array[i] = rank + 1;
99: VecRestoreArray(x,&array);
101: /*
102: View the vector
103: */
104: VecView(x,viewer);
106: /*
107: Free work space. All PETSc objects should be destroyed when they
108: are no longer needed.
109: */
110: PetscViewerDestroy(&viewer);
111: VecDestroy(&x);
113: PetscFinalize();
114: return 0;
115: }