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(NetZoneImpl* father, std::string name, resource::NetworkModel* netmodel)
22 : ClusterZone(father, name, netmodel)
26 DragonflyZone::~DragonflyZone()
28 if (this->routers_ != nullptr) {
29 for (unsigned int i = 0; i < this->num_groups_ * this->num_chassis_per_group_ * this->num_blades_per_chassis_; i++)
35 void DragonflyZone::rankId_to_coords(int rankId, unsigned int coords[4])
37 // coords : group, chassis, blade, node
38 coords[0] = rankId / (num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_);
39 rankId = rankId % (num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_);
40 coords[1] = rankId / (num_blades_per_chassis_ * num_nodes_per_blade_);
41 rankId = rankId % (num_blades_per_chassis_ * num_nodes_per_blade_);
42 coords[2] = rankId / num_nodes_per_blade_;
43 coords[3] = rankId % num_nodes_per_blade_;
46 void DragonflyZone::parse_specific_arguments(ClusterCreationArgs* cluster)
48 std::vector<std::string> parameters;
49 std::vector<std::string> tmp;
50 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
52 if (parameters.size() != 4 || parameters.empty()) {
54 "Dragonfly are defined by the number of groups, chassis per groups, blades per chassis, nodes per blade");
57 // Blue network : number of groups, number of links between each group
58 boost::split(tmp, parameters[0], boost::is_any_of(","));
59 if (tmp.size() != 2) {
60 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
64 this->num_groups_ = std::stoi(tmp[0]);
65 } catch (std::invalid_argument& ia) {
66 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
70 this->num_links_blue_ = std::stoi(tmp[1]);
71 } catch (std::invalid_argument& ia) {
72 throw std::invalid_argument(std::string("Invalid number of links for the blue level:") + tmp[1]);
74 // Black network : number of chassis/group, number of links between each router on the black network
75 boost::split(tmp, parameters[1], boost::is_any_of(","));
76 if (tmp.size() != 2) {
77 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
81 this->num_chassis_per_group_ = std::stoi(tmp[0]);
82 } catch (std::invalid_argument& ia) {
83 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
87 this->num_links_black_ = std::stoi(tmp[1]);
88 } catch (std::invalid_argument& ia) {
89 throw std::invalid_argument(std::string("Invalid number of links for the black level:") + tmp[1]);
92 // Green network : number of blades/chassis, number of links between each router on the green network
93 boost::split(tmp, parameters[2], boost::is_any_of(","));
94 if (tmp.size() != 2) {
95 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
99 this->num_blades_per_chassis_ = std::stoi(tmp[0]);
100 } catch (std::invalid_argument& ia) {
101 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
105 this->num_links_green_ = std::stoi(tmp[1]);
106 } catch (std::invalid_argument& ia) {
107 throw std::invalid_argument(std::string("Invalid number of links for the green level:") + tmp[1]);
110 // The last part of topo_parameters should be the number of nodes per blade
112 this->num_nodes_per_blade_ = std::stoi(parameters[3]);
113 } catch (std::invalid_argument& ia) {
114 throw std::invalid_argument(std::string("Last parameter is not the amount of nodes per blade:") + parameters[3]);
117 this->sharing_policy_ = cluster->sharing_policy;
118 if (cluster->sharing_policy == s4u::Link::SharingPolicy::SPLITDUPLEX)
119 this->num_links_per_link_ = 2;
120 this->bw_ = cluster->bw;
121 this->lat_ = cluster->lat;
124 /* Generate the cluster once every node is created */
125 void DragonflyZone::seal()
127 if (this->num_nodes_per_blade_ == 0) {
131 this->generate_routers();
132 this->generate_links();
135 DragonflyRouter::DragonflyRouter(int group, int chassis, int blade) : group_(group), chassis_(chassis), blade_(blade)
139 DragonflyRouter::~DragonflyRouter()
142 delete[] green_links_;
143 delete[] black_links_;
147 void DragonflyZone::generate_routers()
150 new DragonflyRouter*[this->num_groups_ * this->num_chassis_per_group_ * this->num_blades_per_chassis_];
152 for (unsigned int i = 0; i < this->num_groups_; i++) {
153 for (unsigned int j = 0; j < this->num_chassis_per_group_; j++) {
154 for (unsigned int k = 0; k < this->num_blades_per_chassis_; k++) {
155 DragonflyRouter* router = new DragonflyRouter(i, j, k);
156 this->routers_[i * this->num_chassis_per_group_ * this->num_blades_per_chassis_ +
157 j * this->num_blades_per_chassis_ + k] = router;
163 void DragonflyZone::create_link(const std::string& id, int numlinks, resource::LinkImpl** linkup,
164 resource::LinkImpl** linkdown)
168 LinkCreationArgs linkTemplate;
169 linkTemplate.bandwidth = this->bw_ * numlinks;
170 linkTemplate.latency = this->lat_;
171 linkTemplate.policy = this->sharing_policy_;
172 linkTemplate.id = id;
173 sg_platf_new_link(&linkTemplate);
174 XBT_DEBUG("Generating link %s", id.c_str());
175 resource::LinkImpl* link;
176 if (this->sharing_policy_ == s4u::Link::SharingPolicy::SPLITDUPLEX) {
177 *linkup = s4u::Link::by_name(linkTemplate.id + "_UP")->get_impl(); // check link?
178 *linkdown = s4u::Link::by_name(linkTemplate.id + "_DOWN")->get_impl(); // check link ?
180 link = s4u::Link::by_name(linkTemplate.id)->get_impl();
186 void DragonflyZone::generate_links()
188 static int uniqueId = 0;
189 resource::LinkImpl* linkup;
190 resource::LinkImpl* linkdown;
192 unsigned int numRouters = this->num_groups_ * this->num_chassis_per_group_ * this->num_blades_per_chassis_;
194 // Links from routers to their local nodes.
195 for (unsigned int i = 0; i < numRouters; i++) {
196 // allocate structures
197 this->routers_[i]->my_nodes_ = new resource::LinkImpl*[num_links_per_link_ * this->num_nodes_per_blade_];
198 this->routers_[i]->green_links_ = new resource::LinkImpl*[this->num_blades_per_chassis_];
199 this->routers_[i]->black_links_ = new resource::LinkImpl*[this->num_chassis_per_group_];
201 for (unsigned int j = 0; j < num_links_per_link_ * this->num_nodes_per_blade_; j += num_links_per_link_) {
202 std::string id = "local_link_from_router_" + std::to_string(i) + "_to_node_" +
203 std::to_string(j / num_links_per_link_) + "_" + std::to_string(uniqueId);
204 this->create_link(id, 1, &linkup, &linkdown);
206 this->routers_[i]->my_nodes_[j] = linkup;
207 if (this->sharing_policy_ == s4u::Link::SharingPolicy::SPLITDUPLEX)
208 this->routers_[i]->my_nodes_[j + 1] = linkdown;
214 // Green links from routers to same chassis routers - alltoall
215 for (unsigned int i = 0; i < this->num_groups_ * this->num_chassis_per_group_; i++) {
216 for (unsigned int j = 0; j < this->num_blades_per_chassis_; j++) {
217 for (unsigned int k = j + 1; k < this->num_blades_per_chassis_; k++) {
218 std::string id = "green_link_in_chassis_" + std::to_string(i % num_chassis_per_group_) + "_between_routers_" +
219 std::to_string(j) + "_and_" + std::to_string(k) + "_" + std::to_string(uniqueId);
220 this->create_link(id, this->num_links_green_, &linkup, &linkdown);
222 this->routers_[i * num_blades_per_chassis_ + j]->green_links_[k] = linkup;
223 this->routers_[i * num_blades_per_chassis_ + k]->green_links_[j] = linkdown;
229 // Black links from routers to same group routers - alltoall
230 for (unsigned int i = 0; i < this->num_groups_; i++) {
231 for (unsigned int j = 0; j < this->num_chassis_per_group_; j++) {
232 for (unsigned int k = j + 1; k < this->num_chassis_per_group_; k++) {
233 for (unsigned int l = 0; l < this->num_blades_per_chassis_; l++) {
234 std::string id = "black_link_in_group_" + std::to_string(i) + "_between_chassis_" + std::to_string(j) +
235 "_and_" + std::to_string(k) +"_blade_" + std::to_string(l) + "_" + std::to_string(uniqueId);
236 this->create_link(id, this->num_links_black_, &linkup, &linkdown);
238 this->routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + j * num_blades_per_chassis_ + l]
239 ->black_links_[k] = linkup;
240 this->routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + k * num_blades_per_chassis_ + l]
241 ->black_links_[j] = linkdown;
248 // Blue links between groups - Not all routers involved, only one per group is linked to others. Let's say router n of
249 // each group is linked to group n.
250 // FIXME: in reality blue links may be attached to several different routers
251 for (unsigned int i = 0; i < this->num_groups_; i++) {
252 for (unsigned int j = i + 1; j < this->num_groups_; j++) {
253 unsigned int routernumi = i * num_blades_per_chassis_ * num_chassis_per_group_ + j;
254 unsigned int routernumj = j * num_blades_per_chassis_ * num_chassis_per_group_ + i;
255 this->routers_[routernumi]->blue_links_ = new resource::LinkImpl*;
256 this->routers_[routernumj]->blue_links_ = new resource::LinkImpl*;
257 std::string id = "blue_link_between_group_"+ std::to_string(i) +"_and_" + std::to_string(j) +"_routers_" +
258 std::to_string(routernumi) + "_and_" + std::to_string(routernumj) + "_" + std::to_string(uniqueId);
259 this->create_link(id, this->num_links_blue_, &linkup, &linkdown);
261 this->routers_[routernumi]->blue_links_[0] = linkup;
262 this->routers_[routernumj]->blue_links_[0] = linkdown;
268 void DragonflyZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* latency)
270 // Minimal routing version.
271 // TODO : non-minimal random one, and adaptive ?
273 if (dst->is_router() || src->is_router())
276 XBT_VERB("dragonfly getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(),
279 if ((src->id() == dst->id()) && has_loopback_) {
280 std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos(src->id()));
282 route->link_list.push_back(info.first);
284 *latency += info.first->get_latency();
288 unsigned int myCoords[4];
289 rankId_to_coords(src->id(), myCoords);
290 unsigned int targetCoords[4];
291 rankId_to_coords(dst->id(), targetCoords);
292 XBT_DEBUG("src : %u group, %u chassis, %u blade, %u node", myCoords[0], myCoords[1], myCoords[2], myCoords[3]);
293 XBT_DEBUG("dst : %u group, %u chassis, %u blade, %u node", targetCoords[0], targetCoords[1], targetCoords[2],
296 DragonflyRouter* myRouter = routers_[myCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) +
297 myCoords[1] * num_blades_per_chassis_ + myCoords[2]];
298 DragonflyRouter* targetRouter = routers_[targetCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) +
299 targetCoords[1] * num_blades_per_chassis_ + targetCoords[2]];
300 DragonflyRouter* currentRouter = myRouter;
302 // node->router local link
303 route->link_list.push_back(myRouter->my_nodes_[myCoords[3] * num_links_per_link_]);
305 *latency += myRouter->my_nodes_[myCoords[3] * num_links_per_link_]->get_latency();
307 if (has_limiter_) { // limiter for sender
308 std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos_with_loopback(src->id()));
309 route->link_list.push_back(info.first);
312 if (targetRouter != myRouter) {
314 // are we on a different group ?
315 if (targetRouter->group_ != currentRouter->group_) {
316 // go to the router of our group connected to this one.
317 if (currentRouter->blade_ != targetCoords[0]) {
318 // go to the nth router in our chassis
319 route->link_list.push_back(currentRouter->green_links_[targetCoords[0]]);
321 *latency += currentRouter->green_links_[targetCoords[0]]->get_latency();
322 currentRouter = routers_[myCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) +
323 myCoords[1] * num_blades_per_chassis_ + targetCoords[0]];
326 if (currentRouter->chassis_ != 0) {
327 // go to the first chassis of our group
328 route->link_list.push_back(currentRouter->black_links_[0]);
330 *latency += currentRouter->black_links_[0]->get_latency();
331 currentRouter = routers_[myCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords[0]];
334 // go to destination group - the only optical hop
335 route->link_list.push_back(currentRouter->blue_links_[0]);
337 *latency += currentRouter->blue_links_[0]->get_latency();
338 currentRouter = routers_[targetCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) + myCoords[0]];
341 // same group, but same blade ?
342 if (targetRouter->blade_ != currentRouter->blade_) {
343 route->link_list.push_back(currentRouter->green_links_[targetCoords[2]]);
345 *latency += currentRouter->green_links_[targetCoords[2]]->get_latency();
346 currentRouter = routers_[targetCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords[2]];
349 // same blade, but same chassis ?
350 if (targetRouter->chassis_ != currentRouter->chassis_) {
351 route->link_list.push_back(currentRouter->black_links_[targetCoords[1]]);
353 *latency += currentRouter->black_links_[targetCoords[1]]->get_latency();
357 if (has_limiter_) { // limiter for receiver
358 std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos_with_loopback(dst->id()));
359 route->link_list.push_back(info.first);
362 // router->node local link
363 route->link_list.push_back(targetRouter->my_nodes_[targetCoords[3] * num_links_per_link_ + num_links_per_link_ - 1]);
365 *latency += targetRouter->my_nodes_[targetCoords[3] * num_links_per_link_ + num_links_per_link_ - 1]->get_latency();