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: int main(int argc,char **argv)
 21: {
 22:   int         i,istart,iend,n = 6,ierr,rank,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(PETSC_NULL,"-n",&n,PETSC_NULL);
 31: 
 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,PETSC_NULL,PETSC_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++) {
 96:     array[i] = rank + 1;
 97:   }
 98:   VecRestoreArray(x,&array);

100:   /*
101:      View the vector
102:   */
103:   VecView(x,viewer);

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

112:   PetscFinalize();
113:   return 0;
114: }
115: