Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
AS are called netzones, now
[simgrid.git] / src / kernel / routing / FloydZone.cpp
1 /* Copyright (c) 2009-2016. 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
7 #include "src/kernel/routing/FloydZone.hpp"
8 #include "src/kernel/routing/NetCard.hpp"
9 #include "src/surf/network_interface.hpp"
10 #include "xbt/log.h"
11
12 #include <float.h>
13 #include <limits>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_floyd, surf, "Routing part of surf");
16
17 #define TO_FLOYD_COST(i, j) (costTable_)[(i) + (j)*table_size]
18 #define TO_FLOYD_PRED(i, j) (predecessorTable_)[(i) + (j)*table_size]
19 #define TO_FLOYD_LINK(i, j) (linkTable_)[(i) + (j)*table_size]
20
21 namespace simgrid {
22 namespace kernel {
23 namespace routing {
24
25 FloydZone::FloydZone(NetZone* father, const char* name) : RoutedZone(father, name)
26 {
27   predecessorTable_ = nullptr;
28   costTable_        = nullptr;
29   linkTable_        = nullptr;
30 }
31
32 FloydZone::~FloydZone()
33 {
34   if (linkTable_ == nullptr) // Dealing with a parse error in the file?
35     return;
36   int table_size = vertices_.size();
37   /* Delete link_table */
38   for (int i = 0; i < table_size; i++)
39     for (int j = 0; j < table_size; j++)
40       routing_route_free(TO_FLOYD_LINK(i, j));
41   xbt_free(linkTable_);
42
43   xbt_free(predecessorTable_);
44   xbt_free(costTable_);
45 }
46
47 void FloydZone::getLocalRoute(NetCard* src, NetCard* dst, sg_platf_route_cbarg_t route, double* lat)
48 {
49   size_t table_size = vertices_.size();
50
51   getRouteCheckParams(src, dst);
52
53   /* create a result route */
54   std::vector<sg_platf_route_cbarg_t> route_stack;
55   unsigned int cur = dst->id();
56   do {
57     int pred = TO_FLOYD_PRED(src->id(), cur);
58     if (pred == -1)
59       THROWF(arg_error, 0, "No route from '%s' to '%s'", src->name().c_str(), dst->name().c_str());
60     route_stack.push_back(TO_FLOYD_LINK(pred, cur));
61     cur = pred;
62   } while (cur != src->id());
63
64   if (hierarchy_ == RoutingMode::recursive) {
65     route->gw_src = route_stack.back()->gw_src;
66     route->gw_dst = route_stack.front()->gw_dst;
67   }
68
69   sg_netcard_t prev_dst_gw = nullptr;
70   while (!route_stack.empty()) {
71     sg_platf_route_cbarg_t e_route = route_stack.back();
72     route_stack.pop_back();
73     if (hierarchy_ == RoutingMode::recursive && prev_dst_gw != nullptr &&
74         strcmp(prev_dst_gw->name().c_str(), e_route->gw_src->name().c_str())) {
75       getGlobalRoute(prev_dst_gw, e_route->gw_src, route->link_list, lat);
76     }
77
78     for (auto link : *e_route->link_list) {
79       route->link_list->push_back(link);
80       if (lat)
81         *lat += link->latency();
82     }
83
84     prev_dst_gw = e_route->gw_dst;
85   }
86 }
87
88 void FloydZone::addRoute(sg_platf_route_cbarg_t route)
89 {
90   /* set the size of table routing */
91   int table_size = static_cast<int>(vertices_.size());
92
93   addRouteCheckParams(route);
94
95   if (!linkTable_) {
96     /* Create Cost, Predecessor and Link tables */
97     costTable_        = xbt_new0(double, table_size* table_size);                  /* link cost from host to host */
98     predecessorTable_ = xbt_new0(int, table_size* table_size);                     /* predecessor host numbers */
99     linkTable_        = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size); /* actual link between src and dst */
100
101     /* Initialize costs and predecessors */
102     for (int i = 0; i < table_size; i++)
103       for (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 (route->gw_dst) // netzone route (to adapt the error message, if any)
112     xbt_assert(nullptr == TO_FLOYD_LINK(route->src->id(), route->dst->id()),
113                "The route between %s@%s and %s@%s already exists (Rq: routes are symmetrical by default).",
114                route->src->name().c_str(), route->gw_src->name().c_str(), route->dst->name().c_str(),
115                route->gw_dst->name().c_str());
116   else
117     xbt_assert(nullptr == TO_FLOYD_LINK(route->src->id(), route->dst->id()),
118                "The route between %s and %s already exists (Rq: routes are symmetrical by default).",
119                route->src->name().c_str(), route->dst->name().c_str());
120
121   TO_FLOYD_LINK(route->src->id(), route->dst->id()) = newExtendedRoute(hierarchy_, route, 1);
122   TO_FLOYD_PRED(route->src->id(), route->dst->id()) = route->src->id();
123   TO_FLOYD_COST(route->src->id(), route->dst->id()) =
124       (TO_FLOYD_LINK(route->src->id(), route->dst->id()))->link_list->size();
125
126   if (route->symmetrical == true) {
127     if (route->gw_dst) // netzone route (to adapt the error message, if any)
128       xbt_assert(
129           nullptr == TO_FLOYD_LINK(route->dst->id(), route->src->id()),
130           "The route between %s@%s and %s@%s already exists. You should not declare the reverse path as symmetrical.",
131           route->dst->name().c_str(), route->gw_dst->name().c_str(), route->src->name().c_str(),
132           route->gw_src->name().c_str());
133     else
134       xbt_assert(nullptr == TO_FLOYD_LINK(route->dst->id(), route->src->id()),
135                  "The route between %s and %s already exists. You should not declare the reverse path as symmetrical.",
136                  route->dst->name().c_str(), route->src->name().c_str());
137
138     if (route->gw_dst && route->gw_src) {
139       NetCard* gw_tmp = route->gw_src;
140       route->gw_src   = route->gw_dst;
141       route->gw_dst   = gw_tmp;
142     }
143
144     if (!route->gw_src && !route->gw_dst)
145       XBT_DEBUG("Load Route from \"%s\" to \"%s\"", route->dst->name().c_str(), route->src->name().c_str());
146     else
147       XBT_DEBUG("Load NetzoneRoute from \"%s(%s)\" to \"%s(%s)\"", route->dst->name().c_str(), route->gw_src->name().c_str(),
148                 route->src->name().c_str(), route->gw_dst->name().c_str());
149
150     TO_FLOYD_LINK(route->dst->id(), route->src->id()) = newExtendedRoute(hierarchy_, route, 0);
151     TO_FLOYD_PRED(route->dst->id(), route->src->id()) = route->dst->id();
152     TO_FLOYD_COST(route->dst->id(), route->src->id()) =
153         (TO_FLOYD_LINK(route->dst->id(), route->src->id()))->link_list->size(); /* count of links, old model assume 1 */
154   }
155 }
156
157 void FloydZone::seal()
158 {
159   /* set the size of table routing */
160   size_t table_size = vertices_.size();
161
162   if (!linkTable_) {
163     /* Create Cost, Predecessor and Link tables */
164     costTable_        = xbt_new0(double, table_size* table_size);                  /* link cost from host to host */
165     predecessorTable_ = xbt_new0(int, table_size* table_size);                     /* predecessor host numbers */
166     linkTable_        = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size); /* actual link between src and dst */
167
168     /* Initialize costs and predecessors */
169     for (unsigned int i = 0; i < table_size; i++)
170       for (unsigned int j = 0; j < table_size; j++) {
171         TO_FLOYD_COST(i, j) = DBL_MAX;
172         TO_FLOYD_PRED(i, j) = -1;
173         TO_FLOYD_LINK(i, j) = nullptr;
174       }
175   }
176
177   /* Add the loopback if needed */
178   if (surf_network_model->loopback_ && hierarchy_ == RoutingMode::base) {
179     for (unsigned int i = 0; i < table_size; i++) {
180       sg_platf_route_cbarg_t e_route = TO_FLOYD_LINK(i, i);
181       if (!e_route) {
182         e_route            = xbt_new0(s_sg_platf_route_cbarg_t, 1);
183         e_route->gw_src    = nullptr;
184         e_route->gw_dst    = nullptr;
185         e_route->link_list = new std::vector<Link*>();
186         e_route->link_list->push_back(surf_network_model->loopback_);
187         TO_FLOYD_LINK(i, i) = e_route;
188         TO_FLOYD_PRED(i, i) = i;
189         TO_FLOYD_COST(i, i) = 1;
190       }
191     }
192   }
193   /* Calculate path costs */
194   for (unsigned int c = 0; c < table_size; c++) {
195     for (unsigned int a = 0; a < table_size; a++) {
196       for (unsigned int b = 0; b < table_size; b++) {
197         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
198           if (fabs(TO_FLOYD_COST(a, b) - DBL_MAX) < std::numeric_limits<double>::epsilon() ||
199               (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) < TO_FLOYD_COST(a, b))) {
200             TO_FLOYD_COST(a, b) = TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
201             TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
202           }
203         }
204       }
205     }
206   }
207 }
208 }
209 }
210 }