Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add Floyd routing
[simgrid.git] / src / surf / surf_routing_floyd.cpp
1 #include "surf_routing_private.h"
2 #include "surf_routing_floyd.hpp"
3 #include "network.hpp"
4
5 extern "C" {
6 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_floyd, surf, "Routing part of surf");
7 }
8
9 #define TO_FLOYD_COST(i,j) (p_costTable)[(i)+(j)*table_size]
10 #define TO_FLOYD_PRED(i,j) (p_predecessorTable)[(i)+(j)*table_size]
11 #define TO_FLOYD_LINK(i,j) (p_linkTable)[(i)+(j)*table_size]
12
13 AS_t model_floyd_create(void)
14 {
15   return new AsFloyd();
16 }
17
18 void model_floyd_end(AS_t current_routing)
19 {
20   static_cast<AsFloydPtr>(current_routing)->end();
21 }
22
23 AsFloyd::AsFloyd(): AsGeneric() {
24 }
25
26 AsFloyd::~AsFloyd(){
27   int i, j;
28   size_t table_size;
29   table_size = xbt_dynar_length(p_indexNetworkElm);
30     /* Delete link_table */
31     for (i = 0; i < table_size; i++)
32       for (j = 0; j < table_size; j++)
33         generic_free_route(TO_FLOYD_LINK(i, j));
34     xbt_free(p_linkTable);
35     /* Delete bypass dict */
36     xbt_dict_free(&p_bypassRoutes);
37     /* Delete predecessor and cost table */
38     xbt_free(p_predecessorTable);
39     xbt_free(p_costTable);
40 }
41
42 /* Business methods */
43 xbt_dynar_t AsFloyd::getOneLinkRoutes()
44 {
45   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
46   sg_platf_route_cbarg_t route =   xbt_new0(s_sg_platf_route_cbarg_t, 1);
47   route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
48
49   int src,dst;
50   sg_routing_edge_t src_elm, dst_elm;
51   int table_size = xbt_dynar_length(p_indexNetworkElm);
52   for(src=0; src < table_size; src++) {
53     for(dst=0; dst< table_size; dst++) {
54       xbt_dynar_reset(route->link_list);
55       src_elm = xbt_dynar_get_as(p_indexNetworkElm, src, RoutingEdgePtr);
56       dst_elm = xbt_dynar_get_as(p_indexNetworkElm, dst, RoutingEdgePtr);
57       this->getRouteAndLatency(src_elm, dst_elm, route, NULL);
58
59       if (xbt_dynar_length(route->link_list) == 1) {
60         void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
61         onelink_t onelink = xbt_new0(s_onelink_t, 1);
62         onelink->link_ptr = link;
63         if (p_hierarchy == SURF_ROUTING_BASE) {
64           onelink->src = src_elm;
65           onelink->dst = dst_elm;
66         } else if (p_hierarchy == SURF_ROUTING_RECURSIVE) {
67           onelink->src = route->gw_src;
68           onelink->dst = route->gw_dst;
69         }
70         xbt_dynar_push(ret, &onelink);
71       }
72     }
73   }
74
75   return ret;
76 }
77
78 void AsFloyd::getRouteAndLatency(RoutingEdgePtr src, RoutingEdgePtr dst, sg_platf_route_cbarg_t res, double *lat)
79 {
80
81   /* set utils vars */
82   size_t table_size = xbt_dynar_length(p_indexNetworkElm);
83
84   this->srcDstCheck(src, dst);
85
86   /* create a result route */
87   xbt_dynar_t route_stack = xbt_dynar_new(sizeof(sg_platf_route_cbarg_t), NULL);
88   int pred;
89   int cur = dst->m_id;
90   do {
91     pred = TO_FLOYD_PRED(src->m_id, cur);
92     if (pred == -1)
93       THROWF(arg_error, 0, "No route from '%s' to '%s'", src->p_name, dst->p_name);
94     xbt_dynar_push_as(route_stack, sg_platf_route_cbarg_t, TO_FLOYD_LINK(pred, cur));
95     cur = pred;
96   } while (cur != src->m_id);
97
98   if (p_hierarchy == SURF_ROUTING_RECURSIVE) {
99     res->gw_src = xbt_dynar_getlast_as(route_stack, sg_platf_route_cbarg_t)->gw_src;
100     res->gw_dst = xbt_dynar_getfirst_as(route_stack, sg_platf_route_cbarg_t)->gw_dst;
101   }
102
103   sg_routing_edge_t prev_dst_gw = NULL;
104   while (!xbt_dynar_is_empty(route_stack)) {
105     sg_platf_route_cbarg_t e_route = xbt_dynar_pop_as(route_stack, sg_platf_route_cbarg_t);
106     xbt_dynar_t links;
107     NetworkCm02LinkPtr link;
108     unsigned int cpt;
109
110     if (p_hierarchy == SURF_ROUTING_RECURSIVE && prev_dst_gw != NULL
111         && strcmp(prev_dst_gw->p_name, e_route->gw_src->p_name)) {
112       routing_get_route_and_latency(prev_dst_gw, e_route->gw_src,
113                                     &res->link_list, lat);
114     }
115
116     links = e_route->link_list;
117     xbt_dynar_foreach(links, cpt, link) {
118       xbt_dynar_push_as(res->link_list, sg_routing_link_t, link);
119       if (lat)
120         *lat += link->getLatency();
121     }
122
123     prev_dst_gw = e_route->gw_dst;
124   }
125   xbt_dynar_free(&route_stack);
126 }
127
128 static int floyd_pointer_resource_cmp(const void *a, const void *b) {
129   return a != b;
130 }
131
132 void AsFloyd::parseRoute(sg_platf_route_cbarg_t route)
133 {
134   char *src = (char*)(route->src);
135   char *dst = (char*)(route->dst);
136
137   int as_route = 0;
138
139   /* set the size of table routing */
140   size_t table_size = xbt_dynar_length(p_indexNetworkElm);
141   RoutingEdgePtr src_net_elm, dst_net_elm;
142
143   src_net_elm = sg_routing_edge_by_name_or_null(src);
144   dst_net_elm = sg_routing_edge_by_name_or_null(dst);
145
146   xbt_assert(src_net_elm, "Network elements %s not found", src);
147   xbt_assert(dst_net_elm, "Network elements %s not found", dst);
148
149   if(!p_linkTable)
150   {
151     int i,j;
152     /* Create Cost, Predecessor and Link tables */
153     p_costTable = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
154     p_predecessorTable = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
155     p_linkTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);    /* actual link between src and dst */
156
157     /* Initialize costs and predecessors */
158     for (i = 0; i < table_size; i++)
159       for (j = 0; j < table_size; j++) {
160         TO_FLOYD_COST(i, j) = DBL_MAX;
161         TO_FLOYD_PRED(i, j) = -1;
162         TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
163       }
164   }
165   if(!route->gw_dst && !route->gw_src)
166     XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
167   else{
168     as_route = 1;
169     XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
170         route->gw_src->p_name, dst, route->gw_dst->p_name);
171     if(route->gw_dst->p_rcType == SURF_NETWORK_ELEMENT_NULL)
172       xbt_die("The dst_gateway '%s' does not exist!",route->gw_dst->p_name);
173     if(route->gw_src->p_rcType == SURF_NETWORK_ELEMENT_NULL)
174       xbt_die("The src_gateway '%s' does not exist!",route->gw_src->p_name);
175   }
176
177   if(TO_FLOYD_LINK(src_net_elm->m_id, dst_net_elm->m_id))
178   {
179
180     char * link_name;
181     unsigned int cpt;
182     xbt_dynar_t link_route_to_test = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
183     xbt_dynar_foreach(route->link_list,cpt,link_name)
184     {
185       void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
186       xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
187       xbt_dynar_push(link_route_to_test,&link);
188     }
189     xbt_assert(!xbt_dynar_compare(
190         TO_FLOYD_LINK(src_net_elm->m_id, dst_net_elm->m_id)->link_list,
191         link_route_to_test,
192         (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
193         "The route between \"%s\" and \"%s\" already exists", src,dst);
194   }
195   else
196   {
197     TO_FLOYD_LINK(src_net_elm->m_id, dst_net_elm->m_id) =
198         newExtendedRoute(p_hierarchy, route, 1);
199     TO_FLOYD_PRED(src_net_elm->m_id, dst_net_elm->m_id) = src_net_elm->m_id;
200     TO_FLOYD_COST(src_net_elm->m_id, dst_net_elm->m_id) =
201         ((TO_FLOYD_LINK(src_net_elm->m_id, dst_net_elm->m_id))->link_list)->used;   /* count of links, old model assume 1 */
202   }
203
204   if ( (route->symmetrical == TRUE && as_route == 0)
205       || (route->symmetrical == TRUE && as_route == 1)
206   )
207   {
208     if(TO_FLOYD_LINK(dst_net_elm->m_id, src_net_elm->m_id))
209     {
210       if(!route->gw_dst && !route->gw_src)
211         XBT_DEBUG("See Route from \"%s\" to \"%s\"", dst, src);
212       else
213         XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
214             route->gw_src->p_name, src, route->gw_dst->p_name);
215       char * link_name;
216       unsigned int i;
217       xbt_dynar_t link_route_to_test = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
218       for(i=xbt_dynar_length(route->link_list) ;i>0 ;i--)
219       {
220         link_name = xbt_dynar_get_as(route->link_list,i-1,char *);
221         void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
222         xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
223         xbt_dynar_push(link_route_to_test,&link);
224       }
225       xbt_assert(!xbt_dynar_compare(
226           TO_FLOYD_LINK(dst_net_elm->m_id, src_net_elm->m_id)->link_list,
227           link_route_to_test,
228           (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
229           "The route between \"%s\" and \"%s\" already exists", src,dst);
230     }
231     else
232     {
233       if(route->gw_dst && route->gw_src)
234       {
235         sg_routing_edge_t gw_src = route->gw_src;
236         sg_routing_edge_t gw_dst = route->gw_dst;
237         route->gw_src = gw_dst;
238         route->gw_dst = gw_src;
239       }
240
241       if(!route->gw_src && !route->gw_dst)
242         XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
243       else
244         XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
245             route->gw_src->p_name, src, route->gw_dst->p_name);
246
247       TO_FLOYD_LINK(dst_net_elm->m_id, src_net_elm->m_id) =
248                  newExtendedRoute(p_hierarchy, route, 0);
249       TO_FLOYD_PRED(dst_net_elm->m_id, src_net_elm->m_id) = dst_net_elm->m_id;
250       TO_FLOYD_COST(dst_net_elm->m_id, src_net_elm->m_id) =
251           ((TO_FLOYD_LINK(dst_net_elm->m_id, src_net_elm->m_id))->link_list)->used;   /* count of links, old model assume 1 */
252     }
253   }
254   xbt_dynar_free(&route->link_list);
255 }
256
257 void AsFloyd::end(){
258   unsigned int i, j, a, b, c;
259
260   /* set the size of table routing */
261   size_t table_size = xbt_dynar_length(p_indexNetworkElm);
262
263   if(!p_linkTable) {
264     /* Create Cost, Predecessor and Link tables */
265     p_costTable = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
266     p_predecessorTable = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
267     p_linkTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);    /* actual link between src and dst */
268
269     /* Initialize costs and predecessors */
270     for (i = 0; i < table_size; i++)
271       for (j = 0; j < table_size; j++) {
272         TO_FLOYD_COST(i, j) = DBL_MAX;
273         TO_FLOYD_PRED(i, j) = -1;
274         TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
275       }
276   }
277
278   /* Add the loopback if needed */
279   if (routing_platf->p_loopback && p_hierarchy == SURF_ROUTING_BASE) {
280     for (i = 0; i < table_size; i++) {
281       sg_platf_route_cbarg_t e_route = TO_FLOYD_LINK(i, i);
282       if (!e_route) {
283         e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
284         e_route->gw_src = NULL;
285         e_route->gw_dst = NULL;
286         e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
287         xbt_dynar_push(e_route->link_list, &routing_platf->p_loopback);
288         TO_FLOYD_LINK(i, i) = e_route;
289         TO_FLOYD_PRED(i, i) = i;
290         TO_FLOYD_COST(i, i) = 1;
291       }
292     }
293   }
294   /* Calculate path costs */
295   for (c = 0; c < table_size; c++) {
296     for (a = 0; a < table_size; a++) {
297       for (b = 0; b < table_size; b++) {
298         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
299           if (TO_FLOYD_COST(a, b) == DBL_MAX ||
300               (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
301                   TO_FLOYD_COST(a, b))) {
302             TO_FLOYD_COST(a, b) =
303                 TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
304             TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
305           }
306         }
307       }
308     }
309   }
310 }