Logo AND Algorithmique Numérique Distribuée

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