Actual source code: ex3.c


  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: {
 21:   PetscMPIInt    rank;
 22:   PetscInt       i,istart,iend,n = 6,nlocal;
 23:   PetscScalar    v,*array;
 24:   Vec            x;
 25:   PetscViewer    viewer;

 27:   PetscInitialize(&argc,&argv,(char*)0,help);
 28:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 30:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);

 32:   /*
 33:      Create a vector, specifying only its global dimension.
 34:      When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
 35:      the vector format (currently parallel or sequential) is
 36:      determined at runtime.  Also, the parallel partitioning of
 37:      the vector is determined by PETSc at runtime.
 38:   */
 39:   VecCreate(PETSC_COMM_WORLD,&x);
 40:   VecSetSizes(x,PETSC_DECIDE,n);
 41:   VecSetFromOptions(x);

 43:   /*
 44:      PETSc parallel vectors are partitioned by
 45:      contiguous chunks of rows across the processors.  Determine
 46:      which vector are locally owned.
 47:   */
 48:   VecGetOwnershipRange(x,&istart,&iend);

 50:   /* --------------------------------------------------------------------
 51:      Set the vector elements.
 52:       - Always specify global locations of vector entries.
 53:       - Each processor can insert into any location, even ones it does not own
 54:       - In this case each processor adds values to all the entries,
 55:          this is not practical, but is merely done as an example
 56:    */
 57:   for (i=0; i<n; i++) {
 58:     v    = (PetscReal)(rank*i);
 59:     VecSetValues(x,1,&i,&v,ADD_VALUES);
 60:   }

 62:   /*
 63:      Assemble vector, using the 2-step process:
 64:        VecAssemblyBegin(), VecAssemblyEnd()
 65:      Computations can be done while messages are in transition
 66:      by placing code between these two statements.
 67:   */
 68:   VecAssemblyBegin(x);
 69:   VecAssemblyEnd(x);

 71:   /*
 72:      Open an X-window viewer.  Note that we specify the same communicator
 73:      for the viewer as we used for the distributed vector (PETSC_COMM_WORLD).
 74:        - Helpful runtime option:
 75:             -draw_pause <pause> : sets time (in seconds) that the
 76:                   program pauses after PetscDrawPause() has been called
 77:                   (0 is default, -1 implies until user input).

 79:   */
 80:   PetscViewerDrawOpen(PETSC_COMM_WORLD,NULL,NULL,0,0,300,300,&viewer);
 81:   PetscObjectSetName((PetscObject)viewer,"Line graph Plot");
 82:   PetscViewerPushFormat(viewer,PETSC_VIEWER_DRAW_LG);
 83:   /*
 84:      View the vector
 85:   */
 86:   VecView(x,viewer);

 88:   /* --------------------------------------------------------------------
 89:        Access the vector values directly. Each processor has access only
 90:     to its portion of the vector. For default PETSc vectors VecGetArray()
 91:     does NOT involve a copy
 92:   */
 93:   VecGetLocalSize(x,&nlocal);
 94:   VecGetArray(x,&array);
 95:   for (i=0; i<nlocal; i++) array[i] = rank + 1;
 96:   VecRestoreArray(x,&array);

 98:   /*
 99:      View the vector
100:   */
101:   VecView(x,viewer);

103:   /*
104:      Free work space.  All PETSc objects should be destroyed when they
105:      are no longer needed.
106:   */
107:   PetscViewerPopFormat(viewer);
108:   PetscViewerDestroy(&viewer);
109:   VecDestroy(&x);

111:   PetscFinalize();
112:   return 0;
113: }

115: /*TEST

117:      test:
118:        nsize: 2

120: TEST*/