Logo AND Algorithmique Numérique Distribuée

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