Actual source code: ex3.c

  1: \
  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:      petsc.h       - base PETSc routines   petscis.h     - index sets
 15:      petscsys.h    - system routines       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(PETSC_NULL,"-n",&n,PETSC_NULL);
 34: 
 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,PETSC_NULL,PETSC_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++) {
 99:     array[i] = rank + 1;
100:   }
101:   VecRestoreArray(x,&array);

103:   /*
104:      View the vector
105:   */
106:   VecView(x,viewer);

108:   /* 
109:      Free work space.  All PETSc objects should be destroyed when they
110:      are no longer needed.
111:   */
112:   PetscViewerDestroy(viewer);
113:   VecDestroy(x);

115:   PetscFinalize();
116:   return 0;
117: }
118: