Actual source code: ex17f.F

  1: !
  2: !
  3: !   "Scatters from a parallel vector to a sequential vector.  In
  4: !  this case each local vector is as long as the entire parallel vector.
  5: !
  6:       implicit none
  7: #include "finclude/petscsys.h"
  8: #include "finclude/petscis.h"
  9: #include "finclude/petscvec.h"

 11:       PetscErrorCode ierr
 12:       PetscMPIInt  size,rank
 13:       PetscInt     n,NN,low,high
 14:       PetscInt     iglobal,i,ione
 15:       PetscInt     first,stride
 16:       PetscScalar  value,zero
 17:       Vec          x,y
 18:       IS           is1,is2
 19:       VecScatter   ctx

 21:       n    = 5
 22:       zero = 0.d0
 23:       call  PetscInitialize(PETSC_NULL_CHARACTER,ierr)

 25:       call MPI_Comm_size(MPI_COMM_WORLD,size,ierr)
 26:       call MPI_Comm_rank(MPI_COMM_WORLD,rank,ierr)

 28: !     create two vectors 
 29: !     one parallel and one sequential. The sequential one on each processor
 30: !     is as long as the entire parallel one.

 32:       NN = size*n

 34:       call VecCreateMPI(MPI_COMM_WORLD,PETSC_DECIDE,NN,y,ierr)
 35:       call VecCreateSeq(MPI_COMM_SELF,NN,x,ierr)

 37:       call VecSet(x,zero,ierr)
 38:       call VecGetOwnershipRange(y,low,high,ierr)
 39:       ione = 1
 40:       do 10, i=0,n-1
 41:          iglobal = i + low
 42:          value   = i + 10*rank
 43:          call VecSetValues(y,ione,iglobal,value,INSERT_VALUES,ierr)
 44:  10   continue

 46:       call VecAssemblyBegin(y,ierr)
 47:       call VecAssemblyEnd(y,ierr)
 48: !
 49: !   View the parallel vector
 50: !
 51:       call VecView(y,PETSC_VIEWER_STDOUT_WORLD,ierr)

 53: !     create two index sets and the scatter context to move the contents of
 54: !     of the parallel vector to each sequential vector. If you want the
 55: !     parallel vector delivered to only one processor then create a is2 
 56: !     of length zero on all processors except the one to receive the parallel vector

 58:       first = 0
 59:       stride = 1
 60:       call ISCreateStride(PETSC_COMM_SELF,NN,first,stride,is1,ierr)
 61:       call ISCreateStride(PETSC_COMM_SELF,NN,first,stride,is2,ierr)
 62:       call VecScatterCreate(y,is2,x,is1,ctx,ierr)
 63:       call VecScatterBegin(ctx,y,x,ADD_VALUES,SCATTER_FORWARD,ierr)
 64:       call VecScatterEnd(ctx,y,x,ADD_VALUES,SCATTER_FORWARD,ierr)
 65:       call VecScatterDestroy(ctx,ierr)
 66: !
 67: !   View the sequential vector on the 0th processor
 68: !  
 69:       if (rank .eq. 0) then
 70:         call VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr)
 71:       endif

 73:       call VecDestroy(x,ierr)
 74:       call VecDestroy(y,ierr)
 75:       call ISDestroy(is1,ierr)
 76:       call ISDestroy(is2,ierr)

 78:       call PetscFinalize(ierr) 
 79:       end
 80: