Logo AND Algorithmique Numérique Distribuée

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