Actual source code: ex14f.F90

petsc-3.10.5 2019-03-28
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:  #include <petsc/finclude/petscvec.h>
 19:       use petscvec
 20:       implicit none

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

 31:       nlocal = 6
 32:       nghost = 2
 33:       bs     = 2
 34:       nlocal = bs*nlocal

 36:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 37:       if (ierr .ne. 0) then
 38:         print*,'Unable to initialize PETSc'
 39:         stop
 40:       endif
 41:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
 42:       call MPI_Comm_size(PETSC_COMM_WORLD,size,ierr)

 44:       if (size .ne. 2) then; SETERRA(PETSC_COMM_WORLD,1,'Requires 2 processors'); endif

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


 67:       if (rank .eq. 0) then
 68:         ifrom(1) = 11
 69:         ifrom(2) = 6
 70:       else
 71:         ifrom(1) = 0
 72:         ifrom(2) = 5
 73:       endif

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

 79:       call PetscOptionsHasName(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,   &
 80:      &                         '-allocate',flag,ierr)
 81:       if (flag) then
 82:         call VecCreateGhostBlockWithArray(PETSC_COMM_WORLD,bs,nlocal,    &
 83:      &        PETSC_DECIDE,nghost,ifrom,tarray,gxs,ierr)
 84:       else
 85:         call VecCreateGhostBlock(PETSC_COMM_WORLD,bs,nlocal,             &
 86:      &       PETSC_DECIDE,nghost,ifrom,gxs,ierr)
 87:       endif


 90: !      Test VecDuplicate

 92:       call VecDuplicate(gxs,gx,ierr)
 93:       call VecDestroy(gxs,ierr)

 95: !      Access the local Form

 97:       call VecGhostGetLocalForm(gx,lx,ierr)

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

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

103:       ione = 1
104:       do 10, i=rstart,rend-1
105:         value = i
106:         call VecSetValues(gx,ione,i,value,INSERT_VALUES,ierr)
107:  10   continue

109:       call VecAssemblyBegin(gx,ierr)
110:       call VecAssemblyEnd(gx,ierr)

112:       call VecGhostUpdateBegin(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)
113:       call VecGhostUpdateEnd(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)

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

117:       call PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,singleton,ierr)
118:       call VecView(lx,singleton,ierr)
119:       call PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,singleton,ierr)

121:       call VecGhostRestoreLocalForm(gx,lx,ierr)
122:       call VecDestroy(gx,ierr)
123:       call PetscFinalize(ierr)
124:       end


127: !/*TEST
128: !
129: !     test:
130: !       nsize: 2
131: !
132: !TEST*/