1 #include "artdaq/DAQdata/Globals.hh"
2 #define TRACE_NAME (app_name + "_PreferSameHost_policy").c_str()
3 #include "TRACE/tracemf.h"
5 #include "artdaq/DAQdata/HostMap.hh"
6 #include "artdaq/RoutingPolicies/PolicyMacros.hh"
7 #include "artdaq/RoutingPolicies/RoutingManagerPolicy.hh"
9 #include "fhiclcpp/ParameterSet.h"
29 , minimum_participants_(ps.get<int>(
"minimum_participants", 0))
62 std::map<int, int> sortTokens_();
63 void restoreUnusedTokens_(std::map<int, int>
const& sorted_tokens_);
64 int calculateMinimum_();
66 int minimum_participants_;
72 TLOG(TLVL_DEBUG + 35) <<
"PreferSameHostPolicy::GetCurrentTable token list size is " <<
tokens_.size();
73 auto table = sortTokens_();
74 TLOG(TLVL_DEBUG + 36) <<
"PreferSameHostPolicy::GetCurrentTable table size is " << table.size();
76 int minimum = calculateMinimum_();
77 bool endCondition = table.size() <
static_cast<size_t>(minimum);
78 TLOG(TLVL_DEBUG + 37) <<
"PreferSameHostPolicy::GetCurrentTable initial endCondition is " << endCondition <<
", minimum is " << minimum;
82 for (
auto it = table.begin(); it != table.end();)
84 TLOG(TLVL_DEBUG + 38) <<
"PreferSameHostPolicy::GetCurrentTable assigning sequenceID " <<
next_sequence_id_ <<
" to rank " << it->first;
88 if (table[it->first] <= 0)
97 endCondition = table.size() <
static_cast<size_t>(minimum);
100 restoreUnusedTokens_(table);
101 TLOG(TLVL_DEBUG + 36) <<
"PreferSameHostPolicy::GetCurrentTable " <<
tokens_.size() <<
" unused tokens will be saved for later";
103 TLOG(TLVL_DEBUG + 35) <<
"PreferSameHostPolicy::GetCurrentTable return with table size " << output.size();
108 auto table = sortTokens_();
111 if (table.empty())
return output;
113 if (host_map_.count(requesting_rank) == 0)
115 TLOG(TLVL_WARNING) <<
"Received Routing Request from rank " << requesting_rank <<
", which is not in my Host Map!";
117 auto host = host_map_[requesting_rank];
120 std::set<int> matching_ranks_;
122 int max_rank_tokens = 0;
123 for (
auto& entry : table)
125 if (entry.second == 0)
continue;
126 if (host_map_.count(entry.first) == 0)
128 TLOG(TLVL_WARNING) <<
"Receiver rank " << entry.first <<
" is not in the host map! Is this policy configured correctly?!";
132 if (host_map_[entry.first] == host)
134 matching_ranks_.insert(entry.first);
137 if (entry.second > max_rank_tokens)
139 max_rank = entry.first;
140 max_rank_tokens = entry.second;
144 if (matching_ranks_.size() == 0)
149 else if (matching_ranks_.size() == 1)
152 table[*matching_ranks_.begin()]--;
159 for (
auto& rank : matching_ranks_)
161 if (table[rank] > max)
171 restoreUnusedTokens_(table);
174 std::map<int, int> PreferSameHostPolicy::sortTokens_()
176 auto output = std::map<int, int>();
185 void PreferSameHostPolicy::restoreUnusedTokens_(std::map<int, int>
const& sorted_tokens)
187 for (
auto r : sorted_tokens)
189 for (
auto i = 0; i < r.second; ++i)
191 tokens_.push_back(r.first);
196 int PreferSameHostPolicy::calculateMinimum_()
203 int minimum = minimum_participants_ > 0 ? minimum_participants_ :
GetReceiverCount() + minimum_participants_;
A row of the Routing Table.
PreferSameHostPolicy(const fhicl::ParameterSet &ps)
PreferSameHostPolicy Constructor.
Fragment::sequence_id_t next_sequence_id_
The next sequence ID to be assigned.
The interface through which RoutingManagerCore obtains Routing Tables using received Routing Tokens...
detail::RoutingPacketEntry CreateRouteForSequenceID(artdaq::Fragment::sequence_id_t seq, int requesting_rank) override
Get an artdaq::detail::RoutingPacketEntry for a given sequence ID and rank. Used by RequestBasedEvent...
void CreateRoutingTable(detail::RoutingPacket &output) override
Generate a set of Routing Tables using received tokens.
~PreferSameHostPolicy() override=default
Default virtual Destructor.
std::vector< RoutingPacketEntry > RoutingPacket
A RoutingPacket is simply a vector of RoutingPacketEntry objects. It is not suitable for network tran...
A RoutingManagerPolicy which tries to keep data on the same host. For EventBuilding mode...
size_t GetReceiverCount() const
Get the number of configured receivers.
std::deque< int > tokens_
The list of tokens which are available for use.
std::map< int, std::string > hostMap_t
The host_map is a map associating ranks with artdaq::DestinationInfo objects.
hostMap_t MakeHostMap(fhicl::ParameterSet const &pset, hostMap_t map=hostMap_t())
Make a hostMap_t from a HostMap::Config ParameterSet