Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename facets to extensions for clarity sake
[simgrid.git] / src / surf / surf_routing_vivaldi.cpp
1 /* Copyright (c) 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_vivaldi.hpp"
8 #include "network_interface.hpp"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_vivaldi, surf, "Routing part of surf");
11
12 static XBT_INLINE double euclidean_dist_comp(int index, xbt_dynar_t src, xbt_dynar_t dst) {
13   double src_coord, dst_coord;
14
15   src_coord = xbt_dynar_get_as(src, index, double);
16   dst_coord = xbt_dynar_get_as(dst, index, double);
17
18   return (src_coord-dst_coord)*(src_coord-dst_coord);
19 }
20
21 AS_t model_vivaldi_create(void)
22 {
23   return new simgrid::surf::AsVivaldi();
24 }
25
26 namespace simgrid {
27 namespace surf {
28
29 void AsVivaldi::getRouteAndLatency(RoutingEdge *src, RoutingEdge *dst, sg_platf_route_cbarg_t route, double *lat)
30 {
31   s_surf_parsing_link_up_down_t info;
32
33   XBT_DEBUG("vivaldi_get_route_and_latency from '%s'[%d] '%s'[%d]",
34                   src->getName(), src->getId(), dst->getName(), dst->getId());
35
36   if(src->getRcType() == SURF_NETWORK_ELEMENT_AS) {
37     char *src_name = ROUTER_PEER(src->getName());
38     char *dst_name = ROUTER_PEER(dst->getName());
39     route->gw_src = (sg_routing_edge_t) xbt_lib_get_or_null(as_router_lib, src_name, ROUTING_ASR_LEVEL);
40     route->gw_dst = (sg_routing_edge_t) xbt_lib_get_or_null(as_router_lib, dst_name, ROUTING_ASR_LEVEL);
41     xbt_free(src_name);
42     xbt_free(dst_name);
43   }
44
45   double euclidean_dist;
46   xbt_dynar_t src_ctn, dst_ctn;
47   char *tmp_src_name, *tmp_dst_name;
48
49   if(src->getRcType() == SURF_NETWORK_ELEMENT_HOST){
50     tmp_src_name = HOST_PEER(src->getName());
51
52     if(p_linkUpDownList){
53       info = xbt_dynar_get_as(p_linkUpDownList, src->getId(), s_surf_parsing_link_up_down_t);
54       if(info.link_up) { // link up
55         xbt_dynar_push_as(route->link_list, void*, info.link_up);
56         if (lat)
57           *lat += static_cast<Link*>(info.link_up)->getLatency();
58       }
59     }
60     src_ctn = (xbt_dynar_t) simgrid::Host::by_name_or_create(tmp_src_name)->extension(COORD_HOST_LEVEL);
61     if (src_ctn == nullptr)
62       src_ctn = (xbt_dynar_t) simgrid::Host::by_name_or_create(src->getName())->extension(COORD_HOST_LEVEL);
63   }
64   else if(src->getRcType() == SURF_NETWORK_ELEMENT_ROUTER || src->getRcType() == SURF_NETWORK_ELEMENT_AS){
65     tmp_src_name = ROUTER_PEER(src->getName());
66     src_ctn = (xbt_dynar_t) xbt_lib_get_or_null(as_router_lib, tmp_src_name, COORD_ASR_LEVEL);
67   }
68   else{
69     THROW_IMPOSSIBLE;
70   }
71
72   if(dst->getRcType() == SURF_NETWORK_ELEMENT_HOST){
73     tmp_dst_name = HOST_PEER(dst->getName());
74
75     if(p_linkUpDownList){
76       info = xbt_dynar_get_as(p_linkUpDownList, dst->getId(), s_surf_parsing_link_up_down_t);
77       if(info.link_down) { // link down
78         xbt_dynar_push_as(route->link_list,void*,info.link_down);
79         if (lat)
80           *lat += static_cast<Link*>(info.link_down)->getLatency();
81       }
82     }
83     dst_ctn = (xbt_dynar_t) simgrid::Host::by_name_or_create(tmp_dst_name)
84       ->extension(COORD_HOST_LEVEL);
85     if (dst_ctn == nullptr)
86       dst_ctn = (xbt_dynar_t) simgrid::Host::by_name_or_create(dst->getName())
87         ->extension(COORD_HOST_LEVEL);
88   }
89   else if(dst->getRcType() == SURF_NETWORK_ELEMENT_ROUTER || dst->getRcType() == SURF_NETWORK_ELEMENT_AS){
90     tmp_dst_name = ROUTER_PEER(dst->getName());
91     dst_ctn = (xbt_dynar_t) xbt_lib_get_or_null(as_router_lib, tmp_dst_name, COORD_ASR_LEVEL);
92   }
93   else{
94     THROW_IMPOSSIBLE;
95   }
96
97   xbt_assert(src_ctn,"No coordinate found for element '%s'",tmp_src_name);
98   xbt_assert(dst_ctn,"No coordinate found for element '%s'",tmp_dst_name);
99   free(tmp_src_name);
100   free(tmp_dst_name);
101
102   euclidean_dist = sqrt (euclidean_dist_comp(0,src_ctn,dst_ctn)+euclidean_dist_comp(1,src_ctn,dst_ctn))
103                       + fabs(xbt_dynar_get_as(src_ctn, 2, double))+fabs(xbt_dynar_get_as(dst_ctn, 2, double));
104
105   if (lat){
106     XBT_DEBUG("Updating latency %f += %f",*lat,euclidean_dist);
107     *lat += euclidean_dist / 1000.0; //From .ms to .s
108   }
109 }
110
111 int AsVivaldi::parsePU(RoutingEdge *elm) {
112   XBT_DEBUG("Load process unit \"%s\"", elm->getName());
113   xbt_dynar_push_as(p_indexNetworkElm, sg_routing_edge_t, elm);
114   return xbt_dynar_length(p_indexNetworkElm)-1;
115 }
116
117 }
118 }