subroutine pack_and_send_int(vm, bufsize, recvPets, rootPet, buffer, &
outbuffer, dims)
type(ESMF_VM) :: vm
integer :: bufsize(:)
integer :: recvPets
integer :: rootPet
integer :: buffer(:)
integer :: outbuffer(:,:)
integer :: dims(:)
integer :: xdim, start
integer :: lbnd(2), ubnd(2)
integer :: i,j,k,ii
integer, pointer :: sendbuf(:)
integer :: localrc
! fill my own pointer first
lbnd = lbound(outbuffer)
ubnd = ubound(outbuffer)
xdim = ubnd(1)-lbnd(1)+1
bufsize(2) = ubnd(2)-lbnd(2)+1
do i=1,bufsize(2)
outbuffer(:, lbnd(2)+i-1) = buffer((i-1)*bufsize(1)+1 : (i-1)*bufsize(1)+xdim)
enddo
if (recvPets > 1) then
allocate(sendbuf(dims(1)*bufsize(2)))
start=xdim
do k = 1, recvPets-1
if (k>1) then
if (dims(k) /= dims(k-1)) then
deallocate(sendbuf)
allocate(sendbuf(dims(k)*bufsize(2)))
endif
endif
ii = 1
do j = 1, bufsize(2)
do i = start+1, start+dims(k)
sendbuf(ii) = buffer((j-1)*bufsize(1)+i)
ii=ii+1
enddo
enddo
call ESMF_VMSend(vm, sendbuf, dims(k)*bufsize(2), rootPet+k, rc=localrc)
start=start+dims(k)
enddo
deallocate(sendbuf)
endif
end subroutine pack_and_send_int