!
!   On src:   m(10,10) 
!             send section m(2:6,4:9)
!
!   On dest   m(12,8)
!             recv section m(7:11,2:7)

      program array_sect
  
      include "mpif.h"
      integer ierror, rank, size
      integer  src, dest
      parameter (src = 1, dest = 0)
      integer section
      
      call MPI_Init(ierror)
      
      call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierror)
      call MPI_Comm_size(MPI_COMM_WORLD, size, ierror)
      
      if (rank==src) then
         call src_prog
      else
         call dest_prog
      endif

      call MPI_Finalize(ierror)
      
      stop
      end 


      subroutine src_prog
      implicit none
      include "mpif.h"
      integer  src, dest
      parameter (src = 1, dest = 0)
      integer  mx, my, nx, ny
      parameter(mx = 10, my = 10)
      integer m(mx,my)
      integer section
      integer i,j, ierror

      write(*,'("Source array:")')
      do j = 1,my
         do i = 1,mx
            m(i,j) = (i-1)*my + j-1
         enddo
         write(*,'(10i4)') (m(i,j),i=1,10)
      enddo
      
    ! m(2:6,4:9)
      nx = 6-2+1
      ny = 9-4+1

      call MPI_Type_Vector(ny, nx, mx, MPI_INTEGER, section, ierror)

      call MPI_Type_Commit(section, ierror)

      call MPI_Send( m(2,4), 1, section, dest, 7, MPI_COMM_WORLD, 
     .        ierror)

      return
      end


      
      subroutine dest_prog
      implicit none
      include "mpif.h"
      integer  src, dest
      parameter (src = 1, dest = 0)
      integer  mx, my, nx, ny
      parameter(mx = 12, my = 8)
      integer m(mx,my)
      integer section, status(MPI_STATUS_SIZE)
      integer i,j, ierror

      do j = 1,my
         do i = 1,mx
            m(i,j) = 0.0
         enddo
      enddo
      
    ! m(7:11,2:7)
      nx = 11-7+1
      ny = 7-2+1    

      call MPI_Type_Vector(ny, nx, mx, MPI_INTEGER, section, ierror)

      call MPI_Type_Commit(section, ierror)

      call MPI_Recv( m(7,2), 1, section, src, 7, 
     .     MPI_COMM_WORLD, status, ierror)

      print *, "Target array:"
      do j = 1,7
         write(*,'(12i4)') (m(i,j),i=1,12)
      enddo
      

      return
      end
