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 "src/surf/network_interface.hpp"
9 #include "src/surf/xml/platf_private.hpp"
10 #include "surf/surf.hpp"
11 #include "xbt/string.hpp"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_floyd, surf, "Routing part of surf");
18 #define TO_FLOYD_COST(i, j) (cost_table_)[(i) + (j)*table_size]
19 #define TO_FLOYD_PRED(i, j) (predecessor_table_)[(i) + (j)*table_size]
20 #define TO_FLOYD_LINK(i, j) (link_table_)[(i) + (j)*table_size]
26 FloydZone::FloydZone(NetZoneImpl* father, const std::string& name, resource::NetworkModel* netmodel)
27 : RoutedZone(father, name, netmodel)
31 FloydZone::~FloydZone()
33 /* Delete link_table */
34 for (auto const* link : link_table_)
38 void FloydZone::init_tables(unsigned int table_size)
40 if (link_table_.empty()) {
41 /* Create and initialize Cost, Predecessor and Link tables */
42 cost_table_.resize(table_size * table_size, DBL_MAX); /* link cost from host to host */
43 predecessor_table_.resize(table_size * table_size, -1); /* predecessor host numbers */
44 link_table_.resize(table_size * table_size, nullptr); /* actual link between src and dst */
48 void FloydZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* lat)
50 unsigned int table_size = get_table_size();
52 get_route_check_params(src, dst);
54 /* create a result route */
55 std::vector<RouteCreationArgs*> route_stack;
56 unsigned int cur = dst->id();
58 int pred = TO_FLOYD_PRED(src->id(), cur);
60 throw std::invalid_argument(xbt::string_printf("No route from '%s' to '%s'", src->get_cname(), dst->get_cname()));
61 route_stack.push_back(TO_FLOYD_LINK(pred, cur));
63 } while (cur != src->id());
65 if (hierarchy_ == RoutingMode::recursive) {
66 route->gw_src = route_stack.back()->gw_src;
67 route->gw_dst = route_stack.front()->gw_dst;
70 NetPoint* prev_dst_gw = nullptr;
71 while (not route_stack.empty()) {
72 const RouteCreationArgs* e_route = route_stack.back();
73 route_stack.pop_back();
74 if (hierarchy_ == RoutingMode::recursive && prev_dst_gw != nullptr &&
75 prev_dst_gw->get_cname() != e_route->gw_src->get_cname()) {
76 get_global_route(prev_dst_gw, e_route->gw_src, route->link_list, lat);
79 for (auto const& link : e_route->link_list) {
80 route->link_list.push_back(link);
82 *lat += link->get_latency();
85 prev_dst_gw = e_route->gw_dst;
89 void FloydZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
90 std::vector<resource::LinkImpl*>& link_list, bool symmetrical)
92 /* set the size of table routing */
93 unsigned int table_size = get_table_size();
94 init_tables(table_size);
96 add_route_check_params(src, dst, gw_src, gw_dst, link_list, symmetrical);
98 /* Check that the route does not already exist */
99 if (gw_dst && gw_src) // netzone route (to adapt the error message, if any)
100 xbt_assert(nullptr == TO_FLOYD_LINK(src->id(), dst->id()),
101 "The route between %s@%s and %s@%s already exists (Rq: routes are symmetrical by default).",
102 src->get_cname(), gw_src->get_cname(), dst->get_cname(), gw_dst->get_cname());
104 xbt_assert(nullptr == TO_FLOYD_LINK(src->id(), dst->id()),
105 "The route between %s and %s already exists (Rq: routes are symmetrical by default).", src->get_cname(),
108 TO_FLOYD_LINK(src->id(), dst->id()) = new_extended_route(hierarchy_, gw_src, gw_dst, link_list, true);
109 TO_FLOYD_PRED(src->id(), dst->id()) = src->id();
110 TO_FLOYD_COST(src->id(), dst->id()) = (TO_FLOYD_LINK(src->id(), dst->id()))->link_list.size();
113 if (gw_dst && gw_src) // netzone route (to adapt the error message, if any)
115 nullptr == TO_FLOYD_LINK(dst->id(), src->id()),
116 "The route between %s@%s and %s@%s already exists. You should not declare the reverse path as symmetrical.",
117 dst->get_cname(), gw_dst->get_cname(), src->get_cname(), gw_src->get_cname());
119 xbt_assert(nullptr == TO_FLOYD_LINK(dst->id(), src->id()),
120 "The route between %s and %s already exists. You should not declare the reverse path as symmetrical.",
121 dst->get_cname(), src->get_cname());
123 if (gw_dst && gw_src) {
124 NetPoint* gw_tmp = gw_src;
129 if (not gw_src || not gw_dst)
130 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst->get_cname(), src->get_cname());
132 XBT_DEBUG("Load NetzoneRoute from \"%s(%s)\" to \"%s(%s)\"", dst->get_cname(), gw_src->get_cname(),
133 src->get_cname(), gw_dst->get_cname());
135 TO_FLOYD_LINK(dst->id(), src->id()) = new_extended_route(hierarchy_, gw_src, gw_dst, link_list, false);
136 TO_FLOYD_PRED(dst->id(), src->id()) = dst->id();
137 TO_FLOYD_COST(dst->id(), src->id()) =
138 (TO_FLOYD_LINK(dst->id(), src->id()))->link_list.size(); /* count of links, old model assume 1 */
142 void FloydZone::seal()
144 /* set the size of table routing */
145 unsigned int table_size = get_table_size();
146 init_tables(table_size);
148 /* Add the loopback if needed */
149 if (network_model_->loopback_ && hierarchy_ == RoutingMode::base) {
150 for (unsigned int i = 0; i < table_size; i++) {
151 RouteCreationArgs* route = TO_FLOYD_LINK(i, i);
153 route = new RouteCreationArgs();
154 route->link_list.push_back(network_model_->loopback_);
155 TO_FLOYD_LINK(i, i) = route;
156 TO_FLOYD_PRED(i, i) = i;
157 TO_FLOYD_COST(i, i) = 1;
161 /* Calculate path costs */
162 for (unsigned int c = 0; c < table_size; c++) {
163 for (unsigned int a = 0; a < table_size; a++) {
164 for (unsigned int b = 0; b < table_size; b++) {
165 if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX &&
166 (fabs(TO_FLOYD_COST(a, b) - DBL_MAX) < std::numeric_limits<double>::epsilon() ||
167 (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) < TO_FLOYD_COST(a, b)))) {
168 TO_FLOYD_COST(a, b) = TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
169 TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);