Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make NetZoneImpl public too
[simgrid.git] / src / kernel / routing / VivaldiZone.cpp
1 /* Copyright (c) 2013-2018. 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 "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
12 #include <boost/algorithm/string.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<NetPoint, Coords> Coords::EXTENSION_ID;
21
22 Coords::Coords(NetPoint* netpoint, std::string coordStr)
23 {
24   if (not Coords::EXTENSION_ID.valid())
25     Coords::EXTENSION_ID = NetPoint::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", netpoint->getCname());
30
31   for (auto const& str : string_values)
32     try {
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());
36     }
37   coords.shrink_to_fit();
38
39   netpoint->extension_set<Coords>(this);
40   XBT_DEBUG("Coords of %s %p: %s", netpoint->getCname(), netpoint, coordStr.c_str());
41 }
42 }; // namespace vivaldi
43
44 static inline double euclidean_dist_comp(int index, std::vector<double>* src, std::vector<double>* dst)
45 {
46   double src_coord = src->at(index);
47   double dst_coord = dst->at(index);
48
49   return (src_coord - dst_coord) * (src_coord - dst_coord);
50 }
51
52 static std::vector<double>* getCoordsFromNetpoint(NetPoint* np)
53 {
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;
58 }
59
60 VivaldiZone::VivaldiZone(NetZone* father, std::string name) : ClusterZone(father, name)
61 {
62 }
63
64 void VivaldiZone::setPeerLink(NetPoint* netpoint, double bw_in, double bw_out, std::string coord)
65 {
66   xbt_assert(netpoint->netzone() == this, "Cannot add a peer link to a netpoint that is not in this netzone");
67
68   new simgrid::kernel::routing::vivaldi::Coords(netpoint, coord);
69
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}});
75 }
76
77 void VivaldiZone::getLocalRoute(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* lat)
78 {
79   XBT_DEBUG("vivaldi getLocalRoute from '%s'[%u] '%s'[%u]", src->getCname(), src->id(), dst->getCname(), dst->id());
80
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());
86   }
87
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;
92     if (info.first) {
93       route->link_list.push_back(info.first);
94       if (lat)
95         *lat += info.first->latency();
96     }
97   } else {
98     XBT_DEBUG("Source of private link (%u) doesn't exist", src->id());
99   }
100
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;
104     if (info.second) {
105       route->link_list.push_back(info.second);
106       if (lat)
107         *lat += info.second->latency();
108     }
109   } else {
110     XBT_DEBUG("Destination of private link (%u) doesn't exist", dst->id());
111   }
112
113   /* Compute the extra latency due to the euclidean distance if needed */
114   if (lat) {
115     std::vector<double>* srcCoords = getCoordsFromNetpoint(src);
116     std::vector<double>* dstCoords = getCoordsFromNetpoint(dst);
117
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));
121
122     XBT_DEBUG("Updating latency %f += %f", *lat, euclidean_dist);
123     *lat += euclidean_dist / 1000.0; // From .ms to .s
124   }
125 }
126 }
127 }
128 }