Logo AND Algorithmique Numérique Distribuée

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