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(NetZoneImpl* father, const std::string& name, resource::NetworkModel* netmodel)
22 : ClusterZone(father, name, netmodel)
26 DragonflyZone::Coords DragonflyZone::rankId_to_coords(int rankId) const
28 // coords : group, chassis, blade, node
30 coords.group = rankId / (num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_);
31 rankId = rankId % (num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_);
32 coords.chassis = rankId / (num_blades_per_chassis_ * num_nodes_per_blade_);
33 rankId = rankId % (num_blades_per_chassis_ * num_nodes_per_blade_);
34 coords.blade = rankId / num_nodes_per_blade_;
35 coords.node = rankId % num_nodes_per_blade_;
39 void DragonflyZone::rankId_to_coords(int rankId, unsigned int coords[4]) const // XBT_ATTRIB_DEPRECATED_v330
41 const auto s_coords = rankId_to_coords(rankId);
42 coords[0] = s_coords.group;
43 coords[1] = s_coords.chassis;
44 coords[2] = s_coords.blade;
45 coords[3] = s_coords.node;
48 void DragonflyZone::parse_specific_arguments(ClusterCreationArgs* cluster)
50 std::vector<std::string> parameters;
51 std::vector<std::string> tmp;
52 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
54 if (parameters.size() != 4) {
56 "Dragonfly are defined by the number of groups, chassis per groups, blades per chassis, nodes per blade");
59 // Blue network : number of groups, number of links between each group
60 boost::split(tmp, parameters[0], boost::is_any_of(","));
61 if (tmp.size() != 2) {
62 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
66 this->num_groups_ = std::stoi(tmp[0]);
67 } catch (const std::invalid_argument&) {
68 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
72 this->num_links_blue_ = std::stoi(tmp[1]);
73 } catch (const std::invalid_argument&) {
74 throw std::invalid_argument(std::string("Invalid number of links for the blue level:") + tmp[1]);
76 // Black network : number of chassis/group, number of links between each router on the black network
77 boost::split(tmp, parameters[1], boost::is_any_of(","));
78 if (tmp.size() != 2) {
79 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
83 this->num_chassis_per_group_ = std::stoi(tmp[0]);
84 } catch (const std::invalid_argument&) {
85 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
89 this->num_links_black_ = std::stoi(tmp[1]);
90 } catch (const std::invalid_argument&) {
91 throw std::invalid_argument(std::string("Invalid number of links for the black level:") + tmp[1]);
94 // Green network : number of blades/chassis, number of links between each router on the green network
95 boost::split(tmp, parameters[2], boost::is_any_of(","));
96 if (tmp.size() != 2) {
97 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
101 this->num_blades_per_chassis_ = std::stoi(tmp[0]);
102 } catch (const std::invalid_argument&) {
103 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
107 this->num_links_green_ = std::stoi(tmp[1]);
108 } catch (const std::invalid_argument&) {
109 throw std::invalid_argument(std::string("Invalid number of links for the green level:") + tmp[1]);
112 // The last part of topo_parameters should be the number of nodes per blade
114 this->num_nodes_per_blade_ = std::stoi(parameters[3]);
115 } catch (const std::invalid_argument&) {
116 throw std::invalid_argument(std::string("Last parameter is not the amount of nodes per blade:") + parameters[3]);
119 this->sharing_policy_ = cluster->sharing_policy;
120 if (cluster->sharing_policy == s4u::Link::SharingPolicy::SPLITDUPLEX)
121 this->num_links_per_link_ = 2;
122 this->bw_ = cluster->bw;
123 this->lat_ = cluster->lat;
126 /* Generate the cluster once every node is created */
127 void DragonflyZone::seal()
129 if (this->num_nodes_per_blade_ == 0) {
133 this->generate_routers();
134 this->generate_links();
137 void DragonflyZone::generate_routers()
139 this->routers_.reserve(this->num_groups_ * this->num_chassis_per_group_ * this->num_blades_per_chassis_);
140 for (unsigned int i = 0; i < this->num_groups_; i++)
141 for (unsigned int j = 0; j < this->num_chassis_per_group_; j++)
142 for (unsigned int k = 0; k < this->num_blades_per_chassis_; k++)
143 this->routers_.emplace_back(i, j, k);
146 void DragonflyZone::generate_link(const std::string& id, int numlinks, resource::LinkImpl** linkup,
147 resource::LinkImpl** linkdown) const
151 LinkCreationArgs linkTemplate;
152 linkTemplate.bandwidths.push_back(this->bw_ * numlinks);
153 linkTemplate.latency = this->lat_;
154 linkTemplate.policy = this->sharing_policy_;
155 linkTemplate.id = id;
156 sg_platf_new_link(&linkTemplate);
157 XBT_DEBUG("Generating link %s", linkTemplate.id.c_str());
158 resource::LinkImpl* link;
159 if (this->sharing_policy_ == s4u::Link::SharingPolicy::SPLITDUPLEX) {
160 *linkup = s4u::Link::by_name(linkTemplate.id + "_UP")->get_impl(); // check link?
161 *linkdown = s4u::Link::by_name(linkTemplate.id + "_DOWN")->get_impl(); // check link ?
163 link = s4u::Link::by_name(linkTemplate.id)->get_impl();
169 void DragonflyZone::generate_links()
171 static int uniqueId = 0;
172 resource::LinkImpl* linkup;
173 resource::LinkImpl* linkdown;
175 unsigned int numRouters = this->num_groups_ * this->num_chassis_per_group_ * this->num_blades_per_chassis_;
177 // Links from routers to their local nodes.
178 for (unsigned int i = 0; i < numRouters; i++) {
179 // allocate structures
180 this->routers_[i].my_nodes_.resize(num_links_per_link_ * this->num_nodes_per_blade_);
181 this->routers_[i].green_links_.resize(this->num_blades_per_chassis_);
182 this->routers_[i].black_links_.resize(this->num_chassis_per_group_);
184 for (unsigned int j = 0; j < num_links_per_link_ * this->num_nodes_per_blade_; j += num_links_per_link_) {
185 std::string id = "local_link_from_router_" + std::to_string(i) + "_to_node_" +
186 std::to_string(j / num_links_per_link_) + "_" + std::to_string(uniqueId);
187 this->generate_link(id, 1, &linkup, &linkdown);
189 this->routers_[i].my_nodes_[j] = linkup;
190 if (this->sharing_policy_ == s4u::Link::SharingPolicy::SPLITDUPLEX)
191 this->routers_[i].my_nodes_[j + 1] = linkdown;
197 // Green links from routers to same chassis routers - alltoall
198 for (unsigned int i = 0; i < this->num_groups_ * this->num_chassis_per_group_; i++) {
199 for (unsigned int j = 0; j < this->num_blades_per_chassis_; j++) {
200 for (unsigned int k = j + 1; k < this->num_blades_per_chassis_; k++) {
201 std::string id = "green_link_in_chassis_" + std::to_string(i % num_chassis_per_group_) + "_between_routers_" +
202 std::to_string(j) + "_and_" + std::to_string(k) + "_" + std::to_string(uniqueId);
203 this->generate_link(id, this->num_links_green_, &linkup, &linkdown);
205 this->routers_[i * num_blades_per_chassis_ + j].green_links_[k] = linkup;
206 this->routers_[i * num_blades_per_chassis_ + k].green_links_[j] = linkdown;
212 // Black links from routers to same group routers - alltoall
213 for (unsigned int i = 0; i < this->num_groups_; i++) {
214 for (unsigned int j = 0; j < this->num_chassis_per_group_; j++) {
215 for (unsigned int k = j + 1; k < this->num_chassis_per_group_; k++) {
216 for (unsigned int l = 0; l < this->num_blades_per_chassis_; l++) {
217 std::string id = "black_link_in_group_" + std::to_string(i) + "_between_chassis_" + std::to_string(j) +
218 "_and_" + std::to_string(k) +"_blade_" + std::to_string(l) + "_" + std::to_string(uniqueId);
219 this->generate_link(id, this->num_links_black_, &linkup, &linkdown);
221 this->routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + j * num_blades_per_chassis_ + l]
222 .black_links_[k] = linkup;
223 this->routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + k * num_blades_per_chassis_ + l]
224 .black_links_[j] = linkdown;
231 // Blue links between groups - Not all routers involved, only one per group is linked to others. Let's say router n of
232 // each group is linked to group n.
233 // FIXME: in reality blue links may be attached to several different routers
234 for (unsigned int i = 0; i < this->num_groups_; i++) {
235 for (unsigned int j = i + 1; j < this->num_groups_; j++) {
236 unsigned int routernumi = i * num_blades_per_chassis_ * num_chassis_per_group_ + j;
237 unsigned int routernumj = j * num_blades_per_chassis_ * num_chassis_per_group_ + i;
238 std::string id = "blue_link_between_group_"+ std::to_string(i) +"_and_" + std::to_string(j) +"_routers_" +
239 std::to_string(routernumi) + "_and_" + std::to_string(routernumj) + "_" + std::to_string(uniqueId);
240 this->generate_link(id, this->num_links_blue_, &linkup, &linkdown);
242 this->routers_[routernumi].blue_link_ = linkup;
243 this->routers_[routernumj].blue_link_ = linkdown;
249 void DragonflyZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* latency)
251 // Minimal routing version.
252 // TODO : non-minimal random one, and adaptive ?
254 if (dst->is_router() || src->is_router())
257 XBT_VERB("dragonfly getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(),
260 if ((src->id() == dst->id()) && has_loopback_) {
261 std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos(src->id()));
263 route->link_list.push_back(info.first);
265 *latency += info.first->get_latency();
269 const auto myCoords = rankId_to_coords(src->id());
270 const auto targetCoords = rankId_to_coords(dst->id());
271 XBT_DEBUG("src : %u group, %u chassis, %u blade, %u node", myCoords.group, myCoords.chassis, myCoords.blade,
273 XBT_DEBUG("dst : %u group, %u chassis, %u blade, %u node", targetCoords.group, targetCoords.chassis,
274 targetCoords.blade, targetCoords.node);
276 DragonflyRouter* myRouter = &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
277 myCoords.chassis * num_blades_per_chassis_ + myCoords.blade];
278 DragonflyRouter* targetRouter = &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
279 targetCoords.chassis * num_blades_per_chassis_ + targetCoords.blade];
280 DragonflyRouter* currentRouter = myRouter;
282 // node->router local link
283 route->link_list.push_back(myRouter->my_nodes_[myCoords.node * num_links_per_link_]);
285 *latency += myRouter->my_nodes_[myCoords.node * num_links_per_link_]->get_latency();
287 if (has_limiter_) { // limiter for sender
288 std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos_with_loopback(src->id()));
289 route->link_list.push_back(info.first);
292 if (targetRouter != myRouter) {
293 // are we on a different group ?
294 if (targetRouter->group_ != currentRouter->group_) {
295 // go to the router of our group connected to this one.
296 if (currentRouter->blade_ != targetCoords.group) {
297 // go to the nth router in our chassis
298 route->link_list.push_back(currentRouter->green_links_[targetCoords.group]);
300 *latency += currentRouter->green_links_[targetCoords.group]->get_latency();
301 currentRouter = &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
302 myCoords.chassis * num_blades_per_chassis_ + targetCoords.group];
305 if (currentRouter->chassis_ != 0) {
306 // go to the first chassis of our group
307 route->link_list.push_back(currentRouter->black_links_[0]);
309 *latency += currentRouter->black_links_[0]->get_latency();
311 &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords.group];
314 // go to destination group - the only optical hop
315 route->link_list.push_back(currentRouter->blue_link_);
317 *latency += currentRouter->blue_link_->get_latency();
319 &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + myCoords.group];
322 // same group, but same blade ?
323 if (targetRouter->blade_ != currentRouter->blade_) {
324 route->link_list.push_back(currentRouter->green_links_[targetCoords.blade]);
326 *latency += currentRouter->green_links_[targetCoords.blade]->get_latency();
328 &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords.blade];
331 // same blade, but same chassis ?
332 if (targetRouter->chassis_ != currentRouter->chassis_) {
333 route->link_list.push_back(currentRouter->black_links_[targetCoords.chassis]);
335 *latency += currentRouter->black_links_[targetCoords.chassis]->get_latency();
339 if (has_limiter_) { // limiter for receiver
340 std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos_with_loopback(dst->id()));
341 route->link_list.push_back(info.first);
344 // router->node local link
345 route->link_list.push_back(
346 targetRouter->my_nodes_[targetCoords.node * num_links_per_link_ + num_links_per_link_ - 1]);
349 targetRouter->my_nodes_[targetCoords.node * num_links_per_link_ + num_links_per_link_ - 1]->get_latency();