Logo AND Algorithmique Numérique Distribuée

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