subroutine distribute_and_add_walkers(block_size, gdata_read_handler, &
temp_ilut, temp_sgns, gdata_buf, &
dets, nreceived, CurrWalkers, norm, parts)
use FciMCData, only: MaxSpawned
implicit none
integer(n_int), intent(out) :: dets(:, :)
integer(int64), intent(inout) :: CurrWalkers
integer(hsize_t) :: block_size
type(gdata_io_t), intent(in) :: gdata_read_handler
integer:: nreceived
real(dp), intent(inout) :: norm(lenof_sign), parts(lenof_sign)
integer(hsize_t):: temp_ilut(:, :), temp_sgns(:, :), gdata_buf(:, :)
integer(MPIArg) :: sendcount(0:nProcessors - 1)
integer :: nlocal = 0
integer :: gdata_size
integer :: ierr
integer(hsize_t), allocatable :: gdata_comm(:, :), gdata_loc(:, :)
! allocate the buffers for the gdata
gdata_size = size(gdata_buf, dim=1)
allocate(gdata_comm(gdata_size, MaxSpawned), stat=ierr)
allocate(gdata_loc(gdata_size, MaxSpawned), stat=ierr)
call assign_dets_to_procs_buff(block_size, temp_ilut, temp_sgns, &
gdata_buf, gdata_loc, gdata_comm, sendcount)
!add elements that are on the right processor already
#define localfirst
!#undef localfirst
#ifdef localfirst
nlocal = sendcount(iProcIndex)
call add_new_parts(dets, nlocal, CurrWalkers, norm, parts, gdata_loc, gdata_read_handler)
sendcount(iProcIndex) = 0
#endif
!communicate the remaining elements
nreceived = communicate_read_walkers_buff(sendcount, gdata_comm, gdata_loc)
call add_new_parts(dets, nreceived, CurrWalkers, norm, parts, gdata_loc, gdata_read_handler)
if (allocated(receivebuff)) then
deallocate(receivebuff)
call LogMemDeAlloc('distribute_and_add', receivebuff_tag)
end if
nreceived = nreceived + nlocal
if (allocated(gdata_loc)) deallocate(gdata_loc)
if (allocated(gdata_comm)) deallocate(gdata_comm)
end subroutine distribute_and_add_walkers