Actual source code: ex3.c

  1: /*$Id: ex3.c,v 1.54 2001/04/10 19:35:06 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:   Scalar      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() and VecSetFromOptions(), the vector format (currently parallel
 35:      or sequential) is determined at runtime.  Also, the parallel
 36:      partitioning of the vector is determined by PETSc at runtime.
 37:   */
 38:   VecCreate(PETSC_COMM_WORLD,PETSC_DECIDE,n,&x);
 39:   VecSetFromOptions(x);

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

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

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

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

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

 86:   /* --------------------------------------------------------------------
 87:        Access the vector values directly. Each processor has access only 
 88:     to its portion of the vector. For default PETSc vectors VecGetArray()
 89:     does NOT involve a copy
 90:   */
 91:   VecGetLocalSize(x,&nlocal);
 92:   VecGetArray(x,&array);
 93:   for (i=0; i<nlocal; i++) {
 94:     array[i] = rank + 1;
 95:   }
 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:   PetscViewerDestroy(viewer);
108:   VecDestroy(x);

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