1 /* Copyright (c) 2014-2023. 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/kernel/resource/NetworkModel.hpp"
10 #include <boost/algorithm/string/classification.hpp>
11 #include <boost/algorithm/string/split.hpp>
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_routing_dragonfly, ker_platform, "Kernel Dragonfly Routing");
18 namespace kernel::routing {
20 DragonflyZone::DragonflyZone(const std::string& name) : ClusterBase(name) {}
22 DragonflyZone::Coords DragonflyZone::rankId_to_coords(unsigned long rankId) const
24 // coords : group, chassis, blade, node
26 coords.group = rankId / (num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_);
27 rankId = rankId % (num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_);
28 coords.chassis = rankId / (num_blades_per_chassis_ * num_nodes_per_blade_);
29 rankId = rankId % (num_blades_per_chassis_ * num_nodes_per_blade_);
30 coords.blade = rankId / num_nodes_per_blade_;
31 coords.node = rankId % num_nodes_per_blade_;
35 void DragonflyZone::set_link_characteristics(double bw, double lat, s4u::Link::SharingPolicy sharing_policy)
37 ClusterBase::set_link_characteristics(bw, lat, sharing_policy);
38 if (sharing_policy == s4u::Link::SharingPolicy::SPLITDUPLEX)
39 num_links_per_link_ = 2;
42 void DragonflyZone::set_topology(unsigned int n_groups, unsigned int groups_links, unsigned int n_chassis,
43 unsigned int chassis_links, unsigned int n_routers, unsigned int routers_links,
46 num_groups_ = n_groups;
47 num_links_blue_ = groups_links;
49 num_chassis_per_group_ = n_chassis;
50 num_links_black_ = chassis_links;
52 num_blades_per_chassis_ = n_routers;
53 num_links_green_ = routers_links;
55 num_nodes_per_blade_ = nodes;
58 s4u::DragonflyParams DragonflyZone::parse_topo_parameters(const std::string& topo_parameters)
60 std::vector<std::string> parameters;
61 std::vector<std::string> tmp;
62 boost::split(parameters, topo_parameters, boost::is_any_of(";"));
64 if (parameters.size() != 4)
65 xbt_die("Dragonfly are defined by the number of groups, chassis per groups, blades per chassis, nodes per blade");
67 // Blue network : number of groups, number of links between each group
68 boost::split(tmp, parameters[0], boost::is_any_of(","));
70 xbt_die("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
72 unsigned int n_groups;
74 n_groups = std::stoi(tmp[0]);
75 } catch (const std::invalid_argument&) {
76 throw std::invalid_argument("Invalid number of groups:" + tmp[0]);
81 n_blue = std::stoi(tmp[1]);
82 } catch (const std::invalid_argument&) {
83 throw std::invalid_argument("Invalid number of links for the blue level:" + tmp[1]);
86 // Black network : number of chassis/group, number of links between each router on the black network
87 boost::split(tmp, parameters[1], boost::is_any_of(","));
89 xbt_die("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
91 unsigned int n_chassis;
93 n_chassis = std::stoi(tmp[0]);
94 } catch (const std::invalid_argument&) {
95 throw std::invalid_argument("Invalid number of chassis:" + tmp[0]);
100 n_black = std::stoi(tmp[1]);
101 } catch (const std::invalid_argument&) {
102 throw std::invalid_argument("Invalid number of links for the black level:" + tmp[1]);
105 // Green network : number of blades/chassis, number of links between each router on the green network
106 boost::split(tmp, parameters[2], boost::is_any_of(","));
108 xbt_die("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
110 unsigned int n_routers;
112 n_routers = std::stoi(tmp[0]);
113 } catch (const std::invalid_argument&) {
114 throw std::invalid_argument("Invalid number of routers:" + tmp[0]);
117 unsigned int n_green;
119 n_green = std::stoi(tmp[1]);
120 } catch (const std::invalid_argument&) {
121 throw std::invalid_argument("Invalid number of links for the green level:" + tmp[1]);
124 // The last part of topo_parameters should be the number of nodes per blade
125 unsigned int n_nodes;
127 n_nodes = std::stoi(parameters[3]);
128 } catch (const std::invalid_argument&) {
129 throw std::invalid_argument("Last parameter is not the amount of nodes per blade:" + parameters[3]);
131 return s4u::DragonflyParams({n_groups, n_blue}, {n_chassis, n_black}, {n_routers, n_green}, n_nodes);
134 /* Generate the cluster once every node is created */
135 void DragonflyZone::build_upper_levels(const s4u::ClusterCallbacks& set_callbacks)
137 generate_routers(set_callbacks);
141 void DragonflyZone::generate_routers(const s4u::ClusterCallbacks& set_callbacks)
143 unsigned long id = 2UL * num_groups_ * num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_;
144 /* get limiter for this router */
145 auto get_limiter = [this, &id, &set_callbacks](unsigned int i, unsigned int j,
146 unsigned int k) -> resource::StandardLinkImpl* {
147 kernel::resource::StandardLinkImpl* limiter = nullptr;
148 if (set_callbacks.limiter) {
150 const auto* s4u_link =
151 set_callbacks.limiter(get_iface(), {i, j, k, std::numeric_limits<unsigned int>::max()}, id);
153 limiter = s4u_link->get_impl();
159 routers_.reserve(static_cast<size_t>(num_groups_) * num_chassis_per_group_ * num_blades_per_chassis_);
160 for (unsigned int i = 0; i < num_groups_; i++) {
161 for (unsigned int j = 0; j < num_chassis_per_group_; j++) {
162 for (unsigned int k = 0; k < num_blades_per_chassis_; k++) {
163 routers_.emplace_back(i, j, k, get_limiter(i, j, k));
169 void DragonflyZone::generate_link(const std::string& id, int numlinks, resource::StandardLinkImpl** linkup,
170 resource::StandardLinkImpl** linkdown)
172 XBT_DEBUG("Generating link %s", id.c_str());
175 if (get_link_sharing_policy() == s4u::Link::SharingPolicy::SPLITDUPLEX) {
177 create_link(id + "_UP", {get_link_bandwidth() * numlinks})->set_latency(get_link_latency())->seal()->get_impl();
178 *linkdown = create_link(id + "_DOWN", {get_link_bandwidth() * numlinks})
179 ->set_latency(get_link_latency())
183 *linkup = create_link(id, {get_link_bandwidth() * numlinks})->set_latency(get_link_latency())->seal()->get_impl();
188 void DragonflyZone::generate_links()
190 static int uniqueId = 0;
191 resource::StandardLinkImpl* linkup;
192 resource::StandardLinkImpl* linkdown;
194 unsigned int numRouters = num_groups_ * num_chassis_per_group_ * num_blades_per_chassis_;
196 // Links from routers to their local nodes.
197 for (unsigned int i = 0; i < numRouters; i++) {
198 // allocate structures
199 routers_[i].my_nodes_.resize(static_cast<size_t>(num_links_per_link_) * num_nodes_per_blade_);
200 routers_[i].green_links_.resize(num_blades_per_chassis_);
201 routers_[i].black_links_.resize(num_chassis_per_group_);
203 for (unsigned int j = 0; j < num_links_per_link_ * num_nodes_per_blade_; j += num_links_per_link_) {
204 std::string id = "local_link_from_router_" + std::to_string(i) + "_to_node_" +
205 std::to_string(j / num_links_per_link_) + "_" + std::to_string(uniqueId);
206 generate_link(id, 1, &linkup, &linkdown);
208 routers_[i].my_nodes_[j] = linkup;
209 if (get_link_sharing_policy() == s4u::Link::SharingPolicy::SPLITDUPLEX)
210 routers_[i].my_nodes_[j + 1] = linkdown;
216 // Green links from routers to same chassis routers - alltoall
217 for (unsigned int i = 0; i < num_groups_ * num_chassis_per_group_; i++) {
218 for (unsigned int j = 0; j < num_blades_per_chassis_; j++) {
219 for (unsigned int k = j + 1; k < num_blades_per_chassis_; k++) {
220 std::string id = "green_link_in_chassis_" + std::to_string(i % num_chassis_per_group_) + "_between_routers_" +
221 std::to_string(j) + "_and_" + std::to_string(k) + "_" + std::to_string(uniqueId);
222 generate_link(id, num_links_green_, &linkup, &linkdown);
224 routers_[i * num_blades_per_chassis_ + j].green_links_[k] = linkup;
225 routers_[i * num_blades_per_chassis_ + k].green_links_[j] = linkdown;
231 // Black links from routers to same group routers - alltoall
232 for (unsigned int i = 0; i < num_groups_; i++) {
233 for (unsigned int j = 0; j < num_chassis_per_group_; j++) {
234 for (unsigned int k = j + 1; k < num_chassis_per_group_; k++) {
235 for (unsigned int l = 0; l < num_blades_per_chassis_; l++) {
236 std::string id = "black_link_in_group_" + std::to_string(i) + "_between_chassis_" + std::to_string(j) +
237 "_and_" + std::to_string(k) + "_blade_" + std::to_string(l) + "_" + std::to_string(uniqueId);
238 generate_link(id, num_links_black_, &linkup, &linkdown);
240 routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + j * num_blades_per_chassis_ + l]
241 .black_links_[k] = linkup;
242 routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + k * num_blades_per_chassis_ + l]
243 .black_links_[j] = linkdown;
250 // Blue links between groups - Not all routers involved, only one per group is linked to others. Let's say router n of
251 // each group is linked to group n.
252 // FIXME: in reality blue links may be attached to several different routers
253 for (unsigned int i = 0; i < num_groups_; i++) {
254 for (unsigned int j = i + 1; j < num_groups_; j++) {
255 unsigned int routernumi = i * num_blades_per_chassis_ * num_chassis_per_group_ + j;
256 unsigned int routernumj = j * num_blades_per_chassis_ * num_chassis_per_group_ + i;
257 std::string id = "blue_link_between_group_" + std::to_string(i) + "_and_" + std::to_string(j) + "_routers_" +
258 std::to_string(routernumi) + "_and_" + std::to_string(routernumj) + "_" +
259 std::to_string(uniqueId);
260 generate_link(id, num_links_blue_, &linkup, &linkdown);
262 routers_[routernumi].blue_link_ = linkup;
263 routers_[routernumj].blue_link_ = linkdown;
269 void DragonflyZone::get_local_route(const NetPoint* src, const NetPoint* dst, Route* route, double* latency)
271 // Minimal routing version.
272 // TODO : non-minimal random one, and adaptive ?
274 if (dst->is_router() || src->is_router())
277 XBT_VERB("dragonfly getLocalRoute from '%s'[%lu] to '%s'[%lu]", src->get_cname(), src->id(), dst->get_cname(),
280 if ((src->id() == dst->id()) && has_loopback()) {
281 resource::StandardLinkImpl* uplink = get_uplink_from(node_pos(src->id()));
283 add_link_latency(route->link_list_, uplink, latency);
287 const auto myCoords = rankId_to_coords(src->id());
288 const auto targetCoords = rankId_to_coords(dst->id());
289 XBT_DEBUG("src : %lu group, %lu chassis, %lu blade, %lu node", myCoords.group, myCoords.chassis, myCoords.blade,
291 XBT_DEBUG("dst : %lu group, %lu chassis, %lu blade, %lu node", targetCoords.group, targetCoords.chassis,
292 targetCoords.blade, targetCoords.node);
294 DragonflyRouter* myRouter = &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
295 myCoords.chassis * num_blades_per_chassis_ + myCoords.blade];
296 DragonflyRouter* targetRouter = &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
297 targetCoords.chassis * num_blades_per_chassis_ + targetCoords.blade];
298 DragonflyRouter* currentRouter = myRouter;
300 if (has_limiter()) { // limiter for sender
301 route->link_list_.push_back(get_uplink_from(node_pos_with_loopback(src->id())));
304 // node->router local link
305 add_link_latency(route->link_list_, myRouter->my_nodes_[myCoords.node * num_links_per_link_], latency);
307 if (targetRouter != myRouter) {
308 // are we on a different group ?
309 if (targetRouter->group_ != currentRouter->group_) {
310 // go to the router of our group connected to this one.
311 if (currentRouter->blade_ != targetCoords.group) {
312 if (currentRouter->limiter_)
313 route->link_list_.push_back(currentRouter->limiter_);
314 // go to the nth router in our chassis
315 add_link_latency(route->link_list_, currentRouter->green_links_[targetCoords.group], latency);
316 currentRouter = &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
317 myCoords.chassis * num_blades_per_chassis_ + targetCoords.group];
320 if (currentRouter->chassis_ != 0) {
321 // go to the first chassis of our group
322 if (currentRouter->limiter_)
323 route->link_list_.push_back(currentRouter->limiter_);
324 add_link_latency(route->link_list_, currentRouter->black_links_[0], latency);
326 &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords.group];
329 // go to destination group - the only optical hop
330 add_link_latency(route->link_list_, currentRouter->blue_link_, latency);
331 if (currentRouter->limiter_)
332 route->link_list_.push_back(currentRouter->limiter_);
334 &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + myCoords.group];
337 // same group, but same blade ?
338 if (targetRouter->blade_ != currentRouter->blade_) {
339 if (currentRouter->limiter_)
340 route->link_list_.push_back(currentRouter->limiter_);
341 add_link_latency(route->link_list_, currentRouter->green_links_[targetCoords.blade], latency);
343 &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords.blade];
346 // same blade, but same chassis ?
347 if (targetRouter->chassis_ != currentRouter->chassis_) {
348 if (currentRouter->limiter_)
349 route->link_list_.push_back(currentRouter->limiter_);
350 add_link_latency(route->link_list_, currentRouter->black_links_[targetCoords.chassis], latency);
354 // router->node local link
355 if (targetRouter->limiter_)
356 route->link_list_.push_back(targetRouter->limiter_);
357 add_link_latency(route->link_list_,
358 targetRouter->my_nodes_[targetCoords.node * num_links_per_link_ + num_links_per_link_ - 1], latency);
360 if (has_limiter()) { // limiter for receiver
361 route->link_list_.push_back(get_downlink_to(node_pos_with_loopback(dst->id())));
364 // set gateways (if any)
365 route->gw_src_ = get_gateway(src->id());
366 route->gw_dst_ = get_gateway(dst->id());
368 } // namespace kernel::routing
371 DragonflyParams::DragonflyParams(const std::pair<unsigned int, unsigned int>& groups,
372 const std::pair<unsigned int, unsigned int>& chassis,
373 const std::pair<unsigned int, unsigned int>& routers, unsigned int nodes)
374 : groups(groups), chassis(chassis), routers(routers), nodes(nodes)
376 if (groups.first == 0)
377 throw std::invalid_argument("Dragonfly: Invalid number of groups, must be > 0");
378 if (groups.second == 0)
379 throw std::invalid_argument("Dragonfly: Invalid number of blue (groups) links, must be > 0");
380 if (chassis.first == 0)
381 throw std::invalid_argument("Dragonfly: Invalid number of chassis, must be > 0");
382 if (chassis.second == 0)
383 throw std::invalid_argument("Dragonfly: Invalid number of black (chassis) links, must be > 0");
384 if (routers.first == 0)
385 throw std::invalid_argument("Dragonfly: Invalid number of routers, must be > 0");
386 if (routers.second == 0)
387 throw std::invalid_argument("Dragonfly: Invalid number of green (routers) links, must be > 0");
389 throw std::invalid_argument("Dragonfly: Invalid number of nodes, must be > 0");
392 NetZone* create_dragonfly_zone(const std::string& name, const NetZone* parent, const DragonflyParams& params,
393 const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
394 Link::SharingPolicy sharing_policy)
398 throw std::invalid_argument("DragonflyZone: incorrect bandwidth for internode communication, bw=" +
399 std::to_string(bandwidth));
401 throw std::invalid_argument("DragonflyZone: incorrect latency for internode communication, lat=" +
402 std::to_string(latency));
405 auto* zone = new kernel::routing::DragonflyZone(name);
406 zone->set_topology(params.groups.first, params.groups.second, params.chassis.first, params.chassis.second,
407 params.routers.first, params.routers.second, params.nodes);
409 zone->set_parent(parent->get_impl());
410 zone->set_link_characteristics(bandwidth, latency, sharing_policy);
413 std::vector<unsigned long> dimensions = {params.groups.first, params.chassis.first, params.routers.first,
415 int tot_elements = std::accumulate(dimensions.begin(), dimensions.end(), 1, std::multiplies<>());
416 for (int i = 0; i < tot_elements; i++) {
417 kernel::routing::NetPoint* netpoint;
420 zone->fill_leaf_from_cb(i, dimensions, set_callbacks, &netpoint, &loopback, &limiter);
422 zone->build_upper_levels(set_callbacks);
423 return zone->get_iface();
427 } // namespace simgrid