Logo AND Algorithmique Numérique Distribuée

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