Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add the failing tests name to the final output for mpich3 tests, to ease identification.
[simgrid.git] / src / kernel / routing / DragonflyZone.cpp
1 /* Copyright (c) 2014-2016. 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, const char* 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     xbt_free(routers_);
30   }
31 }
32
33 unsigned int* DragonflyZone::rankId_to_coords(int rankId)
34 {
35   // coords : group, chassis, blade, node
36   unsigned int* coords =  new unsigned int[4];
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_;
43
44   return coords;
45 }
46
47 void DragonflyZone::parse_specific_arguments(sg_platf_cluster_cbarg_t cluster)
48 {
49   std::vector<std::string> parameters;
50   std::vector<std::string> tmp;
51   boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
52
53   // TODO : we have to check for zeros and negative numbers, or it might crash
54   if (parameters.size() != 4) {
55     surf_parse_error(
56         "Dragonfly are defined by the number of groups, chassis per groups, blades per chassis, nodes per blade");
57   }
58
59   // Blue network : number of groups, number of links between each group
60   boost::split(tmp, parameters[0], boost::is_any_of(","));
61   if (tmp.size() != 2) {
62     surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
63   }
64
65   this->numGroups_    = xbt_str_parse_int(tmp[0].c_str(), "Invalid number of groups: %s");
66   this->numLinksBlue_ = xbt_str_parse_int(tmp[1].c_str(), "Invalid number of links for the blue level: %s");
67
68   // Black network : number of chassis/group, number of links between each router on the black network
69   boost::split(tmp, parameters[1], boost::is_any_of(","));
70   if (tmp.size() != 2) {
71     surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
72   }
73
74   this->numChassisPerGroup_ = xbt_str_parse_int(tmp[0].c_str(), "Invalid number of groups: %s");
75   this->numLinksBlack_      = xbt_str_parse_int(tmp[1].c_str(), "Invalid number of links  for the black level: %s");
76
77   // Green network : number of blades/chassis, number of links between each router on the green network
78   boost::split(tmp, parameters[2], boost::is_any_of(","));
79   if (tmp.size() != 2) {
80     surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
81   }
82
83   this->numBladesPerChassis_ = xbt_str_parse_int(tmp[0].c_str(), "Invalid number of groups: %s");
84   this->numLinksGreen_       = xbt_str_parse_int(tmp[1].c_str(), "Invalid number of links for the green level: %s");
85
86   // The last part of topo_parameters should be the number of nodes per blade
87   this->numNodesPerBlade_ =
88       xbt_str_parse_int(parameters[3].c_str(), "Last parameter is not the amount of nodes per blade: %s");
89   this->cluster_ = cluster;
90 }
91
92 /*
93 * Generate the cluster once every node is created
94 */
95 void DragonflyZone::seal()
96 {
97   if (this->numNodesPerBlade_ == 0) {
98     return;
99   }
100
101   this->generateRouters();
102   this->generateLinks();
103 }
104
105 DragonflyRouter::DragonflyRouter(int group, int chassis, int blade) : group_(group), chassis_(chassis), blade_(blade)
106 {
107 }
108
109 DragonflyRouter::~DragonflyRouter()
110 {
111   if (this->myNodes_ != nullptr)
112     xbt_free(myNodes_);
113   if (this->greenLinks_ != nullptr)
114     xbt_free(greenLinks_);
115   if (this->blackLinks_ != nullptr)
116     xbt_free(blackLinks_);
117   if (this->blueLinks_ != nullptr)
118     xbt_free(blueLinks_);
119 }
120
121 void DragonflyZone::generateRouters()
122 {
123   this->routers_ = static_cast<DragonflyRouter**>(xbt_malloc0(this->numGroups_ * this->numChassisPerGroup_ *
124                                                               this->numBladesPerChassis_ * sizeof(DragonflyRouter*)));
125
126   for (unsigned int i = 0; i < this->numGroups_; i++) {
127     for (unsigned int j = 0; j < this->numChassisPerGroup_; j++) {
128       for (unsigned int k = 0; k < this->numBladesPerChassis_; k++) {
129         DragonflyRouter* router = new DragonflyRouter(i, j, k);
130         this->routers_[i * this->numChassisPerGroup_ * this->numBladesPerChassis_ + j * this->numBladesPerChassis_ +
131                        k] = router;
132       }
133     }
134   }
135 }
136
137 void DragonflyZone::createLink(std::string id, int numlinks, surf::LinkImpl** linkup, surf::LinkImpl** linkdown)
138 {
139   *linkup   = nullptr;
140   *linkdown = nullptr;
141   LinkCreationArgs linkTemplate;
142   linkTemplate.bandwidth = this->cluster_->bw * numlinks;
143   linkTemplate.latency   = this->cluster_->lat;
144   linkTemplate.policy    = this->cluster_->sharing_policy; // sthg to do with that ?
145   linkTemplate.id        = id;
146   sg_platf_new_link(&linkTemplate);
147   XBT_DEBUG("Generating link %s", id.c_str());
148   surf::LinkImpl* link;
149   std::string tmpID;
150   if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX) {
151     tmpID     = linkTemplate.id + "_UP";
152     link      = surf::LinkImpl::byName(tmpID.c_str());
153     *linkup   = link; // check link?
154     tmpID     = linkTemplate.id + "_DOWN";
155     link      = surf::LinkImpl::byName(tmpID.c_str());
156     *linkdown = link; // check link ?
157   } else {
158     link      = surf::LinkImpl::byName(linkTemplate.id.c_str());
159     *linkup   = link;
160     *linkdown = link;
161   }
162 }
163
164 void DragonflyZone::generateLinks()
165 {
166   static int uniqueId = 0;
167   surf::LinkImpl* linkup;
168   surf::LinkImpl* linkdown;
169
170   unsigned int numRouters = this->numGroups_ * this->numChassisPerGroup_ * this->numBladesPerChassis_;
171
172   if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX)
173     numLinksperLink_ = 2;
174
175   // Links from routers to their local nodes.
176   for (unsigned int i = 0; i < numRouters; i++) {
177     // allocate structures
178     this->routers_[i]->myNodes_ = static_cast<surf::LinkImpl**>(
179         xbt_malloc0(numLinksperLink_ * this->numNodesPerBlade_ * sizeof(surf::LinkImpl*)));
180     this->routers_[i]->greenLinks_ =
181         static_cast<surf::LinkImpl**>(xbt_malloc0(this->numBladesPerChassis_ * sizeof(surf::LinkImpl*)));
182     this->routers_[i]->blackLinks_ =
183         static_cast<surf::LinkImpl**>(xbt_malloc0(this->numChassisPerGroup_ * sizeof(surf::LinkImpl*)));
184
185     for (unsigned int j = 0; j < numLinksperLink_ * this->numNodesPerBlade_; j += numLinksperLink_) {
186       std::string id = "local_link_from_router_"+ std::to_string(i) + "_to_node_" +
187           std::to_string(j / numLinksperLink_) + "_" + std::to_string(uniqueId);
188       this->createLink(id, 1, &linkup, &linkdown);
189
190       if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX) {
191         this->routers_[i]->myNodes_[j]     = linkup;
192         this->routers_[i]->myNodes_[j + 1] = linkdown;
193       } else {
194         this->routers_[i]->myNodes_[j] = linkup;
195       }
196       uniqueId++;
197     }
198   }
199
200   // Green links from routers to same chassis routers - alltoall
201   for (unsigned int i = 0; i < this->numGroups_ * this->numChassisPerGroup_; i++) {
202     for (unsigned int j = 0; j < this->numBladesPerChassis_; j++) {
203       for (unsigned int k = j + 1; k < this->numBladesPerChassis_; k++) {
204         std::string id = "green_link_in_chassis_" + std::to_string(i % numChassisPerGroup_) +"_between_routers_" +
205             std::to_string(j) + "_and_" + std::to_string(k) + "_" + std::to_string(uniqueId);
206         this->createLink(id, this->numLinksGreen_, &linkup, &linkdown);
207
208         this->routers_[i * numBladesPerChassis_ + j]->greenLinks_[k] = linkup;
209         this->routers_[i * numBladesPerChassis_ + k]->greenLinks_[j] = linkdown;
210         uniqueId++;
211       }
212     }
213   }
214
215   // Black links from routers to same group routers - alltoall
216   for (unsigned int i = 0; i < this->numGroups_; i++) {
217     for (unsigned int j = 0; j < this->numChassisPerGroup_; j++) {
218       for (unsigned int k = j + 1; k < this->numChassisPerGroup_; k++) {
219         for (unsigned int l = 0; l < this->numBladesPerChassis_; l++) {
220           std::string id = "black_link_in_group_" + std::to_string(i) + "_between_chassis_" + std::to_string(j) +
221               "_and_" + std::to_string(k) +"_blade_" + std::to_string(l) + "_" + std::to_string(uniqueId);
222           this->createLink(id, this->numLinksBlack_, &linkup, &linkdown);
223
224           this->routers_[i * numBladesPerChassis_ * numChassisPerGroup_ + j * numBladesPerChassis_ + l]
225               ->blackLinks_[k] = linkup;
226           this->routers_[i * numBladesPerChassis_ * numChassisPerGroup_ + k * numBladesPerChassis_ + l]
227               ->blackLinks_[j] = linkdown;
228           uniqueId++;
229         }
230       }
231     }
232   }
233
234   // Blue links between groups - Not all routers involved, only one per group is linked to others. Let's say router n of
235   // each group is linked to group n.
236   // FIXME: in reality blue links may be attached to several different routers
237   for (unsigned int i = 0; i < this->numGroups_; i++) {
238     for (unsigned int j = i + 1; j < this->numGroups_; j++) {
239       unsigned int routernumi                = i * numBladesPerChassis_ * numChassisPerGroup_ + j;
240       unsigned int routernumj                = j * numBladesPerChassis_ * numChassisPerGroup_ + i;
241       this->routers_[routernumi]->blueLinks_ = static_cast<surf::LinkImpl**>(xbt_malloc0(sizeof(surf::LinkImpl*)));
242       this->routers_[routernumj]->blueLinks_ = static_cast<surf::LinkImpl**>(xbt_malloc0(sizeof(surf::LinkImpl*)));
243       std::string id = "blue_link_between_group_"+ std::to_string(i) +"_and_" + std::to_string(j) +"_routers_" +
244           std::to_string(routernumi) + "_and_" + std::to_string(routernumj) + "_" + std::to_string(uniqueId);
245       this->createLink(id, this->numLinksBlue_, &linkup, &linkdown);
246
247       this->routers_[routernumi]->blueLinks_[0] = linkup;
248       this->routers_[routernumj]->blueLinks_[0] = linkdown;
249       uniqueId++;
250     }
251   }
252 }
253
254 void DragonflyZone::getLocalRoute(NetPoint* src, NetPoint* dst, sg_platf_route_cbarg_t route, double* latency)
255 {
256   // Minimal routing version.
257   // TODO : non-minimal random one, and adaptive ?
258
259   if (dst->isRouter() || src->isRouter())
260     return;
261
262   XBT_VERB("dragonfly getLocalRout from '%s'[%d] to '%s'[%d]", src->name().c_str(), src->id(), dst->name().c_str(),
263            dst->id());
264
265   if ((src->id() == dst->id()) && hasLoopback_) {
266     std::pair<surf::LinkImpl*, surf::LinkImpl*> info = privateLinks_.at(src->id() * linkCountPerNode_);
267
268     route->link_list->push_back(info.first);
269     if (latency)
270       *latency += info.first->latency();
271     return;
272   }
273
274   unsigned int* myCoords     = rankId_to_coords(src->id());
275   unsigned int* targetCoords = rankId_to_coords(dst->id());
276   XBT_DEBUG("src : %u group, %u chassis, %u blade, %u node", myCoords[0], myCoords[1], myCoords[2], myCoords[3]);
277   XBT_DEBUG("dst : %u group, %u chassis, %u blade, %u node", targetCoords[0], targetCoords[1], targetCoords[2],
278             targetCoords[3]);
279
280   DragonflyRouter* myRouter = routers_[myCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) +
281                                        myCoords[1] * numBladesPerChassis_ + myCoords[2]];
282   DragonflyRouter* targetRouter = routers_[targetCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) +
283                                            targetCoords[1] * numBladesPerChassis_ + targetCoords[2]];
284   DragonflyRouter* currentRouter = myRouter;
285
286   // node->router local link
287   route->link_list->push_back(myRouter->myNodes_[myCoords[3] * numLinksperLink_]);
288   if (latency)
289     *latency += myRouter->myNodes_[myCoords[3] * numLinksperLink_]->latency();
290
291   if (hasLimiter_) { // limiter for sender
292     std::pair<surf::LinkImpl*, surf::LinkImpl*> info = privateLinks_.at(src->id() * linkCountPerNode_ + hasLoopback_);
293     route->link_list->push_back(info.first);
294   }
295
296   if (targetRouter != myRouter) {
297
298     // are we on a different group ?
299     if (targetRouter->group_ != currentRouter->group_) {
300       // go to the router of our group connected to this one.
301       if (currentRouter->blade_ != targetCoords[0]) {
302         // go to the nth router in our chassis
303         route->link_list->push_back(currentRouter->greenLinks_[targetCoords[0]]);
304         if (latency)
305           *latency += currentRouter->greenLinks_[targetCoords[0]]->latency();
306         currentRouter = routers_[myCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) +
307                                  myCoords[1] * numBladesPerChassis_ + targetCoords[0]];
308       }
309
310       if (currentRouter->chassis_ != 0) {
311         // go to the first chassis of our group
312         route->link_list->push_back(currentRouter->blackLinks_[0]);
313         if (latency)
314           *latency += currentRouter->blackLinks_[0]->latency();
315         currentRouter = routers_[myCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) + targetCoords[0]];
316       }
317
318       // go to destination group - the only optical hop
319       route->link_list->push_back(currentRouter->blueLinks_[0]);
320       if (latency)
321         *latency += currentRouter->blueLinks_[0]->latency();
322       currentRouter = routers_[targetCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) + myCoords[0]];
323     }
324
325     // same group, but same blade ?
326     if (targetRouter->blade_ != currentRouter->blade_) {
327       route->link_list->push_back(currentRouter->greenLinks_[targetCoords[2]]);
328       if (latency)
329         *latency += currentRouter->greenLinks_[targetCoords[2]]->latency();
330       currentRouter = routers_[targetCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) + targetCoords[2]];
331     }
332
333     // same blade, but same chassis ?
334     if (targetRouter->chassis_ != currentRouter->chassis_) {
335       route->link_list->push_back(currentRouter->blackLinks_[targetCoords[1]]);
336       if (latency)
337         *latency += currentRouter->blackLinks_[targetCoords[1]]->latency();
338       currentRouter = routers_[targetCoords[0] * (numChassisPerGroup_ * numBladesPerChassis_) +
339                                targetCoords[1] * numBladesPerChassis_ + targetCoords[2]];
340     }
341   }
342
343   if (hasLimiter_) { // limiter for receiver
344     std::pair<surf::LinkImpl*, surf::LinkImpl*> info = privateLinks_.at(dst->id() * linkCountPerNode_ + hasLoopback_);
345     route->link_list->push_back(info.first);
346   }
347
348   // router->node local link
349   route->link_list->push_back(targetRouter->myNodes_[targetCoords[3] * numLinksperLink_ + numLinksperLink_ - 1]);
350   if (latency)
351     *latency += targetRouter->myNodes_[targetCoords[3] * numLinksperLink_ + numLinksperLink_ - 1]->latency();
352
353   delete[] myCoords;
354   delete[] targetCoords;
355 }
356 }
357 }
358 } // namespace