Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
74241c8d17fe87bae09430417f86f1faeef055bd
[simgrid.git] / src / kernel / routing / DragonflyZone.cpp
1 /* Copyright (c) 2014-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/kernel/routing/DragonflyZone.hpp"
7 #include "src/kernel/routing/NetPoint.hpp"
8 #include "src/surf/network_interface.hpp"
9
10 #include <boost/algorithm/string/classification.hpp>
11 #include <boost/algorithm/string/split.hpp>
12 #include <string>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster_dragonfly, surf_route_cluster, "Dragonfly Routing part of surf");
15
16 namespace simgrid {
17 namespace kernel {
18 namespace routing {
19
20 DragonflyZone::DragonflyZone(NetZone* father, std::string name) : ClusterZone(father, name)
21 {
22 }
23
24 DragonflyZone::~DragonflyZone()
25 {
26   if (this->routers_ != nullptr) {
27     for (unsigned int i = 0; i < this->numGroups_ * this->numChassisPerGroup_ * this->numBladesPerChassis_; i++)
28       delete routers_[i];
29     delete[] routers_;
30   }
31 }
32
33 void DragonflyZone::rankId_to_coords(int rankId, unsigned int (*coords)[4])
34 {
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_;
42 }
43
44 void DragonflyZone::parse_specific_arguments(ClusterCreationArgs* cluster)
45 {
46   std::vector<std::string> parameters;
47   std::vector<std::string> tmp;
48   boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
49
50   if (parameters.size() != 4 || parameters.empty()) {
51     surf_parse_error(
52         "Dragonfly are defined by the number of groups, chassis per groups, blades per chassis, nodes per blade");
53   }
54
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");
59   }
60
61   try {
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]);
65   }
66
67   try {
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]);
71   }
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");
76   }
77
78   try {
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]);
82   }
83
84   try {
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]);
88   }
89
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");
94   }
95
96   try {
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]);
100   }
101
102   try {
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]);
106   }
107
108   // The last part of topo_parameters should be the number of nodes per blade
109   try {
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]);
113   }
114
115   this->cluster_ = cluster;
116 }
117
118 /*
119 * Generate the cluster once every node is created
120 */
121 void DragonflyZone::seal()
122 {
123   if (this->numNodesPerBlade_ == 0) {
124     return;
125   }
126
127   this->generateRouters();
128   this->generateLinks();
129 }
130
131 DragonflyRouter::DragonflyRouter(int group, int chassis, int blade) : group_(group), chassis_(chassis), blade_(blade)
132 {
133 }
134
135 DragonflyRouter::~DragonflyRouter()
136 {
137   delete[] myNodes_;
138   delete[] greenLinks_;
139   delete[] blackLinks_;
140   delete blueLinks_;
141 }
142
143 void DragonflyZone::generateRouters()
144 {
145   this->routers_ = new DragonflyRouter*[this->numGroups_ * this->numChassisPerGroup_ * this->numBladesPerChassis_];
146
147   for (unsigned int i = 0; i < this->numGroups_; i++) {
148     for (unsigned int j = 0; j < this->numChassisPerGroup_; j++) {
149       for (unsigned int k = 0; k < this->numBladesPerChassis_; k++) {
150         DragonflyRouter* router = new DragonflyRouter(i, j, k);
151         this->routers_[i * this->numChassisPerGroup_ * this->numBladesPerChassis_ + j * this->numBladesPerChassis_ +
152                        k] = router;
153       }
154     }
155   }
156 }
157
158 void DragonflyZone::createLink(const std::string& id, int numlinks, surf::LinkImpl** linkup, surf::LinkImpl** linkdown)
159 {
160   *linkup   = nullptr;
161   *linkdown = nullptr;
162   LinkCreationArgs linkTemplate;
163   linkTemplate.bandwidth = this->cluster_->bw * numlinks;
164   linkTemplate.latency   = this->cluster_->lat;
165   linkTemplate.policy    = this->cluster_->sharing_policy; // sthg to do with that ?
166   linkTemplate.id        = id;
167   sg_platf_new_link(&linkTemplate);
168   XBT_DEBUG("Generating link %s", id.c_str());
169   surf::LinkImpl* link;
170   std::string tmpID;
171   if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX) {
172     tmpID     = linkTemplate.id + "_UP";
173     link      = surf::LinkImpl::byName(tmpID);
174     *linkup   = link; // check link?
175     tmpID     = linkTemplate.id + "_DOWN";
176     link      = surf::LinkImpl::byName(tmpID);
177     *linkdown = link; // check link ?
178   } else {
179     link      = surf::LinkImpl::byName(linkTemplate.id);
180     *linkup   = link;
181     *linkdown = link;
182   }
183 }
184
185 void DragonflyZone::generateLinks()
186 {
187   static int uniqueId = 0;
188   surf::LinkImpl* linkup;
189   surf::LinkImpl* linkdown;
190
191   unsigned int numRouters = this->numGroups_ * this->numChassisPerGroup_ * this->numBladesPerChassis_;
192
193   if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX)
194     numLinksperLink_ = 2;
195
196   // Links from routers to their local nodes.
197   for (unsigned int i = 0; i < numRouters; i++) {
198     // allocate structures
199     this->routers_[i]->myNodes_    = new surf::LinkImpl*[numLinksperLink_ * this->numNodesPerBlade_];
200     this->routers_[i]->greenLinks_ = new surf::LinkImpl*[this->numBladesPerChassis_];
201     this->routers_[i]->blackLinks_ = new surf::LinkImpl*[this->numChassisPerGroup_];
202
203     for (unsigned int j = 0; j < numLinksperLink_ * this->numNodesPerBlade_; j += numLinksperLink_) {
204       std::string id = "local_link_from_router_"+ std::to_string(i) + "_to_node_" +
205           std::to_string(j / numLinksperLink_) + "_" + std::to_string(uniqueId);
206       this->createLink(id, 1, &linkup, &linkdown);
207
208       if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX) {
209         this->routers_[i]->myNodes_[j]     = linkup;
210         this->routers_[i]->myNodes_[j + 1] = linkdown;
211       } else {
212         this->routers_[i]->myNodes_[j] = linkup;
213       }
214       uniqueId++;
215     }
216   }
217
218   // Green links from routers to same chassis routers - alltoall
219   for (unsigned int i = 0; i < this->numGroups_ * this->numChassisPerGroup_; i++) {
220     for (unsigned int j = 0; j < this->numBladesPerChassis_; j++) {
221       for (unsigned int k = j + 1; k < this->numBladesPerChassis_; k++) {
222         std::string id = "green_link_in_chassis_" + std::to_string(i % numChassisPerGroup_) +"_between_routers_" +
223             std::to_string(j) + "_and_" + std::to_string(k) + "_" + std::to_string(uniqueId);
224         this->createLink(id, this->numLinksGreen_, &linkup, &linkdown);
225
226         this->routers_[i * numBladesPerChassis_ + j]->greenLinks_[k] = linkup;
227         this->routers_[i * numBladesPerChassis_ + k]->greenLinks_[j] = linkdown;
228         uniqueId++;
229       }
230     }
231   }
232
233   // Black links from routers to same group routers - alltoall
234   for (unsigned int i = 0; i < this->numGroups_; i++) {
235     for (unsigned int j = 0; j < this->numChassisPerGroup_; j++) {
236       for (unsigned int k = j + 1; k < this->numChassisPerGroup_; k++) {
237         for (unsigned int l = 0; l < this->numBladesPerChassis_; l++) {
238           std::string id = "black_link_in_group_" + std::to_string(i) + "_between_chassis_" + std::to_string(j) +
239               "_and_" + std::to_string(k) +"_blade_" + std::to_string(l) + "_" + std::to_string(uniqueId);
240           this->createLink(id, this->numLinksBlack_, &linkup, &linkdown);
241
242           this->routers_[i * numBladesPerChassis_ * numChassisPerGroup_ + j * numBladesPerChassis_ + l]
243               ->blackLinks_[k] = linkup;
244           this->routers_[i * numBladesPerChassis_ * numChassisPerGroup_ + k * numBladesPerChassis_ + l]
245               ->blackLinks_[j] = linkdown;
246           uniqueId++;
247         }
248       }
249     }
250   }
251
252   // Blue links between groups - Not all routers involved, only one per group is linked to others. Let's say router n of
253   // each group is linked to group n.
254   // FIXME: in reality blue links may be attached to several different routers
255   for (unsigned int i = 0; i < this->numGroups_; i++) {
256     for (unsigned int j = i + 1; j < this->numGroups_; j++) {
257       unsigned int routernumi                = i * numBladesPerChassis_ * numChassisPerGroup_ + j;
258       unsigned int routernumj                = j * numBladesPerChassis_ * numChassisPerGroup_ + i;
259       this->routers_[routernumi]->blueLinks_ = new surf::LinkImpl*;
260       this->routers_[routernumj]->blueLinks_ = new surf::LinkImpl*;
261       std::string id = "blue_link_between_group_"+ std::to_string(i) +"_and_" + std::to_string(j) +"_routers_" +
262           std::to_string(routernumi) + "_and_" + std::to_string(routernumj) + "_" + std::to_string(uniqueId);
263       this->createLink(id, this->numLinksBlue_, &linkup, &linkdown);
264
265       this->routers_[routernumi]->blueLinks_[0] = linkup;
266       this->routers_[routernumj]->blueLinks_[0] = linkdown;
267       uniqueId++;
268     }
269   }
270 }
271
272 void DragonflyZone::getLocalRoute(NetPoint* src, NetPoint* dst, sg_platf_route_cbarg_t route, double* latency)
273 {
274   // Minimal routing version.
275   // TODO : non-minimal random one, and adaptive ?
276
277   if (dst->isRouter() || src->isRouter())
278     return;
279
280   XBT_VERB("dragonfly getLocalRout from '%s'[%u] to '%s'[%u]", src->name().c_str(), src->id(), dst->name().c_str(),
281            dst->id());
282
283   if ((src->id() == dst->id()) && hasLoopback_) {
284     std::pair<surf::LinkImpl*, surf::LinkImpl*> info = privateLinks_.at(nodePosition(src->id()));
285
286     route->link_list->push_back(info.first);
287     if (latency)
288       *latency += info.first->latency();
289     return;
290   }
291
292   unsigned int myCoords[4];
293   rankId_to_coords(src->id(), &myCoords);
294   unsigned int targetCoords[4];
295   rankId_to_coords(dst->id(), &targetCoords);
296   XBT_DEBUG("src : %u group, %u chassis, %u blade, %u node", myCoords[0], myCoords[1], myCoords[2], myCoords[3]);
297   XBT_DEBUG("dst : %u group, %u chassis, %u blade, %u node", targetCoords[0], targetCoords[1], targetCoords[2],
298             targetCoords[3]);
299
300   DragonflyRouter* myRouter = routers_[myCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) +
301                                        myCoords[1] * numBladesPerChassis_ + myCoords[2]];
302   DragonflyRouter* targetRouter = routers_[targetCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) +
303                                            targetCoords[1] * numBladesPerChassis_ + targetCoords[2]];
304   DragonflyRouter* currentRouter = myRouter;
305
306   // node->router local link
307   route->link_list->push_back(myRouter->myNodes_[myCoords[3] * numLinksperLink_]);
308   if (latency)
309     *latency += myRouter->myNodes_[myCoords[3] * numLinksperLink_]->latency();
310
311   if (hasLimiter_) { // limiter for sender
312     std::pair<surf::LinkImpl*, surf::LinkImpl*> info = privateLinks_.at(nodePositionWithLoopback(src->id()));
313     route->link_list->push_back(info.first);
314   }
315
316   if (targetRouter != myRouter) {
317
318     // are we on a different group ?
319     if (targetRouter->group_ != currentRouter->group_) {
320       // go to the router of our group connected to this one.
321       if (currentRouter->blade_ != targetCoords[0]) {
322         // go to the nth router in our chassis
323         route->link_list->push_back(currentRouter->greenLinks_[targetCoords[0]]);
324         if (latency)
325           *latency += currentRouter->greenLinks_[targetCoords[0]]->latency();
326         currentRouter = routers_[myCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) +
327                                  myCoords[1] * numBladesPerChassis_ + targetCoords[0]];
328       }
329
330       if (currentRouter->chassis_ != 0) {
331         // go to the first chassis of our group
332         route->link_list->push_back(currentRouter->blackLinks_[0]);
333         if (latency)
334           *latency += currentRouter->blackLinks_[0]->latency();
335         currentRouter = routers_[myCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) + targetCoords[0]];
336       }
337
338       // go to destination group - the only optical hop
339       route->link_list->push_back(currentRouter->blueLinks_[0]);
340       if (latency)
341         *latency += currentRouter->blueLinks_[0]->latency();
342       currentRouter = routers_[targetCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) + myCoords[0]];
343     }
344
345     // same group, but same blade ?
346     if (targetRouter->blade_ != currentRouter->blade_) {
347       route->link_list->push_back(currentRouter->greenLinks_[targetCoords[2]]);
348       if (latency)
349         *latency += currentRouter->greenLinks_[targetCoords[2]]->latency();
350       currentRouter = routers_[targetCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) + targetCoords[2]];
351     }
352
353     // same blade, but same chassis ?
354     if (targetRouter->chassis_ != currentRouter->chassis_) {
355       route->link_list->push_back(currentRouter->blackLinks_[targetCoords[1]]);
356       if (latency)
357         *latency += currentRouter->blackLinks_[targetCoords[1]]->latency();
358     }
359   }
360
361   if (hasLimiter_) { // limiter for receiver
362     std::pair<surf::LinkImpl*, surf::LinkImpl*> info = privateLinks_.at(nodePositionWithLoopback(dst->id()));
363     route->link_list->push_back(info.first);
364   }
365
366   // router->node local link
367   route->link_list->push_back(targetRouter->myNodes_[targetCoords[3] * numLinksperLink_ + numLinksperLink_ - 1]);
368   if (latency)
369     *latency += targetRouter->myNodes_[targetCoords[3] * numLinksperLink_ + numLinksperLink_ - 1]->latency();
370 }
371 }
372 }
373 } // namespace