Logo AND Algorithmique Numérique Distribuée

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