|
| 1 | +#include "index_router.hpp" |
| 2 | + |
| 3 | +#include "fmt/format.h" |
| 4 | +#include "fmt/ranges.h" |
| 5 | +#include "spdlog/spdlog.h" |
| 6 | + |
| 7 | +#include <cassert> |
| 8 | +#include <stdexcept> |
| 9 | +#include <utility> |
| 10 | + |
| 11 | +using namespace tbb; |
| 12 | +using namespace phlex; |
| 13 | + |
| 14 | +test::index_router::index_router(flow::graph& g, |
| 15 | + std::vector<std::string> layers, |
| 16 | + std::map<std::string, named_index_ports> multilayers) |
| 17 | +{ |
| 18 | + for (auto const& layer : layers) { |
| 19 | + broadcasters_.try_emplace(layer, g); |
| 20 | + } |
| 21 | + for (auto const& [node_name, multilayer] : multilayers) { |
| 22 | + spdlog::trace("Making multilayer caster for {}", node_name); |
| 23 | + multibroadcaster_entries casters; |
| 24 | + casters.reserve(multilayer.size()); |
| 25 | + for (auto const& [layer, flush_port, input_port] : multilayer) { |
| 26 | + auto& entry = casters.emplace_back(layer, index_set_node{g}, flush_node{g}); |
| 27 | + make_edge(entry.broadcaster, *input_port); // Connect with index ports of multi-algorithms |
| 28 | + make_edge(entry.flusher, *flush_port); // Connect with flush ports of multi-algorithms |
| 29 | + } |
| 30 | + multibroadcasters_.try_emplace(node_name, std::move(casters)); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +void test::index_router::shutdown() |
| 35 | +{ |
| 36 | + backout_to(data_cell_index::base_ptr()); |
| 37 | + last_index_ = nullptr; |
| 38 | +} |
| 39 | + |
| 40 | +void test::index_router::route(data_cell_index_ptr const& index) |
| 41 | +{ |
| 42 | + backout_to(index); |
| 43 | + auto msg_id = counter_.fetch_add(1); |
| 44 | + send(index, msg_id); |
| 45 | + multisend(index, msg_id); |
| 46 | + last_index_ = index; |
| 47 | +} |
| 48 | + |
| 49 | +void test::index_router::backout_to(data_cell_index_ptr const& index) |
| 50 | +{ |
| 51 | + assert(index); |
| 52 | + |
| 53 | + if (!last_index_) { |
| 54 | + // This happens when we encounter the first index |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (index->parent() == last_index_->parent()) { |
| 59 | + // At the same level in the hierarchy |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + if (index->parent(last_index_->layer_name())) { |
| 64 | + // Descending further into the hierarchy |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // What's left is situations where we need to go up the hierarchy chain. |
| 69 | + |
| 70 | + auto do_the_put = [this](data_cell_index_ptr const& index) { |
| 71 | + // FIXME: This lookup should be fixed |
| 72 | + for (auto& [_, senders] : cached_multicasters_) { |
| 73 | + for (auto& sender : senders) { |
| 74 | + if (sender.layer() == index->layer_name()) { |
| 75 | + sender.put_end_token(index); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + auto current = last_index_; |
| 82 | + while (current and current->layer_hash() != index->layer_hash()) { |
| 83 | + do_the_put(current); |
| 84 | + current = current->parent(); |
| 85 | + assert(current); // Cannot be non-null |
| 86 | + } |
| 87 | + do_the_put(current); |
| 88 | +} |
| 89 | + |
| 90 | +auto test::index_router::index_node_for(std::string const& layer) -> index_set_node& |
| 91 | +{ |
| 92 | + std::vector<broadcasters_t::iterator> candidates; |
| 93 | + for (auto it = broadcasters_.begin(), e = broadcasters_.end(); it != e; ++it) { |
| 94 | + if (it->first.ends_with("/" + layer)) { |
| 95 | + candidates.push_back(it); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (candidates.size() == 1ull) { |
| 100 | + return candidates[0]->second; |
| 101 | + } |
| 102 | + |
| 103 | + if (candidates.empty()) { |
| 104 | + throw std::runtime_error("No broadcaster found for layer specification" + layer); |
| 105 | + } |
| 106 | + |
| 107 | + std::string msg{"Multiple layers match specification " + layer + ":\n"}; |
| 108 | + for (auto const& it : candidates) { |
| 109 | + msg += "\n- " + it->first; |
| 110 | + } |
| 111 | + throw std::runtime_error(msg); |
| 112 | +} |
| 113 | + |
| 114 | +void test::index_router::send(data_cell_index_ptr const& index, std::size_t message_id) |
| 115 | +{ |
| 116 | + auto it = broadcasters_.find(index->layer_path()); |
| 117 | + assert(it != broadcasters_.end()); |
| 118 | + it->second.try_put({.msg_id = message_id, .index = index}); |
| 119 | +} |
| 120 | + |
| 121 | +void test::index_router::multisend(data_cell_index_ptr const& index, std::size_t message_id) |
| 122 | +{ |
| 123 | + auto const layer_hash = index->layer_hash(); |
| 124 | + // spdlog::trace("Multilayer send for layer hash {} {}", layer_hash, index->to_string()); |
| 125 | + |
| 126 | + auto do_the_put = [](data_cell_index_ptr const& index, |
| 127 | + std::size_t message_id, |
| 128 | + std::vector<multilayer_sender>& nodes) { |
| 129 | + for (auto& sender : nodes) { |
| 130 | + sender.put_message(index, message_id); |
| 131 | + } |
| 132 | + }; |
| 133 | + |
| 134 | + if (auto it = cached_multicasters_.find(layer_hash); it != cached_multicasters_.end()) { |
| 135 | + do_the_put(index, message_id, it->second); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + auto [it, _] = cached_multicasters_.try_emplace(layer_hash); |
| 140 | + |
| 141 | + // spdlog::trace("Assigning new multi-caster for {} (path: {})", layer_hash, index->layer_path()); |
| 142 | + for (auto& [multilayer_str, entries] : multibroadcasters_) { |
| 143 | + // Now we need to check how to match "ports" and the multilayer |
| 144 | + std::vector<multilayer_sender> senders; |
| 145 | + senders.reserve(entries.size()); |
| 146 | + bool name_in_multilayer = false; |
| 147 | + for (auto& [layer, caster, flusher] : entries) { |
| 148 | + if (layer == index->layer_name()) { |
| 149 | + senders.emplace_back(layer, &caster, &flusher); |
| 150 | + name_in_multilayer = true; |
| 151 | + } else if (index->parent(layer)) { |
| 152 | + senders.emplace_back(layer, &caster, &flusher); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if (name_in_multilayer and senders.size() == entries.size()) { |
| 157 | + // spdlog::trace("Match for {}: {} (path: {})", multilayer_str, layer_hash, index->layer_path()); |
| 158 | + it->second.insert(it->second.end(), |
| 159 | + std::make_move_iterator(senders.begin()), |
| 160 | + std::make_move_iterator(senders.end())); |
| 161 | + } |
| 162 | + } |
| 163 | + // if (it->second.empty()) { |
| 164 | + // spdlog::trace("No broadcasters for {}", layer_hash); |
| 165 | + // } else { |
| 166 | + // spdlog::trace("Number of broadcasters for {}: {}", layer_hash, it->second.size()); |
| 167 | + // } |
| 168 | + do_the_put(index, message_id, it->second); |
| 169 | +} |
| 170 | + |
| 171 | +test::index_router::multilayer_sender::multilayer_sender(std::string const& layer, |
| 172 | + index_set_node* broadcaster, |
| 173 | + flush_node* flusher) : |
| 174 | + layer_{layer}, broadcaster_{broadcaster}, flusher_{flusher} |
| 175 | +{ |
| 176 | +} |
| 177 | + |
| 178 | +void test::index_router::multilayer_sender::put_message(data_cell_index_ptr const& index, |
| 179 | + std::size_t message_id) |
| 180 | +{ |
| 181 | + if (layer_ == index->layer_name()) { |
| 182 | + broadcaster_->try_put({.msg_id = message_id, .index = index, .cache = false}); |
| 183 | + return; |
| 184 | + } |
| 185 | + |
| 186 | + // Flush values are needed only used for indices that are *not* the "lowest" in the branch |
| 187 | + // of the hierarchy. |
| 188 | + ++counter_; |
| 189 | + broadcaster_->try_put({.msg_id = message_id, .index = index->parent(layer_)}); |
| 190 | +} |
| 191 | + |
| 192 | +void test::index_router::multilayer_sender::put_end_token(data_cell_index_ptr const& index) |
| 193 | +{ |
| 194 | + auto count = std::exchange(counter_, 0); |
| 195 | + if (count == 0) { |
| 196 | + // See comment above about flush values |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + flusher_->try_put({.index = index, .count = count}); |
| 201 | +} |
0 commit comments