Actual source code: ex9.c

  1: /*$Id: ex9.c,v 1.30 2001/03/23 23:21:37 balay Exp $*/

  3: static char help[] = "Demonstrates use of VecCreateGhost().nn";

  5: /*T
  6:    Concepts: vectors^assembling vectors; ghost padding
  7:    Processors: n

  9:    Comment: Ghost padding is one way to handle local calculations that
 10:       involve values from other processors. VecCreateGhost() provides
 11:       a way to create vectors with extra room at the end of the vector 
 12:       array to contain the needed ghost values from other processors, 
 13:       vector computations are otherwise unaffected.
 14: T*/

 16: /* 
 17:   Include "petscvec.h" so that we can use vectors.  Note that this file
 18:   automatically includes:
 19:      petsc.h       - base PETSc routines   petscis.h     - index sets
 20:      petscsys.h    - system routines       petscviewer.h - viewers
 21: */
 22:  #include petscvec.h

 24: int main(int argc,char **argv)
 25: {
 26:   int        rank,nlocal = 6,nghost = 2,ifrom[2],size,ierr,i,rstart,rend;
 27:   PetscTruth flg;
 28:   Scalar     value,*array,*tarray=0;
 29:   Vec        lx,gx,gxs;

 31:   PetscInitialize(&argc,&argv,(char *)0,help);
 32:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 33:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 34:   if (size != 2) SETERRQ(1,"Must run example with two processorsn");

 36:   /*
 37:      Construct a two dimensional graph connecting nlocal degrees of 
 38:      freedom per processor. From this we will generate the global
 39:      indices of needed ghost values

 41:      For simplicity we generate the entire graph on each processor:
 42:      in real application the graph would stored in parallel, but this
 43:      example is only to demonstrate the management of ghost padding
 44:      with VecCreateGhost().

 46:      In this example we consider the vector as representing 
 47:      degrees of freedom in a one dimensional grid with periodic 
 48:      boundary conditions.

 50:         ----Processor  1---------  ----Processor 2 --------
 51:          0    1   2   3   4    5    6    7   8   9   10   11
 52:                                |----| 
 53:          |-------------------------------------------------|

 55:   */

 57:   if (!rank) {
 58:     ifrom[0] = 11; ifrom[1] = 6;
 59:   } else {
 60:     ifrom[0] = 0;  ifrom[1] = 5;
 61:   }

 63:   /*
 64:      Create the vector with two slots for ghost points. Note that both 
 65:      the local vector (lx) and the global vector (gx) share the same 
 66:      array for storing vector values.
 67:   */
 68:   PetscOptionsHasName(PETSC_NULL,"-allocate",&flg);
 69:   if (flg) {
 70:     PetscMalloc((nlocal+nghost)*sizeof(Scalar),&tarray);
 71:     VecCreateGhostWithArray(PETSC_COMM_WORLD,nlocal,PETSC_DECIDE,nghost,ifrom,tarray,&gxs);
 72:   } else {
 73:     VecCreateGhost(PETSC_COMM_WORLD,nlocal,PETSC_DECIDE,nghost,ifrom,&gxs);
 74:   }

 76:   /*
 77:       Test VecDuplicate()
 78:   */
 79:   VecDuplicate(gxs,&gx);
 80:   VecDestroy(gxs);

 82:   /*
 83:      Access the local representation
 84:   */
 85:   VecGhostGetLocalForm(gx,&lx);

 87:   /*
 88:      Set the values from 0 to 12 into the "global" vector 
 89:   */
 90:   VecGetOwnershipRange(gx,&rstart,&rend);
 91:   for (i=rstart; i<rend; i++) {
 92:     value = (Scalar) i;
 93:     ierr  = VecSetValues(gx,1,&i,&value,INSERT_VALUES);
 94:   }
 95:   VecAssemblyBegin(gx);
 96:   VecAssemblyEnd(gx);

 98:   VecGhostUpdateBegin(gx,INSERT_VALUES,SCATTER_FORWARD);
 99:   VecGhostUpdateEnd(gx,INSERT_VALUES,SCATTER_FORWARD);

101:   /*
102:      Print out each vector, including the ghost padding region. 
103:   */
104:   VecGetArray(lx,&array);
105:   for (i=0; i<nlocal+nghost; i++) {
106:     PetscSynchronizedPrintf(PETSC_COMM_WORLD,"%d %gn",i,PetscRealPart(array[i]));
107:   }
108:   VecRestoreArray(lx,&array);
109:   PetscSynchronizedFlush(PETSC_COMM_WORLD);

111:   VecGhostRestoreLocalForm(gx,&lx);
112:   VecDestroy(gx);
113:   if (flg) {PetscFree(tarray);}
114:   PetscFinalize();
115:   return 0;
116: }
117: