1 /* Copyright (c) 2014-2019. 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 void DragonflyZone::rankId_to_coords(int rankId, unsigned int coords[4])
28 // coords : group, chassis, blade, node
29 coords[0] = rankId / (num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_);
30 rankId = rankId % (num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_);
31 coords[1] = rankId / (num_blades_per_chassis_ * num_nodes_per_blade_);
32 rankId = rankId % (num_blades_per_chassis_ * num_nodes_per_blade_);
33 coords[2] = rankId / num_nodes_per_blade_;
34 coords[3] = rankId % num_nodes_per_blade_;
37 void DragonflyZone::parse_specific_arguments(ClusterCreationArgs* cluster)
39 std::vector<std::string> parameters;
40 std::vector<std::string> tmp;
41 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
43 if (parameters.size() != 4 || parameters.empty()) {
45 "Dragonfly are defined by the number of groups, chassis per groups, blades per chassis, nodes per blade");
48 // Blue network : number of groups, number of links between each group
49 boost::split(tmp, parameters[0], boost::is_any_of(","));
50 if (tmp.size() != 2) {
51 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
55 this->num_groups_ = std::stoi(tmp[0]);
56 } catch (const std::invalid_argument&) {
57 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
61 this->num_links_blue_ = std::stoi(tmp[1]);
62 } catch (const std::invalid_argument&) {
63 throw std::invalid_argument(std::string("Invalid number of links for the blue level:") + tmp[1]);
65 // Black network : number of chassis/group, number of links between each router on the black network
66 boost::split(tmp, parameters[1], boost::is_any_of(","));
67 if (tmp.size() != 2) {
68 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
72 this->num_chassis_per_group_ = std::stoi(tmp[0]);
73 } catch (const std::invalid_argument&) {
74 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
78 this->num_links_black_ = std::stoi(tmp[1]);
79 } catch (const std::invalid_argument&) {
80 throw std::invalid_argument(std::string("Invalid number of links for the black level:") + tmp[1]);
83 // Green network : number of blades/chassis, number of links between each router on the green network
84 boost::split(tmp, parameters[2], boost::is_any_of(","));
85 if (tmp.size() != 2) {
86 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
90 this->num_blades_per_chassis_ = std::stoi(tmp[0]);
91 } catch (const std::invalid_argument&) {
92 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
96 this->num_links_green_ = std::stoi(tmp[1]);
97 } catch (const std::invalid_argument&) {
98 throw std::invalid_argument(std::string("Invalid number of links for the green level:") + tmp[1]);
101 // The last part of topo_parameters should be the number of nodes per blade
103 this->num_nodes_per_blade_ = std::stoi(parameters[3]);
104 } catch (const std::invalid_argument&) {
105 throw std::invalid_argument(std::string("Last parameter is not the amount of nodes per blade:") + parameters[3]);
108 this->sharing_policy_ = cluster->sharing_policy;
109 if (cluster->sharing_policy == s4u::Link::SharingPolicy::SPLITDUPLEX)
110 this->num_links_per_link_ = 2;
111 this->bw_ = cluster->bw;
112 this->lat_ = cluster->lat;
115 /* Generate the cluster once every node is created */
116 void DragonflyZone::seal()
118 if (this->num_nodes_per_blade_ == 0) {
122 this->generate_routers();
123 this->generate_links();
126 void DragonflyZone::generate_routers()
128 this->routers_.reserve(this->num_groups_ * this->num_chassis_per_group_ * this->num_blades_per_chassis_);
129 for (unsigned int i = 0; i < this->num_groups_; i++)
130 for (unsigned int j = 0; j < this->num_chassis_per_group_; j++)
131 for (unsigned int k = 0; k < this->num_blades_per_chassis_; k++)
132 this->routers_.emplace_back(i, j, k);
135 void DragonflyZone::create_link(const std::string& id, int numlinks, resource::LinkImpl** linkup,
136 resource::LinkImpl** linkdown)
140 LinkCreationArgs linkTemplate;
141 linkTemplate.bandwidth = this->bw_ * numlinks;
142 linkTemplate.latency = this->lat_;
143 linkTemplate.policy = this->sharing_policy_;
144 linkTemplate.id = std::move(id);
145 sg_platf_new_link(&linkTemplate);
146 XBT_DEBUG("Generating link %s", linkTemplate.id.c_str());
147 resource::LinkImpl* link;
148 if (this->sharing_policy_ == s4u::Link::SharingPolicy::SPLITDUPLEX) {
149 *linkup = s4u::Link::by_name(linkTemplate.id + "_UP")->get_impl(); // check link?
150 *linkdown = s4u::Link::by_name(linkTemplate.id + "_DOWN")->get_impl(); // check link ?
152 link = s4u::Link::by_name(linkTemplate.id)->get_impl();
158 void DragonflyZone::generate_links()
160 static int uniqueId = 0;
161 resource::LinkImpl* linkup;
162 resource::LinkImpl* linkdown;
164 unsigned int numRouters = this->num_groups_ * this->num_chassis_per_group_ * this->num_blades_per_chassis_;
166 // Links from routers to their local nodes.
167 for (unsigned int i = 0; i < numRouters; i++) {
168 // allocate structures
169 this->routers_[i].my_nodes_.resize(num_links_per_link_ * this->num_nodes_per_blade_);
170 this->routers_[i].green_links_.resize(this->num_blades_per_chassis_);
171 this->routers_[i].black_links_.resize(this->num_chassis_per_group_);
173 for (unsigned int j = 0; j < num_links_per_link_ * this->num_nodes_per_blade_; j += num_links_per_link_) {
174 std::string id = "local_link_from_router_" + std::to_string(i) + "_to_node_" +
175 std::to_string(j / num_links_per_link_) + "_" + std::to_string(uniqueId);
176 this->create_link(id, 1, &linkup, &linkdown);
178 this->routers_[i].my_nodes_[j] = linkup;
179 if (this->sharing_policy_ == s4u::Link::SharingPolicy::SPLITDUPLEX)
180 this->routers_[i].my_nodes_[j + 1] = linkdown;
186 // Green links from routers to same chassis routers - alltoall
187 for (unsigned int i = 0; i < this->num_groups_ * this->num_chassis_per_group_; i++) {
188 for (unsigned int j = 0; j < this->num_blades_per_chassis_; j++) {
189 for (unsigned int k = j + 1; k < this->num_blades_per_chassis_; k++) {
190 std::string id = "green_link_in_chassis_" + std::to_string(i % num_chassis_per_group_) + "_between_routers_" +
191 std::to_string(j) + "_and_" + std::to_string(k) + "_" + std::to_string(uniqueId);
192 this->create_link(id, this->num_links_green_, &linkup, &linkdown);
194 this->routers_[i * num_blades_per_chassis_ + j].green_links_[k] = linkup;
195 this->routers_[i * num_blades_per_chassis_ + k].green_links_[j] = linkdown;
201 // Black links from routers to same group routers - alltoall
202 for (unsigned int i = 0; i < this->num_groups_; i++) {
203 for (unsigned int j = 0; j < this->num_chassis_per_group_; j++) {
204 for (unsigned int k = j + 1; k < this->num_chassis_per_group_; k++) {
205 for (unsigned int l = 0; l < this->num_blades_per_chassis_; l++) {
206 std::string id = "black_link_in_group_" + std::to_string(i) + "_between_chassis_" + std::to_string(j) +
207 "_and_" + std::to_string(k) +"_blade_" + std::to_string(l) + "_" + std::to_string(uniqueId);
208 this->create_link(id, this->num_links_black_, &linkup, &linkdown);
210 this->routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + j * num_blades_per_chassis_ + l]
211 .black_links_[k] = linkup;
212 this->routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + k * num_blades_per_chassis_ + l]
213 .black_links_[j] = linkdown;
220 // Blue links between groups - Not all routers involved, only one per group is linked to others. Let's say router n of
221 // each group is linked to group n.
222 // FIXME: in reality blue links may be attached to several different routers
223 for (unsigned int i = 0; i < this->num_groups_; i++) {
224 for (unsigned int j = i + 1; j < this->num_groups_; j++) {
225 unsigned int routernumi = i * num_blades_per_chassis_ * num_chassis_per_group_ + j;
226 unsigned int routernumj = j * num_blades_per_chassis_ * num_chassis_per_group_ + i;
227 std::string id = "blue_link_between_group_"+ std::to_string(i) +"_and_" + std::to_string(j) +"_routers_" +
228 std::to_string(routernumi) + "_and_" + std::to_string(routernumj) + "_" + std::to_string(uniqueId);
229 this->create_link(id, this->num_links_blue_, &linkup, &linkdown);
231 this->routers_[routernumi].blue_link_ = linkup;
232 this->routers_[routernumj].blue_link_ = linkdown;
238 void DragonflyZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* latency)
240 // Minimal routing version.
241 // TODO : non-minimal random one, and adaptive ?
243 if (dst->is_router() || src->is_router())
246 XBT_VERB("dragonfly getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(),
249 if ((src->id() == dst->id()) && has_loopback_) {
250 std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos(src->id()));
252 route->link_list.push_back(info.first);
254 *latency += info.first->get_latency();
258 unsigned int myCoords[4];
259 rankId_to_coords(src->id(), myCoords);
260 unsigned int targetCoords[4];
261 rankId_to_coords(dst->id(), targetCoords);
262 XBT_DEBUG("src : %u group, %u chassis, %u blade, %u node", myCoords[0], myCoords[1], myCoords[2], myCoords[3]);
263 XBT_DEBUG("dst : %u group, %u chassis, %u blade, %u node", targetCoords[0], targetCoords[1], targetCoords[2],
266 DragonflyRouter* myRouter = &routers_[myCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) +
267 myCoords[1] * num_blades_per_chassis_ + myCoords[2]];
268 DragonflyRouter* targetRouter = &routers_[targetCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) +
269 targetCoords[1] * num_blades_per_chassis_ + targetCoords[2]];
270 DragonflyRouter* currentRouter = myRouter;
272 // node->router local link
273 route->link_list.push_back(myRouter->my_nodes_[myCoords[3] * num_links_per_link_]);
275 *latency += myRouter->my_nodes_[myCoords[3] * num_links_per_link_]->get_latency();
277 if (has_limiter_) { // limiter for sender
278 std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos_with_loopback(src->id()));
279 route->link_list.push_back(info.first);
282 if (targetRouter != myRouter) {
284 // are we on a different group ?
285 if (targetRouter->group_ != currentRouter->group_) {
286 // go to the router of our group connected to this one.
287 if (currentRouter->blade_ != targetCoords[0]) {
288 // go to the nth router in our chassis
289 route->link_list.push_back(currentRouter->green_links_[targetCoords[0]]);
291 *latency += currentRouter->green_links_[targetCoords[0]]->get_latency();
292 currentRouter = &routers_[myCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) +
293 myCoords[1] * num_blades_per_chassis_ + targetCoords[0]];
296 if (currentRouter->chassis_ != 0) {
297 // go to the first chassis of our group
298 route->link_list.push_back(currentRouter->black_links_[0]);
300 *latency += currentRouter->black_links_[0]->get_latency();
301 currentRouter = &routers_[myCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords[0]];
304 // go to destination group - the only optical hop
305 route->link_list.push_back(currentRouter->blue_link_);
307 *latency += currentRouter->blue_link_->get_latency();
308 currentRouter = &routers_[targetCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) + myCoords[0]];
311 // same group, but same blade ?
312 if (targetRouter->blade_ != currentRouter->blade_) {
313 route->link_list.push_back(currentRouter->green_links_[targetCoords[2]]);
315 *latency += currentRouter->green_links_[targetCoords[2]]->get_latency();
316 currentRouter = &routers_[targetCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords[2]];
319 // same blade, but same chassis ?
320 if (targetRouter->chassis_ != currentRouter->chassis_) {
321 route->link_list.push_back(currentRouter->black_links_[targetCoords[1]]);
323 *latency += currentRouter->black_links_[targetCoords[1]]->get_latency();
327 if (has_limiter_) { // limiter for receiver
328 std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos_with_loopback(dst->id()));
329 route->link_list.push_back(info.first);
332 // router->node local link
333 route->link_list.push_back(targetRouter->my_nodes_[targetCoords[3] * num_links_per_link_ + num_links_per_link_ - 1]);
335 *latency += targetRouter->my_nodes_[targetCoords[3] * num_links_per_link_ + num_links_per_link_ - 1]->get_latency();