Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not throw signal from sg_platf
[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 <numeric>
14 #include <string>
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster_dragonfly, surf_route_cluster, "Dragonfly Routing part of surf");
17
18 namespace simgrid {
19 namespace kernel {
20 namespace routing {
21
22 DragonflyZone::DragonflyZone(const std::string& name) : ClusterZone(name) {}
23
24 DragonflyZone::Coords DragonflyZone::rankId_to_coords(int rankId) const
25 {
26   // coords : group, chassis, blade, node
27   Coords coords;
28   coords.group   = rankId / (num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_);
29   rankId         = rankId % (num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_);
30   coords.chassis = rankId / (num_blades_per_chassis_ * num_nodes_per_blade_);
31   rankId         = rankId % (num_blades_per_chassis_ * num_nodes_per_blade_);
32   coords.blade   = rankId / num_nodes_per_blade_;
33   coords.node    = rankId % num_nodes_per_blade_;
34   return coords;
35 }
36
37 void DragonflyZone::rankId_to_coords(int rankId, unsigned int coords[4]) const // XBT_ATTRIB_DEPRECATED_v330
38 {
39   const auto s_coords = rankId_to_coords(rankId);
40   coords[0]           = s_coords.group;
41   coords[1]           = s_coords.chassis;
42   coords[2]           = s_coords.blade;
43   coords[3]           = s_coords.node;
44 }
45
46 void DragonflyZone::set_link_characteristics(double bw, double lat, s4u::Link::SharingPolicy sharing_policy)
47 {
48   sharing_policy_ = sharing_policy;
49   if (sharing_policy == s4u::Link::SharingPolicy::SPLITDUPLEX)
50     num_links_per_link_ = 2;
51   bw_  = bw;
52   lat_ = lat;
53 }
54
55 void DragonflyZone::set_topology(unsigned int n_groups, unsigned int groups_links, unsigned int n_chassis,
56                                  unsigned int chassis_links, unsigned int n_routers, unsigned int routers_links,
57                                  unsigned int nodes)
58 {
59   num_groups_     = n_groups;
60   num_links_blue_ = groups_links;
61
62   num_chassis_per_group_ = n_chassis;
63   num_links_black_       = chassis_links;
64
65   num_blades_per_chassis_ = n_routers;
66   num_links_green_        = routers_links;
67
68   num_nodes_per_blade_ = nodes;
69 }
70
71 s4u::DragonflyParams DragonflyZone::parse_topo_parameters(const std::string& topo_parameters)
72 {
73   std::vector<std::string> parameters;
74   std::vector<std::string> tmp;
75   boost::split(parameters, topo_parameters, boost::is_any_of(";"));
76
77   if (parameters.size() != 4)
78     surf_parse_error(
79         "Dragonfly are defined by the number of groups, chassis per groups, blades per chassis, nodes per blade");
80
81   // Blue network : number of groups, number of links between each group
82   boost::split(tmp, parameters[0], boost::is_any_of(","));
83   if (tmp.size() != 2)
84     surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
85
86   unsigned int n_groups;
87   try {
88     n_groups = std::stoi(tmp[0]);
89   } catch (const std::invalid_argument&) {
90     throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
91   }
92
93   unsigned int n_blue;
94   try {
95     n_blue = std::stoi(tmp[1]);
96   } catch (const std::invalid_argument&) {
97     throw std::invalid_argument(std::string("Invalid number of links for the blue level:") + tmp[1]);
98   }
99
100   // Black network : number of chassis/group, number of links between each router on the black network
101   boost::split(tmp, parameters[1], boost::is_any_of(","));
102   if (tmp.size() != 2)
103     surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
104
105   unsigned int n_chassis;
106   try {
107     n_chassis = std::stoi(tmp[0]);
108   } catch (const std::invalid_argument&) {
109     throw std::invalid_argument(std::string("Invalid number of chassis:") + tmp[0]);
110   }
111
112   unsigned int n_black;
113   try {
114     n_black = std::stoi(tmp[1]);
115   } catch (const std::invalid_argument&) {
116     throw std::invalid_argument(std::string("Invalid number of links for the black level:") + tmp[1]);
117   }
118
119   // Green network : number of blades/chassis, number of links between each router on the green network
120   boost::split(tmp, parameters[2], boost::is_any_of(","));
121   if (tmp.size() != 2)
122     surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
123
124   unsigned int n_routers;
125   try {
126     n_routers = std::stoi(tmp[0]);
127   } catch (const std::invalid_argument&) {
128     throw std::invalid_argument(std::string("Invalid number of routers:") + tmp[0]);
129   }
130
131   unsigned int n_green;
132   try {
133     n_green = std::stoi(tmp[1]);
134   } catch (const std::invalid_argument&) {
135     throw std::invalid_argument(std::string("Invalid number of links for the green level:") + tmp[1]);
136   }
137
138   // The last part of topo_parameters should be the number of nodes per blade
139   unsigned int n_nodes;
140   try {
141     n_nodes = std::stoi(parameters[3]);
142   } catch (const std::invalid_argument&) {
143     throw std::invalid_argument(std::string("Last parameter is not the amount of nodes per blade:") + parameters[3]);
144   }
145   return s4u::DragonflyParams({n_groups, n_blue}, {n_chassis, n_black}, {n_routers, n_green}, n_nodes);
146 }
147
148 /* Generate the cluster once every node is created */
149 void DragonflyZone::do_seal()
150 {
151   if (num_nodes_per_blade_ == 0)
152     return;
153
154   generate_routers();
155   generate_links();
156 }
157
158 void DragonflyZone::generate_routers()
159 {
160   routers_.reserve(num_groups_ * num_chassis_per_group_ * num_blades_per_chassis_);
161   for (unsigned int i = 0; i < num_groups_; i++)
162     for (unsigned int j = 0; j < num_chassis_per_group_; j++)
163       for (unsigned int k = 0; k < num_blades_per_chassis_; k++)
164         routers_.emplace_back(i, j, k);
165 }
166
167 void DragonflyZone::generate_link(const std::string& id, int numlinks, resource::LinkImpl** linkup,
168                                   resource::LinkImpl** linkdown)
169 {
170   XBT_DEBUG("Generating link %s", id.c_str());
171   *linkup   = nullptr;
172   *linkdown = nullptr;
173   if (sharing_policy_ == s4u::Link::SharingPolicy::SPLITDUPLEX) {
174     *linkup   = create_link(id + "_UP", std::vector<double>{bw_ * numlinks})->set_latency(lat_)->seal()->get_impl();
175     *linkdown = create_link(id + "_DOWN", std::vector<double>{bw_ * numlinks})->set_latency(lat_)->seal()->get_impl();
176   } else {
177     *linkup   = create_link(id, std::vector<double>{bw_ * numlinks})->set_latency(lat_)->seal()->get_impl();
178     *linkdown = *linkup;
179   }
180 }
181
182 void DragonflyZone::generate_links()
183 {
184   static int uniqueId = 0;
185   resource::LinkImpl* linkup;
186   resource::LinkImpl* linkdown;
187
188   unsigned int numRouters = num_groups_ * num_chassis_per_group_ * num_blades_per_chassis_;
189
190   // Links from routers to their local nodes.
191   for (unsigned int i = 0; i < numRouters; i++) {
192     // allocate structures
193     routers_[i].my_nodes_.resize(num_links_per_link_ * num_nodes_per_blade_);
194     routers_[i].green_links_.resize(num_blades_per_chassis_);
195     routers_[i].black_links_.resize(num_chassis_per_group_);
196
197     for (unsigned int j = 0; j < num_links_per_link_ * num_nodes_per_blade_; j += num_links_per_link_) {
198       std::string id = "local_link_from_router_" + std::to_string(i) + "_to_node_" +
199                        std::to_string(j / num_links_per_link_) + "_" + std::to_string(uniqueId);
200       generate_link(id, 1, &linkup, &linkdown);
201
202       routers_[i].my_nodes_[j] = linkup;
203       if (sharing_policy_ == s4u::Link::SharingPolicy::SPLITDUPLEX)
204         routers_[i].my_nodes_[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 < num_groups_ * num_chassis_per_group_; i++) {
212     for (unsigned int j = 0; j < num_blades_per_chassis_; j++) {
213       for (unsigned int k = j + 1; k < num_blades_per_chassis_; k++) {
214         std::string id = "green_link_in_chassis_" + std::to_string(i % num_chassis_per_group_) + "_between_routers_" +
215                          std::to_string(j) + "_and_" + std::to_string(k) + "_" + std::to_string(uniqueId);
216         generate_link(id, num_links_green_, &linkup, &linkdown);
217
218         routers_[i * num_blades_per_chassis_ + j].green_links_[k] = linkup;
219         routers_[i * num_blades_per_chassis_ + k].green_links_[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 < num_groups_; i++) {
227     for (unsigned int j = 0; j < num_chassis_per_group_; j++) {
228       for (unsigned int k = j + 1; k < num_chassis_per_group_; k++) {
229         for (unsigned int l = 0; l < num_blades_per_chassis_; 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           generate_link(id, num_links_black_, &linkup, &linkdown);
233
234           routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + j * num_blades_per_chassis_ + l]
235               .black_links_[k] = linkup;
236           routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + k * num_blades_per_chassis_ + l]
237               .black_links_[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 < num_groups_; i++) {
248     for (unsigned int j = i + 1; j < num_groups_; j++) {
249       unsigned int routernumi = i * num_blades_per_chassis_ * num_chassis_per_group_ + j;
250       unsigned int routernumj = j * num_blades_per_chassis_ * num_chassis_per_group_ + i;
251       std::string id = "blue_link_between_group_" + std::to_string(i) + "_and_" + std::to_string(j) + "_routers_" +
252                        std::to_string(routernumi) + "_and_" + std::to_string(routernumj) + "_" +
253                        std::to_string(uniqueId);
254       generate_link(id, num_links_blue_, &linkup, &linkdown);
255
256       routers_[routernumi].blue_link_ = linkup;
257       routers_[routernumj].blue_link_ = linkdown;
258       uniqueId++;
259     }
260   }
261 }
262
263 void DragonflyZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* latency)
264 {
265   // Minimal routing version.
266   // TODO : non-minimal random one, and adaptive ?
267
268   if (dst->is_router() || src->is_router())
269     return;
270
271   XBT_VERB("dragonfly getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(),
272            dst->id());
273
274   if ((src->id() == dst->id()) && has_loopback()) {
275     resource::LinkImpl* uplink = get_uplink_from(node_pos(src->id()));
276
277     route->link_list.push_back(uplink);
278     if (latency)
279       *latency += uplink->get_latency();
280     return;
281   }
282
283   const auto myCoords     = rankId_to_coords(src->id());
284   const auto targetCoords = rankId_to_coords(dst->id());
285   XBT_DEBUG("src : %u group, %u chassis, %u blade, %u node", myCoords.group, myCoords.chassis, myCoords.blade,
286             myCoords.node);
287   XBT_DEBUG("dst : %u group, %u chassis, %u blade, %u node", targetCoords.group, targetCoords.chassis,
288             targetCoords.blade, targetCoords.node);
289
290   DragonflyRouter* myRouter      = &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
291                                         myCoords.chassis * num_blades_per_chassis_ + myCoords.blade];
292   DragonflyRouter* targetRouter  = &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
293                                             targetCoords.chassis * num_blades_per_chassis_ + targetCoords.blade];
294   DragonflyRouter* currentRouter = myRouter;
295
296   // node->router local link
297   route->link_list.push_back(myRouter->my_nodes_[myCoords.node * num_links_per_link_]);
298   if (latency)
299     *latency += myRouter->my_nodes_[myCoords.node * num_links_per_link_]->get_latency();
300
301   if (has_limiter()) { // limiter for sender
302     route->link_list.push_back(get_uplink_from(node_pos_with_loopback(src->id())));
303   }
304
305   if (targetRouter != myRouter) {
306     // are we on a different group ?
307     if (targetRouter->group_ != currentRouter->group_) {
308       // go to the router of our group connected to this one.
309       if (currentRouter->blade_ != targetCoords.group) {
310         // go to the nth router in our chassis
311         route->link_list.push_back(currentRouter->green_links_[targetCoords.group]);
312         if (latency)
313           *latency += currentRouter->green_links_[targetCoords.group]->get_latency();
314         currentRouter = &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
315                                   myCoords.chassis * num_blades_per_chassis_ + targetCoords.group];
316       }
317
318       if (currentRouter->chassis_ != 0) {
319         // go to the first chassis of our group
320         route->link_list.push_back(currentRouter->black_links_[0]);
321         if (latency)
322           *latency += currentRouter->black_links_[0]->get_latency();
323         currentRouter =
324             &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords.group];
325       }
326
327       // go to destination group - the only optical hop
328       route->link_list.push_back(currentRouter->blue_link_);
329       if (latency)
330         *latency += currentRouter->blue_link_->get_latency();
331       currentRouter =
332           &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + myCoords.group];
333     }
334
335     // same group, but same blade ?
336     if (targetRouter->blade_ != currentRouter->blade_) {
337       route->link_list.push_back(currentRouter->green_links_[targetCoords.blade]);
338       if (latency)
339         *latency += currentRouter->green_links_[targetCoords.blade]->get_latency();
340       currentRouter =
341           &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords.blade];
342     }
343
344     // same blade, but same chassis ?
345     if (targetRouter->chassis_ != currentRouter->chassis_) {
346       route->link_list.push_back(currentRouter->black_links_[targetCoords.chassis]);
347       if (latency)
348         *latency += currentRouter->black_links_[targetCoords.chassis]->get_latency();
349     }
350   }
351
352   if (has_limiter()) { // limiter for receiver
353     route->link_list.push_back(get_downlink_to(node_pos_with_loopback(dst->id())));
354   }
355
356   // router->node local link
357   route->link_list.push_back(
358       targetRouter->my_nodes_[targetCoords.node * num_links_per_link_ + num_links_per_link_ - 1]);
359   if (latency)
360     *latency +=
361         targetRouter->my_nodes_[targetCoords.node * num_links_per_link_ + num_links_per_link_ - 1]->get_latency();
362
363   // set gateways (if any)
364   route->gw_src = get_gateway(src->id());
365   route->gw_dst = get_gateway(dst->id());
366 }
367 } // namespace routing
368 } // namespace kernel
369
370 namespace s4u {
371 DragonflyParams::DragonflyParams(const std::pair<unsigned int, unsigned int>& groups,
372                                  const std::pair<unsigned int, unsigned int>& chassis,
373                                  const std::pair<unsigned int, unsigned int>& routers, unsigned int nodes)
374     : groups(groups), chassis(chassis), routers(routers), nodes(nodes)
375 {
376   if (groups.first == 0)
377     throw std::invalid_argument("Dragonfly: Invalid number of groups, must be > 0");
378   if (groups.second == 0)
379     throw std::invalid_argument("Dragonfly: Invalid number of blue (groups) links, must be > 0");
380   if (chassis.first == 0)
381     throw std::invalid_argument("Dragonfly: Invalid number of chassis, must be > 0");
382   if (chassis.second == 0)
383     throw std::invalid_argument("Dragonfly: Invalid number of black (chassis) links, must be > 0");
384   if (routers.first == 0)
385     throw std::invalid_argument("Dragonfly: Invalid number of routers, must be > 0");
386   if (routers.second == 0)
387     throw std::invalid_argument("Dragonfly: Invalid number of green (routers) links, must be > 0");
388   if (nodes == 0)
389     throw std::invalid_argument("Dragonfly: Invalid number of nodes, must be > 0");
390 }
391
392 NetZone* create_dragonfly_zone(const std::string& name, const NetZone* parent, const DragonflyParams& params,
393                                double bandwidth, double latency, Link::SharingPolicy sharing_policy,
394                                const std::function<ClusterNetPointCb>& set_netpoint,
395                                const std::function<ClusterLinkCb>& set_loopback,
396                                const std::function<ClusterLinkCb>& set_limiter)
397 {
398   /* initial checks */
399   if (bandwidth <= 0)
400     throw std::invalid_argument("DragonflyZone: incorrect bandwidth for internode communication, bw=" +
401                                 std::to_string(bandwidth));
402   if (latency < 0)
403     throw std::invalid_argument("DragonflyZone: incorrect latency for internode communication, lat=" +
404                                 std::to_string(latency));
405
406   /* creating zone */
407   auto* zone = new kernel::routing::DragonflyZone(name);
408   zone->set_topology(params.groups.first, params.groups.second, params.chassis.first, params.chassis.second,
409                      params.routers.first, params.routers.second, params.nodes);
410   if (parent)
411     zone->set_parent(parent->get_impl());
412   zone->set_link_characteristics(bandwidth, latency, sharing_policy);
413
414   /* populating it */
415   std::vector<unsigned int> dimensions = {params.groups.first, params.chassis.first, params.routers.first,
416                                           params.nodes};
417   int tot_elements                     = std::accumulate(dimensions.begin(), dimensions.end(), 1, std::multiplies<>());
418   for (int i = 0; i < tot_elements; i++) {
419     kernel::routing::NetPoint* netpoint;
420     Link* limiter;
421     Link* loopback;
422     zone->fill_leaf_from_cb(i, dimensions, set_netpoint, set_loopback, set_limiter, &netpoint, &loopback, &limiter);
423   }
424
425   return zone->get_iface();
426 }
427 } // namespace s4u
428
429 } // namespace simgrid