Logo AND Algorithmique Numérique Distribuée

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