Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'pikachuyann/simgrid-stoprofiles'
[simgrid.git] / src / kernel / routing / FloydZone.cpp
1 /* Copyright (c) 2009-2020. 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()) = new_extended_route(hierarchy_, gw_src, gw_dst, link_list, true);
121   TO_FLOYD_PRED(src->id(), dst->id()) = src->id();
122   TO_FLOYD_COST(src->id(), dst->id()) = (TO_FLOYD_LINK(src->id(), dst->id()))->link_list.size();
123
124   if (symmetrical) {
125     if (gw_dst) // netzone route (to adapt the error message, if any)
126       xbt_assert(
127           nullptr == TO_FLOYD_LINK(dst->id(), src->id()),
128           "The route between %s@%s and %s@%s already exists. You should not declare the reverse path as symmetrical.",
129           dst->get_cname(), gw_dst->get_cname(), src->get_cname(), gw_src->get_cname());
130     else
131       xbt_assert(nullptr == TO_FLOYD_LINK(dst->id(), src->id()),
132                  "The route between %s and %s already exists. You should not declare the reverse path as symmetrical.",
133                  dst->get_cname(), src->get_cname());
134
135     if (gw_dst && gw_src) {
136       NetPoint* gw_tmp = gw_src;
137       gw_src           = gw_dst;
138       gw_dst           = gw_tmp;
139     }
140
141     if (not gw_src || not gw_dst)
142       XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst->get_cname(), src->get_cname());
143     else
144       XBT_DEBUG("Load NetzoneRoute from \"%s(%s)\" to \"%s(%s)\"", dst->get_cname(), gw_src->get_cname(),
145                 src->get_cname(), gw_dst->get_cname());
146
147     TO_FLOYD_LINK(dst->id(), src->id()) = new_extended_route(hierarchy_, gw_src, gw_dst, link_list, false);
148     TO_FLOYD_PRED(dst->id(), src->id()) = dst->id();
149     TO_FLOYD_COST(dst->id(), src->id()) =
150         (TO_FLOYD_LINK(dst->id(), src->id()))->link_list.size(); /* count of links, old model assume 1 */
151   }
152 }
153
154 void FloydZone::seal()
155 {
156   /* set the size of table routing */
157   unsigned int table_size = get_table_size();
158
159   if (not link_table_) {
160     /* Create Cost, Predecessor and Link tables */
161     cost_table_        = new double[table_size * table_size];             /* link cost from host to host */
162     predecessor_table_ = new int[table_size * table_size];                /* predecessor host numbers */
163     link_table_        = new RouteCreationArgs*[table_size * table_size]; /* actual link between src and dst */
164
165     /* Initialize costs and predecessors */
166     for (unsigned int i = 0; i < table_size; i++)
167       for (unsigned int j = 0; j < table_size; j++) {
168         TO_FLOYD_COST(i, j) = DBL_MAX;
169         TO_FLOYD_PRED(i, j) = -1;
170         TO_FLOYD_LINK(i, j) = nullptr;
171       }
172   }
173
174   /* Add the loopback if needed */
175   if (network_model_->loopback_ && hierarchy_ == RoutingMode::base) {
176     for (unsigned int i = 0; i < table_size; i++) {
177       RouteCreationArgs* route = TO_FLOYD_LINK(i, i);
178       if (not route) {
179         route = new RouteCreationArgs();
180         route->link_list.push_back(network_model_->loopback_);
181         TO_FLOYD_LINK(i, i) = route;
182         TO_FLOYD_PRED(i, i) = i;
183         TO_FLOYD_COST(i, i) = 1;
184       }
185     }
186   }
187   /* Calculate path costs */
188   for (unsigned int c = 0; c < table_size; c++) {
189     for (unsigned int a = 0; a < table_size; a++) {
190       for (unsigned int b = 0; b < table_size; b++) {
191         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX &&
192             (fabs(TO_FLOYD_COST(a, b) - DBL_MAX) < std::numeric_limits<double>::epsilon() ||
193              (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) < TO_FLOYD_COST(a, b)))) {
194           TO_FLOYD_COST(a, b) = TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
195           TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
196         }
197       }
198     }
199   }
200 }
201 }
202 }
203 }