Logo AND Algorithmique Numérique Distribuée

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