Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New netzone: Star Zone
[simgrid.git] / src / kernel / routing / StarZone.cpp
1 /* Copyright (c) 2009-2021. 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/StarZone.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "simgrid/kernel/routing/RoutedZone.hpp"
9 #include "src/surf/network_interface.hpp"
10 #include "src/surf/xml/platf_private.hpp" // RouteCreationArgs and friends
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_star, surf, "Routing part of surf");
13
14 namespace simgrid {
15 namespace kernel {
16 namespace routing {
17 StarZone::StarZone(const std::string& name) : NetZoneImpl(name) {}
18
19 void StarZone::add_links_to_route(const std::vector<resource::LinkImpl*>& links, RouteCreationArgs* route,
20                                   double* latency, std::unordered_set<resource::LinkImpl*>& added_links)
21 {
22   for (auto* link : links) {
23     /* do not add duplicated links */
24     if (added_links.find(link) != added_links.end())
25       continue;
26     added_links.insert(link);
27     if (latency)
28       *latency += link->get_latency();
29     route->link_list.push_back(link);
30   }
31 }
32
33 void StarZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* latency)
34 {
35   XBT_VERB("StarZone getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(),
36            dst->id());
37
38   xbt_assert(((src == dst) and (loopback_.find(src->id()) != loopback_.end())) or
39                  (links_up_.find(src->id()) != links_up_.end()),
40              "StarZone routing (%s - %s): no link UP from source node. Did you use add_route() to set it?",
41              src->get_cname(), dst->get_cname());
42   xbt_assert(((src == dst) and (loopback_.find(dst->id()) != loopback_.end())) or
43                  (links_down_.find(dst->id()) != links_down_.end()),
44              "StarZone routing (%s - %s): no link DOWN to destination node. Did you use add_route() to set it?",
45              src->get_cname(), dst->get_cname());
46
47   std::unordered_set<resource::LinkImpl*> added_links;
48   /* loopback */
49   if ((src == dst) and (loopback_.find(src->id()) != loopback_.end())) {
50     add_links_to_route(loopback_[src->id()], route, latency, added_links);
51     return;
52   }
53
54   /* going UP */
55   add_links_to_route(links_up_[src->id()], route, latency, added_links);
56
57   /* going DOWN */
58   add_links_to_route(links_down_[dst->id()], route, latency, added_links);
59 }
60
61 void StarZone::get_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
62                          std::map<std::string, xbt_edge_t, std::less<>>* edges)
63 {
64 }
65
66 void StarZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
67                          const std::vector<kernel::resource::LinkImpl*>& link_list, bool symmetrical)
68 {
69   const char* src_name = src ? src->get_cname() : "nullptr";
70   const char* dst_name = dst ? dst->get_cname() : "nullptr";
71
72   xbt_assert((src == dst) or (not src and dst) or (src and not dst),
73              "Cannot add route from %s to %s. In a StarZone, route must be:  i) from source host to everyone, ii) from "
74              "everyone to a single host or iii) loopback, same source and destination",
75              src_name, dst_name);
76   xbt_assert((not symmetrical) or (symmetrical and src),
77              "Cannot add route from %s to %s. In a StarZone, symmetrical routes must be set from source to everyone "
78              "(not the contrary).",
79              src_name, dst_name);
80
81   /* loopback */
82   if (src == dst) {
83     loopback_[src->id()] = link_list;
84   } else {
85     /* src to everyone */
86     if (src) {
87       links_up_[src->id()] = link_list;
88       if (symmetrical) {
89         links_down_[src->id()] = link_list;
90         /* reverse it for down/symmetrical links */
91         std::reverse(links_down_[src->id()].begin(), links_down_[src->id()].end());
92       }
93     }
94     /* dst to everyone */
95     if (dst) {
96       links_down_[dst->id()] = link_list;
97     }
98   }
99   s4u::NetZone::on_route_creation(symmetrical, gw_src, gw_dst, gw_src, gw_dst, link_list);
100 }
101
102 void StarZone::do_seal()
103 {
104   /* add default empty links if nothing was configured by user */
105   for (auto const& node : get_vertices()) {
106     if ((links_up_.find(node->id()) == links_up_.end()) and (links_down_.find(node->id()) == links_down_.end())) {
107       links_down_[node->id()] = {};
108       links_up_[node->id()]   = {};
109     }
110   }
111 }
112
113 } // namespace routing
114 } // namespace kernel
115
116 namespace s4u {
117 NetZone* create_star_zone(const std::string& name)
118 {
119   return (new kernel::routing::StarZone(name))->get_iface();
120 }
121 } // namespace s4u
122
123 } // namespace simgrid