Actual source code: ex9.c

  1: /*$Id: ex9.c,v 1.32 2001/08/07 03:02:34 balay Exp $*/

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

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

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

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

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

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

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

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

 49:      In this example we consider the vector as representing 
 50:      degrees of freedom in a one dimensional grid with periodic 
 51:      boundary conditions.

 53:         ----Processor  1---------  ----Processor 2 --------
 54:          0    1   2   3   4    5    6    7   8   9   10   11
 55:                                |----| 
 56:          |-------------------------------------------------|

 58:   */

 60:   if (!rank) {
 61:     ifrom[0] = 11; ifrom[1] = 6;
 62:   } else {
 63:     ifrom[0] = 0;  ifrom[1] = 5;
 64:   }

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

 79:   /*
 80:       Test VecDuplicate()
 81:   */
 82:   VecDuplicate(gxs,&gx);
 83:   VecDestroy(gxs);

 85:   /*
 86:      Access the local representation
 87:   */
 88:   VecGhostGetLocalForm(gx,&lx);

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

101:   VecGhostUpdateBegin(gx,INSERT_VALUES,SCATTER_FORWARD);
102:   VecGhostUpdateEnd(gx,INSERT_VALUES,SCATTER_FORWARD);

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

114:   VecGhostRestoreLocalForm(gx,&lx);
115:   VecDestroy(gx);
116:   if (flg) {PetscFree(tarray);}
117:   PetscFinalize();
118:   return 0;
119: }
120: