Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
trimming zones
[simgrid.git] / src / kernel / routing / FloydZone.cpp
1 /* Copyright (c) 2009-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
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"
12
13 #include <cfloat>
14 #include <limits>
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_floyd, surf, "Routing part of surf");
17
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]
21
22 namespace simgrid {
23 namespace kernel {
24 namespace routing {
25
26 FloydZone::~FloydZone()
27 {
28   /* Delete link_table */
29   for (auto const* link : link_table_)
30     delete link;
31 }
32
33 void FloydZone::init_tables(unsigned int table_size)
34 {
35   if (link_table_.empty()) {
36     /* Create and initialize Cost, Predecessor and Link tables */
37     cost_table_.resize(table_size * table_size, DBL_MAX);   /* link cost from host to host */
38     predecessor_table_.resize(table_size * table_size, -1); /* predecessor host numbers */
39     link_table_.resize(table_size * table_size, nullptr);   /* actual link between src and dst */
40   }
41 }
42
43 void FloydZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* lat)
44 {
45   unsigned int table_size = get_table_size();
46
47   get_route_check_params(src, dst);
48
49   /* create a result route */
50   std::vector<RouteCreationArgs*> route_stack;
51   unsigned int cur = dst->id();
52   do {
53     int pred = TO_FLOYD_PRED(src->id(), cur);
54     if (pred == -1)
55       throw std::invalid_argument(xbt::string_printf("No route from '%s' to '%s'", src->get_cname(), dst->get_cname()));
56     route_stack.push_back(TO_FLOYD_LINK(pred, cur));
57     cur = pred;
58   } while (cur != src->id());
59
60   if (hierarchy_ == RoutingMode::recursive) {
61     route->gw_src = route_stack.back()->gw_src;
62     route->gw_dst = route_stack.front()->gw_dst;
63   }
64
65   NetPoint* prev_dst_gw = nullptr;
66   while (not route_stack.empty()) {
67     const RouteCreationArgs* e_route = route_stack.back();
68     route_stack.pop_back();
69     if (hierarchy_ == RoutingMode::recursive && prev_dst_gw != nullptr &&
70         prev_dst_gw->get_cname() != e_route->gw_src->get_cname()) {
71       get_global_route(prev_dst_gw, e_route->gw_src, route->link_list, lat);
72     }
73
74     for (auto const& link : e_route->link_list) {
75       route->link_list.push_back(link);
76       if (lat)
77         *lat += link->get_latency();
78     }
79
80     prev_dst_gw = e_route->gw_dst;
81   }
82 }
83
84 void FloydZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
85                           std::vector<resource::LinkImpl*>& link_list, bool symmetrical)
86 {
87   /* set the size of table routing */
88   unsigned int table_size = get_table_size();
89   init_tables(table_size);
90
91   add_route_check_params(src, dst, gw_src, gw_dst, link_list, symmetrical);
92
93   /* Check that the route does not already exist */
94   if (gw_dst && gw_src) // netzone route (to adapt the error message, if any)
95     xbt_assert(nullptr == TO_FLOYD_LINK(src->id(), dst->id()),
96                "The route between %s@%s and %s@%s already exists (Rq: routes are symmetrical by default).",
97                src->get_cname(), gw_src->get_cname(), dst->get_cname(), gw_dst->get_cname());
98   else
99     xbt_assert(nullptr == TO_FLOYD_LINK(src->id(), dst->id()),
100                "The route between %s and %s already exists (Rq: routes are symmetrical by default).", src->get_cname(),
101                dst->get_cname());
102
103   TO_FLOYD_LINK(src->id(), dst->id()) = new_extended_route(hierarchy_, gw_src, gw_dst, link_list, true);
104   TO_FLOYD_PRED(src->id(), dst->id()) = src->id();
105   TO_FLOYD_COST(src->id(), dst->id()) = (TO_FLOYD_LINK(src->id(), dst->id()))->link_list.size();
106
107   if (symmetrical) {
108     if (gw_dst && gw_src) // netzone route (to adapt the error message, if any)
109       xbt_assert(
110           nullptr == TO_FLOYD_LINK(dst->id(), src->id()),
111           "The route between %s@%s and %s@%s already exists. You should not declare the reverse path as symmetrical.",
112           dst->get_cname(), gw_dst->get_cname(), src->get_cname(), gw_src->get_cname());
113     else
114       xbt_assert(nullptr == TO_FLOYD_LINK(dst->id(), src->id()),
115                  "The route between %s and %s already exists. You should not declare the reverse path as symmetrical.",
116                  dst->get_cname(), src->get_cname());
117
118     if (gw_dst && gw_src) {
119       NetPoint* gw_tmp = gw_src;
120       gw_src           = gw_dst;
121       gw_dst           = gw_tmp;
122     }
123
124     if (not gw_src || not gw_dst)
125       XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst->get_cname(), src->get_cname());
126     else
127       XBT_DEBUG("Load NetzoneRoute from \"%s(%s)\" to \"%s(%s)\"", dst->get_cname(), gw_src->get_cname(),
128                 src->get_cname(), gw_dst->get_cname());
129
130     TO_FLOYD_LINK(dst->id(), src->id()) = new_extended_route(hierarchy_, gw_src, gw_dst, link_list, false);
131     TO_FLOYD_PRED(dst->id(), src->id()) = dst->id();
132     TO_FLOYD_COST(dst->id(), src->id()) =
133         (TO_FLOYD_LINK(dst->id(), src->id()))->link_list.size(); /* count of links, old model assume 1 */
134   }
135 }
136
137 void FloydZone::do_seal()
138 {
139   /* set the size of table routing */
140   unsigned int table_size = get_table_size();
141   init_tables(table_size);
142
143   /* Add the loopback if needed */
144   if (get_network_model()->loopback_ && hierarchy_ == RoutingMode::base) {
145     for (unsigned int i = 0; i < table_size; i++) {
146       RouteCreationArgs* route = TO_FLOYD_LINK(i, i);
147       if (not route) {
148         route = new RouteCreationArgs();
149         route->link_list.push_back(get_network_model()->loopback_);
150         TO_FLOYD_LINK(i, i) = route;
151         TO_FLOYD_PRED(i, i) = i;
152         TO_FLOYD_COST(i, i) = 1;
153       }
154     }
155   }
156   /* Calculate path costs */
157   for (unsigned int c = 0; c < table_size; c++) {
158     for (unsigned int a = 0; a < table_size; a++) {
159       for (unsigned int b = 0; b < table_size; b++) {
160         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX &&
161             (fabs(TO_FLOYD_COST(a, b) - DBL_MAX) < std::numeric_limits<double>::epsilon() ||
162              (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) < TO_FLOYD_COST(a, b)))) {
163           TO_FLOYD_COST(a, b) = TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
164           TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
165         }
166       }
167     }
168   }
169 }
170 } // namespace routing
171 } // namespace kernel
172 } // namespace simgrid