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 "surf/surf.hpp"
10 #include "xbt/string.hpp"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_floyd, surf, "Routing part of surf");
17 #define TO_FLOYD_COST(i, j) (cost_table_)[(i) + (j)*table_size]
18 #define TO_FLOYD_PRED(i, j) (predecessor_table_)[(i) + (j)*table_size]
19 #define TO_FLOYD_LINK(i, j) (link_table_)[(i) + (j)*table_size]
25 FloydZone::~FloydZone()
27 /* Delete link_table */
28 for (auto const* link : link_table_)
32 void FloydZone::init_tables(unsigned int table_size)
34 if (link_table_.empty()) {
35 /* Create and initialize Cost, Predecessor and Link tables */
36 cost_table_.resize(table_size * table_size, DBL_MAX); /* link cost from host to host */
37 predecessor_table_.resize(table_size * table_size, -1); /* predecessor host numbers */
38 link_table_.resize(table_size * table_size, nullptr); /* actual link between src and dst */
42 void FloydZone::get_local_route(NetPoint* src, NetPoint* dst, Route* route, double* lat)
44 unsigned int table_size = get_table_size();
46 get_route_check_params(src, dst);
48 /* create a result route */
49 std::vector<Route*> route_stack;
50 unsigned int cur = dst->id();
52 int pred = TO_FLOYD_PRED(src->id(), cur);
54 throw std::invalid_argument(xbt::string_printf("No route from '%s' to '%s'", src->get_cname(), dst->get_cname()));
55 route_stack.push_back(TO_FLOYD_LINK(pred, cur));
57 } while (cur != src->id());
59 if (get_hierarchy() == RoutingMode::recursive) {
60 route->gw_src_ = route_stack.back()->gw_src_;
61 route->gw_dst_ = route_stack.front()->gw_dst_;
64 NetPoint* prev_dst_gw = nullptr;
65 while (not route_stack.empty()) {
66 const Route* e_route = route_stack.back();
67 route_stack.pop_back();
68 if (get_hierarchy() == RoutingMode::recursive && prev_dst_gw != nullptr &&
69 prev_dst_gw->get_cname() != e_route->gw_src_->get_cname()) {
70 get_global_route(prev_dst_gw, e_route->gw_src_, route->link_list_, lat);
73 for (auto const& link : e_route->link_list_) {
74 route->link_list_.push_back(link);
76 *lat += link->get_latency();
79 prev_dst_gw = e_route->gw_dst_;
83 void FloydZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
84 const std::vector<resource::LinkImpl*>& link_list_, bool symmetrical)
86 /* set the size of table routing */
87 unsigned int table_size = get_table_size();
88 init_tables(table_size);
90 add_route_check_params(src, dst, gw_src, gw_dst, link_list_, symmetrical);
92 /* Check that the route does not already exist */
93 if (gw_dst && gw_src) // netzone route (to adapt the error message, if any)
94 xbt_assert(nullptr == TO_FLOYD_LINK(src->id(), dst->id()),
95 "The route between %s@%s and %s@%s already exists (Rq: routes are symmetrical by default).",
96 src->get_cname(), gw_src->get_cname(), dst->get_cname(), gw_dst->get_cname());
98 xbt_assert(nullptr == TO_FLOYD_LINK(src->id(), dst->id()),
99 "The route between %s and %s already exists (Rq: routes are symmetrical by default).", src->get_cname(),
102 TO_FLOYD_LINK(src->id(), dst->id()) = new_extended_route(get_hierarchy(), gw_src, gw_dst, link_list_, true);
103 TO_FLOYD_PRED(src->id(), dst->id()) = src->id();
104 TO_FLOYD_COST(src->id(), dst->id()) = (TO_FLOYD_LINK(src->id(), dst->id()))->link_list_.size();
107 if (gw_dst && gw_src) // netzone route (to adapt the error message, if any)
109 nullptr == TO_FLOYD_LINK(dst->id(), src->id()),
110 "The route between %s@%s and %s@%s already exists. You should not declare the reverse path as symmetrical.",
111 dst->get_cname(), gw_dst->get_cname(), src->get_cname(), gw_src->get_cname());
113 xbt_assert(nullptr == TO_FLOYD_LINK(dst->id(), src->id()),
114 "The route between %s and %s already exists. You should not declare the reverse path as symmetrical.",
115 dst->get_cname(), src->get_cname());
117 if (gw_dst && gw_src) {
118 NetPoint* gw_tmp = gw_src;
123 if (not gw_src || not gw_dst)
124 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst->get_cname(), src->get_cname());
126 XBT_DEBUG("Load NetzoneRoute from \"%s(%s)\" to \"%s(%s)\"", dst->get_cname(), gw_src->get_cname(),
127 src->get_cname(), gw_dst->get_cname());
129 TO_FLOYD_LINK(dst->id(), src->id()) = new_extended_route(get_hierarchy(), gw_src, gw_dst, link_list_, false);
130 TO_FLOYD_PRED(dst->id(), src->id()) = dst->id();
131 TO_FLOYD_COST(dst->id(), src->id()) =
132 (TO_FLOYD_LINK(dst->id(), src->id()))->link_list_.size(); /* count of links, old model assume 1 */
136 void FloydZone::do_seal()
138 /* set the size of table routing */
139 unsigned int table_size = get_table_size();
140 init_tables(table_size);
142 /* Add the loopback if needed */
143 if (get_network_model()->loopback_ && get_hierarchy() == RoutingMode::base) {
144 for (unsigned int i = 0; i < table_size; i++) {
145 Route* route = TO_FLOYD_LINK(i, i);
148 route->link_list_.push_back(get_network_model()->loopback_);
149 TO_FLOYD_LINK(i, i) = route;
150 TO_FLOYD_PRED(i, i) = i;
151 TO_FLOYD_COST(i, i) = 1;
155 /* Calculate path costs */
156 for (unsigned int c = 0; c < table_size; c++) {
157 for (unsigned int a = 0; a < table_size; a++) {
158 for (unsigned int b = 0; b < table_size; b++) {
159 if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX &&
160 (fabs(TO_FLOYD_COST(a, b) - DBL_MAX) < std::numeric_limits<double>::epsilon() ||
161 (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) < TO_FLOYD_COST(a, b)))) {
162 TO_FLOYD_COST(a, b) = TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
163 TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
169 } // namespace routing
170 } // namespace kernel
173 NetZone* create_floyd_zone(const std::string& name)
175 return (new kernel::routing::FloydZone(name))->get_iface();
179 } // namespace simgrid