1 /* Copyright (c) 2014-2021. 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/DragonflyZone.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "src/surf/network_interface.hpp"
9 #include "src/surf/xml/platf_private.hpp"
11 #include <boost/algorithm/string/classification.hpp>
12 #include <boost/algorithm/string/split.hpp>
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster_dragonfly, surf_route_cluster, "Dragonfly Routing part of surf");
21 DragonflyZone::DragonflyZone(const std::string& name) : ClusterZone(name) {}
23 DragonflyZone::Coords DragonflyZone::rankId_to_coords(int rankId) const
25 // coords : group, chassis, blade, node
27 coords.group = rankId / (num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_);
28 rankId = rankId % (num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_);
29 coords.chassis = rankId / (num_blades_per_chassis_ * num_nodes_per_blade_);
30 rankId = rankId % (num_blades_per_chassis_ * num_nodes_per_blade_);
31 coords.blade = rankId / num_nodes_per_blade_;
32 coords.node = rankId % num_nodes_per_blade_;
36 void DragonflyZone::rankId_to_coords(int rankId, unsigned int coords[4]) const // XBT_ATTRIB_DEPRECATED_v330
38 const auto s_coords = rankId_to_coords(rankId);
39 coords[0] = s_coords.group;
40 coords[1] = s_coords.chassis;
41 coords[2] = s_coords.blade;
42 coords[3] = s_coords.node;
45 void DragonflyZone::parse_specific_arguments(ClusterCreationArgs* cluster)
47 std::vector<std::string> parameters;
48 std::vector<std::string> tmp;
49 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
51 if (parameters.size() != 4) {
53 "Dragonfly are defined by the number of groups, chassis per groups, blades per chassis, nodes per blade");
56 // Blue network : number of groups, number of links between each group
57 boost::split(tmp, parameters[0], boost::is_any_of(","));
58 if (tmp.size() != 2) {
59 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
63 this->num_groups_ = std::stoi(tmp[0]);
64 } catch (const std::invalid_argument&) {
65 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
69 this->num_links_blue_ = std::stoi(tmp[1]);
70 } catch (const std::invalid_argument&) {
71 throw std::invalid_argument(std::string("Invalid number of links for the blue level:") + tmp[1]);
73 // Black network : number of chassis/group, number of links between each router on the black network
74 boost::split(tmp, parameters[1], boost::is_any_of(","));
75 if (tmp.size() != 2) {
76 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
80 this->num_chassis_per_group_ = std::stoi(tmp[0]);
81 } catch (const std::invalid_argument&) {
82 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
86 this->num_links_black_ = std::stoi(tmp[1]);
87 } catch (const std::invalid_argument&) {
88 throw std::invalid_argument(std::string("Invalid number of links for the black level:") + tmp[1]);
91 // Green network : number of blades/chassis, number of links between each router on the green network
92 boost::split(tmp, parameters[2], boost::is_any_of(","));
93 if (tmp.size() != 2) {
94 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
98 this->num_blades_per_chassis_ = std::stoi(tmp[0]);
99 } catch (const std::invalid_argument&) {
100 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
104 this->num_links_green_ = std::stoi(tmp[1]);
105 } catch (const std::invalid_argument&) {
106 throw std::invalid_argument(std::string("Invalid number of links for the green level:") + tmp[1]);
109 // The last part of topo_parameters should be the number of nodes per blade
111 this->num_nodes_per_blade_ = std::stoi(parameters[3]);
112 } catch (const std::invalid_argument&) {
113 throw std::invalid_argument(std::string("Last parameter is not the amount of nodes per blade:") + parameters[3]);
116 this->sharing_policy_ = cluster->sharing_policy;
117 if (cluster->sharing_policy == s4u::Link::SharingPolicy::SPLITDUPLEX)
118 this->num_links_per_link_ = 2;
119 this->bw_ = cluster->bw;
120 this->lat_ = cluster->lat;
123 /* Generate the cluster once every node is created */
124 void DragonflyZone::do_seal()
126 if (this->num_nodes_per_blade_ == 0) {
130 this->generate_routers();
131 this->generate_links();
134 void DragonflyZone::generate_routers()
136 this->routers_.reserve(this->num_groups_ * this->num_chassis_per_group_ * this->num_blades_per_chassis_);
137 for (unsigned int i = 0; i < this->num_groups_; i++)
138 for (unsigned int j = 0; j < this->num_chassis_per_group_; j++)
139 for (unsigned int k = 0; k < this->num_blades_per_chassis_; k++)
140 this->routers_.emplace_back(i, j, k);
143 void DragonflyZone::generate_link(const std::string& id, int numlinks, resource::LinkImpl** linkup,
144 resource::LinkImpl** linkdown) const
148 LinkCreationArgs linkTemplate;
149 linkTemplate.bandwidths.push_back(this->bw_ * numlinks);
150 linkTemplate.latency = this->lat_;
151 linkTemplate.policy = this->sharing_policy_;
152 linkTemplate.id = id;
153 sg_platf_new_link(&linkTemplate);
154 XBT_DEBUG("Generating link %s", linkTemplate.id.c_str());
155 resource::LinkImpl* link;
156 if (this->sharing_policy_ == s4u::Link::SharingPolicy::SPLITDUPLEX) {
157 *linkup = s4u::Link::by_name(linkTemplate.id + "_UP")->get_impl(); // check link?
158 *linkdown = s4u::Link::by_name(linkTemplate.id + "_DOWN")->get_impl(); // check link ?
160 link = s4u::Link::by_name(linkTemplate.id)->get_impl();
166 void DragonflyZone::generate_links()
168 static int uniqueId = 0;
169 resource::LinkImpl* linkup;
170 resource::LinkImpl* linkdown;
172 unsigned int numRouters = this->num_groups_ * this->num_chassis_per_group_ * this->num_blades_per_chassis_;
174 // Links from routers to their local nodes.
175 for (unsigned int i = 0; i < numRouters; i++) {
176 // allocate structures
177 this->routers_[i].my_nodes_.resize(num_links_per_link_ * this->num_nodes_per_blade_);
178 this->routers_[i].green_links_.resize(this->num_blades_per_chassis_);
179 this->routers_[i].black_links_.resize(this->num_chassis_per_group_);
181 for (unsigned int j = 0; j < num_links_per_link_ * this->num_nodes_per_blade_; j += num_links_per_link_) {
182 std::string id = "local_link_from_router_" + std::to_string(i) + "_to_node_" +
183 std::to_string(j / num_links_per_link_) + "_" + std::to_string(uniqueId);
184 this->generate_link(id, 1, &linkup, &linkdown);
186 this->routers_[i].my_nodes_[j] = linkup;
187 if (this->sharing_policy_ == s4u::Link::SharingPolicy::SPLITDUPLEX)
188 this->routers_[i].my_nodes_[j + 1] = linkdown;
194 // Green links from routers to same chassis routers - alltoall
195 for (unsigned int i = 0; i < this->num_groups_ * this->num_chassis_per_group_; i++) {
196 for (unsigned int j = 0; j < this->num_blades_per_chassis_; j++) {
197 for (unsigned int k = j + 1; k < this->num_blades_per_chassis_; k++) {
198 std::string id = "green_link_in_chassis_" + std::to_string(i % num_chassis_per_group_) + "_between_routers_" +
199 std::to_string(j) + "_and_" + std::to_string(k) + "_" + std::to_string(uniqueId);
200 this->generate_link(id, this->num_links_green_, &linkup, &linkdown);
202 this->routers_[i * num_blades_per_chassis_ + j].green_links_[k] = linkup;
203 this->routers_[i * num_blades_per_chassis_ + k].green_links_[j] = linkdown;
209 // Black links from routers to same group routers - alltoall
210 for (unsigned int i = 0; i < this->num_groups_; i++) {
211 for (unsigned int j = 0; j < this->num_chassis_per_group_; j++) {
212 for (unsigned int k = j + 1; k < this->num_chassis_per_group_; k++) {
213 for (unsigned int l = 0; l < this->num_blades_per_chassis_; l++) {
214 std::string id = "black_link_in_group_" + std::to_string(i) + "_between_chassis_" + std::to_string(j) +
215 "_and_" + std::to_string(k) + "_blade_" + std::to_string(l) + "_" + std::to_string(uniqueId);
216 this->generate_link(id, this->num_links_black_, &linkup, &linkdown);
218 this->routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + j * num_blades_per_chassis_ + l]
219 .black_links_[k] = linkup;
220 this->routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + k * num_blades_per_chassis_ + l]
221 .black_links_[j] = linkdown;
228 // Blue links between groups - Not all routers involved, only one per group is linked to others. Let's say router n of
229 // each group is linked to group n.
230 // FIXME: in reality blue links may be attached to several different routers
231 for (unsigned int i = 0; i < this->num_groups_; i++) {
232 for (unsigned int j = i + 1; j < this->num_groups_; j++) {
233 unsigned int routernumi = i * num_blades_per_chassis_ * num_chassis_per_group_ + j;
234 unsigned int routernumj = j * num_blades_per_chassis_ * num_chassis_per_group_ + i;
235 std::string id = "blue_link_between_group_" + std::to_string(i) + "_and_" + std::to_string(j) + "_routers_" +
236 std::to_string(routernumi) + "_and_" + std::to_string(routernumj) + "_" +
237 std::to_string(uniqueId);
238 this->generate_link(id, this->num_links_blue_, &linkup, &linkdown);
240 this->routers_[routernumi].blue_link_ = linkup;
241 this->routers_[routernumj].blue_link_ = linkdown;
247 void DragonflyZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* latency)
249 // Minimal routing version.
250 // TODO : non-minimal random one, and adaptive ?
252 if (dst->is_router() || src->is_router())
255 XBT_VERB("dragonfly getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(),
258 if ((src->id() == dst->id()) && has_loopback()) {
259 resource::LinkImpl* uplink = get_uplink_from(node_pos(src->id()));
261 route->link_list.push_back(uplink);
263 *latency += uplink->get_latency();
267 const auto myCoords = rankId_to_coords(src->id());
268 const auto targetCoords = rankId_to_coords(dst->id());
269 XBT_DEBUG("src : %u group, %u chassis, %u blade, %u node", myCoords.group, myCoords.chassis, myCoords.blade,
271 XBT_DEBUG("dst : %u group, %u chassis, %u blade, %u node", targetCoords.group, targetCoords.chassis,
272 targetCoords.blade, targetCoords.node);
274 DragonflyRouter* myRouter = &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
275 myCoords.chassis * num_blades_per_chassis_ + myCoords.blade];
276 DragonflyRouter* targetRouter = &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
277 targetCoords.chassis * num_blades_per_chassis_ + targetCoords.blade];
278 DragonflyRouter* currentRouter = myRouter;
280 // node->router local link
281 route->link_list.push_back(myRouter->my_nodes_[myCoords.node * num_links_per_link_]);
283 *latency += myRouter->my_nodes_[myCoords.node * num_links_per_link_]->get_latency();
285 if (has_limiter()) { // limiter for sender
286 route->link_list.push_back(get_uplink_from(node_pos_with_loopback(src->id())));
289 if (targetRouter != myRouter) {
290 // are we on a different group ?
291 if (targetRouter->group_ != currentRouter->group_) {
292 // go to the router of our group connected to this one.
293 if (currentRouter->blade_ != targetCoords.group) {
294 // go to the nth router in our chassis
295 route->link_list.push_back(currentRouter->green_links_[targetCoords.group]);
297 *latency += currentRouter->green_links_[targetCoords.group]->get_latency();
298 currentRouter = &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
299 myCoords.chassis * num_blades_per_chassis_ + targetCoords.group];
302 if (currentRouter->chassis_ != 0) {
303 // go to the first chassis of our group
304 route->link_list.push_back(currentRouter->black_links_[0]);
306 *latency += currentRouter->black_links_[0]->get_latency();
308 &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords.group];
311 // go to destination group - the only optical hop
312 route->link_list.push_back(currentRouter->blue_link_);
314 *latency += currentRouter->blue_link_->get_latency();
316 &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + myCoords.group];
319 // same group, but same blade ?
320 if (targetRouter->blade_ != currentRouter->blade_) {
321 route->link_list.push_back(currentRouter->green_links_[targetCoords.blade]);
323 *latency += currentRouter->green_links_[targetCoords.blade]->get_latency();
325 &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords.blade];
328 // same blade, but same chassis ?
329 if (targetRouter->chassis_ != currentRouter->chassis_) {
330 route->link_list.push_back(currentRouter->black_links_[targetCoords.chassis]);
332 *latency += currentRouter->black_links_[targetCoords.chassis]->get_latency();
336 if (has_limiter()) { // limiter for receiver
337 route->link_list.push_back(get_downlink_to(node_pos_with_loopback(dst->id())));
340 // router->node local link
341 route->link_list.push_back(
342 targetRouter->my_nodes_[targetCoords.node * num_links_per_link_ + num_links_per_link_ - 1]);
345 targetRouter->my_nodes_[targetCoords.node * num_links_per_link_ + num_links_per_link_ - 1]->get_latency();
347 } // namespace routing
348 } // namespace kernel
351 NetZone* create_dragonfly_zone(const std::string& name)
353 return (new kernel::routing::DragonflyZone(name))->get_iface();
357 } // namespace simgrid