Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert S4U to my current coding convention
[simgrid.git] / src / surf / surf_routing_full.cpp
1 /* Copyright (c) 2009-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "src/surf/surf_routing_full.hpp"
8 #include "src/surf/network_interface.hpp"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
11
12 #define TO_ROUTE_FULL(i,j) routingTable_[(i)+(j)*table_size]
13
14 namespace simgrid {
15 namespace surf {
16   AsFull::AsFull(const char*name)
17     : AsRoutedGraph(name)
18   {
19   }
20
21 void AsFull::Seal() {
22   int i;
23   sg_platf_route_cbarg_t e_route;
24
25   /* set utils vars */
26   int table_size = (int)xbt_dynar_length(vertices_);
27
28   /* Create table if necessary */
29   if (!routingTable_)
30     routingTable_ = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
31
32   /* Add the loopback if needed */
33   if (routing_platf->loopback_ && hierarchy_ == SURF_ROUTING_BASE) {
34     for (i = 0; i < table_size; i++) {
35       e_route = TO_ROUTE_FULL(i, i);
36       if (!e_route) {
37         e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
38         e_route->gw_src = NULL;
39         e_route->gw_dst = NULL;
40         e_route->link_list = xbt_dynar_new(sizeof(Link*), NULL);
41         xbt_dynar_push(e_route->link_list, &routing_platf->loopback_);
42         TO_ROUTE_FULL(i, i) = e_route;
43       }
44     }
45   }
46 }
47
48 AsFull::~AsFull(){
49   if (routingTable_) {
50     int table_size = (int)xbt_dynar_length(vertices_);
51     int i, j;
52     /* Delete routing table */
53     for (i = 0; i < table_size; i++)
54       for (j = 0; j < table_size; j++) {
55         if (TO_ROUTE_FULL(i,j)){
56           xbt_dynar_free(&TO_ROUTE_FULL(i,j)->link_list);
57           xbt_free(TO_ROUTE_FULL(i,j));
58         }
59       }
60     xbt_free(routingTable_);
61   }
62 }
63
64 xbt_dynar_t AsFull::getOneLinkRoutes()
65 {
66   xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
67
68   int src, dst;
69   int table_size = xbt_dynar_length(vertices_);
70
71   for(src=0; src < table_size; src++) {
72     for(dst=0; dst< table_size; dst++) {
73       sg_platf_route_cbarg_t route = TO_ROUTE_FULL(src,dst);
74       if (route) {
75         if (xbt_dynar_length(route->link_list) == 1) {
76           void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
77           Onelink *onelink;
78           if (hierarchy_ == SURF_ROUTING_BASE) {
79           NetCard *tmp_src = xbt_dynar_get_as(vertices_, src, sg_netcard_t);
80             tmp_src->setId(src);
81           NetCard *tmp_dst = xbt_dynar_get_as(vertices_, dst, sg_netcard_t);
82           tmp_dst->setId(dst);
83             onelink = new Onelink(link, tmp_src, tmp_dst);
84           } else if (hierarchy_ == SURF_ROUTING_RECURSIVE)
85             onelink = new Onelink(link, route->gw_src, route->gw_dst);
86           else
87             onelink = new Onelink(link, NULL, NULL);
88           xbt_dynar_push(ret, &onelink);
89           XBT_DEBUG("Push route from '%d' to '%d'",
90               src,
91               dst);
92         }
93       }
94     }
95   }
96   return ret;
97 }
98
99 void AsFull::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t res, double *lat)
100 {
101   XBT_DEBUG("full_get_route_and_latency from %s[%d] to %s[%d]",
102       src->name(),
103       src->id(),
104       dst->name(),
105       dst->id());
106
107   /* set utils vars */
108   size_t table_size = xbt_dynar_length(vertices_);
109
110   sg_platf_route_cbarg_t e_route = NULL;
111   void *link;
112   unsigned int cpt = 0;
113
114   e_route = TO_ROUTE_FULL(src->id(), dst->id());
115
116   if (e_route) {
117     res->gw_src = e_route->gw_src;
118     res->gw_dst = e_route->gw_dst;
119     xbt_dynar_foreach(e_route->link_list, cpt, link) {
120       xbt_dynar_push(res->link_list, &link);
121       if (lat)
122         *lat += static_cast<Link*>(link)->getLatency();
123     }
124   }
125 }
126
127 static int full_pointer_resource_cmp(const void *a, const void *b)
128 {
129   return a != b;
130 }
131
132 void AsFull::addRoute(sg_platf_route_cbarg_t route)
133 {
134   const char *src = route->src;
135   const char *dst = route->dst;
136   NetCard *src_net_elm = sg_netcard_by_name_or_null(src);
137   NetCard *dst_net_elm = sg_netcard_by_name_or_null(dst);
138
139   addRouteCheckParams(route);
140
141   size_t table_size = xbt_dynar_length(vertices_);
142
143   if (!routingTable_)
144     routingTable_ = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
145
146   /* Check that the route does not already exist */
147   if (route->gw_dst) // AS route (to adapt the error message, if any)
148     xbt_assert(nullptr == TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id()),
149         "The route between %s@%s and %s@%s already exists (Rq: routes are symmetrical by default).",
150         src,route->gw_src->name(),dst,route->gw_dst->name());
151   else
152     xbt_assert(nullptr == TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id()),
153         "The route between %s and %s already exists (Rq: routes are symmetrical by default).", src,dst);
154
155   /* Add the route to the base */
156   TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id()) = newExtendedRoute(hierarchy_, route, 1);
157   xbt_dynar_shrink(TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id())->link_list, 0);
158
159   if (route->symmetrical == TRUE) {
160     if (route->gw_dst && route->gw_src) {
161       NetCard* gw_tmp = route->gw_src;
162       route->gw_src = route->gw_dst;
163       route->gw_dst = gw_tmp;
164     }
165     if (TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id())) {
166       char *link_name;
167       unsigned int i;
168       xbt_dynar_t link_route_to_test = xbt_dynar_new(sizeof(Link*), NULL);
169       for (i = xbt_dynar_length(route->link_list); i > 0; i--) {
170         link_name = xbt_dynar_get_as(route->link_list, i - 1, char *);
171         void *link = Link::byName(link_name);
172         xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
173         xbt_dynar_push(link_route_to_test, &link);
174       }
175       xbt_assert(!xbt_dynar_compare(TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id())->link_list,
176           link_route_to_test,
177           full_pointer_resource_cmp),
178           "The route between \"%s\" and \"%s\" already exists", src,
179           dst);
180     } else {
181       if (!route->gw_dst && !route->gw_src)
182         XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
183       else
184         XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
185             dst, route->gw_src->name(), src, route->gw_dst->name());
186       TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id()) = newExtendedRoute(hierarchy_, route, 0);
187       xbt_dynar_shrink(TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id())->link_list, 0);
188     }
189   }
190   xbt_dynar_free(&route->link_list);
191 }
192
193 }
194 }