subroutine MAPL_CollectiveScatter3D(Grid, GlobArray, LocArray, hw, rc)
type (ESMF_Grid), intent(IN ) :: Grid
real, target, intent(INOUT) :: LocArray(:,:,:)
real, intent(IN ) :: GlobArray(:,:,:)
integer, optional, intent(IN ) :: hw
integer, optional, intent( OUT) :: rc
! Locals
!-------
integer :: status
type (MAPL_CommRequest) :: reqs(size(LocArray,3))
integer :: root(size(LocArray,3))
integer :: nNodes
integer :: LM, L, nc, npes, mype
integer :: nn
type(ESMF_VM) :: VM
logical :: HaveGlobal
integer :: comm
integer :: hw_
! Begin
!------
call ESMF_VMGetCurrent(VM, RC=STATUS)
_VERIFY(STATUS)
call ESMF_VMGet(VM, petcount=npes, localpet=MYPE, mpiCommunicator=comm, RC=STATUS)
_VERIFY(STATUS)
if(present(hw)) then
hw_ = hw
else
hw_ = 0
endif
nNodes = size(MAPL_NodeRankList)
call MAPL_RoundRobinPEList(Root, nNodes, RC=STATUS)
_VERIFY(STATUS)
LM = size(LocArray,3)
NC = count(Root==mype)
HaveGlobal = NC>0
do L=1,LM
call MAPL_CreateRequest(GRID, Root(L), reqs(L), tag=L, &
RequestType=MAPL_IsScatter, &
DstArray=LocArray(:,:,L), &
PrePost=.true., hw=hw_, RC=STATUS)
_VERIFY(STATUS)
enddo
if(HaveGlobal) then
_ASSERT(size(GlobArray,3)==NC, 'inconsisntent rank')
nn = 0
do L=1,LM
if(Root(L)==mype) then
nn = nn + 1
call MAPL_ArrayIScatter (GlobArray(:,:,nn), reqs(L), hw=hw_, RC=STATUS)
_VERIFY(STATUS)
if(nn==NC) exit
endif
enddo
end if
do L=1,LM
call MAPL_CollectiveWait(reqs(L), rc=status)
_VERIFY(STATUS)
end do
_RETURN(ESMF_SUCCESS)
end subroutine MAPL_CollectiveScatter3D