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"
12 #include <boost/algorithm/string.hpp>
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_vivaldi, surf, "Routing part of surf");
20 simgrid::xbt::Extension<NetPoint, Coords> Coords::EXTENSION_ID;
22 Coords::Coords(NetPoint* netpoint, std::string coordStr)
24 if (not Coords::EXTENSION_ID.valid())
25 Coords::EXTENSION_ID = NetPoint::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", netpoint->getCname());
31 for (auto const& str : string_values)
33 coords.push_back(std::stod(str));
34 } catch (std::invalid_argument const& ia) {
35 throw std::invalid_argument(std::string("Invalid coordinate: ") + ia.what());
37 coords.shrink_to_fit();
39 netpoint->extension_set<Coords>(this);
40 XBT_DEBUG("Coords of %s %p: %s", netpoint->getCname(), netpoint, coordStr.c_str());
42 }; // namespace vivaldi
44 static inline double euclidean_dist_comp(int index, std::vector<double>* src, std::vector<double>* dst)
46 double src_coord = src->at(index);
47 double dst_coord = dst->at(index);
49 return (src_coord - dst_coord) * (src_coord - dst_coord);
52 static std::vector<double>* getCoordsFromNetpoint(NetPoint* np)
54 simgrid::kernel::routing::vivaldi::Coords* coords = np->extension<simgrid::kernel::routing::vivaldi::Coords>();
55 xbt_assert(coords, "Please specify the Vivaldi coordinates of %s %s (%p)",
56 (np->isNetZone() ? "Netzone" : (np->isHost() ? "Host" : "Router")), np->getCname(), np);
57 return &coords->coords;
60 VivaldiZone::VivaldiZone(NetZone* father, std::string name) : ClusterZone(father, name)
64 void VivaldiZone::setPeerLink(NetPoint* netpoint, double bw_in, double bw_out, std::string coord)
66 xbt_assert(netpoint->netzone() == this, "Cannot add a peer link to a netpoint that is not in this netzone");
68 new simgrid::kernel::routing::vivaldi::Coords(netpoint, coord);
70 std::string link_up = "link_" + netpoint->getName() + "_UP";
71 std::string link_down = "link_" + netpoint->getName() + "_DOWN";
72 surf::LinkImpl* linkUp = surf_network_model->createLink(link_up, bw_out, 0, SURF_LINK_SHARED);
73 surf::LinkImpl* linkDown = surf_network_model->createLink(link_down, bw_in, 0, SURF_LINK_SHARED);
74 privateLinks_.insert({netpoint->id(), {linkUp, linkDown}});
77 void VivaldiZone::getLocalRoute(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* lat)
79 XBT_DEBUG("vivaldi getLocalRoute from '%s'[%u] '%s'[%u]", src->getCname(), src->id(), dst->getCname(), dst->id());
81 if (src->isNetZone()) {
82 std::string srcName = "router_" + src->getName();
83 std::string dstName = "router_" + dst->getName();
84 route->gw_src = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(srcName.c_str());
85 route->gw_dst = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(dstName.c_str());
88 /* Retrieve the private links */
89 auto src_link = privateLinks_.find(src->id());
90 if (src_link != privateLinks_.end()) {
91 std::pair<surf::LinkImpl*, surf::LinkImpl*> info = src_link->second;
93 route->link_list.push_back(info.first);
95 *lat += info.first->latency();
98 XBT_DEBUG("Source of private link (%u) doesn't exist", src->id());
101 auto dst_link = privateLinks_.find(dst->id());
102 if (dst_link != privateLinks_.end()) {
103 std::pair<surf::LinkImpl*, surf::LinkImpl*> info = dst_link->second;
105 route->link_list.push_back(info.second);
107 *lat += info.second->latency();
110 XBT_DEBUG("Destination of private link (%u) doesn't exist", dst->id());
113 /* Compute the extra latency due to the euclidean distance if needed */
115 std::vector<double>* srcCoords = getCoordsFromNetpoint(src);
116 std::vector<double>* dstCoords = getCoordsFromNetpoint(dst);
118 double euclidean_dist =
119 sqrt(euclidean_dist_comp(0, srcCoords, dstCoords) + euclidean_dist_comp(1, srcCoords, dstCoords)) +
120 fabs(srcCoords->at(2)) + fabs(dstCoords->at(2));
122 XBT_DEBUG("Updating latency %f += %f", *lat, euclidean_dist);
123 *lat += euclidean_dist / 1000.0; // From .ms to .s