Logo AND Algorithmique Numérique Distribuée

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