Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Have a decent name for the private hostLinks in clusters
[simgrid.git] / src / surf / AsVivaldi.cpp
1 /* Copyright (c) 2013-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 #include "src/surf/AsVivaldi.hpp"
7 #include "src/surf/network_interface.hpp"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_vivaldi, surf, "Routing part of surf");
10
11 #define HOST_PEER(peername) bprintf("peer_%s", peername)
12 #define ROUTER_PEER(peername) bprintf("router_%s", peername)
13 #define LINK_PEER(peername) bprintf("link_%s", peername)
14
15 static inline double euclidean_dist_comp(int index, xbt_dynar_t src, xbt_dynar_t dst) {
16   double src_coord, dst_coord;
17
18   src_coord = xbt_dynar_get_as(src, index, double);
19   dst_coord = xbt_dynar_get_as(dst, index, double);
20
21   return (src_coord-dst_coord)*(src_coord-dst_coord);
22 }
23
24 namespace simgrid {
25 namespace surf {
26   AsVivaldi::AsVivaldi(const char *name)
27     : AsCluster(name)
28   {}
29
30 void AsVivaldi::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t route, double *lat)
31 {
32   XBT_DEBUG("vivaldi_get_route_and_latency from '%s'[%d] '%s'[%d]", src->name(), src->id(), dst->name(), dst->id());
33
34   if(src->isAS()) {
35     char *src_name = ROUTER_PEER(src->name());
36     char *dst_name = ROUTER_PEER(dst->name());
37     route->gw_src = (sg_netcard_t) xbt_lib_get_or_null(as_router_lib, src_name, ROUTING_ASR_LEVEL);
38     route->gw_dst = (sg_netcard_t) xbt_lib_get_or_null(as_router_lib, dst_name, ROUTING_ASR_LEVEL);
39     xbt_free(src_name);
40     xbt_free(dst_name);
41   }
42
43   double euclidean_dist;
44   xbt_dynar_t src_ctn, dst_ctn;
45   char *tmp_src_name, *tmp_dst_name;
46
47   if(src->isHost()){
48     tmp_src_name = HOST_PEER(src->name());
49
50     if ((int)xbt_dynar_length(privateLinks_) > src->id()) {
51       s_surf_parsing_link_up_down_t info = xbt_dynar_get_as(privateLinks_, src->id(), s_surf_parsing_link_up_down_t);
52       if(info.link_up) { // link up
53         route->link_list->push_back(info.link_up);
54         if (lat)
55           *lat += info.link_up->getLatency();
56       }
57     }
58     src_ctn = (xbt_dynar_t) simgrid::s4u::Host::by_name_or_create(tmp_src_name)->extension(COORD_HOST_LEVEL);
59     if (src_ctn == nullptr)
60       src_ctn = (xbt_dynar_t) simgrid::s4u::Host::by_name_or_create(src->name())->extension(COORD_HOST_LEVEL);
61   }
62   else if(src->isRouter() || src->isAS()){
63     tmp_src_name = ROUTER_PEER(src->name());
64     src_ctn = (xbt_dynar_t) xbt_lib_get_or_null(as_router_lib, tmp_src_name, COORD_ASR_LEVEL);
65   }
66   else{
67     THROW_IMPOSSIBLE;
68   }
69
70   if(dst->isHost()){
71     tmp_dst_name = HOST_PEER(dst->name());
72
73     if ((int)xbt_dynar_length(privateLinks_)>dst->id()) {
74       s_surf_parsing_link_up_down_t info = xbt_dynar_get_as(privateLinks_, dst->id(), s_surf_parsing_link_up_down_t);
75       if(info.link_down) { // link down
76         route->link_list->push_back(info.link_down);
77         if (lat)
78           *lat += info.link_down->getLatency();
79       }
80     }
81     dst_ctn = (xbt_dynar_t) simgrid::s4u::Host::by_name_or_create(tmp_dst_name)->extension(COORD_HOST_LEVEL);
82     if (dst_ctn == nullptr)
83       dst_ctn = (xbt_dynar_t) simgrid::s4u::Host::by_name_or_create(dst->name())->extension(COORD_HOST_LEVEL);
84   }
85   else if(dst->isRouter() || dst->isAS()){
86     tmp_dst_name = ROUTER_PEER(dst->name());
87     dst_ctn = (xbt_dynar_t) xbt_lib_get_or_null(as_router_lib, tmp_dst_name, COORD_ASR_LEVEL);
88   }
89   else{
90     THROW_IMPOSSIBLE;
91   }
92
93   xbt_assert(src_ctn,"No coordinate found for element '%s'",tmp_src_name);
94   xbt_assert(dst_ctn,"No coordinate found for element '%s'",tmp_dst_name);
95   free(tmp_src_name);
96   free(tmp_dst_name);
97
98   euclidean_dist = sqrt (euclidean_dist_comp(0,src_ctn,dst_ctn)+euclidean_dist_comp(1,src_ctn,dst_ctn))
99                       + fabs(xbt_dynar_get_as(src_ctn, 2, double))+fabs(xbt_dynar_get_as(dst_ctn, 2, double));
100
101   if (lat){
102     XBT_DEBUG("Updating latency %f += %f",*lat,euclidean_dist);
103     *lat += euclidean_dist / 1000.0; //From .ms to .s
104   }
105 }
106
107 }
108 }