Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
DragonflyZone: do not save the whole ClusterCreationArgs structure
[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->num_groups_ * this->num_chassis_per_group_ * this->num_blades_per_chassis_; 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 / (num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_);
38   rankId       = rankId % (num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_);
39   (*coords)[1] = rankId / (num_blades_per_chassis_ * num_nodes_per_blade_);
40   rankId       = rankId % (num_blades_per_chassis_ * num_nodes_per_blade_);
41   (*coords)[2] = rankId / num_nodes_per_blade_;
42   (*coords)[3] = rankId % num_nodes_per_blade_;
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->num_groups_ = 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->num_links_blue_ = 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->num_chassis_per_group_ = 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->num_links_black_ = 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->num_blades_per_chassis_ = 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->num_links_green_ = 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->num_nodes_per_blade_ = 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   this->sharing_policy_ = cluster->sharing_policy;
117   if (cluster->sharing_policy == SURF_LINK_SPLITDUPLEX)
118     this->num_links_per_link_ = 2;
119   this->bw_  = cluster->bw;
120   this->lat_ = cluster->lat;
121 }
122
123 /* Generate the cluster once every node is created */
124 void DragonflyZone::seal()
125 {
126   if (this->num_nodes_per_blade_ == 0) {
127     return;
128   }
129
130   this->generateRouters();
131   this->generateLinks();
132 }
133
134 DragonflyRouter::DragonflyRouter(int group, int chassis, int blade) : group_(group), chassis_(chassis), blade_(blade)
135 {
136 }
137
138 DragonflyRouter::~DragonflyRouter()
139 {
140   delete[] my_nodes_;
141   delete[] green_links_;
142   delete[] black_links_;
143   delete blue_links_;
144 }
145
146 void DragonflyZone::generateRouters()
147 {
148   this->routers_ =
149       new DragonflyRouter*[this->num_groups_ * this->num_chassis_per_group_ * this->num_blades_per_chassis_];
150
151   for (unsigned int i = 0; i < this->num_groups_; i++) {
152     for (unsigned int j = 0; j < this->num_chassis_per_group_; j++) {
153       for (unsigned int k = 0; k < this->num_blades_per_chassis_; k++) {
154         DragonflyRouter* router = new DragonflyRouter(i, j, k);
155         this->routers_[i * this->num_chassis_per_group_ * this->num_blades_per_chassis_ +
156                        j * this->num_blades_per_chassis_ + k] = router;
157       }
158     }
159   }
160 }
161
162 void DragonflyZone::createLink(const std::string& id, int numlinks, surf::LinkImpl** linkup, surf::LinkImpl** linkdown)
163 {
164   *linkup   = nullptr;
165   *linkdown = nullptr;
166   LinkCreationArgs linkTemplate;
167   linkTemplate.bandwidth = this->bw_ * numlinks;
168   linkTemplate.latency   = this->lat_;
169   linkTemplate.policy    = this->sharing_policy_;
170   linkTemplate.id        = id;
171   sg_platf_new_link(&linkTemplate);
172   XBT_DEBUG("Generating link %s", id.c_str());
173   surf::LinkImpl* link;
174   if (this->sharing_policy_ == SURF_LINK_SPLITDUPLEX) {
175     *linkup   = surf::LinkImpl::byName(linkTemplate.id + "_UP");   // check link?
176     *linkdown = surf::LinkImpl::byName(linkTemplate.id + "_DOWN"); // check link ?
177   } else {
178     link      = surf::LinkImpl::byName(linkTemplate.id);
179     *linkup   = link;
180     *linkdown = link;
181   }
182 }
183
184 void DragonflyZone::generateLinks()
185 {
186   static int uniqueId = 0;
187   surf::LinkImpl* linkup;
188   surf::LinkImpl* linkdown;
189
190   unsigned int numRouters = this->num_groups_ * this->num_chassis_per_group_ * this->num_blades_per_chassis_;
191
192   // Links from routers to their local nodes.
193   for (unsigned int i = 0; i < numRouters; i++) {
194     // allocate structures
195     this->routers_[i]->my_nodes_    = new surf::LinkImpl*[num_links_per_link_ * this->num_nodes_per_blade_];
196     this->routers_[i]->green_links_ = new surf::LinkImpl*[this->num_blades_per_chassis_];
197     this->routers_[i]->black_links_ = new surf::LinkImpl*[this->num_chassis_per_group_];
198
199     for (unsigned int j = 0; j < num_links_per_link_ * this->num_nodes_per_blade_; j += num_links_per_link_) {
200       std::string id = "local_link_from_router_" + std::to_string(i) + "_to_node_" +
201                        std::to_string(j / num_links_per_link_) + "_" + std::to_string(uniqueId);
202       this->createLink(id, 1, &linkup, &linkdown);
203
204       this->routers_[i]->my_nodes_[j] = linkup;
205       if (this->sharing_policy_ == SURF_LINK_SPLITDUPLEX)
206         this->routers_[i]->my_nodes_[j + 1] = linkdown;
207
208       uniqueId++;
209     }
210   }
211
212   // Green links from routers to same chassis routers - alltoall
213   for (unsigned int i = 0; i < this->num_groups_ * this->num_chassis_per_group_; i++) {
214     for (unsigned int j = 0; j < this->num_blades_per_chassis_; j++) {
215       for (unsigned int k = j + 1; k < this->num_blades_per_chassis_; k++) {
216         std::string id = "green_link_in_chassis_" + std::to_string(i % num_chassis_per_group_) + "_between_routers_" +
217                          std::to_string(j) + "_and_" + std::to_string(k) + "_" + std::to_string(uniqueId);
218         this->createLink(id, this->num_links_green_, &linkup, &linkdown);
219
220         this->routers_[i * num_blades_per_chassis_ + j]->green_links_[k] = linkup;
221         this->routers_[i * num_blades_per_chassis_ + k]->green_links_[j] = linkdown;
222         uniqueId++;
223       }
224     }
225   }
226
227   // Black links from routers to same group routers - alltoall
228   for (unsigned int i = 0; i < this->num_groups_; i++) {
229     for (unsigned int j = 0; j < this->num_chassis_per_group_; j++) {
230       for (unsigned int k = j + 1; k < this->num_chassis_per_group_; k++) {
231         for (unsigned int l = 0; l < this->num_blades_per_chassis_; l++) {
232           std::string id = "black_link_in_group_" + std::to_string(i) + "_between_chassis_" + std::to_string(j) +
233               "_and_" + std::to_string(k) +"_blade_" + std::to_string(l) + "_" + std::to_string(uniqueId);
234           this->createLink(id, this->num_links_black_, &linkup, &linkdown);
235
236           this->routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + j * num_blades_per_chassis_ + l]
237               ->black_links_[k] = linkup;
238           this->routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + k * num_blades_per_chassis_ + l]
239               ->black_links_[j] = linkdown;
240           uniqueId++;
241         }
242       }
243     }
244   }
245
246   // Blue links between groups - Not all routers involved, only one per group is linked to others. Let's say router n of
247   // each group is linked to group n.
248   // FIXME: in reality blue links may be attached to several different routers
249   for (unsigned int i = 0; i < this->num_groups_; i++) {
250     for (unsigned int j = i + 1; j < this->num_groups_; j++) {
251       unsigned int routernumi                 = i * num_blades_per_chassis_ * num_chassis_per_group_ + j;
252       unsigned int routernumj                 = j * num_blades_per_chassis_ * num_chassis_per_group_ + i;
253       this->routers_[routernumi]->blue_links_ = new surf::LinkImpl*;
254       this->routers_[routernumj]->blue_links_ = new surf::LinkImpl*;
255       std::string id = "blue_link_between_group_"+ std::to_string(i) +"_and_" + std::to_string(j) +"_routers_" +
256           std::to_string(routernumi) + "_and_" + std::to_string(routernumj) + "_" + std::to_string(uniqueId);
257       this->createLink(id, this->num_links_blue_, &linkup, &linkdown);
258
259       this->routers_[routernumi]->blue_links_[0] = linkup;
260       this->routers_[routernumj]->blue_links_[0] = linkdown;
261       uniqueId++;
262     }
263   }
264 }
265
266 void DragonflyZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* latency)
267 {
268   // Minimal routing version.
269   // TODO : non-minimal random one, and adaptive ?
270
271   if (dst->is_router() || src->is_router())
272     return;
273
274   XBT_VERB("dragonfly getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(),
275            dst->id());
276
277   if ((src->id() == dst->id()) && has_loopback_) {
278     std::pair<surf::LinkImpl*, surf::LinkImpl*> info = private_links_.at(node_pos(src->id()));
279
280     route->link_list.push_back(info.first);
281     if (latency)
282       *latency += info.first->latency();
283     return;
284   }
285
286   unsigned int myCoords[4];
287   rankId_to_coords(src->id(), &myCoords);
288   unsigned int targetCoords[4];
289   rankId_to_coords(dst->id(), &targetCoords);
290   XBT_DEBUG("src : %u group, %u chassis, %u blade, %u node", myCoords[0], myCoords[1], myCoords[2], myCoords[3]);
291   XBT_DEBUG("dst : %u group, %u chassis, %u blade, %u node", targetCoords[0], targetCoords[1], targetCoords[2],
292             targetCoords[3]);
293
294   DragonflyRouter* myRouter      = routers_[myCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) +
295                                        myCoords[1] * num_blades_per_chassis_ + myCoords[2]];
296   DragonflyRouter* targetRouter  = routers_[targetCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) +
297                                            targetCoords[1] * num_blades_per_chassis_ + targetCoords[2]];
298   DragonflyRouter* currentRouter = myRouter;
299
300   // node->router local link
301   route->link_list.push_back(myRouter->my_nodes_[myCoords[3] * num_links_per_link_]);
302   if (latency)
303     *latency += myRouter->my_nodes_[myCoords[3] * num_links_per_link_]->latency();
304
305   if (has_limiter_) { // limiter for sender
306     std::pair<surf::LinkImpl*, surf::LinkImpl*> info = private_links_.at(node_pos_with_loopback(src->id()));
307     route->link_list.push_back(info.first);
308   }
309
310   if (targetRouter != myRouter) {
311
312     // are we on a different group ?
313     if (targetRouter->group_ != currentRouter->group_) {
314       // go to the router of our group connected to this one.
315       if (currentRouter->blade_ != targetCoords[0]) {
316         // go to the nth router in our chassis
317         route->link_list.push_back(currentRouter->green_links_[targetCoords[0]]);
318         if (latency)
319           *latency += currentRouter->green_links_[targetCoords[0]]->latency();
320         currentRouter = routers_[myCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) +
321                                  myCoords[1] * num_blades_per_chassis_ + targetCoords[0]];
322       }
323
324       if (currentRouter->chassis_ != 0) {
325         // go to the first chassis of our group
326         route->link_list.push_back(currentRouter->black_links_[0]);
327         if (latency)
328           *latency += currentRouter->black_links_[0]->latency();
329         currentRouter = routers_[myCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords[0]];
330       }
331
332       // go to destination group - the only optical hop
333       route->link_list.push_back(currentRouter->blue_links_[0]);
334       if (latency)
335         *latency += currentRouter->blue_links_[0]->latency();
336       currentRouter = routers_[targetCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) + myCoords[0]];
337     }
338
339     // same group, but same blade ?
340     if (targetRouter->blade_ != currentRouter->blade_) {
341       route->link_list.push_back(currentRouter->green_links_[targetCoords[2]]);
342       if (latency)
343         *latency += currentRouter->green_links_[targetCoords[2]]->latency();
344       currentRouter = routers_[targetCoords[0] * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords[2]];
345     }
346
347     // same blade, but same chassis ?
348     if (targetRouter->chassis_ != currentRouter->chassis_) {
349       route->link_list.push_back(currentRouter->black_links_[targetCoords[1]]);
350       if (latency)
351         *latency += currentRouter->black_links_[targetCoords[1]]->latency();
352     }
353   }
354
355   if (has_limiter_) { // limiter for receiver
356     std::pair<surf::LinkImpl*, surf::LinkImpl*> info = private_links_.at(node_pos_with_loopback(dst->id()));
357     route->link_list.push_back(info.first);
358   }
359
360   // router->node local link
361   route->link_list.push_back(targetRouter->my_nodes_[targetCoords[3] * num_links_per_link_ + num_links_per_link_ - 1]);
362   if (latency)
363     *latency += targetRouter->my_nodes_[targetCoords[3] * num_links_per_link_ + num_links_per_link_ - 1]->latency();
364 }
365 }
366 }
367 } // namespace