|
| 1 | +/* |
| 2 | + * The router lookahead provides an estimate of the cost from an intermediate node to the target node |
| 3 | + * during directed (A*-like) routing. |
| 4 | + * |
| 5 | + * The lookahead in this file requires an externally loaded lookahead cost map which will be queried |
| 6 | + * based only on the distance between the currently explored node and the target sink. |
| 7 | + * |
| 8 | + * This lookahead can be made admissible if the loaded cost map contains the minimum delay and minimum |
| 9 | + * congestion costs per distance. |
| 10 | + */ |
| 11 | + |
| 12 | +#include <algorithm> |
| 13 | +#include <thread> |
| 14 | +#include <unordered_map> |
| 15 | +#include <vector> |
| 16 | +#include <queue> |
| 17 | +#include <fstream> |
| 18 | +#include "globals.h" |
| 19 | +#include "vtr_time.h" |
| 20 | +#include "router_lookahead.h" |
| 21 | +#include "router_lookahead_simple.h" |
| 22 | +#include "router_lookahead_map_utils.h" |
| 23 | +#include "rr_graph.h" |
| 24 | +#include "connection_router_interface.h" |
| 25 | +#include "arch_util.h" |
| 26 | + |
| 27 | +#ifdef VTR_ENABLE_CAPNPROTO |
| 28 | +#include "capnp/serialize.h" |
| 29 | +#include "map_lookahead.capnp.h" |
| 30 | +#include "ndmatrix_serdes.h" |
| 31 | +#include "intra_cluster_serdes.h" |
| 32 | +#include "mmap_file.h" |
| 33 | +#include "serdes_utils.h" |
| 34 | +#endif /* VTR_ENABLE_CAPNPROTO */ |
| 35 | + |
| 36 | +/******** File-Scope Variables ********/ |
| 37 | + |
| 38 | +//Look-up table from CHANX/CHANY (to SINKs) for various distances |
| 39 | +static t_simple_cost_map simple_cost_map; |
| 40 | + |
| 41 | +/******** File-Scope Functions ********/ |
| 42 | + |
| 43 | +static util::Cost_Entry get_wire_cost_entry(e_rr_type rr_type, |
| 44 | + int seg_index, |
| 45 | + int from_layer_num, |
| 46 | + int delta_x, |
| 47 | + int delta_y, |
| 48 | + int to_layer_num); |
| 49 | + |
| 50 | +static void read_router_lookahead(const std::string& file); |
| 51 | +static void write_router_lookahead(const std::string& file); |
| 52 | + |
| 53 | +/******** Interface class member function definitions ********/ |
| 54 | + |
| 55 | +float SimpleLookahead::get_expected_cost(RRNodeId current_node, RRNodeId target_node, const t_conn_cost_params& params, float R_upstream) const { |
| 56 | + // Get the total cost using the combined delay and congestion costs |
| 57 | + auto [delay_cost, cong_cost] = get_expected_delay_and_cong(current_node, target_node, params, R_upstream); |
| 58 | + return delay_cost + cong_cost; |
| 59 | +} |
| 60 | + |
| 61 | +std::pair<float, float> SimpleLookahead::get_expected_delay_and_cong(RRNodeId from_node, RRNodeId to_node, const t_conn_cost_params& params, float /*R_upstream*/) const { |
| 62 | + auto& device_ctx = g_vpr_ctx.device(); |
| 63 | + auto& rr_graph = device_ctx.rr_graph; |
| 64 | + |
| 65 | + float expected_delay_cost = std::numeric_limits<float>::max() / 1e12; |
| 66 | + float expected_cong_cost = std::numeric_limits<float>::max() / 1e12; |
| 67 | + |
| 68 | + e_rr_type from_type = rr_graph.node_type(from_node); |
| 69 | + util::Cost_Entry cost_entry(0, 0); |
| 70 | + if (is_chanxy(from_type) || is_chanz(from_type)) { |
| 71 | + int from_layer_num = rr_graph.node_layer(from_node); |
| 72 | + int to_layer_num = rr_graph.node_layer(to_node); |
| 73 | + |
| 74 | + auto [delta_x, delta_y] = util::get_xy_deltas(from_node, to_node); |
| 75 | + delta_x = abs(delta_x); |
| 76 | + delta_y = abs(delta_y); |
| 77 | + |
| 78 | + auto from_cost_index = rr_graph.node_cost_index(from_node); |
| 79 | + int from_seg_index = device_ctx.rr_indexed_data[from_cost_index].seg_index; |
| 80 | + |
| 81 | + cost_entry = get_wire_cost_entry(from_type, from_seg_index, from_layer_num, delta_x, delta_y, to_layer_num); |
| 82 | + } |
| 83 | + |
| 84 | + if (cost_entry.valid()) { |
| 85 | + float expected_delay = cost_entry.delay; |
| 86 | + float expected_cong = cost_entry.congestion; |
| 87 | + |
| 88 | + expected_delay_cost = params.criticality * expected_delay; |
| 89 | + expected_cong_cost = (1.0 - params.criticality) * expected_cong; |
| 90 | + } |
| 91 | + |
| 92 | + return std::make_pair(expected_delay_cost, expected_cong_cost); |
| 93 | +} |
| 94 | + |
| 95 | +void SimpleLookahead::read(const std::string& file) { |
| 96 | + read_router_lookahead(file); |
| 97 | +} |
| 98 | + |
| 99 | +void SimpleLookahead::write(const std::string& file) const { |
| 100 | + if (vtr::check_file_name_extension(file, ".csv")) { |
| 101 | + std::vector<int> simple_cost_map_size(simple_cost_map.ndims()); |
| 102 | + for (size_t i = 0; i < simple_cost_map.ndims(); ++i) { |
| 103 | + simple_cost_map_size[i] = static_cast<int>(simple_cost_map.dim_size(i)); |
| 104 | + } |
| 105 | + dump_readable_router_lookahead_map(file, simple_cost_map_size, get_wire_cost_entry); |
| 106 | + } else { |
| 107 | + VTR_ASSERT(vtr::check_file_name_extension(file, ".capnp") || vtr::check_file_name_extension(file, ".bin")); |
| 108 | + write_router_lookahead(file); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/******** Function Definitions ********/ |
| 113 | + |
| 114 | +static util::Cost_Entry get_wire_cost_entry(e_rr_type rr_type, int seg_index, int from_layer_num, int delta_x, int delta_y, int to_layer_num) { |
| 115 | + VTR_ASSERT_SAFE(rr_type == e_rr_type::CHANX || rr_type == e_rr_type::CHANY); |
| 116 | + VTR_ASSERT_SAFE(from_layer_num < static_cast<int>(simple_cost_map.dim_size(0))); |
| 117 | + VTR_ASSERT_SAFE(to_layer_num < static_cast<int>(simple_cost_map.dim_size(1))); |
| 118 | + VTR_ASSERT_SAFE(seg_index < static_cast<int>(simple_cost_map.dim_size(3))); |
| 119 | + VTR_ASSERT_SAFE(delta_x < static_cast<int>(simple_cost_map.dim_size(4))); |
| 120 | + VTR_ASSERT_SAFE(delta_y < static_cast<int>(simple_cost_map.dim_size(5))); |
| 121 | + |
| 122 | + int chan_index = 0; |
| 123 | + if (rr_type == e_rr_type::CHANY) { |
| 124 | + chan_index = 1; |
| 125 | + } |
| 126 | + |
| 127 | + return simple_cost_map[from_layer_num][to_layer_num][chan_index][seg_index][delta_x][delta_y]; |
| 128 | +} |
| 129 | + |
| 130 | +// |
| 131 | +// When writing capnp targetted serialization, always allow compilation when |
| 132 | +// VTR_ENABLE_CAPNPROTO=OFF. Generally this means throwing an exception |
| 133 | +// instead. |
| 134 | +// |
| 135 | +#ifndef VTR_ENABLE_CAPNPROTO |
| 136 | + |
| 137 | +#define DISABLE_ERROR \ |
| 138 | + "is disabled because VTR_ENABLE_CAPNPROTO=OFF." \ |
| 139 | + "Re-compile with CMake option VTR_ENABLE_CAPNPROTO=ON to enable." |
| 140 | + |
| 141 | +void read_router_lookahead(const std::string& /*file*/) { |
| 142 | + VPR_THROW(VPR_ERROR_PLACE, "SimpleLookahead::read_router_lookahead " DISABLE_ERROR); |
| 143 | +} |
| 144 | + |
| 145 | +void write_router_lookahead(const std::string& /*file*/) { |
| 146 | + VPR_THROW(VPR_ERROR_PLACE, "SimpleLookahead::write_router_lookahead " DISABLE_ERROR); |
| 147 | +} |
| 148 | + |
| 149 | +#else /* VTR_ENABLE_CAPNPROTO */ |
| 150 | + |
| 151 | +static void ToCostEntry(util::Cost_Entry* out, const VprMapCostEntry::Reader& in) { |
| 152 | + out->delay = in.getDelay(); |
| 153 | + out->congestion = in.getCongestion(); |
| 154 | +} |
| 155 | + |
| 156 | +static void FromCostEntry(VprMapCostEntry::Builder* out, const util::Cost_Entry& in) { |
| 157 | + out->setDelay(in.delay); |
| 158 | + out->setCongestion(in.congestion); |
| 159 | +} |
| 160 | + |
| 161 | +void read_router_lookahead(const std::string& file) { |
| 162 | + vtr::ScopedStartFinishTimer timer("Loading router wire lookahead map"); |
| 163 | + MmapFile f(file); |
| 164 | + |
| 165 | + /* Increase reader limit to 1G words to allow for large files. */ |
| 166 | + ::capnp::ReaderOptions opts = default_large_capnp_opts(); |
| 167 | + ::capnp::FlatArrayMessageReader reader(f.getData(), opts); |
| 168 | + |
| 169 | + auto map = reader.getRoot<VprMapLookahead>(); |
| 170 | + |
| 171 | + ToNdMatrix<6, VprMapCostEntry, util::Cost_Entry>(&simple_cost_map, map.getCostMap(), ToCostEntry); |
| 172 | +} |
| 173 | + |
| 174 | +void write_router_lookahead(const std::string& file) { |
| 175 | + ::capnp::MallocMessageBuilder builder; |
| 176 | + |
| 177 | + auto map = builder.initRoot<VprMapLookahead>(); |
| 178 | + |
| 179 | + auto cost_map = map.initCostMap(); |
| 180 | + FromNdMatrix<6, VprMapCostEntry, util::Cost_Entry>(&cost_map, simple_cost_map, FromCostEntry); |
| 181 | + |
| 182 | + writeMessageToFile(file, &builder); |
| 183 | +} |
| 184 | + |
| 185 | +#endif |
0 commit comments