Actual source code: ex9f.F90

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

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

     31:       nlocal = 6
     32:       nghost = 2

     34:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
     35:       if (ierr /= 0) then
     36:         print*,'PetscInitialize failed'
     37:         stop
     38:       endif
     39:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
     40:       call MPI_Comm_size(PETSC_COMM_WORLD,mySize,ierr)

     42:       if (mySize /= 2) then; SETERRA(PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,'Requires 2 processors'); endif

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

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

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

     77:       call PetscOptionsHasName(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,   &
     78:      &                         '-allocate',flag,ierr)
     79:       if (flag) then
     80:         call VecCreateGhostWithArray(PETSC_COMM_WORLD,nlocal,            &
     81:      &        PETSC_DECIDE,nghost,ifrom,tarray,gxs,ierr)
     82:       else
     83:         call VecCreateGhost(PETSC_COMM_WORLD,nlocal,PETSC_DECIDE,        &
     84:      &       nghost,ifrom,gxs,ierr)
     85:       endif

     88: !      Test VecDuplicate

     90:        call VecDuplicate(gxs,gx,ierr)
     91:        call VecDestroy(gxs,ierr)

     93: !      Access the local Form

     95:        call VecGhostGetLocalForm(gx,lx,ierr)

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

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

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

    107:        call VecAssemblyBegin(gx,ierr)
    108:        call VecAssemblyEnd(gx,ierr)

    110:        call VecGhostUpdateBegin(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)
    111:        call VecGhostUpdateEnd(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)

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

    115:        call PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,subviewer,ierr)
    116:        call VecView(lx,subviewer,ierr)
    117:        call PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,subviewer,ierr)

    119:        call VecGhostRestoreLocalForm(gx,lx,ierr)
    120:        call VecDestroy(gx,ierr)
    121:        call PetscFinalize(ierr)
    122:        end

    125: !/*TEST
    126: !
    127: !     test:
    128: !       nsize: 2
    129: !
    130: !     test:
    131: !       suffix: 2
    132: !       nsize: 2
    133: !       args: -allocate
    134: !
    135: !TEST*/