1 /* Copyright (c) 2009-2021. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include <simgrid/kernel/routing/FloydZone.hpp>
7 #include <simgrid/kernel/routing/NetPoint.hpp>
8 #include <xbt/string.hpp>
10 #include "src/surf/network_interface.hpp"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_floyd, surf, "Routing part of surf");
20 void FloydZone::init_tables(unsigned int table_size)
22 if (link_table_.size() != table_size) {
23 /* Resize and initialize Cost, Predecessor and Link tables */
24 cost_table_.resize(table_size);
25 link_table_.resize(table_size);
26 predecessor_table_.resize(table_size);
27 for (auto& cost : cost_table_)
28 cost.resize(table_size, ULONG_MAX); /* link cost from host to host */
29 for (auto& link : link_table_)
30 link.resize(table_size); /* actual link between src and dst */
31 for (auto& predecessor : predecessor_table_)
32 predecessor.resize(table_size, -1); /* predecessor host numbers */
36 void FloydZone::get_local_route(const NetPoint* src, const NetPoint* dst, Route* route, double* lat)
38 get_route_check_params(src, dst);
40 /* create a result route */
41 std::vector<Route*> route_stack;
42 unsigned long cur = dst->id();
44 long pred = predecessor_table_[src->id()][cur];
46 throw std::invalid_argument(xbt::string_printf("No route from '%s' to '%s'", src->get_cname(), dst->get_cname()));
47 route_stack.push_back(link_table_[pred][cur].get());
49 } while (cur != src->id());
51 if (get_hierarchy() == RoutingMode::recursive) {
52 route->gw_src_ = route_stack.back()->gw_src_;
53 route->gw_dst_ = route_stack.front()->gw_dst_;
56 const NetPoint* prev_dst_gw = nullptr;
57 while (not route_stack.empty()) {
58 const Route* e_route = route_stack.back();
59 route_stack.pop_back();
60 if (get_hierarchy() == RoutingMode::recursive && prev_dst_gw != nullptr &&
61 prev_dst_gw->get_cname() != e_route->gw_src_->get_cname()) {
62 get_global_route(prev_dst_gw, e_route->gw_src_, route->link_list_, lat);
65 add_link_latency(route->link_list_, e_route->link_list_, lat);
67 prev_dst_gw = e_route->gw_dst_;
71 void FloydZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
72 const std::vector<s4u::LinkInRoute>& link_list, bool symmetrical)
74 /* set the size of table routing */
75 unsigned int table_size = get_table_size();
76 init_tables(table_size);
78 add_route_check_params(src, dst, gw_src, gw_dst, link_list, symmetrical);
80 /* Check that the route does not already exist */
81 if (gw_dst && gw_src) // netzone route (to adapt the error message, if any)
82 xbt_assert(nullptr == link_table_[src->id()][dst->id()],
83 "The route between %s@%s and %s@%s already exists (Rq: routes are symmetrical by default).",
84 src->get_cname(), gw_src->get_cname(), dst->get_cname(), gw_dst->get_cname());
86 xbt_assert(nullptr == link_table_[src->id()][dst->id()],
87 "The route between %s and %s already exists (Rq: routes are symmetrical by default).", src->get_cname(),
90 link_table_[src->id()][dst->id()] = std::unique_ptr<Route>(
91 new_extended_route(get_hierarchy(), gw_src, gw_dst, get_link_list_impl(link_list, false), true));
92 predecessor_table_[src->id()][dst->id()] = src->id();
93 cost_table_[src->id()][dst->id()] = link_table_[src->id()][dst->id()]->link_list_.size();
96 if (gw_dst && gw_src) // netzone route (to adapt the error message, if any)
98 nullptr == link_table_[dst->id()][src->id()],
99 "The route between %s@%s and %s@%s already exists. You should not declare the reverse path as symmetrical.",
100 dst->get_cname(), gw_dst->get_cname(), src->get_cname(), gw_src->get_cname());
102 xbt_assert(nullptr == link_table_[dst->id()][src->id()],
103 "The route between %s and %s already exists. You should not declare the reverse path as symmetrical.",
104 dst->get_cname(), src->get_cname());
106 if (gw_dst && gw_src) {
107 NetPoint* gw_tmp = gw_src;
112 if (not gw_src || not gw_dst)
113 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst->get_cname(), src->get_cname());
115 XBT_DEBUG("Load NetzoneRoute from \"%s(%s)\" to \"%s(%s)\"", dst->get_cname(), gw_src->get_cname(),
116 src->get_cname(), gw_dst->get_cname());
118 link_table_[dst->id()][src->id()] = std::unique_ptr<Route>(
119 new_extended_route(get_hierarchy(), gw_src, gw_dst, get_link_list_impl(link_list, true), false));
120 predecessor_table_[dst->id()][src->id()] = dst->id();
121 cost_table_[dst->id()][src->id()] =
122 link_table_[dst->id()][src->id()]->link_list_.size(); /* count of links, old model assume 1 */
126 void FloydZone::do_seal()
128 /* set the size of table routing */
129 unsigned int table_size = get_table_size();
130 init_tables(table_size);
132 /* Add the loopback if needed */
133 if (get_network_model()->loopback_ && get_hierarchy() == RoutingMode::base) {
134 for (unsigned int i = 0; i < table_size; i++) {
135 auto& route = link_table_[i][i];
137 route.reset(new Route());
138 route->link_list_.push_back(get_network_model()->loopback_);
139 predecessor_table_[i][i] = i;
140 cost_table_[i][i] = 1;
144 /* Calculate path costs */
145 for (unsigned int c = 0; c < table_size; c++) {
146 for (unsigned int a = 0; a < table_size; a++) {
147 for (unsigned int b = 0; b < table_size; b++) {
148 if (cost_table_[a][c] < ULONG_MAX && cost_table_[c][b] < ULONG_MAX &&
149 (cost_table_[a][b] == ULONG_MAX || (cost_table_[a][c] + cost_table_[c][b] < cost_table_[a][b]))) {
150 cost_table_[a][b] = cost_table_[a][c] + cost_table_[c][b];
151 predecessor_table_[a][b] = predecessor_table_[c][b];
157 } // namespace routing
158 } // namespace kernel
161 NetZone* create_floyd_zone(const std::string& name)
163 return (new kernel::routing::FloydZone(name))->get_iface();
167 } // namespace simgrid