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->numGroups_ * this->numChassisPerGroup_ * this->numBladesPerChassis_; i++)
34 void DragonflyZone::rankId_to_coords(int rankId, unsigned int (*coords)[4])
36 // coords : group, chassis, blade, node
37 (*coords)[0] = rankId / (numChassisPerGroup_ * numBladesPerChassis_ * numNodesPerBlade_);
38 rankId = rankId % (numChassisPerGroup_ * numBladesPerChassis_ * numNodesPerBlade_);
39 (*coords)[1] = rankId / (numBladesPerChassis_ * numNodesPerBlade_);
40 rankId = rankId % (numBladesPerChassis_ * numNodesPerBlade_);
41 (*coords)[2] = rankId / numNodesPerBlade_;
42 (*coords)[3] = rankId % numNodesPerBlade_;
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->numGroups_ = 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->numLinksBlue_ = 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->numChassisPerGroup_ = 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->numLinksBlack_ = 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->numBladesPerChassis_ = 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->numLinksGreen_ = 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->numNodesPerBlade_ = 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 if (cluster->sharing_policy == SURF_LINK_SPLITDUPLEX)
117 this->numLinksperLink_ = 2;
119 this->cluster_ = cluster;
122 /* Generate the cluster once every node is created */
123 void DragonflyZone::seal()
125 if (this->numNodesPerBlade_ == 0) {
129 this->generateRouters();
130 this->generateLinks();
133 DragonflyRouter::DragonflyRouter(int group, int chassis, int blade) : group_(group), chassis_(chassis), blade_(blade)
137 DragonflyRouter::~DragonflyRouter()
140 delete[] greenLinks_;
141 delete[] blackLinks_;
145 void DragonflyZone::generateRouters()
147 this->routers_ = new DragonflyRouter*[this->numGroups_ * this->numChassisPerGroup_ * this->numBladesPerChassis_];
149 for (unsigned int i = 0; i < this->numGroups_; i++) {
150 for (unsigned int j = 0; j < this->numChassisPerGroup_; j++) {
151 for (unsigned int k = 0; k < this->numBladesPerChassis_; k++) {
152 DragonflyRouter* router = new DragonflyRouter(i, j, k);
153 this->routers_[i * this->numChassisPerGroup_ * this->numBladesPerChassis_ + j * this->numBladesPerChassis_ +
160 void DragonflyZone::createLink(const std::string& id, int numlinks, surf::LinkImpl** linkup, surf::LinkImpl** linkdown)
164 LinkCreationArgs linkTemplate;
165 linkTemplate.bandwidth = this->cluster_->bw * numlinks;
166 linkTemplate.latency = this->cluster_->lat;
167 linkTemplate.policy = this->cluster_->sharing_policy; // sthg to do with that ?
168 linkTemplate.id = id;
169 sg_platf_new_link(&linkTemplate);
170 XBT_DEBUG("Generating link %s", id.c_str());
171 surf::LinkImpl* link;
172 if (this->cluster_->sharing_policy == SURF_LINK_SPLITDUPLEX) {
173 *linkup = surf::LinkImpl::byName(linkTemplate.id + "_UP"); // check link?
174 *linkdown = surf::LinkImpl::byName(linkTemplate.id + "_DOWN"); // check link ?
176 link = surf::LinkImpl::byName(linkTemplate.id);
182 void DragonflyZone::generateLinks()
184 static int uniqueId = 0;
185 surf::LinkImpl* linkup;
186 surf::LinkImpl* linkdown;
188 unsigned int numRouters = this->numGroups_ * this->numChassisPerGroup_ * this->numBladesPerChassis_;
190 // Links from routers to their local nodes.
191 for (unsigned int i = 0; i < numRouters; i++) {
192 // allocate structures
193 this->routers_[i]->myNodes_ = new surf::LinkImpl*[numLinksperLink_ * this->numNodesPerBlade_];
194 this->routers_[i]->greenLinks_ = new surf::LinkImpl*[this->numBladesPerChassis_];
195 this->routers_[i]->blackLinks_ = new surf::LinkImpl*[this->numChassisPerGroup_];
197 for (unsigned int j = 0; j < numLinksperLink_ * this->numNodesPerBlade_; j += numLinksperLink_) {
198 std::string id = "local_link_from_router_"+ std::to_string(i) + "_to_node_" +
199 std::to_string(j / numLinksperLink_) + "_" + std::to_string(uniqueId);
200 this->createLink(id, 1, &linkup, &linkdown);
202 this->routers_[i]->myNodes_[j] = linkup;
203 if (this->cluster_->sharing_policy == SURF_LINK_SPLITDUPLEX)
204 this->routers_[i]->myNodes_[j + 1] = linkdown;
210 // Green links from routers to same chassis routers - alltoall
211 for (unsigned int i = 0; i < this->numGroups_ * this->numChassisPerGroup_; i++) {
212 for (unsigned int j = 0; j < this->numBladesPerChassis_; j++) {
213 for (unsigned int k = j + 1; k < this->numBladesPerChassis_; k++) {
214 std::string id = "green_link_in_chassis_" + std::to_string(i % numChassisPerGroup_) +"_between_routers_" +
215 std::to_string(j) + "_and_" + std::to_string(k) + "_" + std::to_string(uniqueId);
216 this->createLink(id, this->numLinksGreen_, &linkup, &linkdown);
218 this->routers_[i * numBladesPerChassis_ + j]->greenLinks_[k] = linkup;
219 this->routers_[i * numBladesPerChassis_ + k]->greenLinks_[j] = linkdown;
225 // Black links from routers to same group routers - alltoall
226 for (unsigned int i = 0; i < this->numGroups_; i++) {
227 for (unsigned int j = 0; j < this->numChassisPerGroup_; j++) {
228 for (unsigned int k = j + 1; k < this->numChassisPerGroup_; k++) {
229 for (unsigned int l = 0; l < this->numBladesPerChassis_; l++) {
230 std::string id = "black_link_in_group_" + std::to_string(i) + "_between_chassis_" + std::to_string(j) +
231 "_and_" + std::to_string(k) +"_blade_" + std::to_string(l) + "_" + std::to_string(uniqueId);
232 this->createLink(id, this->numLinksBlack_, &linkup, &linkdown);
234 this->routers_[i * numBladesPerChassis_ * numChassisPerGroup_ + j * numBladesPerChassis_ + l]
235 ->blackLinks_[k] = linkup;
236 this->routers_[i * numBladesPerChassis_ * numChassisPerGroup_ + k * numBladesPerChassis_ + l]
237 ->blackLinks_[j] = linkdown;
244 // Blue links between groups - Not all routers involved, only one per group is linked to others. Let's say router n of
245 // each group is linked to group n.
246 // FIXME: in reality blue links may be attached to several different routers
247 for (unsigned int i = 0; i < this->numGroups_; i++) {
248 for (unsigned int j = i + 1; j < this->numGroups_; j++) {
249 unsigned int routernumi = i * numBladesPerChassis_ * numChassisPerGroup_ + j;
250 unsigned int routernumj = j * numBladesPerChassis_ * numChassisPerGroup_ + i;
251 this->routers_[routernumi]->blueLinks_ = new surf::LinkImpl*;
252 this->routers_[routernumj]->blueLinks_ = new surf::LinkImpl*;
253 std::string id = "blue_link_between_group_"+ std::to_string(i) +"_and_" + std::to_string(j) +"_routers_" +
254 std::to_string(routernumi) + "_and_" + std::to_string(routernumj) + "_" + std::to_string(uniqueId);
255 this->createLink(id, this->numLinksBlue_, &linkup, &linkdown);
257 this->routers_[routernumi]->blueLinks_[0] = linkup;
258 this->routers_[routernumj]->blueLinks_[0] = linkdown;
264 void DragonflyZone::getLocalRoute(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* latency)
266 // Minimal routing version.
267 // TODO : non-minimal random one, and adaptive ?
269 if (dst->isRouter() || src->isRouter())
272 XBT_VERB("dragonfly getLocalRoute from '%s'[%u] to '%s'[%u]", src->getCname(), src->id(), dst->getCname(), dst->id());
274 if ((src->id() == dst->id()) && hasLoopback_) {
275 std::pair<surf::LinkImpl*, surf::LinkImpl*> info = privateLinks_.at(nodePosition(src->id()));
277 route->link_list.push_back(info.first);
279 *latency += info.first->latency();
283 unsigned int myCoords[4];
284 rankId_to_coords(src->id(), &myCoords);
285 unsigned int targetCoords[4];
286 rankId_to_coords(dst->id(), &targetCoords);
287 XBT_DEBUG("src : %u group, %u chassis, %u blade, %u node", myCoords[0], myCoords[1], myCoords[2], myCoords[3]);
288 XBT_DEBUG("dst : %u group, %u chassis, %u blade, %u node", targetCoords[0], targetCoords[1], targetCoords[2],
291 DragonflyRouter* myRouter = routers_[myCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) +
292 myCoords[1] * numBladesPerChassis_ + myCoords[2]];
293 DragonflyRouter* targetRouter = routers_[targetCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) +
294 targetCoords[1] * numBladesPerChassis_ + targetCoords[2]];
295 DragonflyRouter* currentRouter = myRouter;
297 // node->router local link
298 route->link_list.push_back(myRouter->myNodes_[myCoords[3] * numLinksperLink_]);
300 *latency += myRouter->myNodes_[myCoords[3] * numLinksperLink_]->latency();
302 if (hasLimiter_) { // limiter for sender
303 std::pair<surf::LinkImpl*, surf::LinkImpl*> info = privateLinks_.at(nodePositionWithLoopback(src->id()));
304 route->link_list.push_back(info.first);
307 if (targetRouter != myRouter) {
309 // are we on a different group ?
310 if (targetRouter->group_ != currentRouter->group_) {
311 // go to the router of our group connected to this one.
312 if (currentRouter->blade_ != targetCoords[0]) {
313 // go to the nth router in our chassis
314 route->link_list.push_back(currentRouter->greenLinks_[targetCoords[0]]);
316 *latency += currentRouter->greenLinks_[targetCoords[0]]->latency();
317 currentRouter = routers_[myCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) +
318 myCoords[1] * numBladesPerChassis_ + targetCoords[0]];
321 if (currentRouter->chassis_ != 0) {
322 // go to the first chassis of our group
323 route->link_list.push_back(currentRouter->blackLinks_[0]);
325 *latency += currentRouter->blackLinks_[0]->latency();
326 currentRouter = routers_[myCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) + targetCoords[0]];
329 // go to destination group - the only optical hop
330 route->link_list.push_back(currentRouter->blueLinks_[0]);
332 *latency += currentRouter->blueLinks_[0]->latency();
333 currentRouter = routers_[targetCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) + myCoords[0]];
336 // same group, but same blade ?
337 if (targetRouter->blade_ != currentRouter->blade_) {
338 route->link_list.push_back(currentRouter->greenLinks_[targetCoords[2]]);
340 *latency += currentRouter->greenLinks_[targetCoords[2]]->latency();
341 currentRouter = routers_[targetCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) + targetCoords[2]];
344 // same blade, but same chassis ?
345 if (targetRouter->chassis_ != currentRouter->chassis_) {
346 route->link_list.push_back(currentRouter->blackLinks_[targetCoords[1]]);
348 *latency += currentRouter->blackLinks_[targetCoords[1]]->latency();
352 if (hasLimiter_) { // limiter for receiver
353 std::pair<surf::LinkImpl*, surf::LinkImpl*> info = privateLinks_.at(nodePositionWithLoopback(dst->id()));
354 route->link_list.push_back(info.first);
357 // router->node local link
358 route->link_list.push_back(targetRouter->myNodes_[targetCoords[3] * numLinksperLink_ + numLinksperLink_ - 1]);
360 *latency += targetRouter->myNodes_[targetCoords[3] * numLinksperLink_ + numLinksperLink_ - 1]->latency();