1 /* Copyright (c) 2013-2018. 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"
13 #include <boost/algorithm/string.hpp>
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_vivaldi, surf, "Routing part of surf");
21 simgrid::xbt::Extension<NetPoint, Coords> Coords::EXTENSION_ID;
23 Coords::Coords(NetPoint* netpoint, std::string coordStr)
25 if (not Coords::EXTENSION_ID.valid())
26 Coords::EXTENSION_ID = NetPoint::extension_create<Coords>();
28 std::vector<std::string> string_values;
29 boost::split(string_values, coordStr, boost::is_any_of(" "));
30 xbt_assert(string_values.size() == 3, "Coordinates of %s must have 3 dimensions", netpoint->getCname());
32 for (auto const& str : string_values)
34 coords.push_back(std::stod(str));
35 } catch (std::invalid_argument const& ia) {
36 throw std::invalid_argument(std::string("Invalid coordinate: ") + ia.what());
38 coords.shrink_to_fit();
40 netpoint->extension_set<Coords>(this);
41 XBT_DEBUG("Coords of %s %p: %s", netpoint->getCname(), netpoint, coordStr.c_str());
43 }; // namespace vivaldi
45 static inline double euclidean_dist_comp(int index, std::vector<double>* src, std::vector<double>* dst)
47 double src_coord = src->at(index);
48 double dst_coord = dst->at(index);
50 return (src_coord - dst_coord) * (src_coord - dst_coord);
53 static std::vector<double>* getCoordsFromNetpoint(NetPoint* np)
55 simgrid::kernel::routing::vivaldi::Coords* coords = np->extension<simgrid::kernel::routing::vivaldi::Coords>();
56 xbt_assert(coords, "Please specify the Vivaldi coordinates of %s %s (%p)",
57 (np->isNetZone() ? "Netzone" : (np->isHost() ? "Host" : "Router")), np->getCname(), np);
58 return &coords->coords;
61 VivaldiZone::VivaldiZone(NetZone* father, std::string name) : ClusterZone(father, name)
65 void VivaldiZone::setPeerLink(NetPoint* netpoint, double bw_in, double bw_out, std::string coord)
67 xbt_assert(netpoint->netzone() == this, "Cannot add a peer link to a netpoint that is not in this netzone");
69 new simgrid::kernel::routing::vivaldi::Coords(netpoint, coord);
71 std::string link_up = "link_" + netpoint->getName() + "_UP";
72 std::string link_down = "link_" + netpoint->getName() + "_DOWN";
73 surf::LinkImpl* linkUp = surf_network_model->createLink(link_up, bw_out, 0, SURF_LINK_SHARED);
74 surf::LinkImpl* linkDown = surf_network_model->createLink(link_down, bw_in, 0, SURF_LINK_SHARED);
75 privateLinks_.insert({netpoint->id(), {linkUp, linkDown}});
78 void VivaldiZone::getLocalRoute(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* lat)
80 XBT_DEBUG("vivaldi getLocalRoute from '%s'[%u] '%s'[%u]", src->getCname(), src->id(), dst->getCname(), dst->id());
82 if (src->isNetZone()) {
83 std::string srcName = "router_" + src->getName();
84 std::string dstName = "router_" + dst->getName();
85 route->gw_src = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(srcName.c_str());
86 route->gw_dst = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(dstName.c_str());
89 /* Retrieve the private links */
90 auto src_link = privateLinks_.find(src->id());
91 if (src_link != privateLinks_.end()) {
92 std::pair<surf::LinkImpl*, surf::LinkImpl*> info = src_link->second;
94 route->link_list.push_back(info.first);
96 *lat += info.first->latency();
99 XBT_DEBUG("Source of private link (%u) doesn't exist", src->id());
102 auto dst_link = privateLinks_.find(dst->id());
103 if (dst_link != privateLinks_.end()) {
104 std::pair<surf::LinkImpl*, surf::LinkImpl*> info = dst_link->second;
106 route->link_list.push_back(info.second);
108 *lat += info.second->latency();
111 XBT_DEBUG("Destination of private link (%u) doesn't exist", dst->id());
114 /* Compute the extra latency due to the euclidean distance if needed */
116 std::vector<double>* srcCoords = getCoordsFromNetpoint(src);
117 std::vector<double>* dstCoords = getCoordsFromNetpoint(dst);
119 double euclidean_dist =
120 sqrt(euclidean_dist_comp(0, srcCoords, dstCoords) + euclidean_dist_comp(1, srcCoords, dstCoords)) +
121 fabs(srcCoords->at(2)) + fabs(dstCoords->at(2));
123 XBT_DEBUG("Updating latency %f += %f", *lat, euclidean_dist);
124 *lat += euclidean_dist / 1000.0; // From .ms to .s