1 /* Copyright (c) 2013-2021. 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 "simgrid/kernel/routing/VivaldiZone.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/s4u/Host.hpp"
10 #include "src/surf/network_interface.hpp"
11 #include "src/surf/xml/platf_private.hpp"
12 #include "surf/surf.hpp"
14 #include <boost/algorithm/string.hpp>
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_vivaldi, surf, "Routing part of surf");
24 xbt::Extension<NetPoint, Coords> Coords::EXTENSION_ID;
26 Coords::Coords(NetPoint* netpoint, const std::string& coordStr)
28 if (not Coords::EXTENSION_ID.valid())
29 Coords::EXTENSION_ID = NetPoint::extension_create<Coords>();
31 std::vector<std::string> string_values;
32 boost::split(string_values, coordStr, boost::is_any_of(" "));
33 xbt_assert(string_values.size() == 3, "Coordinates of %s must have 3 dimensions", netpoint->get_cname());
35 for (auto const& str : string_values)
37 coords.push_back(std::stod(str));
38 } catch (std::invalid_argument const& ia) {
39 throw std::invalid_argument(std::string("Invalid coordinate: ") + ia.what());
41 coords.shrink_to_fit();
43 netpoint->extension_set<Coords>(this);
44 XBT_DEBUG("Coords of %s %p: %s", netpoint->get_cname(), netpoint, coordStr.c_str());
46 } // namespace vivaldi
48 static inline double euclidean_dist_comp(int index, std::vector<double>* src, std::vector<double>* dst)
50 double src_coord = src->at(index);
51 double dst_coord = dst->at(index);
53 return (src_coord - dst_coord) * (src_coord - dst_coord);
56 static std::vector<double>* netpoint_get_coords(NetPoint* np)
58 auto* coords = np->extension<vivaldi::Coords>();
59 xbt_assert(coords, "Please specify the Vivaldi coordinates of %s %s (%p)",
60 (np->is_netzone() ? "Netzone" : (np->is_host() ? "Host" : "Router")), np->get_cname(), np);
61 return &coords->coords;
64 void VivaldiZone::set_peer_link(NetPoint* netpoint, double bw_in, double bw_out, const std::string& coord)
66 xbt_assert(netpoint->get_englobing_zone() == this,
67 "Cannot add a peer link to a netpoint that is not in this netzone");
69 new vivaldi::Coords(netpoint, coord);
71 std::string link_up = "link_" + netpoint->get_name() + "_UP";
72 std::string link_down = "link_" + netpoint->get_name() + "_DOWN";
73 resource::LinkImpl* linkUp =
74 get_network_model()->create_link(link_up, std::vector<double>(1, bw_out), s4u::Link::SharingPolicy::SHARED);
76 resource::LinkImpl* linkDown =
77 get_network_model()->create_link(link_down, std::vector<double>(1, bw_in), s4u::Link::SharingPolicy::SHARED);
79 add_private_link_at(netpoint->id(), {linkUp, linkDown});
82 void VivaldiZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* lat)
84 XBT_DEBUG("vivaldi getLocalRoute from '%s'[%u] '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(), dst->id());
86 if (src->is_netzone()) {
87 std::string srcName = "router_" + src->get_name();
88 std::string dstName = "router_" + dst->get_name();
89 route->gw_src = s4u::Engine::get_instance()->netpoint_by_name_or_null(srcName);
90 route->gw_dst = s4u::Engine::get_instance()->netpoint_by_name_or_null(dstName);
93 /* Retrieve the private links */
94 if (private_link_exists_at(src->id())) {
95 auto src_link = get_uplink_from(src->id());
96 route->link_list.push_back(src_link);
98 *lat += src_link->get_latency();
101 if (private_link_exists_at(dst->id())) {
102 auto dst_link = get_downlink_to(dst->id());
103 route->link_list.push_back(dst_link);
105 *lat += dst_link->get_latency();
107 /* Compute the extra latency due to the euclidean distance if needed */
109 std::vector<double>* srcCoords = netpoint_get_coords(src);
110 std::vector<double>* dstCoords = netpoint_get_coords(dst);
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));
116 XBT_DEBUG("Updating latency %f += %f", *lat, euclidean_dist);
117 *lat += euclidean_dist / 1000.0; // From .ms to .s
120 } // namespace routing
121 } // namespace kernel
122 } // namespace simgrid