1 /* Copyright (c) 2009-2020. 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 if (link_table_ == nullptr) // Dealing with a parse error in the file?
35 unsigned int table_size = get_table_size();
36 /* Delete link_table */
37 for (unsigned int i = 0; i < table_size; i++)
38 for (unsigned int j = 0; j < table_size; j++)
39 delete TO_FLOYD_LINK(i, j);
42 delete[] predecessor_table_;
46 void FloydZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* lat)
48 unsigned int table_size = get_table_size();
50 get_route_check_params(src, dst);
52 /* create a result route */
53 std::vector<RouteCreationArgs*> route_stack;
54 unsigned int cur = dst->id();
56 int pred = TO_FLOYD_PRED(src->id(), cur);
58 throw std::invalid_argument(xbt::string_printf("No route from '%s' to '%s'", src->get_cname(), dst->get_cname()));
59 route_stack.push_back(TO_FLOYD_LINK(pred, cur));
61 } while (cur != src->id());
63 if (hierarchy_ == RoutingMode::recursive) {
64 route->gw_src = route_stack.back()->gw_src;
65 route->gw_dst = route_stack.front()->gw_dst;
68 NetPoint* prev_dst_gw = nullptr;
69 while (not route_stack.empty()) {
70 const RouteCreationArgs* e_route = route_stack.back();
71 route_stack.pop_back();
72 if (hierarchy_ == RoutingMode::recursive && prev_dst_gw != nullptr &&
73 prev_dst_gw->get_cname() != e_route->gw_src->get_cname()) {
74 get_global_route(prev_dst_gw, e_route->gw_src, route->link_list, lat);
77 for (auto const& link : e_route->link_list) {
78 route->link_list.push_back(link);
80 *lat += link->get_latency();
83 prev_dst_gw = e_route->gw_dst;
87 void FloydZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
88 std::vector<resource::LinkImpl*>& link_list, bool symmetrical)
90 /* set the size of table routing */
91 unsigned int table_size = get_table_size();
93 add_route_check_params(src, dst, gw_src, gw_dst, link_list, symmetrical);
95 if (not link_table_) {
96 /* Create Cost, Predecessor and Link tables */
97 cost_table_ = new double[table_size * table_size]; /* link cost from host to host */
98 predecessor_table_ = new int[table_size * table_size]; /* predecessor host numbers */
99 link_table_ = new RouteCreationArgs*[table_size * table_size]; /* actual link between src and dst */
101 /* Initialize costs and predecessors */
102 for (unsigned int i = 0; i < table_size; i++)
103 for (unsigned int j = 0; j < table_size; j++) {
104 TO_FLOYD_COST(i, j) = DBL_MAX;
105 TO_FLOYD_PRED(i, j) = -1;
106 TO_FLOYD_LINK(i, j) = nullptr;
110 /* Check that the route does not already exist */
111 if (gw_dst) // netzone route (to adapt the error message, if any)
112 xbt_assert(nullptr == TO_FLOYD_LINK(src->id(), dst->id()),
113 "The route between %s@%s and %s@%s already exists (Rq: routes are symmetrical by default).",
114 src->get_cname(), gw_src->get_cname(), dst->get_cname(), gw_dst->get_cname());
116 xbt_assert(nullptr == TO_FLOYD_LINK(src->id(), dst->id()),
117 "The route between %s and %s already exists (Rq: routes are symmetrical by default).", src->get_cname(),
120 TO_FLOYD_LINK(src->id(), dst->id()) =
121 new_extended_route(hierarchy_, src, dst, gw_src, gw_dst, link_list, symmetrical, true);
122 TO_FLOYD_PRED(src->id(), dst->id()) = src->id();
123 TO_FLOYD_COST(src->id(), dst->id()) = (TO_FLOYD_LINK(src->id(), dst->id()))->link_list.size();
125 if (symmetrical == true) {
126 if (gw_dst) // netzone route (to adapt the error message, if any)
128 nullptr == TO_FLOYD_LINK(dst->id(), src->id()),
129 "The route between %s@%s and %s@%s already exists. You should not declare the reverse path as symmetrical.",
130 dst->get_cname(), gw_dst->get_cname(), src->get_cname(), gw_src->get_cname());
132 xbt_assert(nullptr == TO_FLOYD_LINK(dst->id(), src->id()),
133 "The route between %s and %s already exists. You should not declare the reverse path as symmetrical.",
134 dst->get_cname(), src->get_cname());
136 if (gw_dst && gw_src) {
137 NetPoint* gw_tmp = gw_src;
142 if (not gw_src || not gw_dst)
143 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst->get_cname(), src->get_cname());
145 XBT_DEBUG("Load NetzoneRoute from \"%s(%s)\" to \"%s(%s)\"", dst->get_cname(), gw_src->get_cname(),
146 src->get_cname(), gw_dst->get_cname());
148 TO_FLOYD_LINK(dst->id(), src->id()) =
149 new_extended_route(hierarchy_, src, dst, gw_src, gw_dst, link_list, symmetrical, false);
150 TO_FLOYD_PRED(dst->id(), src->id()) = dst->id();
151 TO_FLOYD_COST(dst->id(), src->id()) =
152 (TO_FLOYD_LINK(dst->id(), src->id()))->link_list.size(); /* count of links, old model assume 1 */
156 void FloydZone::seal()
158 /* set the size of table routing */
159 unsigned int table_size = get_table_size();
161 if (not link_table_) {
162 /* Create Cost, Predecessor and Link tables */
163 cost_table_ = new double[table_size * table_size]; /* link cost from host to host */
164 predecessor_table_ = new int[table_size * table_size]; /* predecessor host numbers */
165 link_table_ = new RouteCreationArgs*[table_size * table_size]; /* actual link between src and dst */
167 /* Initialize costs and predecessors */
168 for (unsigned int i = 0; i < table_size; i++)
169 for (unsigned int j = 0; j < table_size; j++) {
170 TO_FLOYD_COST(i, j) = DBL_MAX;
171 TO_FLOYD_PRED(i, j) = -1;
172 TO_FLOYD_LINK(i, j) = nullptr;
176 /* Add the loopback if needed */
177 if (network_model_->loopback_ && hierarchy_ == RoutingMode::base) {
178 for (unsigned int i = 0; i < table_size; i++) {
179 RouteCreationArgs* route = TO_FLOYD_LINK(i, i);
181 route = new RouteCreationArgs();
182 route->link_list.push_back(network_model_->loopback_);
183 TO_FLOYD_LINK(i, i) = route;
184 TO_FLOYD_PRED(i, i) = i;
185 TO_FLOYD_COST(i, i) = 1;
189 /* Calculate path costs */
190 for (unsigned int c = 0; c < table_size; c++) {
191 for (unsigned int a = 0; a < table_size; a++) {
192 for (unsigned int b = 0; b < table_size; b++) {
193 if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX &&
194 (fabs(TO_FLOYD_COST(a, b) - DBL_MAX) < std::numeric_limits<double>::epsilon() ||
195 (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) < TO_FLOYD_COST(a, b)))) {
196 TO_FLOYD_COST(a, b) = TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
197 TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);