1 /* Copyright (c) 2014-2017. 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 "src/kernel/routing/DragonflyZone.hpp"
7 #include "src/kernel/routing/NetPoint.hpp"
8 #include "src/surf/network_interface.hpp"
10 #include <boost/algorithm/string/classification.hpp>
11 #include <boost/algorithm/string/split.hpp>
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster_dragonfly, surf_route_cluster, "Dragonfly Routing part of surf");
20 DragonflyZone::DragonflyZone(NetZone* father, std::string name) : ClusterZone(father, name)
24 DragonflyZone::~DragonflyZone()
26 if (this->routers_ != nullptr) {
27 for (unsigned int i = 0; i < this->numGroups_ * this->numChassisPerGroup_ * this->numBladesPerChassis_; i++)
33 void DragonflyZone::rankId_to_coords(int rankId, unsigned int (*coords)[4])
35 // coords : group, chassis, blade, node
36 (*coords)[0] = rankId / (numChassisPerGroup_ * numBladesPerChassis_ * numNodesPerBlade_);
37 rankId = rankId % (numChassisPerGroup_ * numBladesPerChassis_ * numNodesPerBlade_);
38 (*coords)[1] = rankId / (numBladesPerChassis_ * numNodesPerBlade_);
39 rankId = rankId % (numBladesPerChassis_ * numNodesPerBlade_);
40 (*coords)[2] = rankId / numNodesPerBlade_;
41 (*coords)[3] = rankId % numNodesPerBlade_;
44 void DragonflyZone::parse_specific_arguments(ClusterCreationArgs* cluster)
46 std::vector<std::string> parameters;
47 std::vector<std::string> tmp;
48 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
50 if (parameters.size() != 4 || parameters.empty()) {
52 "Dragonfly are defined by the number of groups, chassis per groups, blades per chassis, nodes per blade");
55 // Blue network : number of groups, number of links between each group
56 boost::split(tmp, parameters[0], boost::is_any_of(","));
57 if (tmp.size() != 2) {
58 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
62 this->numGroups_ = std::stoi(tmp[0]);
63 } catch (std::invalid_argument& ia) {
64 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
68 this->numLinksBlue_ = std::stoi(tmp[1]);
69 } catch (std::invalid_argument& ia) {
70 throw std::invalid_argument(std::string("Invalid number of links for the blue level:") + tmp[1]);
72 // Black network : number of chassis/group, number of links between each router on the black network
73 boost::split(tmp, parameters[1], boost::is_any_of(","));
74 if (tmp.size() != 2) {
75 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
79 this->numChassisPerGroup_ = std::stoi(tmp[0]);
80 } catch (std::invalid_argument& ia) {
81 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
85 this->numLinksBlack_ = std::stoi(tmp[1]);
86 } catch (std::invalid_argument& ia) {
87 throw std::invalid_argument(std::string("Invalid number of links for the black level:") + tmp[1]);
90 // Green network : number of blades/chassis, number of links between each router on the green network
91 boost::split(tmp, parameters[2], boost::is_any_of(","));
92 if (tmp.size() != 2) {
93 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
97 this->numBladesPerChassis_ = std::stoi(tmp[0]);
98 } catch (std::invalid_argument& ia) {
99 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
103 this->numLinksGreen_ = std::stoi(tmp[1]);
104 } catch (std::invalid_argument& ia) {
105 throw std::invalid_argument(std::string("Invalid number of links for the green level:") + tmp[1]);
108 // The last part of topo_parameters should be the number of nodes per blade
110 this->numNodesPerBlade_ = std::stoi(parameters[3]);
111 } catch (std::invalid_argument& ia) {
112 throw std::invalid_argument(std::string("Last parameter is not the amount of nodes per blade:") + parameters[3]);
115 if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX)
116 this->numLinksperLink_ = 2;
118 this->cluster_ = cluster;
121 /* Generate the cluster once every node is created */
122 void DragonflyZone::seal()
124 if (this->numNodesPerBlade_ == 0) {
128 this->generateRouters();
129 this->generateLinks();
132 DragonflyRouter::DragonflyRouter(int group, int chassis, int blade) : group_(group), chassis_(chassis), blade_(blade)
136 DragonflyRouter::~DragonflyRouter()
139 delete[] greenLinks_;
140 delete[] blackLinks_;
144 void DragonflyZone::generateRouters()
146 this->routers_ = new DragonflyRouter*[this->numGroups_ * this->numChassisPerGroup_ * this->numBladesPerChassis_];
148 for (unsigned int i = 0; i < this->numGroups_; i++) {
149 for (unsigned int j = 0; j < this->numChassisPerGroup_; j++) {
150 for (unsigned int k = 0; k < this->numBladesPerChassis_; k++) {
151 DragonflyRouter* router = new DragonflyRouter(i, j, k);
152 this->routers_[i * this->numChassisPerGroup_ * this->numBladesPerChassis_ + j * this->numBladesPerChassis_ +
159 void DragonflyZone::createLink(const std::string& id, int numlinks, surf::LinkImpl** linkup, surf::LinkImpl** linkdown)
163 LinkCreationArgs linkTemplate;
164 linkTemplate.bandwidth = this->cluster_->bw * numlinks;
165 linkTemplate.latency = this->cluster_->lat;
166 linkTemplate.policy = this->cluster_->sharing_policy; // sthg to do with that ?
167 linkTemplate.id = id;
168 sg_platf_new_link(&linkTemplate);
169 XBT_DEBUG("Generating link %s", id.c_str());
170 surf::LinkImpl* link;
171 if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX) {
172 *linkup = surf::LinkImpl::byName(linkTemplate.id + "_UP"); // check link?
173 *linkdown = surf::LinkImpl::byName(linkTemplate.id + "_DOWN"); // check link ?
175 link = surf::LinkImpl::byName(linkTemplate.id);
181 void DragonflyZone::generateLinks()
183 static int uniqueId = 0;
184 surf::LinkImpl* linkup;
185 surf::LinkImpl* linkdown;
187 unsigned int numRouters = this->numGroups_ * this->numChassisPerGroup_ * this->numBladesPerChassis_;
189 // Links from routers to their local nodes.
190 for (unsigned int i = 0; i < numRouters; i++) {
191 // allocate structures
192 this->routers_[i]->myNodes_ = new surf::LinkImpl*[numLinksperLink_ * this->numNodesPerBlade_];
193 this->routers_[i]->greenLinks_ = new surf::LinkImpl*[this->numBladesPerChassis_];
194 this->routers_[i]->blackLinks_ = new surf::LinkImpl*[this->numChassisPerGroup_];
196 for (unsigned int j = 0; j < numLinksperLink_ * this->numNodesPerBlade_; j += numLinksperLink_) {
197 std::string id = "local_link_from_router_"+ std::to_string(i) + "_to_node_" +
198 std::to_string(j / numLinksperLink_) + "_" + std::to_string(uniqueId);
199 this->createLink(id, 1, &linkup, &linkdown);
201 this->routers_[i]->myNodes_[j] = linkup;
202 if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX)
203 this->routers_[i]->myNodes_[j + 1] = linkdown;
209 // Green links from routers to same chassis routers - alltoall
210 for (unsigned int i = 0; i < this->numGroups_ * this->numChassisPerGroup_; i++) {
211 for (unsigned int j = 0; j < this->numBladesPerChassis_; j++) {
212 for (unsigned int k = j + 1; k < this->numBladesPerChassis_; k++) {
213 std::string id = "green_link_in_chassis_" + std::to_string(i % numChassisPerGroup_) +"_between_routers_" +
214 std::to_string(j) + "_and_" + std::to_string(k) + "_" + std::to_string(uniqueId);
215 this->createLink(id, this->numLinksGreen_, &linkup, &linkdown);
217 this->routers_[i * numBladesPerChassis_ + j]->greenLinks_[k] = linkup;
218 this->routers_[i * numBladesPerChassis_ + k]->greenLinks_[j] = linkdown;
224 // Black links from routers to same group routers - alltoall
225 for (unsigned int i = 0; i < this->numGroups_; i++) {
226 for (unsigned int j = 0; j < this->numChassisPerGroup_; j++) {
227 for (unsigned int k = j + 1; k < this->numChassisPerGroup_; k++) {
228 for (unsigned int l = 0; l < this->numBladesPerChassis_; l++) {
229 std::string id = "black_link_in_group_" + std::to_string(i) + "_between_chassis_" + std::to_string(j) +
230 "_and_" + std::to_string(k) +"_blade_" + std::to_string(l) + "_" + std::to_string(uniqueId);
231 this->createLink(id, this->numLinksBlack_, &linkup, &linkdown);
233 this->routers_[i * numBladesPerChassis_ * numChassisPerGroup_ + j * numBladesPerChassis_ + l]
234 ->blackLinks_[k] = linkup;
235 this->routers_[i * numBladesPerChassis_ * numChassisPerGroup_ + k * numBladesPerChassis_ + l]
236 ->blackLinks_[j] = linkdown;
243 // Blue links between groups - Not all routers involved, only one per group is linked to others. Let's say router n of
244 // each group is linked to group n.
245 // FIXME: in reality blue links may be attached to several different routers
246 for (unsigned int i = 0; i < this->numGroups_; i++) {
247 for (unsigned int j = i + 1; j < this->numGroups_; j++) {
248 unsigned int routernumi = i * numBladesPerChassis_ * numChassisPerGroup_ + j;
249 unsigned int routernumj = j * numBladesPerChassis_ * numChassisPerGroup_ + i;
250 this->routers_[routernumi]->blueLinks_ = new surf::LinkImpl*;
251 this->routers_[routernumj]->blueLinks_ = new surf::LinkImpl*;
252 std::string id = "blue_link_between_group_"+ std::to_string(i) +"_and_" + std::to_string(j) +"_routers_" +
253 std::to_string(routernumi) + "_and_" + std::to_string(routernumj) + "_" + std::to_string(uniqueId);
254 this->createLink(id, this->numLinksBlue_, &linkup, &linkdown);
256 this->routers_[routernumi]->blueLinks_[0] = linkup;
257 this->routers_[routernumj]->blueLinks_[0] = linkdown;
263 void DragonflyZone::getLocalRoute(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* latency)
265 // Minimal routing version.
266 // TODO : non-minimal random one, and adaptive ?
268 if (dst->isRouter() || src->isRouter())
271 XBT_VERB("dragonfly getLocalRoute from '%s'[%u] to '%s'[%u]", src->getCname(), src->id(), dst->getCname(), dst->id());
273 if ((src->id() == dst->id()) && hasLoopback_) {
274 std::pair<surf::LinkImpl*, surf::LinkImpl*> info = privateLinks_.at(nodePosition(src->id()));
276 route->link_list.push_back(info.first);
278 *latency += info.first->latency();
282 unsigned int myCoords[4];
283 rankId_to_coords(src->id(), &myCoords);
284 unsigned int targetCoords[4];
285 rankId_to_coords(dst->id(), &targetCoords);
286 XBT_DEBUG("src : %u group, %u chassis, %u blade, %u node", myCoords[0], myCoords[1], myCoords[2], myCoords[3]);
287 XBT_DEBUG("dst : %u group, %u chassis, %u blade, %u node", targetCoords[0], targetCoords[1], targetCoords[2],
290 DragonflyRouter* myRouter = routers_[myCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) +
291 myCoords[1] * numBladesPerChassis_ + myCoords[2]];
292 DragonflyRouter* targetRouter = routers_[targetCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) +
293 targetCoords[1] * numBladesPerChassis_ + targetCoords[2]];
294 DragonflyRouter* currentRouter = myRouter;
296 // node->router local link
297 route->link_list.push_back(myRouter->myNodes_[myCoords[3] * numLinksperLink_]);
299 *latency += myRouter->myNodes_[myCoords[3] * numLinksperLink_]->latency();
301 if (hasLimiter_) { // limiter for sender
302 std::pair<surf::LinkImpl*, surf::LinkImpl*> info = privateLinks_.at(nodePositionWithLoopback(src->id()));
303 route->link_list.push_back(info.first);
306 if (targetRouter != myRouter) {
308 // are we on a different group ?
309 if (targetRouter->group_ != currentRouter->group_) {
310 // go to the router of our group connected to this one.
311 if (currentRouter->blade_ != targetCoords[0]) {
312 // go to the nth router in our chassis
313 route->link_list.push_back(currentRouter->greenLinks_[targetCoords[0]]);
315 *latency += currentRouter->greenLinks_[targetCoords[0]]->latency();
316 currentRouter = routers_[myCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) +
317 myCoords[1] * numBladesPerChassis_ + targetCoords[0]];
320 if (currentRouter->chassis_ != 0) {
321 // go to the first chassis of our group
322 route->link_list.push_back(currentRouter->blackLinks_[0]);
324 *latency += currentRouter->blackLinks_[0]->latency();
325 currentRouter = routers_[myCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) + targetCoords[0]];
328 // go to destination group - the only optical hop
329 route->link_list.push_back(currentRouter->blueLinks_[0]);
331 *latency += currentRouter->blueLinks_[0]->latency();
332 currentRouter = routers_[targetCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) + myCoords[0]];
335 // same group, but same blade ?
336 if (targetRouter->blade_ != currentRouter->blade_) {
337 route->link_list.push_back(currentRouter->greenLinks_[targetCoords[2]]);
339 *latency += currentRouter->greenLinks_[targetCoords[2]]->latency();
340 currentRouter = routers_[targetCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) + targetCoords[2]];
343 // same blade, but same chassis ?
344 if (targetRouter->chassis_ != currentRouter->chassis_) {
345 route->link_list.push_back(currentRouter->blackLinks_[targetCoords[1]]);
347 *latency += currentRouter->blackLinks_[targetCoords[1]]->latency();
351 if (hasLimiter_) { // limiter for receiver
352 std::pair<surf::LinkImpl*, surf::LinkImpl*> info = privateLinks_.at(nodePositionWithLoopback(dst->id()));
353 route->link_list.push_back(info.first);
356 // router->node local link
357 route->link_list.push_back(targetRouter->myNodes_[targetCoords[3] * numLinksperLink_ + numLinksperLink_ - 1]);
359 *latency += targetRouter->myNodes_[targetCoords[3] * numLinksperLink_ + numLinksperLink_ - 1]->latency();