Logo AND Algorithmique Numérique Distribuée

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