subroutine handle_group_node_requests(resources, term_queue, term_ids, atom_sets, finished_nodes)
use mqc_resources, only: resources_t
type(resources_t), intent(in) :: resources
type(queue_t), intent(inout) :: term_queue
integer(int64), intent(in) :: term_ids(:)
integer, intent(in) :: atom_sets(:, :)
integer, intent(inout) :: finished_nodes
integer :: request_source, dummy_msg
integer(int64) :: local_idx, term_idx
type(MPI_Status) :: status
logical :: has_pending, has_item
type(request_t) :: req
call iprobe(resources%mpi_comms%world_comm, MPI_ANY_SOURCE, TAG_NODE_REQUEST, has_pending, status)
if (.not. has_pending) return
call irecv(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(term_queue, local_idx, has_item)
if (has_item) then
term_idx = term_ids(local_idx)
call send_pie_term_payload(resources%mpi_comms%world_comm, TAG_NODE_FRAGMENT, term_idx, &
atom_sets(:, local_idx), request_source)
else
call isend(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