Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not use RouteCreationArgs outside of XML parsing
[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 "surf/surf.hpp"
10 #include "xbt/string.hpp"
11
12 #include <cfloat>
13 #include <limits>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_floyd, surf, "Routing part of surf");
16
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]
20
21 namespace simgrid {
22 namespace kernel {
23 namespace routing {
24
25 FloydZone::~FloydZone()
26 {
27   /* Delete link_table */
28   for (auto const* link : link_table_)
29     delete link;
30 }
31
32 void FloydZone::init_tables(unsigned int table_size)
33 {
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 */
39   }
40 }
41
42 void FloydZone::get_local_route(NetPoint* src, NetPoint* dst, Route* route, double* lat)
43 {
44   unsigned int table_size = get_table_size();
45
46   get_route_check_params(src, dst);
47
48   /* create a result route */
49   std::vector<Route*> route_stack;
50   unsigned int cur = dst->id();
51   do {
52     int pred = TO_FLOYD_PRED(src->id(), cur);
53     if (pred == -1)
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));
56     cur = pred;
57   } while (cur != src->id());
58
59   if (get_hierarchy() == RoutingMode::recursive) {
60     route->gw_src_ = route_stack.back()->gw_src_;
61     route->gw_dst_ = route_stack.front()->gw_dst_;
62   }
63
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);
71     }
72
73     for (auto const& link : e_route->link_list_) {
74       route->link_list_.push_back(link);
75       if (lat)
76         *lat += link->get_latency();
77     }
78
79     prev_dst_gw = e_route->gw_dst_;
80   }
81 }
82
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)
85 {
86   /* set the size of table routing */
87   unsigned int table_size = get_table_size();
88   init_tables(table_size);
89
90   add_route_check_params(src, dst, gw_src, gw_dst, link_list_, symmetrical);
91
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());
97   else
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(),
100                dst->get_cname());
101
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();
105
106   if (symmetrical) {
107     if (gw_dst && gw_src) // netzone route (to adapt the error message, if any)
108       xbt_assert(
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());
112     else
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());
116
117     if (gw_dst && gw_src) {
118       NetPoint* gw_tmp = gw_src;
119       gw_src           = gw_dst;
120       gw_dst           = gw_tmp;
121     }
122
123     if (not gw_src || not gw_dst)
124       XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst->get_cname(), src->get_cname());
125     else
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());
128
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 */
133   }
134 }
135
136 void FloydZone::do_seal()
137 {
138   /* set the size of table routing */
139   unsigned int table_size = get_table_size();
140   init_tables(table_size);
141
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);
146       if (not route) {
147         route = new Route();
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;
152       }
153     }
154   }
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);
164         }
165       }
166     }
167   }
168 }
169 } // namespace routing
170 } // namespace kernel
171
172 namespace s4u {
173 NetZone* create_floyd_zone(const std::string& name)
174 {
175   return (new kernel::routing::FloydZone(name))->get_iface();
176 }
177 } // namespace s4u
178
179 } // namespace simgrid