Actual source code: ex9f.F

petsc-3.7.5 2017-01-01
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. 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:       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 <petsc/finclude/petscsys.h>
 28: #include <petsc/finclude/petscvec.h>

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

 38:       nlocal = 6
 39:       nghost = 2

 41:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 42:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
 43:       call MPI_Comm_size(PETSC_COMM_WORLD,size,ierr)

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

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


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

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

 82:       call PetscOptionsHasName(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,   &
 83:      &                         '-allocate',flag,ierr)
 84:       if (flag) then
 85:         call VecCreateGhostWithArray(PETSC_COMM_WORLD,nlocal,            &
 86:      &        PETSC_DECIDE,nghost,ifrom,tarray,gxs,ierr)
 87:       else
 88:         call VecCreateGhost(PETSC_COMM_WORLD,nlocal,PETSC_DECIDE,        &
 89:      &       nghost,ifrom,gxs,ierr)
 90:       endif


 93: !      Test VecDuplicate

 95:        call VecDuplicate(gxs,gx,ierr)
 96:        call VecDestroy(gxs,ierr)

 98: !      Access the local Form

100:        call VecGhostGetLocalForm(gx,lx,ierr)

102: !     Set the values from 0 to 12 into the 'global' vector

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

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

112:        call VecAssemblyBegin(gx,ierr)
113:        call VecAssemblyEnd(gx,ierr)

115:        call VecGhostUpdateBegin(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)
116:        call VecGhostUpdateEnd(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)

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

120:        if (rank .eq. 0) then
121:           call VecView(lx,PETSC_VIEWER_STDOUT_SELF,ierr)
122:        endif
123:        call MPI_Barrier(PETSC_COMM_WORLD,ierr)
124:        if (rank .eq. 1) then
125:           call VecView(lx,PETSC_VIEWER_STDOUT_SELF,ierr)
126:        endif

128:        call VecGhostRestoreLocalForm(gx,lx,ierr)
129:        call VecDestroy(gx,ierr)
130:        call PetscFinalize(ierr)
131:        end