Actual source code: ex14f.F90

  1: !
  2: !
  3: ! Description: Illustrates the use of VecCreateGhost()
  4: !
  5: !
  6: !      Ghost padding is one way to handle local calculations that
  7: !      involve values from other processors. VecCreateGhostBlock() provides
  8: !      a way to create vectors with extra room at the end of the vector
  9: !      array to contain the needed ghost values from other processors,
 10: !      vector computations are otherwise unaffected.
 11: !
 12: #include <petsc/finclude/petscvec.h>
 13: program main
 14:   use petscvec
 15:   implicit none

 17:   PetscMPIInt size, rank
 18:   PetscInt nlocal, nghost, ifrom(2)
 19:   PetscInt i, rstart, rend, bs, ione
 20:   PetscBool flag
 21:   PetscErrorCode ierr
 22:   PetscScalar value, tarray(20)
 23:   Vec lx, gx, gxs
 24:   PetscViewer singleton

 26:   nlocal = 6
 27:   nghost = 2
 28:   bs = 2
 29:   nlocal = bs*nlocal

 31:   PetscCallA(PetscInitialize(ierr))
 32:   PetscCallMPIA(MPI_Comm_rank(PETSC_COMM_WORLD, rank, ierr))
 33:   PetscCallMPIA(MPI_Comm_size(PETSC_COMM_WORLD, size, ierr))

 35:   PetscCheckA(size == 2, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, 'Requires 2 processors')

 37: !
 38: !     Construct a two dimensional graph connecting nlocal degrees of
 39: !     freedom per processor. From this we will generate the global
 40: !     indices of needed ghost values
 41: !
 42: !     For simplicity we generate the entire graph on each processor:
 43: !     in real application the graph would stored in parallel, but this
 44: !     example is only to demonstrate the management of ghost padding
 45: !     with VecCreateGhost().
 46: !
 47: !     In this example we consider the vector as representing
 48: !     degrees of freedom in a one dimensional grid with periodic
 49: !     boundary conditions.
 50: !
 51: !        ----Processor  1-----------  ----Processor 2 --------
 52: !         0 1 2 3 4 5 6 7 8 9 10 11    12 13 14 15 16 17 18 19 20 21 22 23
 53: !                              |--|----|---|
 54: !         |-|--------------------------------------------------------|--|
 55: !

 57:   if (rank == 0) then
 58:     ifrom(1) = 11
 59:     ifrom(2) = 6
 60:   else
 61:     ifrom(1) = 0
 62:     ifrom(2) = 5
 63:   end if

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

 69:   PetscCallA(PetscOptionsHasName(PETSC_NULL_OPTIONS, PETSC_NULL_CHARACTER, '-allocate', flag, ierr))
 70:   if (flag) then
 71:     PetscCallA(VecCreateGhostBlockWithArray(PETSC_COMM_WORLD, bs, nlocal, PETSC_DECIDE, nghost, ifrom, tarray, gxs, ierr))
 72:   else
 73:     PetscCallA(VecCreateGhostBlock(PETSC_COMM_WORLD, bs, nlocal, PETSC_DECIDE, nghost, ifrom, gxs, ierr))
 74:   end if

 76: !      Test VecDuplicate

 78:   PetscCallA(VecDuplicate(gxs, gx, ierr))
 79:   PetscCallA(VecDestroy(gxs, ierr))

 81: !      Access the local Form

 83:   PetscCallA(VecGhostGetLocalForm(gx, lx, ierr))

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

 87:   PetscCallA(VecGetOwnershipRange(gx, rstart, rend, ierr))

 89:   ione = 1
 90:   do i = rstart, rend - 1
 91:     value = i
 92:     PetscCallA(VecSetValues(gx, ione, [i], [value], INSERT_VALUES, ierr))
 93:   end do

 95:   PetscCallA(VecAssemblyBegin(gx, ierr))
 96:   PetscCallA(VecAssemblyEnd(gx, ierr))

 98:   PetscCallA(VecGhostUpdateBegin(gx, INSERT_VALUES, SCATTER_FORWARD, ierr))
 99:   PetscCallA(VecGhostUpdateEnd(gx, INSERT_VALUES, SCATTER_FORWARD, ierr))

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

103:   PetscCallA(PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, singleton, ierr))
104:   PetscCallA(VecView(lx, singleton, ierr))
105:   PetscCallA(PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, singleton, ierr))

107:   PetscCallA(VecGhostRestoreLocalForm(gx, lx, ierr))
108:   PetscCallA(VecDestroy(gx, ierr))
109:   PetscCallA(PetscFinalize(ierr))
110: end

112: !/*TEST
113: !
114: !     test:
115: !       nsize: 2
116: !
117: !TEST*/