Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Constify pointer and reference local variables in src/kernel/.
[simgrid.git] / src / kernel / routing / FloydZone.cpp
1 /* Copyright (c) 2009-2019. 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   if (link_table_ == nullptr) // Dealing with a parse error in the file?
34     return;
35   unsigned int table_size = get_table_size();
36   /* Delete link_table */
37   for (unsigned int i = 0; i < table_size; i++)
38     for (unsigned int j = 0; j < table_size; j++)
39       delete TO_FLOYD_LINK(i, j);
40   delete[] link_table_;
41
42   delete[] predecessor_table_;
43   delete[] cost_table_;
44 }
45
46 void FloydZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* lat)
47 {
48   unsigned int table_size = get_table_size();
49
50   get_route_check_params(src, dst);
51
52   /* create a result route */
53   std::vector<RouteCreationArgs*> route_stack;
54   unsigned int cur = dst->id();
55   do {
56     int pred = TO_FLOYD_PRED(src->id(), cur);
57     if (pred == -1)
58       throw std::invalid_argument(xbt::string_printf("No route from '%s' to '%s'", src->get_cname(), dst->get_cname()));
59     route_stack.push_back(TO_FLOYD_LINK(pred, cur));
60     cur = pred;
61   } while (cur != src->id());
62
63   if (hierarchy_ == RoutingMode::recursive) {
64     route->gw_src = route_stack.back()->gw_src;
65     route->gw_dst = route_stack.front()->gw_dst;
66   }
67
68   NetPoint* prev_dst_gw = nullptr;
69   while (not route_stack.empty()) {
70     const RouteCreationArgs* e_route = route_stack.back();
71     route_stack.pop_back();
72     if (hierarchy_ == RoutingMode::recursive && prev_dst_gw != nullptr &&
73         prev_dst_gw->get_cname() != e_route->gw_src->get_cname()) {
74       get_global_route(prev_dst_gw, e_route->gw_src, route->link_list, lat);
75     }
76
77     for (auto const& link : e_route->link_list) {
78       route->link_list.push_back(link);
79       if (lat)
80         *lat += link->get_latency();
81     }
82
83     prev_dst_gw = e_route->gw_dst;
84   }
85 }
86
87 void FloydZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
88                           std::vector<resource::LinkImpl*>& link_list, bool symmetrical)
89 {
90   /* set the size of table routing */
91   unsigned int table_size = get_table_size();
92
93   add_route_check_params(src, dst, gw_src, gw_dst, link_list, symmetrical);
94
95   if (not link_table_) {
96     /* Create Cost, Predecessor and Link tables */
97     cost_table_        = new double[table_size * table_size];             /* link cost from host to host */
98     predecessor_table_ = new int[table_size * table_size];                /* predecessor host numbers */
99     link_table_        = new RouteCreationArgs*[table_size * table_size]; /* actual link between src and dst */
100
101     /* Initialize costs and predecessors */
102     for (unsigned int i = 0; i < table_size; i++)
103       for (unsigned int j = 0; j < table_size; j++) {
104         TO_FLOYD_COST(i, j) = DBL_MAX;
105         TO_FLOYD_PRED(i, j) = -1;
106         TO_FLOYD_LINK(i, j) = nullptr;
107       }
108   }
109
110   /* Check that the route does not already exist */
111   if (gw_dst) // netzone route (to adapt the error message, if any)
112     xbt_assert(nullptr == TO_FLOYD_LINK(src->id(), dst->id()),
113                "The route between %s@%s and %s@%s already exists (Rq: routes are symmetrical by default).",
114                src->get_cname(), gw_src->get_cname(), dst->get_cname(), gw_dst->get_cname());
115   else
116     xbt_assert(nullptr == TO_FLOYD_LINK(src->id(), dst->id()),
117                "The route between %s and %s already exists (Rq: routes are symmetrical by default).", src->get_cname(),
118                dst->get_cname());
119
120   TO_FLOYD_LINK(src->id(), dst->id()) =
121       new_extended_route(hierarchy_, src, dst, gw_src, gw_dst, link_list, symmetrical, 1);
122   TO_FLOYD_PRED(src->id(), dst->id()) = src->id();
123   TO_FLOYD_COST(src->id(), dst->id()) = (TO_FLOYD_LINK(src->id(), dst->id()))->link_list.size();
124
125   if (symmetrical == true) {
126     if (gw_dst) // netzone route (to adapt the error message, if any)
127       xbt_assert(
128           nullptr == TO_FLOYD_LINK(dst->id(), src->id()),
129           "The route between %s@%s and %s@%s already exists. You should not declare the reverse path as symmetrical.",
130           dst->get_cname(), gw_dst->get_cname(), src->get_cname(), gw_src->get_cname());
131     else
132       xbt_assert(nullptr == TO_FLOYD_LINK(dst->id(), src->id()),
133                  "The route between %s and %s already exists. You should not declare the reverse path as symmetrical.",
134                  dst->get_cname(), src->get_cname());
135
136     if (gw_dst && gw_src) {
137       NetPoint* gw_tmp = gw_src;
138       gw_src           = gw_dst;
139       gw_dst           = gw_tmp;
140     }
141
142     if (not gw_src || not gw_dst)
143       XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst->get_cname(), src->get_cname());
144     else
145       XBT_DEBUG("Load NetzoneRoute from \"%s(%s)\" to \"%s(%s)\"", dst->get_cname(), gw_src->get_cname(),
146                 src->get_cname(), gw_dst->get_cname());
147
148     TO_FLOYD_LINK(dst->id(), src->id()) =
149         new_extended_route(hierarchy_, src, dst, gw_src, gw_dst, link_list, symmetrical, 0);
150     TO_FLOYD_PRED(dst->id(), src->id()) = dst->id();
151     TO_FLOYD_COST(dst->id(), src->id()) =
152         (TO_FLOYD_LINK(dst->id(), src->id()))->link_list.size(); /* count of links, old model assume 1 */
153   }
154 }
155
156 void FloydZone::seal()
157 {
158   /* set the size of table routing */
159   unsigned int table_size = get_table_size();
160
161   if (not link_table_) {
162     /* Create Cost, Predecessor and Link tables */
163     cost_table_        = new double[table_size * table_size];             /* link cost from host to host */
164     predecessor_table_ = new int[table_size * table_size];                /* predecessor host numbers */
165     link_table_        = new RouteCreationArgs*[table_size * table_size]; /* actual link between src and dst */
166
167     /* Initialize costs and predecessors */
168     for (unsigned int i = 0; i < table_size; i++)
169       for (unsigned int j = 0; j < table_size; j++) {
170         TO_FLOYD_COST(i, j) = DBL_MAX;
171         TO_FLOYD_PRED(i, j) = -1;
172         TO_FLOYD_LINK(i, j) = nullptr;
173       }
174   }
175
176   /* Add the loopback if needed */
177   if (network_model_->loopback_ && hierarchy_ == RoutingMode::base) {
178     for (unsigned int i = 0; i < table_size; i++) {
179       RouteCreationArgs* route = TO_FLOYD_LINK(i, i);
180       if (not route) {
181         route = new RouteCreationArgs();
182         route->link_list.push_back(network_model_->loopback_);
183         TO_FLOYD_LINK(i, i) = route;
184         TO_FLOYD_PRED(i, i) = i;
185         TO_FLOYD_COST(i, i) = 1;
186       }
187     }
188   }
189   /* Calculate path costs */
190   for (unsigned int c = 0; c < table_size; c++) {
191     for (unsigned int a = 0; a < table_size; a++) {
192       for (unsigned int b = 0; b < table_size; b++) {
193         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX &&
194             (fabs(TO_FLOYD_COST(a, b) - DBL_MAX) < std::numeric_limits<double>::epsilon() ||
195              (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) < TO_FLOYD_COST(a, b)))) {
196           TO_FLOYD_COST(a, b) = TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
197           TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
198         }
199       }
200     }
201   }
202 }
203 }
204 }
205 }