1 /* Copyright (c) 2013-2016. The SimGrid Team. All rights reserved. */
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. */
6 #include <boost/algorithm/string.hpp>
8 #include <simgrid/s4u/host.hpp>
10 #include "src/kernel/routing/AsVivaldi.hpp"
11 #include "src/kernel/routing/NetCard.hpp"
12 #include "src/surf/network_interface.hpp"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_vivaldi, surf, "Routing part of surf");
20 simgrid::xbt::Extension<NetCard, Coords> Coords::EXTENSION_ID;
22 Coords::Coords(NetCard* netcard, const char* coordStr)
24 if (!Coords::EXTENSION_ID.valid())
25 Coords::EXTENSION_ID = NetCard::extension_create<Coords>();
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());
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();
35 netcard->extension_set<Coords>(this);
36 XBT_DEBUG("Coords of %s %p: %s", netcard->cname(), netcard, coordStr);
38 Coords::~Coords() = default;
39 }; // namespace vivaldi
41 static inline double euclidean_dist_comp(int index, std::vector<double>* src, std::vector<double>* dst)
43 double src_coord = src->at(index);
44 double dst_coord = dst->at(index);
46 return (src_coord - dst_coord) * (src_coord - dst_coord);
49 static std::vector<double>* getCoordsFromNetcard(NetCard* nc)
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;
56 AsVivaldi::AsVivaldi(As* father, const char* name) : AsCluster(father, name)
60 void AsVivaldi::setPeerLink(NetCard* netcard, double bw_in, double bw_out, double latency, const char* coord)
62 xbt_assert(netcard->containingAS() == this, "Cannot add a peer link to a netcard that is not in this AS");
64 new simgrid::kernel::routing::vivaldi::Coords(netcard, coord);
66 s_surf_parsing_link_up_down_t info;
67 char* link_up = bprintf("link_%s_UP", netcard->cname());
68 char* link_down = bprintf("link_%s_DOWN", netcard->cname());
69 info.linkUp = surf_network_model->createLink(link_up, bw_out, latency, SURF_LINK_SHARED);
70 info.linkDown = surf_network_model->createLink(link_down, bw_in, latency, SURF_LINK_SHARED);
71 privateLinks_.insert({netcard->id(), info});
77 void AsVivaldi::getLocalRoute(NetCard* src, NetCard* dst, sg_platf_route_cbarg_t route, double* lat)
79 XBT_DEBUG("vivaldi getLocalRoute from '%s'[%d] '%s'[%d]", src->cname(), src->id(), dst->cname(), dst->id());
82 char* srcName = bprintf("router_%s", src->cname());
83 char* dstName = bprintf("router_%s", dst->cname());
84 route->gw_src = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, srcName, ROUTING_ASR_LEVEL);
85 route->gw_dst = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, dstName, ROUTING_ASR_LEVEL);
90 /* Retrieve the private links */
91 if (privateLinks_.find(src->id()) != privateLinks_.end()) {
92 s_surf_parsing_link_up_down_t info = privateLinks_.at(src->id());
94 route->link_list->push_back(info.linkUp);
96 *lat += info.linkUp->latency();
99 if (privateLinks_.find(dst->id()) != privateLinks_.end()) {
100 s_surf_parsing_link_up_down_t info = privateLinks_.at(dst->id());
102 route->link_list->push_back(info.linkDown);
104 *lat += info.linkDown->latency();
108 /* Compute the extra latency due to the euclidean distance if needed */
110 std::vector<double>* srcCoords = getCoordsFromNetcard(src);
111 std::vector<double>* dstCoords = getCoordsFromNetcard(dst);
113 double euclidean_dist =
114 sqrt(euclidean_dist_comp(0, srcCoords, dstCoords) + euclidean_dist_comp(1, srcCoords, dstCoords)) +
115 fabs(srcCoords->at(2)) + fabs(dstCoords->at(2));
117 XBT_DEBUG("Updating latency %f += %f", *lat, euclidean_dist);
118 *lat += euclidean_dist / 1000.0; // From .ms to .s