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