Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / kernel / routing / 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 <xbt/dynar.h>
7
8 #include <simgrid/s4u/host.hpp>
9
10 #include "src/kernel/routing/AsVivaldi.hpp"
11 #include "src/surf/network_interface.hpp"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_vivaldi, surf, "Routing part of surf");
14
15 namespace simgrid {
16 namespace kernel {
17 namespace routing {
18 namespace vivaldi {
19 simgrid::xbt::Extension<s4u::Host, Coords> Coords::EXTENSION_ID;
20
21 Coords::Coords(s4u::Host* host, const char* coordStr)
22 {
23   if (!Coords::EXTENSION_ID.valid()) {
24     Coords::EXTENSION_ID = s4u::Host::extension_create<Coords>();
25   }
26
27   unsigned int cursor;
28   char* str;
29
30   xbt_dynar_t ctn_str = xbt_str_split_str(coordStr, " ");
31   xbt_assert(xbt_dynar_length(ctn_str) == 3, "Coordinates of %s must have 3 dimensions", host->name().c_str());
32
33   this->coords = xbt_dynar_new(sizeof(double), nullptr);
34   xbt_dynar_foreach (ctn_str, cursor, str) {
35     double val = xbt_str_parse_double(str, "Invalid coordinate: %s");
36     xbt_dynar_push(this->coords, &val);
37   }
38   xbt_dynar_free(&ctn_str);
39   xbt_dynar_shrink(this->coords, 0);
40   host->extension_set<Coords>(this);
41 }
42 Coords::~Coords()
43 {
44   xbt_dynar_free(&coords);
45 }
46 }; // namespace vivaldi
47
48 static inline double euclidean_dist_comp(int index, xbt_dynar_t src, xbt_dynar_t dst)
49 {
50   double src_coord = xbt_dynar_get_as(src, index, double);
51   double dst_coord = xbt_dynar_get_as(dst, index, double);
52
53   return (src_coord - dst_coord) * (src_coord - dst_coord);
54   }
55
56   static xbt_dynar_t getCoordsFromNetcard(NetCard *nc)
57   {
58     xbt_dynar_t res = nullptr;
59     char *tmp_name;
60
61     if (nc->isHost()) {
62       tmp_name                 = bprintf("peer_%s", nc->name().c_str());
63       simgrid::s4u::Host *host = simgrid::s4u::Host::by_name_or_null(tmp_name);
64       if (host == nullptr)
65         host = simgrid::s4u::Host::by_name_or_null(nc->name());
66       if (host != nullptr)
67         res = host->extension<simgrid::kernel::routing::vivaldi::Coords>()->coords;
68     }
69     else if(nc->isRouter() || nc->isAS()){
70       tmp_name = bprintf("router_%s", nc->name().c_str());
71       res = (xbt_dynar_t) xbt_lib_get_or_null(as_router_lib, tmp_name, COORD_ASR_LEVEL);
72     }
73     else{
74       THROW_IMPOSSIBLE;
75     }
76
77     xbt_assert(res,"No coordinate found for element '%s'",tmp_name);
78     free(tmp_name);
79
80     return res;
81   }
82   AsVivaldi::AsVivaldi(As* father, const char* name) : AsCluster(father, name)
83   {}
84
85 void AsVivaldi::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t route, double *lat)
86 {
87   XBT_DEBUG("vivaldi_get_route_and_latency from '%s'[%d] '%s'[%d]", src->name().c_str(), src->id(), dst->name().c_str(),
88             dst->id());
89
90   if (src->isAS()) {
91     char* srcName = bprintf("router_%s", src->name().c_str());
92     char* dstName = bprintf("router_%s", dst->name().c_str());
93     route->gw_src = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, srcName, ROUTING_ASR_LEVEL);
94     route->gw_dst = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, dstName, ROUTING_ASR_LEVEL);
95     xbt_free(srcName);
96     xbt_free(dstName);
97   }
98
99   /* Retrieve the private links */
100   if (privateLinks_.size() > src->id()) {
101     s_surf_parsing_link_up_down_t info = privateLinks_.at(src->id());
102     if(info.linkUp) {
103       route->link_list->push_back(info.linkUp);
104       if (lat)
105         *lat += info.linkUp->latency();
106     }
107   }
108   if (privateLinks_.size() >dst->id()) {
109     s_surf_parsing_link_up_down_t info = privateLinks_.at(dst->id());
110     if(info.linkDown) {
111       route->link_list->push_back(info.linkDown);
112       if (lat)
113         *lat += info.linkDown->latency();
114     }
115   }
116
117   /* Compute the extra latency due to the euclidean distance if needed */
118   if (lat){
119     xbt_dynar_t srcCoords = getCoordsFromNetcard(src);
120     xbt_dynar_t dstCoords = getCoordsFromNetcard(dst);
121
122     double euclidean_dist = sqrt (euclidean_dist_comp(0,srcCoords,dstCoords)+euclidean_dist_comp(1,srcCoords,dstCoords))
123                               + fabs(xbt_dynar_get_as(srcCoords, 2, double))+fabs(xbt_dynar_get_as(dstCoords, 2, double));
124
125     XBT_DEBUG("Updating latency %f += %f",*lat,euclidean_dist);
126     *lat += euclidean_dist / 1000.0; //From .ms to .s
127   }
128 }
129
130 }}}