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: !

 13: program main
 14: #include <petsc/finclude/petscvec.h>
 15:   use petscvec
 16:   implicit none

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

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

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

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

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

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

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

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

 77: !      Test VecDuplicate

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

 82: !      Access the local Form

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

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

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

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

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

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

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

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

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

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