1 #include "artdaq/DAQdata/Globals.hh"
2 #define TRACE_NAME (app_name + "_PreferSameHost_policy").c_str()
4 #include "artdaq/DAQdata/HostMap.hh"
5 #include "artdaq/RoutingPolicies/PolicyMacros.hh"
6 #include "artdaq/RoutingPolicies/RoutingManagerPolicy.hh"
8 #include "fhiclcpp/ParameterSet.h"
27 , minimum_participants_(ps.get<int>(
"minimum_participants", 0))
60 std::map<int, int> sortTokens_();
61 void restoreUnusedTokens_(std::map<int, int>
const& sorted_tokens_);
62 int calculateMinimum_();
64 int minimum_participants_;
70 TLOG(12) <<
"PreferSameHostPolicy::GetCurrentTable token list size is " <<
tokens_.size();
71 auto table = sortTokens_();
72 TLOG(13) <<
"PreferSameHostPolicy::GetCurrentTable table size is " << table.size();
74 int minimum = calculateMinimum_();
75 bool endCondition = table.size() <
static_cast<size_t>(minimum);
76 TLOG(15) <<
"PreferSameHostPolicy::GetCurrentTable initial endCondition is " << endCondition <<
", minimum is " << minimum;
80 for (
auto it = table.begin(); it != table.end();)
82 TLOG(16) <<
"PreferSameHostPolicy::GetCurrentTable assigning sequenceID " <<
next_sequence_id_ <<
" to rank " << it->first;
86 if (table[it->first] <= 0)
95 endCondition = table.size() <
static_cast<size_t>(minimum);
98 restoreUnusedTokens_(table);
99 TLOG(13) <<
"PreferSameHostPolicy::GetCurrentTable " <<
tokens_.size() <<
" unused tokens will be saved for later";
101 TLOG(12) <<
"PreferSameHostPolicy::GetCurrentTable return with table size " << output.size();
106 auto table = sortTokens_();
109 if (table.empty())
return output;
111 if (host_map_.count(requesting_rank) == 0)
113 TLOG(TLVL_WARNING) <<
"Received Routing Request from rank " << requesting_rank <<
", which is not in my Host Map!";
115 auto host = host_map_[requesting_rank];
118 std::set<int> matching_ranks_;
120 int max_rank_tokens = 0;
121 for (
auto& entry : table)
123 if (entry.second == 0)
continue;
124 if (host_map_.count(entry.first) == 0)
126 TLOG(TLVL_WARNING) <<
"Receiver rank " << entry.first <<
" is not in the host map! Is this policy configured correctly?!";
130 if (host_map_[entry.first] == host)
132 matching_ranks_.insert(entry.first);
135 if (entry.second > max_rank_tokens)
137 max_rank = entry.first;
138 max_rank_tokens = entry.second;
142 if (matching_ranks_.size() == 0)
147 else if (matching_ranks_.size() == 1)
150 table[*matching_ranks_.begin()]--;
157 for (
auto& rank : matching_ranks_)
159 if (table[rank] > max)
169 restoreUnusedTokens_(table);
172 std::map<int, int> PreferSameHostPolicy::sortTokens_()
174 auto output = std::map<int, int>();
183 void PreferSameHostPolicy::restoreUnusedTokens_(std::map<int, int>
const& sorted_tokens)
185 for (
auto r : sorted_tokens)
187 for (
auto i = 0; i < r.second; ++i)
189 tokens_.push_back(r.first);
194 int PreferSameHostPolicy::calculateMinimum_()
201 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