Logo AND Algorithmique Numérique Distribuée

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