Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New: s4u::create_fatTree_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 <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::parse_specific_arguments(ClusterCreationArgs* cluster)
46 {
47   std::vector<std::string> parameters;
48   std::vector<std::string> tmp;
49   boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
50
51   if (parameters.size() != 4)
52     surf_parse_error(
53         "Dragonfly are defined by the number of groups, chassis per groups, blades per chassis, nodes per blade");
54
55   // Blue network : number of groups, number of links between each group
56   boost::split(tmp, parameters[0], boost::is_any_of(","));
57   if (tmp.size() != 2)
58     surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
59
60   try {
61     num_groups_ = std::stoi(tmp[0]);
62   } catch (const std::invalid_argument&) {
63     throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
64   }
65
66   try {
67     num_links_blue_ = std::stoi(tmp[1]);
68   } catch (const std::invalid_argument&) {
69     throw std::invalid_argument(std::string("Invalid number of links for the blue level:") + tmp[1]);
70   }
71
72   // Black network : number of chassis/group, number of links between each router on the black network
73   boost::split(tmp, parameters[1], boost::is_any_of(","));
74   if (tmp.size() != 2)
75     surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
76
77   try {
78     num_chassis_per_group_ = std::stoi(tmp[0]);
79   } catch (const std::invalid_argument&) {
80     throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
81   }
82
83   try {
84     num_links_black_ = std::stoi(tmp[1]);
85   } catch (const std::invalid_argument&) {
86     throw std::invalid_argument(std::string("Invalid number of links for the black level:") + tmp[1]);
87   }
88
89   // Green network : number of blades/chassis, number of links between each router on the green network
90   boost::split(tmp, parameters[2], boost::is_any_of(","));
91   if (tmp.size() != 2)
92     surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
93
94   try {
95     num_blades_per_chassis_ = std::stoi(tmp[0]);
96   } catch (const std::invalid_argument&) {
97     throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
98   }
99
100   try {
101     num_links_green_ = std::stoi(tmp[1]);
102   } catch (const std::invalid_argument&) {
103     throw std::invalid_argument(std::string("Invalid number of links for the green level:") + tmp[1]);
104   }
105
106   // The last part of topo_parameters should be the number of nodes per blade
107   try {
108     num_nodes_per_blade_ = std::stoi(parameters[3]);
109   } catch (const std::invalid_argument&) {
110     throw std::invalid_argument(std::string("Last parameter is not the amount of nodes per blade:") + parameters[3]);
111   }
112
113   sharing_policy_ = cluster->sharing_policy;
114   if (cluster->sharing_policy == s4u::Link::SharingPolicy::SPLITDUPLEX)
115     num_links_per_link_ = 2;
116   bw_  = cluster->bw;
117   lat_ = cluster->lat;
118 }
119
120 /* Generate the cluster once every node is created */
121 void DragonflyZone::do_seal()
122 {
123   if (num_nodes_per_blade_ == 0)
124     return;
125
126   generate_routers();
127   generate_links();
128 }
129
130 void DragonflyZone::generate_routers()
131 {
132   routers_.reserve(num_groups_ * num_chassis_per_group_ * num_blades_per_chassis_);
133   for (unsigned int i = 0; i < num_groups_; i++)
134     for (unsigned int j = 0; j < num_chassis_per_group_; j++)
135       for (unsigned int k = 0; k < num_blades_per_chassis_; k++)
136         routers_.emplace_back(i, j, k);
137 }
138
139 void DragonflyZone::generate_link(const std::string& id, int numlinks, resource::LinkImpl** linkup,
140                                   resource::LinkImpl** linkdown)
141 {
142   XBT_DEBUG("Generating link %s", id.c_str());
143   *linkup   = nullptr;
144   *linkdown = nullptr;
145   if (sharing_policy_ == s4u::Link::SharingPolicy::SPLITDUPLEX) {
146     *linkup   = create_link(id + "_UP", std::vector<double>{bw_ * numlinks})->set_latency(lat_)->seal()->get_impl();
147     *linkdown = create_link(id + "_DOWN", std::vector<double>{bw_ * numlinks})->set_latency(lat_)->seal()->get_impl();
148   } else {
149     *linkup   = create_link(id, std::vector<double>{bw_ * numlinks})->set_latency(lat_)->seal()->get_impl();
150     *linkdown = *linkup;
151   }
152 }
153
154 void DragonflyZone::generate_links()
155 {
156   static int uniqueId = 0;
157   resource::LinkImpl* linkup;
158   resource::LinkImpl* linkdown;
159
160   unsigned int numRouters = num_groups_ * num_chassis_per_group_ * num_blades_per_chassis_;
161
162   // Links from routers to their local nodes.
163   for (unsigned int i = 0; i < numRouters; i++) {
164     // allocate structures
165     routers_[i].my_nodes_.resize(num_links_per_link_ * num_nodes_per_blade_);
166     routers_[i].green_links_.resize(num_blades_per_chassis_);
167     routers_[i].black_links_.resize(num_chassis_per_group_);
168
169     for (unsigned int j = 0; j < num_links_per_link_ * num_nodes_per_blade_; j += num_links_per_link_) {
170       std::string id = "local_link_from_router_" + std::to_string(i) + "_to_node_" +
171                        std::to_string(j / num_links_per_link_) + "_" + std::to_string(uniqueId);
172       generate_link(id, 1, &linkup, &linkdown);
173
174       routers_[i].my_nodes_[j] = linkup;
175       if (sharing_policy_ == s4u::Link::SharingPolicy::SPLITDUPLEX)
176         routers_[i].my_nodes_[j + 1] = linkdown;
177
178       uniqueId++;
179     }
180   }
181
182   // Green links from routers to same chassis routers - alltoall
183   for (unsigned int i = 0; i < num_groups_ * num_chassis_per_group_; i++) {
184     for (unsigned int j = 0; j < num_blades_per_chassis_; j++) {
185       for (unsigned int k = j + 1; k < num_blades_per_chassis_; k++) {
186         std::string id = "green_link_in_chassis_" + std::to_string(i % num_chassis_per_group_) + "_between_routers_" +
187                          std::to_string(j) + "_and_" + std::to_string(k) + "_" + std::to_string(uniqueId);
188         generate_link(id, num_links_green_, &linkup, &linkdown);
189
190         routers_[i * num_blades_per_chassis_ + j].green_links_[k] = linkup;
191         routers_[i * num_blades_per_chassis_ + k].green_links_[j] = linkdown;
192         uniqueId++;
193       }
194     }
195   }
196
197   // Black links from routers to same group routers - alltoall
198   for (unsigned int i = 0; i < num_groups_; i++) {
199     for (unsigned int j = 0; j < num_chassis_per_group_; j++) {
200       for (unsigned int k = j + 1; k < num_chassis_per_group_; k++) {
201         for (unsigned int l = 0; l < num_blades_per_chassis_; l++) {
202           std::string id = "black_link_in_group_" + std::to_string(i) + "_between_chassis_" + std::to_string(j) +
203                            "_and_" + std::to_string(k) + "_blade_" + std::to_string(l) + "_" + std::to_string(uniqueId);
204           generate_link(id, num_links_black_, &linkup, &linkdown);
205
206           routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + j * num_blades_per_chassis_ + l]
207               .black_links_[k] = linkup;
208           routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + k * num_blades_per_chassis_ + l]
209               .black_links_[j] = linkdown;
210           uniqueId++;
211         }
212       }
213     }
214   }
215
216   // Blue links between groups - Not all routers involved, only one per group is linked to others. Let's say router n of
217   // each group is linked to group n.
218   // FIXME: in reality blue links may be attached to several different routers
219   for (unsigned int i = 0; i < num_groups_; i++) {
220     for (unsigned int j = i + 1; j < num_groups_; j++) {
221       unsigned int routernumi = i * num_blades_per_chassis_ * num_chassis_per_group_ + j;
222       unsigned int routernumj = j * num_blades_per_chassis_ * num_chassis_per_group_ + i;
223       std::string id = "blue_link_between_group_" + std::to_string(i) + "_and_" + std::to_string(j) + "_routers_" +
224                        std::to_string(routernumi) + "_and_" + std::to_string(routernumj) + "_" +
225                        std::to_string(uniqueId);
226       generate_link(id, num_links_blue_, &linkup, &linkdown);
227
228       routers_[routernumi].blue_link_ = linkup;
229       routers_[routernumj].blue_link_ = linkdown;
230       uniqueId++;
231     }
232   }
233 }
234
235 void DragonflyZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* latency)
236 {
237   // Minimal routing version.
238   // TODO : non-minimal random one, and adaptive ?
239
240   if (dst->is_router() || src->is_router())
241     return;
242
243   XBT_VERB("dragonfly getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(),
244            dst->id());
245
246   if ((src->id() == dst->id()) && has_loopback()) {
247     resource::LinkImpl* uplink = get_uplink_from(node_pos(src->id()));
248
249     route->link_list.push_back(uplink);
250     if (latency)
251       *latency += uplink->get_latency();
252     return;
253   }
254
255   const auto myCoords     = rankId_to_coords(src->id());
256   const auto targetCoords = rankId_to_coords(dst->id());
257   XBT_DEBUG("src : %u group, %u chassis, %u blade, %u node", myCoords.group, myCoords.chassis, myCoords.blade,
258             myCoords.node);
259   XBT_DEBUG("dst : %u group, %u chassis, %u blade, %u node", targetCoords.group, targetCoords.chassis,
260             targetCoords.blade, targetCoords.node);
261
262   DragonflyRouter* myRouter      = &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
263                                         myCoords.chassis * num_blades_per_chassis_ + myCoords.blade];
264   DragonflyRouter* targetRouter  = &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
265                                             targetCoords.chassis * num_blades_per_chassis_ + targetCoords.blade];
266   DragonflyRouter* currentRouter = myRouter;
267
268   // node->router local link
269   route->link_list.push_back(myRouter->my_nodes_[myCoords.node * num_links_per_link_]);
270   if (latency)
271     *latency += myRouter->my_nodes_[myCoords.node * num_links_per_link_]->get_latency();
272
273   if (has_limiter()) { // limiter for sender
274     route->link_list.push_back(get_uplink_from(node_pos_with_loopback(src->id())));
275   }
276
277   if (targetRouter != myRouter) {
278     // are we on a different group ?
279     if (targetRouter->group_ != currentRouter->group_) {
280       // go to the router of our group connected to this one.
281       if (currentRouter->blade_ != targetCoords.group) {
282         // go to the nth router in our chassis
283         route->link_list.push_back(currentRouter->green_links_[targetCoords.group]);
284         if (latency)
285           *latency += currentRouter->green_links_[targetCoords.group]->get_latency();
286         currentRouter = &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
287                                   myCoords.chassis * num_blades_per_chassis_ + targetCoords.group];
288       }
289
290       if (currentRouter->chassis_ != 0) {
291         // go to the first chassis of our group
292         route->link_list.push_back(currentRouter->black_links_[0]);
293         if (latency)
294           *latency += currentRouter->black_links_[0]->get_latency();
295         currentRouter =
296             &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords.group];
297       }
298
299       // go to destination group - the only optical hop
300       route->link_list.push_back(currentRouter->blue_link_);
301       if (latency)
302         *latency += currentRouter->blue_link_->get_latency();
303       currentRouter =
304           &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + myCoords.group];
305     }
306
307     // same group, but same blade ?
308     if (targetRouter->blade_ != currentRouter->blade_) {
309       route->link_list.push_back(currentRouter->green_links_[targetCoords.blade]);
310       if (latency)
311         *latency += currentRouter->green_links_[targetCoords.blade]->get_latency();
312       currentRouter =
313           &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords.blade];
314     }
315
316     // same blade, but same chassis ?
317     if (targetRouter->chassis_ != currentRouter->chassis_) {
318       route->link_list.push_back(currentRouter->black_links_[targetCoords.chassis]);
319       if (latency)
320         *latency += currentRouter->black_links_[targetCoords.chassis]->get_latency();
321     }
322   }
323
324   if (has_limiter()) { // limiter for receiver
325     route->link_list.push_back(get_downlink_to(node_pos_with_loopback(dst->id())));
326   }
327
328   // router->node local link
329   route->link_list.push_back(
330       targetRouter->my_nodes_[targetCoords.node * num_links_per_link_ + num_links_per_link_ - 1]);
331   if (latency)
332     *latency +=
333         targetRouter->my_nodes_[targetCoords.node * num_links_per_link_ + num_links_per_link_ - 1]->get_latency();
334 }
335 } // namespace routing
336 } // namespace kernel
337
338 namespace s4u {
339 NetZone* create_dragonfly_zone(const std::string& name)
340 {
341   return (new kernel::routing::DragonflyZone(name))->get_iface();
342 }
343 } // namespace s4u
344
345 } // namespace simgrid