Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[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(NetZoneImpl* father, const std::string& name, resource::NetworkModel* netmodel)
27     : RoutedZone(father, name, netmodel)
28 {
29 }
30
31 FloydZone::~FloydZone()
32 {
33   /* Delete link_table */
34   for (auto const* link : link_table_)
35     delete link;
36 }
37
38 void FloydZone::init_tables(unsigned int table_size)
39 {
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 */
45   }
46 }
47
48 void FloydZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* lat)
49 {
50   unsigned int table_size = get_table_size();
51
52   get_route_check_params(src, dst);
53
54   /* create a result route */
55   std::vector<RouteCreationArgs*> route_stack;
56   unsigned int cur = dst->id();
57   do {
58     int pred = TO_FLOYD_PRED(src->id(), cur);
59     if (pred == -1)
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));
62     cur = pred;
63   } while (cur != src->id());
64
65   if (hierarchy_ == RoutingMode::recursive) {
66     route->gw_src = route_stack.back()->gw_src;
67     route->gw_dst = route_stack.front()->gw_dst;
68   }
69
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);
77     }
78
79     for (auto const& link : e_route->link_list) {
80       route->link_list.push_back(link);
81       if (lat)
82         *lat += link->get_latency();
83     }
84
85     prev_dst_gw = e_route->gw_dst;
86   }
87 }
88
89 void FloydZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
90                           std::vector<resource::LinkImpl*>& link_list, bool symmetrical)
91 {
92   /* set the size of table routing */
93   unsigned int table_size = get_table_size();
94   init_tables(table_size);
95
96   add_route_check_params(src, dst, gw_src, gw_dst, link_list, symmetrical);
97
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());
103   else
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(),
106                dst->get_cname());
107
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();
111
112   if (symmetrical) {
113     if (gw_dst && gw_src) // netzone route (to adapt the error message, if any)
114       xbt_assert(
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());
118     else
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());
122
123     if (gw_dst && gw_src) {
124       NetPoint* gw_tmp = gw_src;
125       gw_src           = gw_dst;
126       gw_dst           = gw_tmp;
127     }
128
129     if (not gw_src || not gw_dst)
130       XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst->get_cname(), src->get_cname());
131     else
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());
134
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 */
139   }
140 }
141
142 void FloydZone::seal()
143 {
144   /* set the size of table routing */
145   unsigned int table_size = get_table_size();
146   init_tables(table_size);
147
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);
152       if (not route) {
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;
158       }
159     }
160   }
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);
170         }
171       }
172     }
173   }
174 }
175 }
176 }
177 }