Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge remote-tracking branch 'origin/master'
[simgrid.git] / src / surf / surf_routing.cpp
1 /* Copyright (c) 2009-2011, 2013-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 "surf_routing.hpp"
8 #include "surf_routing_private.hpp"
9 #include "surf_routing_cluster.hpp"
10 #include "surf_routing_cluster_torus.hpp"
11 #include "surf_routing_cluster_fat_tree.hpp"
12
13 #include "simgrid/platf_interface.h"    // platform creation API internal interface
14 #include "simgrid/sg_config.h"
15 #include "storage_interface.hpp"
16
17 #include "surf/surfxml_parse_values.h"
18
19 /*************
20  * Callbacks *
21  *************/
22
23 namespace simgrid {
24 namespace surf {
25
26 surf_callback(void, simgrid::surf::RoutingEdge*) routingEdgeCreatedCallbacks;
27 surf_callback(void, simgrid::surf::As*) asCreatedCallbacks;
28
29 }
30 }
31
32 /**
33  * @ingroup SURF_build_api
34  * @brief A library containing all known hosts
35  */
36 xbt_lib_t host_lib;
37
38 int SURF_HOST_LEVEL;            //Surf host level
39 int COORD_HOST_LEVEL=0;         //Coordinates level
40 int NS3_HOST_LEVEL;             //host node for ns3
41
42 int MSG_FILE_LEVEL;             //Msg file level
43
44 int SIMIX_STORAGE_LEVEL;        //Simix storage level
45 int MSG_STORAGE_LEVEL;          //Msg storage level
46 int SD_STORAGE_LEVEL;           //Simdag storage level
47
48 xbt_lib_t as_router_lib;
49 int ROUTING_ASR_LEVEL;          //Routing level
50 int COORD_ASR_LEVEL;            //Coordinates level
51 int NS3_ASR_LEVEL;              //host node for ns3
52 int ROUTING_PROP_ASR_LEVEL;     //Where the properties are stored
53
54 static xbt_dict_t random_value = NULL;
55
56
57 /** @brief Retrieve a routing edge from its name
58  *
59  * Routing edges are either host and routers, whatever
60  */
61 simgrid::surf::RoutingEdge *sg_routing_edge_by_name_or_null(const char *name)
62 {
63   sg_host_t h = sg_host_by_name(name);
64   simgrid::surf::RoutingEdge *net_elm = h==NULL?NULL: sg_host_edge(h);
65   if (!net_elm)
66         net_elm = (simgrid::surf::RoutingEdge*) xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
67   return net_elm;
68 }
69
70 /* Global vars */
71 simgrid::surf::RoutingPlatf *routing_platf = NULL;
72
73 /* global parse functions */
74 extern xbt_dynar_t mount_list;
75
76 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
77
78 /** The current AS in the parsing */
79 static simgrid::surf::As *current_routing = NULL;
80 simgrid::surf::As* routing_get_current()
81 {
82   return current_routing;
83 }
84
85 // static void routing_parse_Srandom(void);        /* random bypass */
86
87 static void routing_parse_postparse(void);
88
89 /* this lines are only for replace use like index in the model table */
90 typedef enum {
91   SURF_MODEL_FULL = 0,
92   SURF_MODEL_FLOYD,
93   SURF_MODEL_DIJKSTRA,
94   SURF_MODEL_DIJKSTRACACHE,
95   SURF_MODEL_NONE,
96   SURF_MODEL_VIVALDI,
97   SURF_MODEL_CLUSTER,
98   SURF_MODEL_TORUS_CLUSTER,
99   SURF_MODEL_FAT_TREE_CLUSTER,
100 } e_routing_types;
101
102 struct s_model_type routing_models[] = {
103   {"Full",
104    "Full routing data (fast, large memory requirements, fully expressive)",
105    model_full_create, model_full_end},
106   {"Floyd",
107    "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
108    model_floyd_create, model_floyd_end},
109   {"Dijkstra",
110    "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
111    model_dijkstra_create, model_dijkstra_both_end},
112   {"DijkstraCache",
113    "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
114    model_dijkstracache_create, model_dijkstra_both_end},
115   {"none", "No routing (Unless you know what you are doing, avoid using this mode in combination with a non Constant network model).",
116    model_none_create,  NULL},
117   {"Vivaldi", "Vivaldi routing",
118    model_vivaldi_create, NULL},
119   {"Cluster", "Cluster routing",
120    model_cluster_create, NULL},
121   {"Torus_Cluster", "Torus Cluster routing",
122    model_torus_cluster_create, NULL},
123   {"Fat_Tree_Cluster", "Fat Tree Cluster routing",
124    model_fat_tree_cluster_create, NULL},
125   {NULL, NULL, NULL, NULL}
126 };
127
128 /**
129  * \brief Add a "host_link" to the network element list
130  */
131 void sg_platf_new_host_link(sg_platf_host_link_cbarg_t host)
132 {
133   simgrid::surf::RoutingEdge *info = sg_host_edge(sg_host_by_name(host->id));
134   xbt_assert(info, "Host '%s' not found!", host->id);
135   xbt_assert(current_routing->p_modelDesc == &routing_models[SURF_MODEL_CLUSTER] ||
136       current_routing->p_modelDesc == &routing_models[SURF_MODEL_VIVALDI],
137       "You have to be in model Cluster to use tag host_link!");
138
139   s_surf_parsing_link_up_down_t link_up_down;
140   link_up_down.link_up = Link::byName(host->link_up);
141   link_up_down.link_down = Link::byName(host->link_down);
142
143   xbt_assert(link_up_down.link_up, "Link '%s' not found!",host->link_up);
144   xbt_assert(link_up_down.link_down, "Link '%s' not found!",host->link_down);
145
146   if(!current_routing->p_linkUpDownList)
147     current_routing->p_linkUpDownList = xbt_dynar_new(sizeof(s_surf_parsing_link_up_down_t),NULL);
148
149   // If dynar is is greater than edge id and if the host_link is already defined
150   if((int)xbt_dynar_length(current_routing->p_linkUpDownList) > info->getId() &&
151       xbt_dynar_get_as(current_routing->p_linkUpDownList, info->getId(), void*))
152         surf_parse_error("Host_link for '%s' is already defined!",host->id);
153
154   XBT_DEBUG("Push Host_link for host '%s' to position %d", info->getName(), info->getId());
155   xbt_dynar_set_as(current_routing->p_linkUpDownList, info->getId(), s_surf_parsing_link_up_down_t, link_up_down);
156 }
157
158 /**
159  * \brief Add a "host" to the network element list
160  */
161 simgrid::surf::RoutingEdge *routing_add_host(
162   simgrid::surf::As* current_routing, sg_platf_host_cbarg_t host)
163 {
164   if (current_routing->p_hierarchy == SURF_ROUTING_NULL)
165     current_routing->p_hierarchy = SURF_ROUTING_BASE;
166   xbt_assert(!sg_host_by_name(host->id),
167                      "Reading a host, processing unit \"%s\" already exists", host->id);
168
169   simgrid::surf::RoutingEdge *routingEdge =
170     new simgrid::surf::RoutingEdgeImpl(xbt_strdup(host->id),
171                                                     -1,
172                                                     SURF_NETWORK_ELEMENT_HOST,
173                                                     current_routing);
174   routingEdge->setId(current_routing->parsePU(routingEdge));
175   sg_host_edge_set(sg_host_by_name_or_create(host->id), routingEdge);
176   XBT_DEBUG("Having set name '%s' id '%d'", host->id, routingEdge->getId());
177   simgrid::surf::routingEdgeCreatedCallbacks(routingEdge);
178
179   if(mount_list){
180     xbt_lib_set(storage_lib, host->id, ROUTING_STORAGE_HOST_LEVEL, (void *) mount_list);
181     mount_list = NULL;
182   }
183
184   if (host->coord && strcmp(host->coord, "")) {
185     unsigned int cursor;
186     char*str;
187
188     if (!COORD_HOST_LEVEL)
189       xbt_die ("To use host coordinates, please add --cfg=network/coordinates:yes to your command line");
190     /* Pre-parse the host coordinates -- FIXME factorize with routers by overloading the routing->parse_PU function*/
191     xbt_dynar_t ctn_str = xbt_str_split_str(host->coord, " ");
192     xbt_dynar_t ctn = xbt_dynar_new(sizeof(double),NULL);
193     xbt_dynar_foreach(ctn_str,cursor, str) {
194       double val = atof(str);
195       xbt_dynar_push(ctn,&val);
196     }
197     xbt_dynar_shrink(ctn, 0);
198     xbt_dynar_free(&ctn_str);
199     xbt_lib_set(host_lib, host->id, COORD_HOST_LEVEL, (void *) ctn);
200     XBT_DEBUG("Having set host coordinates for '%s'",host->id);
201   }
202
203   return routingEdge;
204 }
205
206 /**
207  * \brief Store the ASroute by calling the set_ASroute function of the current routing component
208  */
209 static void parse_E_ASroute(sg_platf_route_cbarg_t ASroute)
210 {
211   /*FIXME:REMOVE:xbt_assert(current_routing->parse_ASroute,
212              "no defined method \"set_ASroute\" in \"%s\"",
213              current_routing->name);*/
214   current_routing->parseASroute(ASroute);
215 }
216
217 void sg_platf_new_trace(sg_platf_trace_cbarg_t trace)
218 {
219   tmgr_trace_t tmgr_trace;
220   if (!trace->file || strcmp(trace->file, "") != 0) {
221     tmgr_trace = tmgr_trace_new_from_file(trace->file);
222   } else if (strcmp(trace->pc_data, "") == 0) {
223     tmgr_trace = NULL;
224   } else {
225     tmgr_trace =
226           tmgr_trace_new_from_string(trace->id, trace->pc_data,
227                                      trace->periodicity);
228   }
229   xbt_dict_set(traces_set_list, trace->id, (void *) tmgr_trace, NULL);
230 }
231
232 void sg_platf_trace_connect(sg_platf_trace_connect_cbarg_t trace_connect)
233 {
234   xbt_assert(xbt_dict_get_or_null
235               (traces_set_list, trace_connect->trace),
236               "Cannot connect trace %s to %s: trace unknown",
237               trace_connect->trace,
238               trace_connect->element);
239
240   switch (trace_connect->kind) {
241   case SURF_TRACE_CONNECT_KIND_HOST_AVAIL:
242     xbt_dict_set(trace_connect_list_host_avail,
243         trace_connect->trace,
244         xbt_strdup(trace_connect->element), NULL);
245     break;
246   case SURF_TRACE_CONNECT_KIND_POWER:
247     xbt_dict_set(trace_connect_list_power, trace_connect->trace,
248         xbt_strdup(trace_connect->element), NULL);
249     break;
250   case SURF_TRACE_CONNECT_KIND_LINK_AVAIL:
251     xbt_dict_set(trace_connect_list_link_avail,
252         trace_connect->trace,
253         xbt_strdup(trace_connect->element), NULL);
254     break;
255   case SURF_TRACE_CONNECT_KIND_BANDWIDTH:
256     xbt_dict_set(trace_connect_list_bandwidth,
257         trace_connect->trace,
258         xbt_strdup(trace_connect->element), NULL);
259     break;
260   case SURF_TRACE_CONNECT_KIND_LATENCY:
261     xbt_dict_set(trace_connect_list_latency, trace_connect->trace,
262         xbt_strdup(trace_connect->element), NULL);
263     break;
264   default:
265         surf_parse_error("Cannot connect trace %s to %s: kind of trace unknown",
266         trace_connect->trace, trace_connect->element);
267     break;
268   }
269 }
270
271 /**
272  * \brief Make a new routing component to the platform
273  *
274  * Add a new autonomous system to the platform. Any elements (such as host,
275  * router or sub-AS) added after this call and before the corresponding call
276  * to sg_platf_new_AS_close() will be added to this AS.
277  *
278  * Once this function was called, the configuration concerning the used
279  * models cannot be changed anymore.
280  *
281  * @param AS_id name of this autonomous system. Must be unique in the platform
282  * @param wanted_routing_type one of Full, Floyd, Dijkstra or similar. Full list in the variable routing_models, in src/surf/surf_routing.c
283  */
284 void routing_AS_begin(sg_platf_AS_cbarg_t AS)
285 {
286   XBT_DEBUG("routing_AS_begin");
287   routing_model_description_t model = NULL;
288
289   xbt_assert(!xbt_lib_get_or_null
290              (as_router_lib, AS->id, ROUTING_ASR_LEVEL),
291              "The AS \"%s\" already exists", AS->id);
292
293   _sg_cfg_init_status = 2; /* horrible hack: direct access to the global
294                             * controlling the level of configuration to prevent
295                             * any further config */
296
297   /* search the routing model */
298   switch(AS->routing){
299     case A_surfxml_AS_routing_Cluster:               model = &routing_models[SURF_MODEL_CLUSTER];break;
300     case A_surfxml_AS_routing_Cluster___torus:       model = &routing_models[SURF_MODEL_TORUS_CLUSTER];break;
301     case A_surfxml_AS_routing_Cluster___fat___tree:  model = &routing_models[SURF_MODEL_FAT_TREE_CLUSTER];break;
302     case A_surfxml_AS_routing_Dijkstra:              model = &routing_models[SURF_MODEL_DIJKSTRA];break;
303     case A_surfxml_AS_routing_DijkstraCache:         model = &routing_models[SURF_MODEL_DIJKSTRACACHE];break;
304     case A_surfxml_AS_routing_Floyd:                 model = &routing_models[SURF_MODEL_FLOYD];break;
305     case A_surfxml_AS_routing_Full:                  model = &routing_models[SURF_MODEL_FULL];break;
306     case A_surfxml_AS_routing_None:                  model = &routing_models[SURF_MODEL_NONE];break;
307     case A_surfxml_AS_routing_Vivaldi:               model = &routing_models[SURF_MODEL_VIVALDI];break;
308     default: xbt_die("Not a valid model!!!");
309     break;
310   }
311
312   /* make a new routing component */
313   simgrid::surf::As *new_as = model->create();
314
315   new_as->p_modelDesc = model;
316   new_as->p_hierarchy = SURF_ROUTING_NULL;
317   new_as->p_name = xbt_strdup(AS->id);
318
319   simgrid::surf::RoutingEdge *info =
320     new simgrid::surf::RoutingEdgeImpl(xbt_strdup(new_as->p_name),
321                                             -1,
322                                             SURF_NETWORK_ELEMENT_AS,
323                                             current_routing);
324   if (current_routing == NULL && routing_platf->p_root == NULL) {
325
326     /* it is the first one */
327     new_as->p_routingFather = NULL;
328     routing_platf->p_root = new_as;
329     info->setId(-1);
330   } else if (current_routing != NULL && routing_platf->p_root != NULL) {
331
332     xbt_assert(!xbt_dict_get_or_null
333                (current_routing->p_routingSons, AS->id),
334                "The AS \"%s\" already exists", AS->id);
335     /* it is a part of the tree */
336     new_as->p_routingFather = current_routing;
337     /* set the father behavior */
338     if (current_routing->p_hierarchy == SURF_ROUTING_NULL)
339       current_routing->p_hierarchy = SURF_ROUTING_RECURSIVE;
340     /* add to the sons dictionary */
341     xbt_dict_set(current_routing->p_routingSons, AS->id,
342                  (void *) new_as, NULL);
343     /* add to the father element list */
344     info->setId(current_routing->parseAS(info));
345   } else {
346     THROWF(arg_error, 0, "All defined components must belong to a AS");
347   }
348
349   xbt_lib_set(as_router_lib, info->getName(), ROUTING_ASR_LEVEL,
350               (void *) info);
351   XBT_DEBUG("Having set name '%s' id '%d'", new_as->p_name, info->getId());
352
353   /* set the new current component of the tree */
354   current_routing = new_as;
355   current_routing->p_netElem = info;
356
357   simgrid::surf::routingEdgeCreatedCallbacks(info);
358   simgrid::surf::asCreatedCallbacks(new_as);
359 }
360
361 /**
362  * \brief Specify that the current description of AS is finished
363  *
364  * Once you've declared all the content of your AS, you have to close
365  * it with this call. Your AS is not usable until you call this function.
366  *
367  * @fixme: this call is not as robust as wanted: bad things WILL happen
368  * if you call it twice for the same AS, or if you forget calling it, or
369  * even if you add stuff to a closed AS
370  *
371  */
372 void routing_AS_end()
373 {
374
375   if (current_routing == NULL) {
376     THROWF(arg_error, 0, "Close an AS, but none was under construction");
377   } else {
378     if (current_routing->p_modelDesc->end)
379       current_routing->p_modelDesc->end(current_routing);
380     current_routing = current_routing->p_routingFather;
381   }
382 }
383
384 /* Aux Business methods */
385
386 /**
387  * \brief Get the AS father and the first elements of the chain
388  *
389  * \param src the source host name
390  * \param dst the destination host name
391  *
392  * Get the common father of the to processing units, and the first different
393  * father in the chain
394  */
395 static void elements_father(sg_routing_edge_t src, sg_routing_edge_t dst,
396                             AS_t * res_father,
397                             AS_t * res_src,
398                             AS_t * res_dst)
399 {
400   xbt_assert(src && dst, "bad parameters for \"elements_father\" method");
401 #define ELEMENTS_FATHER_MAXDEPTH 16     /* increase if it is not enough */
402   simgrid::surf::As *src_as, *dst_as;
403   simgrid::surf::As *path_src[ELEMENTS_FATHER_MAXDEPTH];
404   simgrid::surf::As *path_dst[ELEMENTS_FATHER_MAXDEPTH];
405   int index_src = 0;
406   int index_dst = 0;
407   simgrid::surf::As *current;
408   simgrid::surf::As *current_src;
409   simgrid::surf::As *current_dst;
410   simgrid::surf::As *father;
411
412   /* (1) find the as where the src and dst are located */
413   sg_routing_edge_t src_data = src;
414   sg_routing_edge_t dst_data = dst;
415   src_as = src_data->getRcComponent();
416   dst_as = dst_data->getRcComponent();
417 #ifndef NDEBUG
418   char* src_name = src_data->getName();
419   char* dst_name = dst_data->getName();
420 #endif
421
422   xbt_assert(src_as && dst_as,
423              "Ask for route \"from\"(%s) or \"to\"(%s) no found", src_name, dst_name);
424
425   /* (2) find the path to the root routing component */
426   for (current = src_as; current != NULL; current = current->p_routingFather) {
427     if (index_src >= ELEMENTS_FATHER_MAXDEPTH)
428       xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_src");
429     path_src[index_src++] = current;
430   }
431   for (current = dst_as; current != NULL; current = current->p_routingFather) {
432     if (index_dst >= ELEMENTS_FATHER_MAXDEPTH)
433       xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_dst");
434     path_dst[index_dst++] = current;
435   }
436
437   /* (3) find the common father */
438   do {
439     current_src = path_src[--index_src];
440     current_dst = path_dst[--index_dst];
441   } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
442
443   /* (4) they are not in the same routing component, make the path */
444   if (current_src == current_dst)
445     father = current_src;
446   else
447     father = path_src[index_src + 1];
448
449   /* (5) result generation */
450   *res_father = father;         /* first the common father of src and dst */
451   *res_src = current_src;       /* second the first different father of src */
452   *res_dst = current_dst;       /* three  the first different father of dst */
453
454 #undef ELEMENTS_FATHER_MAXDEPTH
455 }
456
457 /* Global Business methods */
458
459 /**
460  * \brief Recursive function for get_route_latency
461  *
462  * \param src the source host name
463  * \param dst the destination host name
464  * \param *route the route where the links are stored. It is either NULL or a ready to use dynar
465  * \param *latency the latency, if needed
466  *
467  * This function is called by "get_route" and "get_latency". It allows to walk
468  * recursively through the ASes tree.
469  */
470 static void _get_route_and_latency(
471   simgrid::surf::RoutingEdge *src, simgrid::surf::RoutingEdge *dst,
472   xbt_dynar_t * links, double *latency)
473 {
474   s_sg_platf_route_cbarg_t route = SG_PLATF_ROUTE_INITIALIZER;
475   memset(&route,0,sizeof(route));
476
477   xbt_assert(src && dst, "bad parameters for \"_get_route_latency\" method");
478   XBT_DEBUG("Solve route/latency  \"%s\" to \"%s\"", src->getName(), dst->getName());
479
480   /* Find how src and dst are interconnected */
481   simgrid::surf::As *common_father, *src_father, *dst_father;
482   elements_father(src, dst, &common_father, &src_father, &dst_father);
483   XBT_DEBUG("elements_father: common father '%s' src_father '%s' dst_father '%s'",
484       common_father->p_name, src_father->p_name, dst_father->p_name);
485
486   /* Check whether a direct bypass is defined */
487   sg_platf_route_cbarg_t e_route_bypass = NULL;
488   //FIXME:REMOVE:if (common_father->get_bypass_route)
489
490   e_route_bypass = common_father->getBypassRoute(src, dst, latency);
491
492   /* Common ancestor is kind enough to declare a bypass route from src to dst -- use it and bail out */
493   if (e_route_bypass) {
494     xbt_dynar_merge(links, &e_route_bypass->link_list);
495     generic_free_route(e_route_bypass);
496     return;
497   }
498
499   /* If src and dst are in the same AS, life is good */
500   if (src_father == dst_father) {       /* SURF_ROUTING_BASE */
501     route.link_list = *links;
502     common_father->getRouteAndLatency(src, dst, &route, latency);
503     // if vivaldi latency+=vivaldi(src,dst)
504     return;
505   }
506
507   /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
508
509   route.link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
510   // Find the net_card corresponding to father
511   simgrid::surf::RoutingEdge *src_father_net_elm = src_father->p_netElem;
512   simgrid::surf::RoutingEdge *dst_father_net_elm = dst_father->p_netElem;
513
514   common_father->getRouteAndLatency(src_father_net_elm, dst_father_net_elm,
515                                     &route, latency);
516
517   xbt_assert((route.gw_src != NULL) && (route.gw_dst != NULL),
518       "bad gateways for route from \"%s\" to \"%s\"", src->getName(), dst->getName());
519
520   sg_routing_edge_t src_gateway_net_elm = route.gw_src;
521   sg_routing_edge_t dst_gateway_net_elm = route.gw_dst;
522
523   /* If source gateway is not our source, we have to recursively find our way up to this point */
524   if (src != src_gateway_net_elm)
525     _get_route_and_latency(src, src_gateway_net_elm, links, latency);
526   xbt_dynar_merge(links, &route.link_list);
527
528   /* If dest gateway is not our destination, we have to recursively find our way from this point */
529   if (dst_gateway_net_elm != dst)
530     _get_route_and_latency(dst_gateway_net_elm, dst, links, latency);
531
532   // if vivaldi latency+=vivaldi(src_gateway,dst_gateway)
533 }
534
535 AS_t surf_platf_get_root(routing_platf_t platf){
536   return platf->p_root;
537 }
538
539 e_surf_network_element_type_t surf_routing_edge_get_rc_type(sg_routing_edge_t edge){
540   return edge->getRcType();
541 }
542
543 namespace simgrid {
544 namespace surf {
545
546 /**
547  * \brief Find a route between hosts
548  *
549  * \param src the network_element_t for src host
550  * \param dst the network_element_t for dst host
551  * \param route where to store the list of links.
552  *              If *route=NULL, create a short lived dynar. Else, fill the provided dynar
553  * \param latency where to store the latency experienced on the path (or NULL if not interested)
554  *                It is the caller responsability to initialize latency to 0 (we add to provided route)
555  * \pre route!=NULL
556  *
557  * walk through the routing components tree and find a route between hosts
558  * by calling the differents "get_route" functions in each routing component.
559  */
560 void RoutingPlatf::getRouteAndLatency(
561   simgrid::surf::RoutingEdge *src, simgrid::surf::RoutingEdge *dst,
562   xbt_dynar_t* route, double *latency)
563 {
564   XBT_DEBUG("routing_get_route_and_latency from %s to %s", src->getName(), dst->getName());
565   if (!*route) {
566     xbt_dynar_reset(routing_platf->p_lastRoute);
567     *route = routing_platf->p_lastRoute;
568   }
569
570   _get_route_and_latency(src, dst, route, latency);
571
572   xbt_assert(!latency || *latency >= 0.0,
573              "negative latency on route between \"%s\" and \"%s\"", src->getName(), dst->getName());
574 }
575
576 xbt_dynar_t RoutingPlatf::getOneLinkRoutes(){
577   return recursiveGetOneLinkRoutes(p_root);
578 }
579
580 xbt_dynar_t RoutingPlatf::recursiveGetOneLinkRoutes(As *rc)
581 {
582   xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
583
584   //adding my one link routes
585   xbt_dynar_t onelink_mine = rc->getOneLinkRoutes();
586   if (onelink_mine)
587     xbt_dynar_merge(&ret,&onelink_mine);
588
589   //recursing
590   char *key;
591   xbt_dict_cursor_t cursor = NULL;
592   AS_t rc_child;
593   xbt_dict_foreach(rc->p_routingSons, cursor, key, rc_child) {
594     xbt_dynar_t onelink_child = recursiveGetOneLinkRoutes(rc_child);
595     if (onelink_child)
596       xbt_dynar_merge(&ret,&onelink_child);
597   }
598   return ret;
599 }
600
601 }
602 }
603
604 e_surf_network_element_type_t routing_get_network_element_type(const char *name)
605 {
606   simgrid::surf::RoutingEdge *rc = sg_routing_edge_by_name_or_null(name);
607   if (rc)
608     return rc->getRcType();
609
610   return SURF_NETWORK_ELEMENT_NULL;
611 }
612
613 /**
614  * \brief Generic method: create the global routing schema
615  *
616  * Make a global routing structure and set all the parsing functions.
617  */
618 void routing_model_create( void *loopback)
619 {
620   /* config the uniq global routing */
621   routing_platf = new simgrid::surf::RoutingPlatf();
622   routing_platf->p_root = NULL;
623   routing_platf->p_loopback = loopback;
624   routing_platf->p_lastRoute = xbt_dynar_new(sizeof(sg_routing_link_t),NULL);
625   /* no current routing at moment */
626   current_routing = NULL;
627 }
628
629
630 /* ************************************************** */
631 /* ********** PATERN FOR NEW ROUTING **************** */
632
633 /* The minimal configuration of a new routing model need the next functions,
634  * also you need to set at the start of the file, the new model in the model
635  * list. Remember keep the null ending of the list.
636  */
637 /*** Routing model structure ***/
638 // typedef struct {
639 //   s_routing_component_t generic_routing;
640 //   /* things that your routing model need */
641 // } s_routing_component_NEW_t,*routing_component_NEW_t;
642
643 /*** Parse routing model functions ***/
644 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
645 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
646 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
647 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
648 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
649
650 /*** Business methods ***/
651 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
652 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
653 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
654
655 /*** Creation routing model functions ***/
656 // static void* model_NEW_create(void) {
657 //   routing_component_NEW_t new_component =  xbt_new0(s_routing_component_NEW_t,1);
658 //   new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
659 //   new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
660 //   new_component->generic_routing.set_route = model_NEW_set_route;
661 //   new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
662 //   new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
663 //   new_component->generic_routing.get_route = NEW_get_route;
664 //   new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
665 //   new_component->generic_routing.finalize = NEW_finalize;
666 //   /* initialization of internal structures */
667 //   return new_component;
668 // } /* mandatory */
669 // static void  model_NEW_load(void) {}   /* mandatory */
670 // static void  model_NEW_unload(void) {} /* mandatory */
671 // static void  model_NEW_end(void) {}    /* mandatory */
672
673 /* ************************************************************************** */
674 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
675
676 void routing_cluster_add_backbone(void* bb) {
677   xbt_assert(current_routing->p_modelDesc == &routing_models[SURF_MODEL_CLUSTER],
678         "You have to be in model Cluster to use tag backbone!");
679   xbt_assert(!static_cast<simgrid::surf::AsCluster*>(current_routing)->p_backbone, "The backbone link is already defined!");
680   static_cast<simgrid::surf::AsCluster*>(current_routing)->p_backbone =
681     static_cast<simgrid::surf::Link*>(bb);
682   XBT_DEBUG("Add a backbone to AS '%s'", current_routing->p_name);
683 }
684
685 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet)
686 {
687   int start, end, i;
688   char *groups , *host_id , *link_id = NULL;
689   unsigned int iter;
690   xbt_dynar_t radical_elements;
691   xbt_dynar_t radical_ends;
692
693   //Make all hosts
694   radical_elements = xbt_str_split(cabinet->radical, ",");
695   xbt_dynar_foreach(radical_elements, iter, groups) {
696
697     radical_ends = xbt_str_split(groups, "-");
698     start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
699
700     switch (xbt_dynar_length(radical_ends)) {
701     case 1:
702       end = start;
703       break;
704     case 2:
705       end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
706       break;
707     default:
708       surf_parse_error("Malformed radical");
709       break;
710     }
711     s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
712     memset(&host, 0, sizeof(host));
713     host.initial_state = SURF_RESOURCE_ON;
714     host.pstate        = 0;
715     host.speed_scale   = 1.0;
716     host.core_amount   = 1;
717
718     s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
719     memset(&link, 0, sizeof(link));
720     link.state     = SURF_RESOURCE_ON;
721     link.policy    = SURF_LINK_FULLDUPLEX;
722     link.latency   = cabinet->lat;
723     link.bandwidth = cabinet->bw;
724
725     s_sg_platf_host_link_cbarg_t host_link = SG_PLATF_HOST_LINK_INITIALIZER;
726     memset(&host_link, 0, sizeof(host_link));
727
728     for (i = start; i <= end; i++) {
729       host_id                      = bprintf("%s%d%s",cabinet->prefix,i,cabinet->suffix);
730       link_id                      = bprintf("link_%s%d%s",cabinet->prefix,i,cabinet->suffix);
731       host.id                      = host_id;
732       link.id                      = link_id;
733       host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
734       xbt_dynar_push(host.speed_peak,&cabinet->speed);
735       sg_platf_new_host(&host);
736       xbt_dynar_free(&host.speed_peak);
737       sg_platf_new_link(&link);
738
739       char* link_up       = bprintf("%s_UP",link_id);
740       char* link_down     = bprintf("%s_DOWN",link_id);
741       host_link.id        = host_id;
742       host_link.link_up   = link_up;
743       host_link.link_down = link_down;
744       sg_platf_new_host_link(&host_link);
745
746       free(host_id);
747       free(link_id);
748       free(link_up);
749       free(link_down);
750     }
751
752     xbt_dynar_free(&radical_ends);
753   }
754   xbt_dynar_free(&radical_elements);
755 }
756
757 void routing_new_cluster(sg_platf_cluster_cbarg_t cluster)
758 {
759   using simgrid::surf::AsCluster;
760   using simgrid::surf::AsClusterTorus;
761   using simgrid::surf::AsClusterFatTree;
762
763   char *host_id, *groups, *link_id = NULL;
764   xbt_dict_t patterns = NULL;
765   int rankId=0;
766
767   s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
768   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
769
770   unsigned int iter;
771   int start, end, i;
772   xbt_dynar_t radical_elements;
773   xbt_dynar_t radical_ends;
774
775   if ((cluster->availability_trace && strcmp(cluster->availability_trace, ""))
776       || (cluster->state_trace && strcmp(cluster->state_trace, ""))) {
777     patterns = xbt_dict_new_homogeneous(xbt_free_f);
778     xbt_dict_set(patterns, "id", xbt_strdup(cluster->id), NULL);
779     xbt_dict_set(patterns, "prefix", xbt_strdup(cluster->prefix), NULL);
780     xbt_dict_set(patterns, "suffix", xbt_strdup(cluster->suffix), NULL);
781   }
782
783   /* parse the topology attribute. If we are not in a flat cluster,
784    * switch to the right mode and initialize the routing with
785    * the parameters in topo_parameters attribute
786    */
787   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
788   AS.id = cluster->id;
789
790   if(cluster->topology == SURF_CLUSTER_TORUS){
791     XBT_DEBUG("<AS id=\"%s\"\trouting=\"Torus_Cluster\">", cluster->id);
792     AS.routing = A_surfxml_AS_routing_Cluster___torus;
793     sg_platf_new_AS_begin(&AS);
794     ((AsClusterTorus*)current_routing)->parse_specific_arguments(cluster);
795   }
796   else if (cluster->topology == SURF_CLUSTER_FAT_TREE) {
797     XBT_DEBUG("<AS id=\"%s\"\trouting=\"Fat_Tree_Cluster\">", cluster->id);
798     AS.routing = A_surfxml_AS_routing_Cluster___fat___tree;
799     sg_platf_new_AS_begin(&AS);
800     ((AsClusterFatTree*)current_routing)->parse_specific_arguments(cluster);
801   }
802
803   else{
804     XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", cluster->id);
805     AS.routing = A_surfxml_AS_routing_Cluster;
806     sg_platf_new_AS_begin(&AS);
807   }
808
809   if(cluster->loopback_bw!=0 || cluster->loopback_lat!=0){
810       ((AsCluster*)current_routing)->p_nb_links_per_node++;
811       ((AsCluster*)current_routing)->p_has_loopback=1;
812   }
813
814   if(cluster->limiter_link!=0){
815       ((AsCluster*)current_routing)->p_nb_links_per_node++;
816       ((AsCluster*)current_routing)->p_has_limiter=1;
817   }
818
819
820
821   current_routing->p_linkUpDownList
822             = xbt_dynar_new(sizeof(s_surf_parsing_link_up_down_t),NULL);
823
824   //Make all hosts
825   radical_elements = xbt_str_split(cluster->radical, ",");
826   xbt_dynar_foreach(radical_elements, iter, groups) {
827
828     radical_ends = xbt_str_split(groups, "-");
829     start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
830
831     switch (xbt_dynar_length(radical_ends)) {
832     case 1:
833       end = start;
834       break;
835     case 2:
836       end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
837       break;
838     default:
839       surf_parse_error("Malformed radical");
840       break;
841     }
842     for (i = start; i <= end; i++) {
843       host_id =
844           bprintf("%s%d%s", cluster->prefix, i, cluster->suffix);
845       link_id = bprintf("%s_link_%d", cluster->id, i);
846
847       XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, cluster->speed);
848
849       memset(&host, 0, sizeof(host));
850       host.id = host_id;
851       if ((cluster->properties != NULL) && (!xbt_dict_is_empty(cluster->properties))) {
852           xbt_dict_cursor_t cursor=NULL;
853           char *key,*data;
854           host.properties = xbt_dict_new();
855
856           xbt_dict_foreach(cluster->properties,cursor,key,data) {
857                   xbt_dict_set(host.properties, key, xbt_strdup(data),free);
858           }
859       }
860       if (cluster->availability_trace && strcmp(cluster->availability_trace, "")) {
861         xbt_dict_set(patterns, "radical", bprintf("%d", i), NULL);
862         char *avail_file = xbt_str_varsubst(cluster->availability_trace, patterns);
863         XBT_DEBUG("\tavailability_file=\"%s\"", avail_file);
864         host.speed_trace = tmgr_trace_new_from_file(avail_file);
865         xbt_free(avail_file);
866       } else {
867         XBT_DEBUG("\tavailability_file=\"\"");
868       }
869
870       if (cluster->state_trace && strcmp(cluster->state_trace, "")) {
871         char *avail_file = xbt_str_varsubst(cluster->state_trace, patterns);
872         XBT_DEBUG("\tstate_file=\"%s\"", avail_file);
873         host.state_trace = tmgr_trace_new_from_file(avail_file);
874         xbt_free(avail_file);
875       } else {
876         XBT_DEBUG("\tstate_file=\"\"");
877       }
878
879       host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
880       xbt_dynar_push(host.speed_peak,&cluster->speed);
881       host.pstate = 0;
882
883       //host.power_peak = cluster->power;
884       host.speed_scale = 1.0;
885       host.core_amount = cluster->core_amount;
886       host.initial_state = SURF_RESOURCE_ON;
887       host.coord = "";
888       sg_platf_new_host(&host);
889       xbt_dynar_free(&host.speed_peak);
890       XBT_DEBUG("</host>");
891
892       XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,
893                 cluster->bw, cluster->lat);
894
895
896       s_surf_parsing_link_up_down_t info_lim, info_loop;
897       // All links are saved in a matrix;
898       // every row describes a single node; every node
899       // may have multiple links.
900       // the first column may store a link from x to x if p_has_loopback is set
901       // the second column may store a limiter link if p_has_limiter is set
902       // other columns are to store one or more link for the node
903
904       //add a loopback link
905       if(cluster->loopback_bw!=0 || cluster->loopback_lat!=0){
906         char *tmp_link = bprintf("%s_loopback", link_id);
907         XBT_DEBUG("<loopback\tid=\"%s\"\tbw=\"%f\"/>", tmp_link,
908                 cluster->limiter_link);
909
910
911         memset(&link, 0, sizeof(link));
912         link.id        = tmp_link;
913         link.bandwidth = cluster->loopback_bw;
914         link.latency   = cluster->loopback_lat;
915         link.state     = SURF_RESOURCE_ON;
916         link.policy    = SURF_LINK_FATPIPE;
917         sg_platf_new_link(&link);
918         info_loop.link_up   = Link::byName(tmp_link);
919         info_loop.link_down = info_loop.link_up;
920         free(tmp_link);
921         xbt_dynar_set(current_routing->p_linkUpDownList,
922           rankId*(static_cast<AsCluster*>(current_routing))->p_nb_links_per_node, &info_loop);
923       }
924
925       //add a limiter link (shared link to account for maximal bandwidth of the node)
926       if(cluster->limiter_link!=0){
927         char *tmp_link = bprintf("%s_limiter", link_id);
928         XBT_DEBUG("<limiter\tid=\"%s\"\tbw=\"%f\"/>", tmp_link,
929                 cluster->limiter_link);
930
931
932         memset(&link, 0, sizeof(link));
933         link.id = tmp_link;
934         link.bandwidth = cluster->limiter_link;
935         link.latency = 0;
936         link.state = SURF_RESOURCE_ON;
937         link.policy = SURF_LINK_SHARED;
938         sg_platf_new_link(&link);
939         info_lim.link_up = Link::byName(tmp_link);
940         info_lim.link_down = info_lim.link_up;
941         free(tmp_link);
942         auto as_cluster = static_cast<AsCluster*>(current_routing);
943         xbt_dynar_set(current_routing->p_linkUpDownList,
944             rankId*(as_cluster)->p_nb_links_per_node + as_cluster->p_has_loopback ,
945             &info_lim);
946
947       }
948
949
950       //call the cluster function that adds the others links
951       if (cluster->topology == SURF_CLUSTER_FAT_TREE) {
952         ((AsClusterFatTree*) current_routing)->addProcessingNode(i);
953       }
954       else {
955       static_cast<AsCluster*>(current_routing)->create_links_for_node(cluster, i, rankId, rankId*
956                   static_cast<AsCluster*>(current_routing)->p_nb_links_per_node
957           + static_cast<AsCluster*>(current_routing)->p_has_loopback
958           + static_cast<AsCluster*>(current_routing)->p_has_limiter );
959       }
960       xbt_free(link_id);
961       xbt_free(host_id);
962       rankId++;
963     }
964
965     xbt_dynar_free(&radical_ends);
966   }
967   xbt_dynar_free(&radical_elements);
968
969   // For fat trees, the links must be created once all nodes have been added
970   if(cluster->topology == SURF_CLUSTER_FAT_TREE) {
971     static_cast<simgrid::surf::AsClusterFatTree*>(current_routing)->create_links();
972   }
973   // Add a router. It is magically used thanks to the way in which surf_routing_cluster is written,
974   // and it's very useful to connect clusters together
975   XBT_DEBUG(" ");
976   XBT_DEBUG("<router id=\"%s\"/>", cluster->router_id);
977   char *newid = NULL;
978   s_sg_platf_router_cbarg_t router = SG_PLATF_ROUTER_INITIALIZER;
979   memset(&router, 0, sizeof(router));
980   router.id = cluster->router_id;
981   router.coord = "";
982   if (!router.id || !strcmp(router.id, ""))
983     router.id = newid =
984         bprintf("%s%s_router%s", cluster->prefix, cluster->id,
985                 cluster->suffix);
986   sg_platf_new_router(&router);
987   ((AsCluster*)current_routing)->p_router = (simgrid::surf::RoutingEdge*) xbt_lib_get_or_null(as_router_lib, router.id, ROUTING_ASR_LEVEL);
988   free(newid);
989
990   //Make the backbone
991   if ((cluster->bb_bw != 0) || (cluster->bb_lat != 0)) {
992     char *link_backbone = bprintf("%s_backbone", cluster->id);
993     XBT_DEBUG("<link\tid=\"%s\" bw=\"%f\" lat=\"%f\"/>", link_backbone,
994               cluster->bb_bw, cluster->bb_lat);
995
996     memset(&link, 0, sizeof(link));
997     link.id        = link_backbone;
998     link.bandwidth = cluster->bb_bw;
999     link.latency   = cluster->bb_lat;
1000     link.state     = SURF_RESOURCE_ON;
1001     link.policy    = cluster->bb_sharing_policy;
1002
1003     sg_platf_new_link(&link);
1004
1005     routing_cluster_add_backbone(Link::byName(link_backbone));
1006
1007     free(link_backbone);
1008   }
1009
1010   XBT_DEBUG("</AS>");
1011   sg_platf_new_AS_end();
1012   XBT_DEBUG(" ");
1013   xbt_dict_free(&patterns); // no op if it were never set
1014 }
1015
1016 static void routing_parse_postparse(void) {
1017   xbt_dict_free(&random_value);
1018 }
1019
1020 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer)
1021 {
1022   using simgrid::surf::RoutingEdge;
1023   using simgrid::surf::AsCluster;
1024
1025   char *host_id = NULL;
1026   char *link_id = NULL;
1027   char *router_id = NULL;
1028
1029   XBT_DEBUG(" ");
1030   host_id = HOST_PEER(peer->id);
1031   link_id = LINK_PEER(peer->id);
1032   router_id = ROUTER_PEER(peer->id);
1033
1034   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", peer->id);
1035   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
1036   AS.id                    = peer->id;
1037   AS.routing               = A_surfxml_AS_routing_Cluster;
1038   sg_platf_new_AS_begin(&AS);
1039
1040   current_routing->p_linkUpDownList = xbt_dynar_new(sizeof(s_surf_parsing_link_up_down_t),NULL);
1041
1042   XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\"/>", host_id, peer->speed);
1043   s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
1044   memset(&host, 0, sizeof(host));
1045   host.initial_state = SURF_RESOURCE_ON;
1046   host.id = host_id;
1047
1048   host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
1049   xbt_dynar_push(host.speed_peak,&peer->speed);
1050   host.pstate = 0;
1051   //host.power_peak = peer->power;
1052   host.speed_scale = 1.0;
1053   host.speed_trace = peer->availability_trace;
1054   host.state_trace = peer->state_trace;
1055   host.core_amount = 1;
1056   sg_platf_new_host(&host);
1057   xbt_dynar_free(&host.speed_peak);
1058
1059   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
1060   memset(&link, 0, sizeof(link));
1061   link.state   = SURF_RESOURCE_ON;
1062   link.policy  = SURF_LINK_SHARED;
1063   link.latency = peer->lat;
1064
1065   char* link_up = bprintf("%s_UP",link_id);
1066   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_up,
1067             peer->bw_out, peer->lat);
1068   link.id = link_up;
1069   link.bandwidth = peer->bw_out;
1070   sg_platf_new_link(&link);
1071
1072   char* link_down = bprintf("%s_DOWN",link_id);
1073   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_down,
1074             peer->bw_in, peer->lat);
1075   link.id = link_down;
1076   link.bandwidth = peer->bw_in;
1077   sg_platf_new_link(&link);
1078
1079   XBT_DEBUG("<host_link\tid=\"%s\"\tup=\"%s\"\tdown=\"%s\" />", host_id,link_up,link_down);
1080   s_sg_platf_host_link_cbarg_t host_link = SG_PLATF_HOST_LINK_INITIALIZER;
1081   memset(&host_link, 0, sizeof(host_link));
1082   host_link.id        = host_id;
1083   host_link.link_up   = link_up;
1084   host_link.link_down = link_down;
1085   sg_platf_new_host_link(&host_link);
1086
1087   XBT_DEBUG("<router id=\"%s\"/>", router_id);
1088   s_sg_platf_router_cbarg_t router = SG_PLATF_ROUTER_INITIALIZER;
1089   memset(&router, 0, sizeof(router));
1090   router.id = router_id;
1091   router.coord = peer->coord;
1092   sg_platf_new_router(&router);
1093   static_cast<AsCluster*>(current_routing)->p_router = static_cast<RoutingEdge*>(xbt_lib_get_or_null(as_router_lib, router.id, ROUTING_ASR_LEVEL));
1094
1095   XBT_DEBUG("</AS>");
1096   sg_platf_new_AS_end();
1097   XBT_DEBUG(" ");
1098
1099   //xbt_dynar_free(&tab_elements_num);
1100   free(router_id);
1101   free(host_id);
1102   free(link_id);
1103   free(link_up);
1104   free(link_down);
1105 }
1106
1107 // static void routing_parse_Srandom(void)
1108 // {
1109 //   double mean, std, min, max, seed;
1110 //   char *random_id = A_surfxml_random_id;
1111 //   char *random_radical = A_surfxml_random_radical;
1112 //   char *rd_name = NULL;
1113 //   char *rd_value;
1114 //   mean = surf_parse_get_double(A_surfxml_random_mean);
1115 //   std = surf_parse_get_double(A_surfxml_random_std___deviation);
1116 //   min = surf_parse_get_double(A_surfxml_random_min);
1117 //   max = surf_parse_get_double(A_surfxml_random_max);
1118 //   seed = surf_parse_get_double(A_surfxml_random_seed);
1119
1120 //   double res = 0;
1121 //   int i = 0;
1122 //   random_data_t random = xbt_new0(s_random_data_t, 1);
1123 //   char *tmpbuf;
1124
1125 //   xbt_dynar_t radical_elements;
1126 //   unsigned int iter;
1127 //   char *groups;
1128 //   int start, end;
1129 //   xbt_dynar_t radical_ends;
1130
1131 //   switch (A_surfxml_random_generator) {
1132 //   case AU_surfxml_random_generator:
1133 //   case A_surfxml_random_generator_NONE:
1134 //     random->generator = NONE;
1135 //     break;
1136 //   case A_surfxml_random_generator_DRAND48:
1137 //     random->generator = DRAND48;
1138 //     break;
1139 //   case A_surfxml_random_generator_RAND:
1140 //     random->generator = RAND;
1141 //     break;
1142 //   case A_surfxml_random_generator_RNGSTREAM:
1143 //     random->generator = RNGSTREAM;
1144 //     break;
1145 //   default:
1146 //     surf_parse_error("Invalid random generator");
1147 //     break;
1148 //   }
1149 //   random->seed = seed;
1150 //   random->min = min;
1151 //   random->max = max;
1152
1153 //   /* Check user stupidities */
1154 //   if (max < min)
1155 //     THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
1156 //   if (mean < min)
1157 //     THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean, min);
1158 //   if (mean > max)
1159 //     THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean, max);
1160
1161 //   /* normalize the mean and standard deviation before storing */
1162 //   random->mean = (mean - min) / (max - min);
1163 //   random->std = std / (max - min);
1164
1165 //   if (random->mean * (1 - random->mean) < random->std * random->std)
1166 //     THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
1167 //            random->mean, random->std);
1168
1169 //   XBT_DEBUG
1170 //       ("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
1171 //        random_id, random->min, random->max, random->mean, random->std,
1172 //        (int)random->generator, random->seed, random_radical);
1173
1174 //   if (!random_value)
1175 //     random_value = xbt_dict_new_homogeneous(free);
1176
1177 //   if (!strcmp(random_radical, "")) {
1178 //     res = random_generate(random);
1179 //     rd_value = bprintf("%f", res);
1180 //     xbt_dict_set(random_value, random_id, rd_value, NULL);
1181 //   } else {
1182 //     radical_elements = xbt_str_split(random_radical, ",");
1183 //     xbt_dynar_foreach(radical_elements, iter, groups) {
1184 //       radical_ends = xbt_str_split(groups, "-");
1185 //       switch (xbt_dynar_length(radical_ends)) {
1186 //       case 1:
1187 //         xbt_assert(!xbt_dict_get_or_null(random_value, random_id),
1188 //                    "Custom Random '%s' already exists !", random_id);
1189 //         res = random_generate(random);
1190 //         tmpbuf =
1191 //             bprintf("%s%d", random_id,
1192 //                     atoi(xbt_dynar_getfirst_as(radical_ends, char *)));
1193 //         xbt_dict_set(random_value, tmpbuf, bprintf("%f", res), NULL);
1194 //         xbt_free(tmpbuf);
1195 //         break;
1196
1197 //       case 2:
1198 //         start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1199 //         end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1200 //         for (i = start; i <= end; i++) {
1201 //           xbt_assert(!xbt_dict_get_or_null(random_value, random_id),
1202 //                      "Custom Random '%s' already exists !", bprintf("%s%d",
1203 //                                                                     random_id,
1204 //                                                                     i));
1205 //           res = random_generate(random);
1206 //           tmpbuf = bprintf("%s%d", random_id, i);
1207 //           xbt_dict_set(random_value, tmpbuf, bprintf("%f", res), NULL);
1208 //           xbt_free(tmpbuf);
1209 //         }
1210 //         break;
1211 //       default:
1212 //         XBT_CRITICAL("Malformed radical");
1213 //         break;
1214 //       }
1215 //       res = random_generate(random);
1216 //       rd_name = bprintf("%s_router", random_id);
1217 //       rd_value = bprintf("%f", res);
1218 //       xbt_dict_set(random_value, rd_name, rd_value, NULL);
1219
1220 //       xbt_dynar_free(&radical_ends);
1221 //     }
1222 //     free(rd_name);
1223 //     xbt_dynar_free(&radical_elements);
1224 //   }
1225 // }
1226
1227 static void check_disk_attachment()
1228 {
1229   xbt_lib_cursor_t cursor;
1230   char *key;
1231   void **data;
1232   simgrid::surf::RoutingEdge *host_elm;
1233   xbt_lib_foreach(storage_lib, cursor, key, data) {
1234     if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != NULL) {
1235           simgrid::surf::Storage *storage = static_cast<simgrid::surf::Storage*>(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL));
1236           host_elm = sg_routing_edge_by_name_or_null(storage->p_attach);
1237           if(!host_elm)
1238                   surf_parse_error("Unable to attach storage %s: host %s doesn't exist.", storage->getName(), storage->p_attach);
1239     }
1240   }
1241 }
1242
1243 void routing_register_callbacks()
1244 {
1245   sg_platf_postparse_add_cb(routing_parse_postparse);
1246   sg_platf_postparse_add_cb(check_disk_attachment);
1247
1248   instr_routing_define_callbacks();
1249 }
1250
1251 /**
1252  * \brief Recursive function for finalize
1253  *
1254  * \param rc the source host name
1255  *
1256  * This fuction is call by "finalize". It allow to finalize the
1257  * AS or routing components. It delete all the structures.
1258  */
1259 static void finalize_rec(simgrid::surf::As *as) {
1260   xbt_dict_cursor_t cursor = NULL;
1261   char *key;
1262   AS_t elem;
1263
1264   xbt_dict_foreach(as->p_routingSons, cursor, key, elem) {
1265     finalize_rec(elem);
1266   }
1267
1268   delete as;;
1269 }
1270
1271 /** \brief Frees all memory allocated by the routing module */
1272 void routing_exit(void) {
1273   delete routing_platf;
1274 }
1275
1276 namespace simgrid {
1277 namespace surf {
1278
1279 RoutingPlatf::~RoutingPlatf()
1280 {
1281         xbt_dynar_free(&p_lastRoute);
1282         finalize_rec(p_root);
1283 }
1284
1285 }
1286 }
1287
1288 AS_t surf_AS_get_routing_root() {
1289   return routing_platf->p_root;
1290 }
1291
1292 const char *surf_AS_get_name(simgrid::surf::As *as) {
1293   return as->p_name;
1294 }
1295
1296 static simgrid::surf::As *surf_AS_recursive_get_by_name(
1297   simgrid::surf::As *current, const char * name)
1298 {
1299   xbt_dict_cursor_t cursor = NULL;
1300   char *key;
1301   AS_t elem;
1302   simgrid::surf::As *tmp = NULL;
1303
1304   if(!strcmp(current->p_name, name))
1305     return current;
1306
1307   xbt_dict_foreach(current->p_routingSons, cursor, key, elem) {
1308     tmp = surf_AS_recursive_get_by_name(elem, name);
1309     if(tmp != NULL ) {
1310         break;
1311     }
1312   }
1313   return tmp;
1314 }
1315
1316 simgrid::surf::As *surf_AS_get_by_name(const char * name)
1317 {
1318   simgrid::surf::As *as = surf_AS_recursive_get_by_name(routing_platf->p_root, name);
1319   if(as == NULL)
1320     XBT_WARN("Impossible to find an AS with name %s, please check your input", name);
1321   return as;
1322 }
1323
1324 xbt_dict_t surf_AS_get_routing_sons(simgrid::surf::As *as)
1325 {
1326   return as->p_routingSons;
1327 }
1328
1329 const char *surf_AS_get_model(simgrid::surf::As *as)
1330 {
1331   return as->p_modelDesc->name;
1332 }
1333
1334 xbt_dynar_t surf_AS_get_hosts(simgrid::surf::As *as)
1335 {
1336   xbt_dynar_t elms = as->p_indexNetworkElm;
1337   sg_routing_edge_t relm;
1338   xbt_dictelm_t delm;
1339   int index;
1340   int count = xbt_dynar_length(elms);
1341   xbt_dynar_t res =  xbt_dynar_new(sizeof(xbt_dictelm_t), NULL);
1342   for (index = 0; index < count; index++) {
1343      relm = xbt_dynar_get_as(elms, index, simgrid::surf::RoutingEdge*);
1344      delm = xbt_lib_get_elm_or_null(host_lib, relm->getName());
1345      if (delm!=NULL) {
1346        xbt_dynar_push(res, &delm);
1347      }
1348   }
1349   return res;
1350 }
1351
1352 void surf_AS_get_graph(AS_t as, xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges) {
1353   as->getGraph(graph, nodes, edges);
1354 }