Logo AND Algorithmique Numérique Distribuée

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