Actual source code: ex14f.F

      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: !     petsc.h       - base PETSc routines
     24: !     petscvec.h    - vectors
     25: !

     27:  #include include/finclude/petsc.h
     28:  #include include/finclude/petscvec.h

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

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

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

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

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

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

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

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

     94: !      Test VecDuplicate

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

     99: !      Access the local Form

    101:        call VecGhostGetLocalForm(gx,lx,ierr)

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

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

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

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

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

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

    121:        call VecView(lx,PETSC_VIEWER_STDOUT_SELF,ierr)

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