Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
change way vivaldi coordinates are managed internally
[simgrid.git] / src / kernel / routing / ClusterZone.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/ClusterZone.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" // FIXME: RouteCreationArgs and friends
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster, surf, "Routing part of surf");
13
14 /* This routing is specifically setup to represent clusters, aka homogeneous sets of machines
15  * Note that a router is created, easing the interconnection with the rest of the world. */
16
17 namespace simgrid {
18 namespace kernel {
19 namespace routing {
20 ClusterZone::ClusterZone(const std::string& name) : NetZoneImpl(name) {}
21
22 void ClusterZone::set_loopback()
23 {
24   num_links_per_node_++;
25   has_loopback_ = true;
26 }
27
28 void ClusterZone::set_limiter()
29 {
30   num_links_per_node_++;
31   has_limiter_ = true;
32 }
33
34 void ClusterZone::add_private_link_at(unsigned int position, std::pair<resource::LinkImpl*, resource::LinkImpl*> link)
35 {
36   private_links_.insert({position, link});
37 }
38
39 void ClusterZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* lat)
40 {
41   XBT_VERB("cluster getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(), dst->id());
42   xbt_assert(not private_links_.empty(),
43              "Cluster routing: no links attached to the source node - did you use host_link tag?");
44
45   if ((src->id() == dst->id()) && has_loopback_) {
46     if (src->is_router()) {
47       XBT_WARN("Routing from a cluster private router to itself is meaningless");
48     } else {
49       std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos(src->id()));
50       route->link_list.push_back(info.first);
51       if (lat)
52         *lat += info.first->get_latency();
53     }
54     return;
55   }
56
57   if (not src->is_router()) { // No private link for the private router
58     if (has_limiter_) {       // limiter for sender
59       std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos_with_loopback(src->id()));
60       route->link_list.push_back(info.first);
61     }
62
63     std::pair<resource::LinkImpl*, resource::LinkImpl*> info =
64         private_links_.at(node_pos_with_loopback_limiter(src->id()));
65     if (info.first) { // link up
66       route->link_list.push_back(info.first);
67       if (lat)
68         *lat += info.first->get_latency();
69     }
70   }
71
72   if (backbone_) {
73     route->link_list.push_back(backbone_);
74     if (lat)
75       *lat += backbone_->get_latency();
76   }
77
78   if (not dst->is_router()) { // No specific link for router
79     std::pair<resource::LinkImpl*, resource::LinkImpl*> info =
80         private_links_.at(node_pos_with_loopback_limiter(dst->id()));
81
82     if (info.second) { // link down
83       route->link_list.push_back(info.second);
84       if (lat)
85         *lat += info.second->get_latency();
86     }
87     if (has_limiter_) { // limiter for receiver
88       info = private_links_.at(node_pos_with_loopback(dst->id()));
89       route->link_list.push_back(info.first);
90     }
91   }
92 }
93
94 void ClusterZone::get_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
95                             std::map<std::string, xbt_edge_t, std::less<>>* edges)
96 {
97   xbt_assert(router_,
98              "Malformed cluster. This may be because your platform file is a hypergraph while it must be a graph.");
99
100   /* create the router */
101   xbt_node_t routerNode = new_xbt_graph_node(graph, router_->get_cname(), nodes);
102
103   xbt_node_t backboneNode = nullptr;
104   if (backbone_) {
105     backboneNode = new_xbt_graph_node(graph, backbone_->get_cname(), nodes);
106     new_xbt_graph_edge(graph, routerNode, backboneNode, edges);
107   }
108
109   for (auto const& src : get_vertices()) {
110     if (not src->is_router()) {
111       xbt_node_t previous = new_xbt_graph_node(graph, src->get_cname(), nodes);
112
113       std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(src->id());
114
115       if (info.first) { // link up
116         xbt_node_t current = new_xbt_graph_node(graph, info.first->get_cname(), nodes);
117         new_xbt_graph_edge(graph, previous, current, edges);
118
119         if (backbone_) {
120           new_xbt_graph_edge(graph, current, backboneNode, edges);
121         } else {
122           new_xbt_graph_edge(graph, current, routerNode, edges);
123         }
124       }
125
126       if (info.second) { // link down
127         xbt_node_t current = new_xbt_graph_node(graph, info.second->get_cname(), nodes);
128         new_xbt_graph_edge(graph, previous, current, edges);
129
130         if (backbone_) {
131           new_xbt_graph_edge(graph, current, backboneNode, edges);
132         } else {
133           new_xbt_graph_edge(graph, current, routerNode, edges);
134         }
135       }
136     }
137   }
138 }
139
140 void ClusterZone::create_links_for_node(ClusterCreationArgs* cluster, int id, int /*rank*/, unsigned int position)
141 {
142   std::string link_id = cluster->id + "_link_" + std::to_string(id);
143
144   LinkCreationArgs link;
145   link.id = link_id;
146   link.bandwidths.push_back(cluster->bw);
147   link.latency = cluster->lat;
148   link.policy  = cluster->sharing_policy;
149   sg_platf_new_link(&link);
150
151   const s4u::Link* linkUp;
152   const s4u::Link* linkDown;
153   if (link.policy == simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX) {
154     linkUp   = s4u::Link::by_name(link_id + "_UP");
155     linkDown = s4u::Link::by_name(link_id + "_DOWN");
156   } else {
157     linkUp   = s4u::Link::by_name(link_id);
158     linkDown = linkUp;
159   }
160   private_links_.insert({position, {linkUp->get_impl(), linkDown->get_impl()}});
161 }
162 } // namespace routing
163 } // namespace kernel
164
165 namespace s4u {
166 NetZone* create_cluster_zone(const std::string& name)
167 {
168   return (new kernel::routing::ClusterZone(name))->get_iface();
169 }
170 } // namespace s4u
171
172 } // namespace simgrid