Logo AND Algorithmique Numérique Distribuée

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