1 /* Copyright (c) 2014-2021. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/kernel/routing/DragonflyZone.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "src/surf/network_interface.hpp"
10 #include <boost/algorithm/string/classification.hpp>
11 #include <boost/algorithm/string/split.hpp>
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster_dragonfly, surf_route_cluster, "Dragonfly Routing part of surf");
21 DragonflyZone::DragonflyZone(const std::string& name) : ClusterZone(name) {}
23 DragonflyZone::Coords DragonflyZone::rankId_to_coords(int rankId) const
25 // coords : group, chassis, blade, node
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_;
36 void DragonflyZone::rankId_to_coords(int rankId, unsigned int coords[4]) const // XBT_ATTRIB_DEPRECATED_v330
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;
45 void DragonflyZone::set_link_characteristics(double bw, double lat, s4u::Link::SharingPolicy sharing_policy)
47 ClusterZone::set_link_characteristics(bw, lat, sharing_policy);
48 if (sharing_policy == s4u::Link::SharingPolicy::SPLITDUPLEX)
49 num_links_per_link_ = 2;
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,
56 num_groups_ = n_groups;
57 num_links_blue_ = groups_links;
59 num_chassis_per_group_ = n_chassis;
60 num_links_black_ = chassis_links;
62 num_blades_per_chassis_ = n_routers;
63 num_links_green_ = routers_links;
65 num_nodes_per_blade_ = nodes;
68 s4u::DragonflyParams DragonflyZone::parse_topo_parameters(const std::string& topo_parameters)
70 std::vector<std::string> parameters;
71 std::vector<std::string> tmp;
72 boost::split(parameters, topo_parameters, boost::is_any_of(";"));
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");
77 // Blue network : number of groups, number of links between each group
78 boost::split(tmp, parameters[0], boost::is_any_of(","));
80 xbt_die("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
82 unsigned int n_groups;
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]);
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]);
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(","));
99 xbt_die("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
101 unsigned int n_chassis;
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]);
108 unsigned int n_black;
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]);
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(","));
118 xbt_die("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
120 unsigned int n_routers;
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]);
127 unsigned int n_green;
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]);
134 // The last part of topo_parameters should be the number of nodes per blade
135 unsigned int n_nodes;
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]);
141 return s4u::DragonflyParams({n_groups, n_blue}, {n_chassis, n_black}, {n_routers, n_green}, n_nodes);
144 /* Generate the cluster once every node is created */
145 void DragonflyZone::do_seal()
147 if (num_nodes_per_blade_ == 0)
154 void DragonflyZone::generate_routers()
156 routers_.reserve(num_groups_ * num_chassis_per_group_ * num_blades_per_chassis_);
157 for (unsigned int i = 0; i < num_groups_; i++)
158 for (unsigned int j = 0; j < num_chassis_per_group_; j++)
159 for (unsigned int k = 0; k < num_blades_per_chassis_; k++)
160 routers_.emplace_back(i, j, k);
163 void DragonflyZone::generate_link(const std::string& id, int numlinks, resource::LinkImpl** linkup,
164 resource::LinkImpl** linkdown)
166 XBT_DEBUG("Generating link %s", id.c_str());
169 if (get_link_sharing_policy() == s4u::Link::SharingPolicy::SPLITDUPLEX) {
170 *linkup = create_link(id + "_UP", std::vector<double>{get_link_bandwidth() * numlinks})
171 ->set_latency(get_link_latency())
174 *linkdown = create_link(id + "_DOWN", std::vector<double>{get_link_bandwidth() * numlinks})
175 ->set_latency(get_link_latency())
179 *linkup = create_link(id, std::vector<double>{get_link_bandwidth() * numlinks})
180 ->set_latency(get_link_latency())
187 void DragonflyZone::generate_links()
189 static int uniqueId = 0;
190 resource::LinkImpl* linkup;
191 resource::LinkImpl* linkdown;
193 unsigned int numRouters = num_groups_ * num_chassis_per_group_ * num_blades_per_chassis_;
195 // Links from routers to their local nodes.
196 for (unsigned int i = 0; i < numRouters; i++) {
197 // allocate structures
198 routers_[i].my_nodes_.resize(num_links_per_link_ * num_nodes_per_blade_);
199 routers_[i].green_links_.resize(num_blades_per_chassis_);
200 routers_[i].black_links_.resize(num_chassis_per_group_);
202 for (unsigned int j = 0; j < num_links_per_link_ * num_nodes_per_blade_; j += num_links_per_link_) {
203 std::string id = "local_link_from_router_" + std::to_string(i) + "_to_node_" +
204 std::to_string(j / num_links_per_link_) + "_" + std::to_string(uniqueId);
205 generate_link(id, 1, &linkup, &linkdown);
207 routers_[i].my_nodes_[j] = linkup;
208 if (get_link_sharing_policy() == s4u::Link::SharingPolicy::SPLITDUPLEX)
209 routers_[i].my_nodes_[j + 1] = linkdown;
215 // Green links from routers to same chassis routers - alltoall
216 for (unsigned int i = 0; i < num_groups_ * num_chassis_per_group_; i++) {
217 for (unsigned int j = 0; j < num_blades_per_chassis_; j++) {
218 for (unsigned int k = j + 1; k < num_blades_per_chassis_; k++) {
219 std::string id = "green_link_in_chassis_" + std::to_string(i % num_chassis_per_group_) + "_between_routers_" +
220 std::to_string(j) + "_and_" + std::to_string(k) + "_" + std::to_string(uniqueId);
221 generate_link(id, num_links_green_, &linkup, &linkdown);
223 routers_[i * num_blades_per_chassis_ + j].green_links_[k] = linkup;
224 routers_[i * num_blades_per_chassis_ + k].green_links_[j] = linkdown;
230 // Black links from routers to same group routers - alltoall
231 for (unsigned int i = 0; i < num_groups_; i++) {
232 for (unsigned int j = 0; j < num_chassis_per_group_; j++) {
233 for (unsigned int k = j + 1; k < num_chassis_per_group_; k++) {
234 for (unsigned int l = 0; l < num_blades_per_chassis_; l++) {
235 std::string id = "black_link_in_group_" + std::to_string(i) + "_between_chassis_" + std::to_string(j) +
236 "_and_" + std::to_string(k) + "_blade_" + std::to_string(l) + "_" + std::to_string(uniqueId);
237 generate_link(id, num_links_black_, &linkup, &linkdown);
239 routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + j * num_blades_per_chassis_ + l]
240 .black_links_[k] = linkup;
241 routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + k * num_blades_per_chassis_ + l]
242 .black_links_[j] = linkdown;
249 // Blue links between groups - Not all routers involved, only one per group is linked to others. Let's say router n of
250 // each group is linked to group n.
251 // FIXME: in reality blue links may be attached to several different routers
252 for (unsigned int i = 0; i < num_groups_; i++) {
253 for (unsigned int j = i + 1; j < num_groups_; j++) {
254 unsigned int routernumi = i * num_blades_per_chassis_ * num_chassis_per_group_ + j;
255 unsigned int routernumj = j * num_blades_per_chassis_ * num_chassis_per_group_ + i;
256 std::string id = "blue_link_between_group_" + std::to_string(i) + "_and_" + std::to_string(j) + "_routers_" +
257 std::to_string(routernumi) + "_and_" + std::to_string(routernumj) + "_" +
258 std::to_string(uniqueId);
259 generate_link(id, num_links_blue_, &linkup, &linkdown);
261 routers_[routernumi].blue_link_ = linkup;
262 routers_[routernumj].blue_link_ = linkdown;
268 void DragonflyZone::get_local_route(NetPoint* src, NetPoint* dst, Route* route, double* latency)
270 // Minimal routing version.
271 // TODO : non-minimal random one, and adaptive ?
273 if (dst->is_router() || src->is_router())
276 XBT_VERB("dragonfly getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(),
279 if ((src->id() == dst->id()) && has_loopback()) {
280 resource::LinkImpl* uplink = get_uplink_from(node_pos(src->id()));
282 route->link_list_.push_back(uplink);
284 *latency += uplink->get_latency();
288 const auto myCoords = rankId_to_coords(src->id());
289 const auto targetCoords = rankId_to_coords(dst->id());
290 XBT_DEBUG("src : %u group, %u chassis, %u blade, %u node", myCoords.group, myCoords.chassis, myCoords.blade,
292 XBT_DEBUG("dst : %u group, %u chassis, %u blade, %u node", targetCoords.group, targetCoords.chassis,
293 targetCoords.blade, targetCoords.node);
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;
301 // node->router local link
302 route->link_list_.push_back(myRouter->my_nodes_[myCoords.node * num_links_per_link_]);
304 *latency += myRouter->my_nodes_[myCoords.node * num_links_per_link_]->get_latency();
306 if (has_limiter()) { // limiter for sender
307 route->link_list_.push_back(get_uplink_from(node_pos_with_loopback(src->id())));
310 if (targetRouter != myRouter) {
311 // are we on a different group ?
312 if (targetRouter->group_ != currentRouter->group_) {
313 // go to the router of our group connected to this one.
314 if (currentRouter->blade_ != targetCoords.group) {
315 // go to the nth router in our chassis
316 route->link_list_.push_back(currentRouter->green_links_[targetCoords.group]);
318 *latency += currentRouter->green_links_[targetCoords.group]->get_latency();
319 currentRouter = &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
320 myCoords.chassis * num_blades_per_chassis_ + targetCoords.group];
323 if (currentRouter->chassis_ != 0) {
324 // go to the first chassis of our group
325 route->link_list_.push_back(currentRouter->black_links_[0]);
327 *latency += currentRouter->black_links_[0]->get_latency();
329 &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords.group];
332 // go to destination group - the only optical hop
333 route->link_list_.push_back(currentRouter->blue_link_);
335 *latency += currentRouter->blue_link_->get_latency();
337 &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + myCoords.group];
340 // same group, but same blade ?
341 if (targetRouter->blade_ != currentRouter->blade_) {
342 route->link_list_.push_back(currentRouter->green_links_[targetCoords.blade]);
344 *latency += currentRouter->green_links_[targetCoords.blade]->get_latency();
346 &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords.blade];
349 // same blade, but same chassis ?
350 if (targetRouter->chassis_ != currentRouter->chassis_) {
351 route->link_list_.push_back(currentRouter->black_links_[targetCoords.chassis]);
353 *latency += currentRouter->black_links_[targetCoords.chassis]->get_latency();
357 if (has_limiter()) { // limiter for receiver
358 route->link_list_.push_back(get_downlink_to(node_pos_with_loopback(dst->id())));
361 // router->node local link
362 route->link_list_.push_back(
363 targetRouter->my_nodes_[targetCoords.node * num_links_per_link_ + num_links_per_link_ - 1]);
366 targetRouter->my_nodes_[targetCoords.node * num_links_per_link_ + num_links_per_link_ - 1]->get_latency();
368 // set gateways (if any)
369 route->gw_src_ = get_gateway(src->id());
370 route->gw_dst_ = get_gateway(dst->id());
372 } // namespace routing
373 } // namespace kernel
376 DragonflyParams::DragonflyParams(const std::pair<unsigned int, unsigned int>& groups,
377 const std::pair<unsigned int, unsigned int>& chassis,
378 const std::pair<unsigned int, unsigned int>& routers, unsigned int nodes)
379 : groups(groups), chassis(chassis), routers(routers), nodes(nodes)
381 if (groups.first == 0)
382 throw std::invalid_argument("Dragonfly: Invalid number of groups, must be > 0");
383 if (groups.second == 0)
384 throw std::invalid_argument("Dragonfly: Invalid number of blue (groups) links, must be > 0");
385 if (chassis.first == 0)
386 throw std::invalid_argument("Dragonfly: Invalid number of chassis, must be > 0");
387 if (chassis.second == 0)
388 throw std::invalid_argument("Dragonfly: Invalid number of black (chassis) links, must be > 0");
389 if (routers.first == 0)
390 throw std::invalid_argument("Dragonfly: Invalid number of routers, must be > 0");
391 if (routers.second == 0)
392 throw std::invalid_argument("Dragonfly: Invalid number of green (routers) links, must be > 0");
394 throw std::invalid_argument("Dragonfly: Invalid number of nodes, must be > 0");
397 NetZone* create_dragonfly_zone(const std::string& name, const NetZone* parent, const DragonflyParams& params,
398 const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
399 Link::SharingPolicy sharing_policy)
403 throw std::invalid_argument("DragonflyZone: incorrect bandwidth for internode communication, bw=" +
404 std::to_string(bandwidth));
406 throw std::invalid_argument("DragonflyZone: incorrect latency for internode communication, lat=" +
407 std::to_string(latency));
410 auto* zone = new kernel::routing::DragonflyZone(name);
411 zone->set_topology(params.groups.first, params.groups.second, params.chassis.first, params.chassis.second,
412 params.routers.first, params.routers.second, params.nodes);
414 zone->set_parent(parent->get_impl());
415 zone->set_link_characteristics(bandwidth, latency, sharing_policy);
418 std::vector<unsigned int> dimensions = {params.groups.first, params.chassis.first, params.routers.first,
420 int tot_elements = std::accumulate(dimensions.begin(), dimensions.end(), 1, std::multiplies<>());
421 for (int i = 0; i < tot_elements; i++) {
422 kernel::routing::NetPoint* netpoint;
425 zone->fill_leaf_from_cb(i, dimensions, set_callbacks, &netpoint, &loopback, &limiter);
428 return zone->get_iface();
432 } // namespace simgrid