Actual source code: ex14f.F

petsc-3.5.1 2014-08-06
Report Typos and Errors
  1: !
  2: !
  3: ! Description: Illustrates the use of VecCreateGhost()
  4: !
  5: !/*T
  6: !   Concepts: vectors^assembling vectors;
  7: !   Concepts: vectors^ghost padding;
  8: !   Processors: n
  9: !
 10: !   Description: Ghost padding is one way to handle local calculations that
 11: !      involve values from other processors. VecCreateGhostBlock() 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:       program main
 18:       implicit none

 20: !
 21: !  The following include statements are required for Fortran programs
 22: !  that use PETSc vectors:
 23: !     petscsys.h       - base PETSc routines
 24: !     petscvec.h    - vectors
 25: !

 27: #include <finclude/petscsys.h>
 28: #include <finclude/petscvec.h>

 30:       PetscMPIInt size,rank
 31:       PetscInt nlocal,nghost,ifrom(2)
 32:       PetscInt i,rstart,rend,bs,ione
 33:       PetscBool       flag
 34:       PetscErrorCode ierr
 35:       PetscScalar  value,tarray(20)
 36:       Vec          lx,gx,gxs

 38:       nlocal = 6
 39:       nghost = 2
 40:       bs     = 2
 41:       nlocal = bs*nlocal

 43:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 44:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
 45:       call MPI_Comm_size(PETSC_COMM_WORLD,size,ierr)

 47:       if (size .ne. 2) then
 48:        SETERRQ(PETSC_COMM_SELF,1,'Must run with two processors',ierr)
 49:       endif

 51: !
 52: !     Construct a two dimensional graph connecting nlocal degrees of
 53: !     freedom per processor. From this we will generate the global
 54: !     indices of needed ghost values
 55: !
 56: !     For simplicity we generate the entire graph on each processor:
 57: !     in real application the graph would stored in parallel, but this
 58: !     example is only to demonstrate the management of ghost padding
 59: !     with VecCreateGhost().
 60: !
 61: !     In this example we consider the vector as representing
 62: !     degrees of freedom in a one dimensional grid with periodic
 63: !     boundary conditions.
 64: !
 65: !        ----Processor  1-----------  ----Processor 2 --------
 66: !         0 1 2 3 4 5 6 7 8 9 10 11    12 13 14 15 16 17 18 19 20 21 22 23
 67: !                              |--|----|---|
 68: !         |-|--------------------------------------------------------|--|
 69: !


 72:       if (rank .eq. 0) then
 73:         ifrom(1) = 11
 74:         ifrom(2) = 6
 75:       else
 76:         ifrom(1) = 0
 77:         ifrom(2) = 5
 78:       endif

 80: !     Create the vector with two slots for ghost points. Note that both
 81: !     the local vector (lx) and the global vector (gx) share the same
 82: !     array for storing vector values.

 84:       call PetscOptionsHasName(PETSC_NULL_CHARACTER,'-allocate',flag,    &
 85:      &                         ierr)
 86:       if (flag) then
 87:         call VecCreateGhostBlockWithArray(PETSC_COMM_WORLD,bs,nlocal,    &
 88:      &        PETSC_DECIDE,nghost,ifrom,tarray,gxs,ierr)
 89:       else
 90:         call VecCreateGhostBlock(PETSC_COMM_WORLD,bs,nlocal,             &
 91:      &       PETSC_DECIDE,nghost,ifrom,gxs,ierr)
 92:       endif


 95: !      Test VecDuplicate

 97:        call VecDuplicate(gxs,gx,ierr)
 98:        call VecDestroy(gxs,ierr)

100: !      Access the local Form

102:        call VecGhostGetLocalForm(gx,lx,ierr)

104: !     Set the values from 0 to 12 into the "global" vector

106:        call VecGetOwnershipRange(gx,rstart,rend,ierr)

108:        ione = 1
109:        do 10, i=rstart,rend-1
110:          value = i
111:          call VecSetValues(gx,ione,i,value,INSERT_VALUES,ierr)
112:  10    continue

114:        call VecAssemblyBegin(gx,ierr)
115:        call VecAssemblyEnd(gx,ierr)

117:        call VecGhostUpdateBegin(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)
118:        call VecGhostUpdateEnd(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)

120: !     Print out each vector, including the ghost padding region.

122:        call VecView(lx,PETSC_VIEWER_STDOUT_SELF,ierr)

124:        call VecGhostRestoreLocalForm(gx,lx,ierr)
125:        call VecDestroy(gx,ierr)
126:        call PetscFinalize(ierr)
127:        end