     module wrap_pvm
     implicit none

     integer pkset

     interface pk 
        module procedure pk_i1
        module procedure pk_r1
        module procedure pk_in
        module procedure pk_rn
     end interface pk

     interface upk
        module procedure upk_i1
        module procedure upk_r1
        module procedure upk_in
        module procedure upk_rn
     end interface upk

     data pkset /0/
     contains

        subroutine destin( nproc,tids, msgtype, info)
          implicit none
          include 'fpvm3.h'
          integer info, msgtype, tids(0:32), nproc
          call pvmfmcast( nproc, tids, msgtype, info )
          pkset=0
        end subroutine destin

        subroutine recv( msgtype )
          implicit none
          integer, intent(in) :: msgtype
          integer info
          call pvmfrecv( -1, msgtype, info )
        end subroutine recv

        subroutine pk_i1(i)
          include 'fpvm3.h'
          integer, intent(in)  :: i
          integer info
          if(pkset.eq.0)then
            call pvmfinitsend( PVMDEFAULT, info ) 
            pkset=1
          endif
          call pvmfpack( INTEGER4, i, 1, 1, info )
        end subroutine pk_i1

        subroutine pk_r1(r)
          include 'fpvm3.h'
          real(8), intent(in) :: r
          integer info
          if(pkset.eq.0)then
            call pvmfinitsend( PVMDEFAULT, info ) 
            pkset=1
          endif
          call pvmfpack( REAL8, r, 1, 1, info)
        end subroutine pk_r1

        subroutine pk_in(i,n,strd)
          implicit none
          include 'fpvm3.h'
          integer, dimension(:), intent(in) :: i
          integer, intent(in) :: n
          integer, intent(in), optional :: strd
          logical :: strd_chk
          integer info
          if(pkset.eq.0)then
            call pvmfinitsend( PVMDEFAULT, info ) 
            pkset=1
          endif
          strd_chk = present(strd)
          call pvmfpack( INTEGER4, n, 1, 1, info )
          select case(strd_chk)
          case(.true.)
            call pvmfpack( INTEGER4, i, n, strd, info )
          case(.false.)
            call pvmfpack( INTEGER4, i, n, 1, info )
          end select
        end subroutine pk_in 

        subroutine pk_rn(r,n)
          implicit none
          include 'fpvm3.h'
          integer info
          real(8), intent(in), dimension(:) :: r
          integer, intent(in) :: n
          if(pkset.eq.0)then
            call pvmfinitsend( PVMDEFAULT, info ) 
            pkset=1
          endif
          call pvmfpack( INTEGER4, n, 1, 1, info )
          call pvmfpack( REAL8,    r, n, 1, info )
        end subroutine pk_rn

        subroutine upk_i1(i)
          implicit none
          include 'fpvm3.h'
          integer, intent(out) :: i
          integer info
          call pvmfunpack( INTEGER4, i, 1, 1, info )          
        end subroutine upk_i1

        subroutine upk_r1(r)
          implicit none
          include 'fpvm3.h'
          real(8), intent(out) :: r 
          integer info
          call pvmfunpack( REAL8, r, 1, 1, info )
        end subroutine upk_r1

        subroutine upk_in(i,n)
          implicit none
          include 'fpvm3.h'
          integer, dimension(:), intent(out) :: i
          integer, intent(out) :: n
          integer info
          call pvmfunpack( INTEGER4, n, 1, 1, info )
          call pvmfunpack( INTEGER4, i, n, 1, info )
        end subroutine upk_in

        subroutine upk_rn(r,n)
          implicit none
          include 'fpvm3.h'
          real(8), dimension(:), intent(out) :: r
          integer, intent(out) :: n
          integer info
          call pvmfunpack( INTEGER4, n, 1, 1, info )
          call pvmfunpack( REAL8, r, n, 1, info )
        end subroutine upk_rn 

     end module wrap_pvm 
  
