Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a04f1e99709e2db5fdb1854ada15d542c486f086
[simgrid.git] / src / kernel / routing / VivaldiZone.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 <boost/algorithm/string.hpp>
7
8 #include <simgrid/s4u/host.hpp>
9
10 #include "src/kernel/routing/NetCard.hpp"
11 #include "src/kernel/routing/VivaldiZone.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<NetCard, Coords> Coords::EXTENSION_ID;
21
22 Coords::Coords(NetCard* netcard, const char* coordStr)
23 {
24   if (!Coords::EXTENSION_ID.valid())
25     Coords::EXTENSION_ID = NetCard::extension_create<Coords>();
26
27   std::vector<std::string> string_values;
28   boost::split(string_values, coordStr, boost::is_any_of(" "));
29   xbt_assert(string_values.size() == 3, "Coordinates of %s must have 3 dimensions", netcard->cname());
30
31   for (auto str : string_values)
32     coords.push_back(xbt_str_parse_double(str.c_str(), "Invalid coordinate: %s"));
33   coords.shrink_to_fit();
34
35   netcard->extension_set<Coords>(this);
36   XBT_DEBUG("Coords of %s %p: %s", netcard->cname(), netcard, coordStr);
37 }
38 Coords::~Coords() = default;
39 }; // namespace vivaldi
40
41 static inline double euclidean_dist_comp(int index, std::vector<double>* src, std::vector<double>* dst)
42 {
43   double src_coord = src->at(index);
44   double dst_coord = dst->at(index);
45
46   return (src_coord - dst_coord) * (src_coord - dst_coord);
47 }
48
49 static std::vector<double>* getCoordsFromNetcard(NetCard* nc)
50 {
51   simgrid::kernel::routing::vivaldi::Coords* coords = nc->extension<simgrid::kernel::routing::vivaldi::Coords>();
52   xbt_assert(coords, "Please specify the Vivaldi coordinates of %s %s (%p)",
53              (nc->isAS() ? "AS" : (nc->isHost() ? "Host" : "Router")), nc->cname(), nc);
54   return &coords->coords;
55 }
56 VivaldiZone::VivaldiZone(NetZone* father, const char* name) : ClusterZone(father, name)
57 {
58 }
59
60 void VivaldiZone::setPeerLink(NetCard* netcard, double bw_in, double bw_out, double latency, const char* coord)
61 {
62   xbt_assert(netcard->containingAS() == this, "Cannot add a peer link to a netcard that is not in this AS");
63
64   new simgrid::kernel::routing::vivaldi::Coords(netcard, coord);
65
66   char* link_up   = bprintf("link_%s_UP", netcard->cname());
67   char* link_down = bprintf("link_%s_DOWN", netcard->cname());
68   Link* linkUp    = surf_network_model->createLink(link_up, bw_out, latency, SURF_LINK_SHARED);
69   Link* linkDown  = surf_network_model->createLink(link_down, bw_in, latency, SURF_LINK_SHARED);
70   privateLinks_.insert({netcard->id(), {linkUp, linkDown}});
71
72   free(link_up);
73   free(link_down);
74 }
75
76 void VivaldiZone::getLocalRoute(NetCard* src, NetCard* dst, sg_platf_route_cbarg_t route, double* lat)
77 {
78   XBT_DEBUG("vivaldi getLocalRoute from '%s'[%d] '%s'[%d]", src->cname(), src->id(), dst->cname(), dst->id());
79
80   if (src->isAS()) {
81     char* srcName = bprintf("router_%s", src->cname());
82     char* dstName = bprintf("router_%s", dst->cname());
83     route->gw_src = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, srcName, ROUTING_ASR_LEVEL);
84     route->gw_dst = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, dstName, ROUTING_ASR_LEVEL);
85     xbt_free(srcName);
86     xbt_free(dstName);
87   }
88
89   /* Retrieve the private links */
90   if (privateLinks_.find(src->id()) != privateLinks_.end()) {
91     std::pair<Link*, Link*> info = privateLinks_.at(src->id());
92     if (info.first) {
93       route->link_list->push_back(info.first);
94       if (lat)
95         *lat += info.first->latency();
96     }
97   }
98   if (privateLinks_.find(dst->id()) != privateLinks_.end()) {
99     std::pair<Link*, Link*> info = privateLinks_.at(dst->id());
100     if (info.second) {
101       route->link_list->push_back(info.second);
102       if (lat)
103         *lat += info.second->latency();
104     }
105   }
106
107   /* Compute the extra latency due to the euclidean distance if needed */
108   if (lat) {
109     std::vector<double>* srcCoords = getCoordsFromNetcard(src);
110     std::vector<double>* dstCoords = getCoordsFromNetcard(dst);
111
112     double euclidean_dist =
113         sqrt(euclidean_dist_comp(0, srcCoords, dstCoords) + euclidean_dist_comp(1, srcCoords, dstCoords)) +
114         fabs(srcCoords->at(2)) + fabs(dstCoords->at(2));
115
116     XBT_DEBUG("Updating latency %f += %f", *lat, euclidean_dist);
117     *lat += euclidean_dist / 1000.0; // From .ms to .s
118   }
119 }
120 }
121 }
122 }