1 /* Copyright (c) 2014-2018. 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(NetZone* father, std::string name) : ClusterZone(father, name)
25 DragonflyZone::~DragonflyZone()
27 if (this->routers_ != nullptr) {
28 for (unsigned int i = 0; i < this->num_groups_ * this->num_chassis_per_group_ * this->num_blades_per_chassis_; i++)
34 void DragonflyZone::rankId_to_coords(int rankId, unsigned int coords[4])
36 // coords : group, chassis, blade, node
37 coords[0] = rankId / (num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_);
38 rankId = rankId % (num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_);
39 coords[1] = rankId / (num_blades_per_chassis_ * num_nodes_per_blade_);
40 rankId = rankId % (num_blades_per_chassis_ * num_nodes_per_blade_);
41 coords[2] = rankId / num_nodes_per_blade_;
42 coords[3] = rankId % num_nodes_per_blade_;
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 || parameters.empty()) {
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 (std::invalid_argument& ia) {
65 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
69 this->num_links_blue_ = std::stoi(tmp[1]);
70 } catch (std::invalid_argument& ia) {
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 (std::invalid_argument& ia) {
82 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
86 this->num_links_black_ = std::stoi(tmp[1]);
87 } catch (std::invalid_argument& ia) {
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 (std::invalid_argument& ia) {
100 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
104 this->num_links_green_ = std::stoi(tmp[1]);
105 } catch (std::invalid_argument& ia) {
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 (std::invalid_argument& ia) {
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::seal()
126 if (this->num_nodes_per_blade_ == 0) {
130 this->generate_routers();
131 this->generate_links();
134 DragonflyRouter::DragonflyRouter(int group, int chassis, int blade) : group_(group), chassis_(chassis), blade_(blade)
138 DragonflyRouter::~DragonflyRouter()
141 delete[] green_links_;
142 delete[] black_links_;
146 void DragonflyZone::generate_routers()
149 new DragonflyRouter*[this->num_groups_ * this->num_chassis_per_group_ * this->num_blades_per_chassis_];
151 for (unsigned int i = 0; i < this->num_groups_; i++) {
152 for (unsigned int j = 0; j < this->num_chassis_per_group_; j++) {
153 for (unsigned int k = 0; k < this->num_blades_per_chassis_; k++) {
154 DragonflyRouter* router = new DragonflyRouter(i, j, k);
155 this->routers_[i * this->num_chassis_per_group_ * this->num_blades_per_chassis_ +
156 j * this->num_blades_per_chassis_ + k] = router;
162 void DragonflyZone::create_link(const std::string& id, int numlinks, resource::LinkImpl** linkup,
163 resource::LinkImpl** linkdown)
167 LinkCreationArgs linkTemplate;
168 linkTemplate.bandwidth = this->bw_ * numlinks;
169 linkTemplate.latency = this->lat_;
170 linkTemplate.policy = this->sharing_policy_;
171 linkTemplate.id = id;
172 sg_platf_new_link(&linkTemplate);
173 XBT_DEBUG("Generating link %s", id.c_str());
174 resource::LinkImpl* link;
175 if (this->sharing_policy_ == s4u::Link::SharingPolicy::SPLITDUPLEX) {
176 *linkup = s4u::Link::by_name(linkTemplate.id + "_UP")->get_impl(); // check link?
177 *linkdown = s4u::Link::by_name(linkTemplate.id + "_DOWN")->get_impl(); // check link ?
179 link = s4u::Link::by_name(linkTemplate.id)->get_impl();
185 void DragonflyZone::generate_links()
187 static int uniqueId = 0;
188 resource::LinkImpl* linkup;
189 resource::LinkImpl* linkdown;
191 unsigned int numRouters = this->num_groups_ * this->num_chassis_per_group_ * this->num_blades_per_chassis_;
193 // Links from routers to their local nodes.
194 for (unsigned int i = 0; i < numRouters; i++) {
195 // allocate structures
196 this->routers_[i]->my_nodes_ = new resource::LinkImpl*[num_links_per_link_ * this->num_nodes_per_blade_];
197 this->routers_[i]->green_links_ = new resource::LinkImpl*[this->num_blades_per_chassis_];
198 this->routers_[i]->black_links_ = new resource::LinkImpl*[this->num_chassis_per_group_];
200 for (unsigned int j = 0; j < num_links_per_link_ * this->num_nodes_per_blade_; j += num_links_per_link_) {
201 std::string id = "local_link_from_router_" + std::to_string(i) + "_to_node_" +
202 std::to_string(j / num_links_per_link_) + "_" + std::to_string(uniqueId);
203 this->create_link(id, 1, &linkup, &linkdown);
205 this->routers_[i]->my_nodes_[j] = linkup;
206 if (this->sharing_policy_ == s4u::Link::SharingPolicy::SPLITDUPLEX)
207 this->routers_[i]->my_nodes_[j + 1] = linkdown;
213 // Green links from routers to same chassis routers - alltoall
214 for (unsigned int i = 0; i < this->num_groups_ * this->num_chassis_per_group_; i++) {
215 for (unsigned int j = 0; j < this->num_blades_per_chassis_; j++) {
216 for (unsigned int k = j + 1; k < this->num_blades_per_chassis_; k++) {
217 std::string id = "green_link_in_chassis_" + std::to_string(i % num_chassis_per_group_) + "_between_routers_" +
218 std::to_string(j) + "_and_" + std::to_string(k) + "_" + std::to_string(uniqueId);
219 this->create_link(id, this->num_links_green_, &linkup, &linkdown);
221 this->routers_[i * num_blades_per_chassis_ + j]->green_links_[k] = linkup;
222 this->routers_[i * num_blades_per_chassis_ + k]->green_links_[j] = linkdown;
228 // Black links from routers to same group routers - alltoall
229 for (unsigned int i = 0; i < this->num_groups_; i++) {
230 for (unsigned int j = 0; j < this->num_chassis_per_group_; j++) {
231 for (unsigned int k = j + 1; k < this->num_chassis_per_group_; k++) {
232 for (unsigned int l = 0; l < this->num_blades_per_chassis_; l++) {
233 std::string id = "black_link_in_group_" + std::to_string(i) + "_between_chassis_" + std::to_string(j) +
234 "_and_" + std::to_string(k) +"_blade_" + std::to_string(l) + "_" + std::to_string(uniqueId);
235 this->create_link(id, this->num_links_black_, &linkup, &linkdown);
237 this->routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + j * num_blades_per_chassis_ + l]
238 ->black_links_[k] = linkup;
239 this->routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + k * num_blades_per_chassis_ + l]
240 ->black_links_[j] = linkdown;
247 // Blue links between groups - Not all routers involved, only one per group is linked to others. Let's say router n of
248 // each group is linked to group n.
249 // FIXME: in reality blue links may be attached to several different routers
250 for (unsigned int i = 0; i < this->num_groups_; i++) {
251 for (unsigned int j = i + 1; j < this->num_groups_; j++) {
252 unsigned int routernumi = i * num_blades_per_chassis_ * num_chassis_per_group_ + j;
253 unsigned int routernumj = j * num_blades_per_chassis_ * num_chassis_per_group_ + i;
254 this->routers_[routernumi]->blue_links_ = new resource::LinkImpl*;
255 this->routers_[routernumj]->blue_links_ = new resource::LinkImpl*;
256 std::string id = "blue_link_between_group_"+ std::to_string(i) +"_and_" + std::to_string(j) +"_routers_" +
257 std::to_string(routernumi) + "_and_" + std::to_string(routernumj) + "_" + std::to_string(uniqueId);
258 this->create_link(id, this->num_links_blue_, &linkup, &linkdown);
260 this->routers_[routernumi]->blue_links_[0] = linkup;
261 this->routers_[routernumj]->blue_links_[0] = linkdown;
267 void DragonflyZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* latency)
269 // Minimal routing version.
270 // TODO : non-minimal random one, and adaptive ?
272 if (dst->is_router() || src->is_router())
275 XBT_VERB("dragonfly getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(),
278 if ((src->id() == dst->id()) && has_loopback_) {
279 std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos(src->id()));
281 route->link_list.push_back(info.first);
283 *latency += info.first->get_latency();
287 unsigned int myCoords[4];
288 rankId_to_coords(src->id(), myCoords);
289 unsigned int targetCoords[4];
290 rankId_to_coords(dst->id(), targetCoords);
291 XBT_DEBUG("src : %u group, %u chassis, %u blade, %u node", myCoords[0], myCoords[1], myCoords[2], myCoords[3]);
292 XBT_DEBUG("dst : %u group, %u chassis, %u blade, %u node", targetCoords[0], targetCoords[1], targetCoords[2],
295 DragonflyRouter* myRouter = routers_[myCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) +
296 myCoords[1] * num_blades_per_chassis_ + myCoords[2]];
297 DragonflyRouter* targetRouter = routers_[targetCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) +
298 targetCoords[1] * num_blades_per_chassis_ + targetCoords[2]];
299 DragonflyRouter* currentRouter = myRouter;
301 // node->router local link
302 route->link_list.push_back(myRouter->my_nodes_[myCoords[3] * num_links_per_link_]);
304 *latency += myRouter->my_nodes_[myCoords[3] * num_links_per_link_]->get_latency();
306 if (has_limiter_) { // limiter for sender
307 std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos_with_loopback(src->id()));
308 route->link_list.push_back(info.first);
311 if (targetRouter != myRouter) {
313 // are we on a different group ?
314 if (targetRouter->group_ != currentRouter->group_) {
315 // go to the router of our group connected to this one.
316 if (currentRouter->blade_ != targetCoords[0]) {
317 // go to the nth router in our chassis
318 route->link_list.push_back(currentRouter->green_links_[targetCoords[0]]);
320 *latency += currentRouter->green_links_[targetCoords[0]]->get_latency();
321 currentRouter = routers_[myCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) +
322 myCoords[1] * num_blades_per_chassis_ + targetCoords[0]];
325 if (currentRouter->chassis_ != 0) {
326 // go to the first chassis of our group
327 route->link_list.push_back(currentRouter->black_links_[0]);
329 *latency += currentRouter->black_links_[0]->get_latency();
330 currentRouter = routers_[myCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords[0]];
333 // go to destination group - the only optical hop
334 route->link_list.push_back(currentRouter->blue_links_[0]);
336 *latency += currentRouter->blue_links_[0]->get_latency();
337 currentRouter = routers_[targetCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) + myCoords[0]];
340 // same group, but same blade ?
341 if (targetRouter->blade_ != currentRouter->blade_) {
342 route->link_list.push_back(currentRouter->green_links_[targetCoords[2]]);
344 *latency += currentRouter->green_links_[targetCoords[2]]->get_latency();
345 currentRouter = routers_[targetCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords[2]];
348 // same blade, but same chassis ?
349 if (targetRouter->chassis_ != currentRouter->chassis_) {
350 route->link_list.push_back(currentRouter->black_links_[targetCoords[1]]);
352 *latency += currentRouter->black_links_[targetCoords[1]]->get_latency();
356 if (has_limiter_) { // limiter for receiver
357 std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos_with_loopback(dst->id()));
358 route->link_list.push_back(info.first);
361 // router->node local link
362 route->link_list.push_back(targetRouter->my_nodes_[targetCoords[3] * num_links_per_link_ + num_links_per_link_ - 1]);
364 *latency += targetRouter->my_nodes_[targetCoords[3] * num_links_per_link_ + num_links_per_link_ - 1]->get_latency();