Logo AND Algorithmique Numérique Distribuée

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