Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
22da9c7a65d8459757b5f07c91fca9326b826c48
[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
14 namespace simgrid {
15 namespace surf {
16   static inline double euclidean_dist_comp(int index, xbt_dynar_t src, xbt_dynar_t dst) {
17     double src_coord = xbt_dynar_get_as(src, index, double);
18     double dst_coord = xbt_dynar_get_as(dst, index, double);
19
20     return (src_coord-dst_coord)*(src_coord-dst_coord);
21   }
22
23   static xbt_dynar_t getCoordsFromNetcard(NetCard *nc)
24   {
25     xbt_dynar_t res;
26     char *tmp_name;
27
28     if(nc->isHost()){
29       tmp_name = HOST_PEER(nc->name());
30       res = (xbt_dynar_t) simgrid::s4u::Host::by_name_or_create(tmp_name)->extension(COORD_HOST_LEVEL);
31       if (res == nullptr)
32         res = (xbt_dynar_t) simgrid::s4u::Host::by_name_or_create(nc->name())->extension(COORD_HOST_LEVEL);
33     }
34     else if(nc->isRouter() || nc->isAS()){
35       tmp_name = ROUTER_PEER(nc->name());
36       res = (xbt_dynar_t) xbt_lib_get_or_null(as_router_lib, tmp_name, COORD_ASR_LEVEL);
37     }
38     else{
39       THROW_IMPOSSIBLE;
40     }
41
42     xbt_assert(res,"No coordinate found for element '%s'",tmp_name);
43     free(tmp_name);
44
45     return res;
46   }
47   AsVivaldi::AsVivaldi(const char *name)
48     : AsCluster(name)
49   {}
50
51 void AsVivaldi::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t route, double *lat)
52 {
53   XBT_DEBUG("vivaldi_get_route_and_latency from '%s'[%d] '%s'[%d]", src->name(), src->id(), dst->name(), dst->id());
54
55   if(src->isAS()) {
56     char *src_name = ROUTER_PEER(src->name());
57     char *dst_name = ROUTER_PEER(dst->name());
58     route->gw_src = (sg_netcard_t) xbt_lib_get_or_null(as_router_lib, src_name, ROUTING_ASR_LEVEL);
59     route->gw_dst = (sg_netcard_t) xbt_lib_get_or_null(as_router_lib, dst_name, ROUTING_ASR_LEVEL);
60     xbt_free(src_name);
61     xbt_free(dst_name);
62   }
63
64   /* Retrieve the private links */
65   if ((int)xbt_dynar_length(privateLinks_) > src->id()) {
66     s_surf_parsing_link_up_down_t info = xbt_dynar_get_as(privateLinks_, src->id(), s_surf_parsing_link_up_down_t);
67     if(info.link_up) {
68       route->link_list->push_back(info.link_up);
69       if (lat)
70         *lat += info.link_up->getLatency();
71     }
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) {
76       route->link_list->push_back(info.link_down);
77       if (lat)
78         *lat += info.link_down->getLatency();
79     }
80   }
81
82   /* Compute the extra latency due to the euclidean distance if needed */
83   if (lat){
84     xbt_dynar_t srcCoords = getCoordsFromNetcard(src);
85     xbt_dynar_t dstCoords = getCoordsFromNetcard(dst);
86
87     double euclidean_dist = sqrt (euclidean_dist_comp(0,srcCoords,dstCoords)+euclidean_dist_comp(1,srcCoords,dstCoords))
88                               + fabs(xbt_dynar_get_as(srcCoords, 2, double))+fabs(xbt_dynar_get_as(dstCoords, 2, double));
89
90     XBT_DEBUG("Updating latency %f += %f",*lat,euclidean_dist);
91     *lat += euclidean_dist / 1000.0; //From .ms to .s
92   }
93 }
94
95 }
96 }