Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please codacy: use long form of negation in C++
[simgrid.git] / src / kernel / routing / VivaldiZone.cpp
1 /* Copyright (c) 2013-2017. 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 <boost/algorithm/string.hpp>
7
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/s4u/Host.hpp"
10
11 #include "src/kernel/routing/NetPoint.hpp"
12 #include "src/kernel/routing/VivaldiZone.hpp"
13 #include "src/surf/network_interface.hpp"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_vivaldi, surf, "Routing part of surf");
16
17 namespace simgrid {
18 namespace kernel {
19 namespace routing {
20 namespace vivaldi {
21 simgrid::xbt::Extension<NetPoint, Coords> Coords::EXTENSION_ID;
22
23 Coords::Coords(NetPoint* netpoint, const char* coordStr)
24 {
25   if (not Coords::EXTENSION_ID.valid())
26     Coords::EXTENSION_ID = NetPoint::extension_create<Coords>();
27
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->cname());
31
32   for (auto str : string_values)
33     coords.push_back(xbt_str_parse_double(str.c_str(), "Invalid coordinate: %s"));
34   coords.shrink_to_fit();
35
36   netpoint->extension_set<Coords>(this);
37   XBT_DEBUG("Coords of %s %p: %s", netpoint->cname(), netpoint, coordStr);
38 }
39 Coords::~Coords() = default;
40 }; // namespace vivaldi
41
42 static inline double euclidean_dist_comp(int index, std::vector<double>* src, std::vector<double>* dst)
43 {
44   double src_coord = src->at(index);
45   double dst_coord = dst->at(index);
46
47   return (src_coord - dst_coord) * (src_coord - dst_coord);
48 }
49
50 static std::vector<double>* getCoordsFromNetpoint(NetPoint* np)
51 {
52   simgrid::kernel::routing::vivaldi::Coords* coords = np->extension<simgrid::kernel::routing::vivaldi::Coords>();
53   xbt_assert(coords, "Please specify the Vivaldi coordinates of %s %s (%p)",
54              (np->isNetZone() ? "Netzone" : (np->isHost() ? "Host" : "Router")), np->cname(), np);
55   return &coords->coords;
56 }
57 VivaldiZone::VivaldiZone(NetZone* father, const char* name) : ClusterZone(father, name)
58 {
59 }
60
61 void VivaldiZone::setPeerLink(NetPoint* netpoint, double bw_in, double bw_out, const char* coord)
62 {
63   xbt_assert(netpoint->netzone() == this, "Cannot add a peer link to a netpoint that is not in this netzone");
64
65   new simgrid::kernel::routing::vivaldi::Coords(netpoint, coord);
66
67   std::string link_up   = "link_" + netpoint->name() + "_UP";
68   std::string link_down = "link_" + netpoint->name() + "_DOWN";
69   surf::LinkImpl* linkUp   = surf_network_model->createLink(link_up.c_str(), bw_out, 0, SURF_LINK_SHARED);
70   surf::LinkImpl* linkDown = surf_network_model->createLink(link_down.c_str(), bw_in, 0, SURF_LINK_SHARED);
71   privateLinks_.insert({netpoint->id(), {linkUp, linkDown}});
72 }
73
74 void VivaldiZone::getLocalRoute(NetPoint* src, NetPoint* dst, sg_platf_route_cbarg_t route, double* lat)
75 {
76   XBT_DEBUG("vivaldi getLocalRoute from '%s'[%d] '%s'[%d]", src->cname(), src->id(), dst->cname(), dst->id());
77
78   if (src->isNetZone()) {
79     std::string srcName = "router_" + src->name();
80     std::string dstName = "router_" + dst->name();
81     route->gw_src       = simgrid::s4u::Engine::instance()->netpointByNameOrNull(srcName.c_str());
82     route->gw_dst       = simgrid::s4u::Engine::instance()->netpointByNameOrNull(dstName.c_str());
83   }
84
85   /* Retrieve the private links */
86   if (privateLinks_.find(src->id()) != privateLinks_.end()) {
87     std::pair<surf::LinkImpl*, surf::LinkImpl*> info = privateLinks_.at(src->id());
88     if (info.first) {
89       route->link_list->push_back(info.first);
90       if (lat)
91         *lat += info.first->latency();
92     }
93   }
94   if (privateLinks_.find(dst->id()) != privateLinks_.end()) {
95     std::pair<surf::LinkImpl*, surf::LinkImpl*> info = privateLinks_.at(dst->id());
96     if (info.second) {
97       route->link_list->push_back(info.second);
98       if (lat)
99         *lat += info.second->latency();
100     }
101   }
102
103   /* Compute the extra latency due to the euclidean distance if needed */
104   if (lat) {
105     std::vector<double>* srcCoords = getCoordsFromNetpoint(src);
106     std::vector<double>* dstCoords = getCoordsFromNetpoint(dst);
107
108     double euclidean_dist =
109         sqrt(euclidean_dist_comp(0, srcCoords, dstCoords) + euclidean_dist_comp(1, srcCoords, dstCoords)) +
110         fabs(srcCoords->at(2)) + fabs(dstCoords->at(2));
111
112     XBT_DEBUG("Updating latency %f += %f", *lat, euclidean_dist);
113     *lat += euclidean_dist / 1000.0; // From .ms to .s
114   }
115 }
116 }
117 }
118 }