Logo AND Algorithmique Numérique Distribuée

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