subroutine handle_group_node_requests(ctx, fragment_queue, group_fragment_ids, group_polymers, finished_nodes)
!! Handle a single pending node coordinator request for a group shard, if any.
use mqc_many_body_expansion, only: many_body_expansion_t
class(many_body_expansion_t), intent(in) :: ctx
type(queue_t), intent(inout) :: fragment_queue
integer(int64), intent(in) :: group_fragment_ids(:)
integer, intent(in) :: group_polymers(:, :)
integer, intent(inout) :: finished_nodes
integer :: request_source, dummy_msg
integer(int64) :: local_idx, fragment_idx
type(MPI_Status) :: status
logical :: has_pending, has_fragment
type(request_t) :: req
call iprobe(ctx%resources%mpi_comms%world_comm, MPI_ANY_SOURCE, TAG_NODE_REQUEST, has_pending, status)
if (.not. has_pending) return
call irecv(ctx%resources%mpi_comms%world_comm, dummy_msg, status%MPI_SOURCE, TAG_NODE_REQUEST, req)
call wait(req)
request_source = status%MPI_SOURCE
call queue_pop(fragment_queue, local_idx, has_fragment)
if (has_fragment) then
fragment_idx = group_fragment_ids(local_idx)
call send_fragment_payload_from_row(ctx%resources%mpi_comms%world_comm, TAG_NODE_FRAGMENT, fragment_idx, &
group_polymers(local_idx, :), request_source)
else
call isend(ctx%resources%mpi_comms%world_comm, -1, request_source, TAG_NODE_FINISH, req)
call wait(req)
finished_nodes = finished_nodes + 1
end if
end subroutine handle_group_node_requests