1 /* Copyright (c) 2014-2022. 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/StandardLinkImpl.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_routing, "Kernel Dragonfly Routing");
21 DragonflyZone::DragonflyZone(const std::string& name) : ClusterBase(name) {}
23 DragonflyZone::Coords DragonflyZone::rankId_to_coords(unsigned long 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::set_link_characteristics(double bw, double lat, s4u::Link::SharingPolicy sharing_policy)
38 ClusterBase::set_link_characteristics(bw, lat, sharing_policy);
39 if (sharing_policy == s4u::Link::SharingPolicy::SPLITDUPLEX)
40 num_links_per_link_ = 2;
43 void DragonflyZone::set_topology(unsigned int n_groups, unsigned int groups_links, unsigned int n_chassis,
44 unsigned int chassis_links, unsigned int n_routers, unsigned int routers_links,
47 num_groups_ = n_groups;
48 num_links_blue_ = groups_links;
50 num_chassis_per_group_ = n_chassis;
51 num_links_black_ = chassis_links;
53 num_blades_per_chassis_ = n_routers;
54 num_links_green_ = routers_links;
56 num_nodes_per_blade_ = nodes;
59 s4u::DragonflyParams DragonflyZone::parse_topo_parameters(const std::string& topo_parameters)
61 std::vector<std::string> parameters;
62 std::vector<std::string> tmp;
63 boost::split(parameters, topo_parameters, boost::is_any_of(";"));
65 if (parameters.size() != 4)
66 xbt_die("Dragonfly are defined by the number of groups, chassis per groups, blades per chassis, nodes per blade");
68 // Blue network : number of groups, number of links between each group
69 boost::split(tmp, parameters[0], boost::is_any_of(","));
71 xbt_die("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
73 unsigned int n_groups;
75 n_groups = std::stoi(tmp[0]);
76 } catch (const std::invalid_argument&) {
77 throw std::invalid_argument(std::string("Invalid number of groups:") + tmp[0]);
82 n_blue = std::stoi(tmp[1]);
83 } catch (const std::invalid_argument&) {
84 throw std::invalid_argument(std::string("Invalid number of links for the blue level:") + tmp[1]);
87 // Black network : number of chassis/group, number of links between each router on the black network
88 boost::split(tmp, parameters[1], boost::is_any_of(","));
90 xbt_die("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
92 unsigned int n_chassis;
94 n_chassis = std::stoi(tmp[0]);
95 } catch (const std::invalid_argument&) {
96 throw std::invalid_argument(std::string("Invalid number of chassis:") + tmp[0]);
101 n_black = std::stoi(tmp[1]);
102 } catch (const std::invalid_argument&) {
103 throw std::invalid_argument(std::string("Invalid number of links for the black level:") + tmp[1]);
106 // Green network : number of blades/chassis, number of links between each router on the green network
107 boost::split(tmp, parameters[2], boost::is_any_of(","));
109 xbt_die("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
111 unsigned int n_routers;
113 n_routers = std::stoi(tmp[0]);
114 } catch (const std::invalid_argument&) {
115 throw std::invalid_argument(std::string("Invalid number of routers:") + tmp[0]);
118 unsigned int n_green;
120 n_green = std::stoi(tmp[1]);
121 } catch (const std::invalid_argument&) {
122 throw std::invalid_argument(std::string("Invalid number of links for the green level:") + tmp[1]);
125 // The last part of topo_parameters should be the number of nodes per blade
126 unsigned int n_nodes;
128 n_nodes = std::stoi(parameters[3]);
129 } catch (const std::invalid_argument&) {
130 throw std::invalid_argument(std::string("Last parameter is not the amount of nodes per blade:") + parameters[3]);
132 return s4u::DragonflyParams({n_groups, n_blue}, {n_chassis, n_black}, {n_routers, n_green}, n_nodes);
135 /* Generate the cluster once every node is created */
136 void DragonflyZone::build_upper_levels(const s4u::ClusterCallbacks& set_callbacks)
138 generate_routers(set_callbacks);
142 void DragonflyZone::generate_routers(const s4u::ClusterCallbacks& set_callbacks)
144 unsigned long id = 2UL * num_groups_ * num_chassis_per_group_ * num_blades_per_chassis_ * num_nodes_per_blade_;
145 /* get limiter for this router */
146 auto get_limiter = [this, &id, &set_callbacks](unsigned int i, unsigned int j,
147 unsigned int k) -> resource::StandardLinkImpl* {
148 kernel::resource::StandardLinkImpl* limiter = nullptr;
149 if (set_callbacks.limiter) {
151 const auto* s4u_link =
152 set_callbacks.limiter(get_iface(), {i, j, k, std::numeric_limits<unsigned int>::max()}, id);
154 limiter = s4u_link->get_impl();
160 routers_.reserve(static_cast<size_t>(num_groups_) * num_chassis_per_group_ * num_blades_per_chassis_);
161 for (unsigned int i = 0; i < num_groups_; i++) {
162 for (unsigned int j = 0; j < num_chassis_per_group_; j++) {
163 for (unsigned int k = 0; k < num_blades_per_chassis_; k++) {
164 routers_.emplace_back(i, j, k, get_limiter(i, j, k));
170 void DragonflyZone::generate_link(const std::string& id, int numlinks, resource::StandardLinkImpl** linkup,
171 resource::StandardLinkImpl** linkdown)
173 XBT_DEBUG("Generating link %s", id.c_str());
176 if (get_link_sharing_policy() == s4u::Link::SharingPolicy::SPLITDUPLEX) {
178 create_link(id + "_UP", {get_link_bandwidth() * numlinks})->set_latency(get_link_latency())->seal()->get_impl();
179 *linkdown = create_link(id + "_DOWN", {get_link_bandwidth() * numlinks})
180 ->set_latency(get_link_latency())
184 *linkup = create_link(id, {get_link_bandwidth() * numlinks})->set_latency(get_link_latency())->seal()->get_impl();
189 void DragonflyZone::generate_links()
191 static int uniqueId = 0;
192 resource::StandardLinkImpl* linkup;
193 resource::StandardLinkImpl* linkdown;
195 unsigned int numRouters = num_groups_ * num_chassis_per_group_ * num_blades_per_chassis_;
197 // Links from routers to their local nodes.
198 for (unsigned int i = 0; i < numRouters; i++) {
199 // allocate structures
200 routers_[i].my_nodes_.resize(static_cast<size_t>(num_links_per_link_) * num_nodes_per_blade_);
201 routers_[i].green_links_.resize(num_blades_per_chassis_);
202 routers_[i].black_links_.resize(num_chassis_per_group_);
204 for (unsigned int j = 0; j < num_links_per_link_ * num_nodes_per_blade_; j += num_links_per_link_) {
205 std::string id = "local_link_from_router_" + std::to_string(i) + "_to_node_" +
206 std::to_string(j / num_links_per_link_) + "_" + std::to_string(uniqueId);
207 generate_link(id, 1, &linkup, &linkdown);
209 routers_[i].my_nodes_[j] = linkup;
210 if (get_link_sharing_policy() == s4u::Link::SharingPolicy::SPLITDUPLEX)
211 routers_[i].my_nodes_[j + 1] = linkdown;
217 // Green links from routers to same chassis routers - alltoall
218 for (unsigned int i = 0; i < num_groups_ * num_chassis_per_group_; i++) {
219 for (unsigned int j = 0; j < num_blades_per_chassis_; j++) {
220 for (unsigned int k = j + 1; k < num_blades_per_chassis_; k++) {
221 std::string id = "green_link_in_chassis_" + std::to_string(i % num_chassis_per_group_) + "_between_routers_" +
222 std::to_string(j) + "_and_" + std::to_string(k) + "_" + std::to_string(uniqueId);
223 generate_link(id, num_links_green_, &linkup, &linkdown);
225 routers_[i * num_blades_per_chassis_ + j].green_links_[k] = linkup;
226 routers_[i * num_blades_per_chassis_ + k].green_links_[j] = linkdown;
232 // Black links from routers to same group routers - alltoall
233 for (unsigned int i = 0; i < num_groups_; i++) {
234 for (unsigned int j = 0; j < num_chassis_per_group_; j++) {
235 for (unsigned int k = j + 1; k < num_chassis_per_group_; k++) {
236 for (unsigned int l = 0; l < num_blades_per_chassis_; l++) {
237 std::string id = "black_link_in_group_" + std::to_string(i) + "_between_chassis_" + std::to_string(j) +
238 "_and_" + std::to_string(k) + "_blade_" + std::to_string(l) + "_" + std::to_string(uniqueId);
239 generate_link(id, num_links_black_, &linkup, &linkdown);
241 routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + j * num_blades_per_chassis_ + l]
242 .black_links_[k] = linkup;
243 routers_[i * num_blades_per_chassis_ * num_chassis_per_group_ + k * num_blades_per_chassis_ + l]
244 .black_links_[j] = linkdown;
251 // Blue links between groups - Not all routers involved, only one per group is linked to others. Let's say router n of
252 // each group is linked to group n.
253 // FIXME: in reality blue links may be attached to several different routers
254 for (unsigned int i = 0; i < num_groups_; i++) {
255 for (unsigned int j = i + 1; j < num_groups_; j++) {
256 unsigned int routernumi = i * num_blades_per_chassis_ * num_chassis_per_group_ + j;
257 unsigned int routernumj = j * num_blades_per_chassis_ * num_chassis_per_group_ + i;
258 std::string id = "blue_link_between_group_" + std::to_string(i) + "_and_" + std::to_string(j) + "_routers_" +
259 std::to_string(routernumi) + "_and_" + std::to_string(routernumj) + "_" +
260 std::to_string(uniqueId);
261 generate_link(id, num_links_blue_, &linkup, &linkdown);
263 routers_[routernumi].blue_link_ = linkup;
264 routers_[routernumj].blue_link_ = linkdown;
270 void DragonflyZone::get_local_route(const NetPoint* src, const NetPoint* dst, Route* route, double* latency)
272 // Minimal routing version.
273 // TODO : non-minimal random one, and adaptive ?
275 if (dst->is_router() || src->is_router())
278 XBT_VERB("dragonfly getLocalRoute from '%s'[%lu] to '%s'[%lu]", src->get_cname(), src->id(), dst->get_cname(),
281 if ((src->id() == dst->id()) && has_loopback()) {
282 resource::StandardLinkImpl* uplink = get_uplink_from(node_pos(src->id()));
284 add_link_latency(route->link_list_, uplink, latency);
288 const auto myCoords = rankId_to_coords(src->id());
289 const auto targetCoords = rankId_to_coords(dst->id());
290 XBT_DEBUG("src : %lu group, %lu chassis, %lu blade, %lu node", myCoords.group, myCoords.chassis, myCoords.blade,
292 XBT_DEBUG("dst : %lu group, %lu chassis, %lu blade, %lu 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 if (has_limiter()) { // limiter for sender
302 route->link_list_.push_back(get_uplink_from(node_pos_with_loopback(src->id())));
305 // node->router local link
306 add_link_latency(route->link_list_, myRouter->my_nodes_[myCoords.node * num_links_per_link_], latency);
308 if (targetRouter != myRouter) {
309 // are we on a different group ?
310 if (targetRouter->group_ != currentRouter->group_) {
311 // go to the router of our group connected to this one.
312 if (currentRouter->blade_ != targetCoords.group) {
313 if (currentRouter->limiter_)
314 route->link_list_.push_back(currentRouter->limiter_);
315 // go to the nth router in our chassis
316 add_link_latency(route->link_list_, currentRouter->green_links_[targetCoords.group], latency);
317 currentRouter = &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) +
318 myCoords.chassis * num_blades_per_chassis_ + targetCoords.group];
321 if (currentRouter->chassis_ != 0) {
322 // go to the first chassis of our group
323 if (currentRouter->limiter_)
324 route->link_list_.push_back(currentRouter->limiter_);
325 add_link_latency(route->link_list_, currentRouter->black_links_[0], latency);
327 &routers_[myCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords.group];
330 // go to destination group - the only optical hop
331 add_link_latency(route->link_list_, currentRouter->blue_link_, latency);
332 if (currentRouter->limiter_)
333 route->link_list_.push_back(currentRouter->limiter_);
335 &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + myCoords.group];
338 // same group, but same blade ?
339 if (targetRouter->blade_ != currentRouter->blade_) {
340 if (currentRouter->limiter_)
341 route->link_list_.push_back(currentRouter->limiter_);
342 add_link_latency(route->link_list_, currentRouter->green_links_[targetCoords.blade], latency);
344 &routers_[targetCoords.group * (num_chassis_per_group_ * num_blades_per_chassis_) + targetCoords.blade];
347 // same blade, but same chassis ?
348 if (targetRouter->chassis_ != currentRouter->chassis_) {
349 if (currentRouter->limiter_)
350 route->link_list_.push_back(currentRouter->limiter_);
351 add_link_latency(route->link_list_, currentRouter->black_links_[targetCoords.chassis], latency);
355 // router->node local link
356 if (targetRouter->limiter_)
357 route->link_list_.push_back(targetRouter->limiter_);
358 add_link_latency(route->link_list_,
359 targetRouter->my_nodes_[targetCoords.node * num_links_per_link_ + num_links_per_link_ - 1], latency);
361 if (has_limiter()) { // limiter for receiver
362 route->link_list_.push_back(get_downlink_to(node_pos_with_loopback(dst->id())));
365 // set gateways (if any)
366 route->gw_src_ = get_gateway(src->id());
367 route->gw_dst_ = get_gateway(dst->id());
369 } // namespace routing
370 } // namespace kernel
373 DragonflyParams::DragonflyParams(const std::pair<unsigned int, unsigned int>& groups,
374 const std::pair<unsigned int, unsigned int>& chassis,
375 const std::pair<unsigned int, unsigned int>& routers, unsigned int nodes)
376 : groups(groups), chassis(chassis), routers(routers), nodes(nodes)
378 if (groups.first == 0)
379 throw std::invalid_argument("Dragonfly: Invalid number of groups, must be > 0");
380 if (groups.second == 0)
381 throw std::invalid_argument("Dragonfly: Invalid number of blue (groups) links, must be > 0");
382 if (chassis.first == 0)
383 throw std::invalid_argument("Dragonfly: Invalid number of chassis, must be > 0");
384 if (chassis.second == 0)
385 throw std::invalid_argument("Dragonfly: Invalid number of black (chassis) links, must be > 0");
386 if (routers.first == 0)
387 throw std::invalid_argument("Dragonfly: Invalid number of routers, must be > 0");
388 if (routers.second == 0)
389 throw std::invalid_argument("Dragonfly: Invalid number of green (routers) links, must be > 0");
391 throw std::invalid_argument("Dragonfly: Invalid number of nodes, must be > 0");
394 NetZone* create_dragonfly_zone(const std::string& name, const NetZone* parent, const DragonflyParams& params,
395 const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
396 Link::SharingPolicy sharing_policy)
400 throw std::invalid_argument("DragonflyZone: incorrect bandwidth for internode communication, bw=" +
401 std::to_string(bandwidth));
403 throw std::invalid_argument("DragonflyZone: incorrect latency for internode communication, lat=" +
404 std::to_string(latency));
407 auto* zone = new kernel::routing::DragonflyZone(name);
408 zone->set_topology(params.groups.first, params.groups.second, params.chassis.first, params.chassis.second,
409 params.routers.first, params.routers.second, params.nodes);
411 zone->set_parent(parent->get_impl());
412 zone->set_link_characteristics(bandwidth, latency, sharing_policy);
415 std::vector<unsigned long> dimensions = {params.groups.first, params.chassis.first, params.routers.first,
417 int tot_elements = std::accumulate(dimensions.begin(), dimensions.end(), 1, std::multiplies<>());
418 for (int i = 0; i < tot_elements; i++) {
419 kernel::routing::NetPoint* netpoint;
422 zone->fill_leaf_from_cb(i, dimensions, set_callbacks, &netpoint, &loopback, &limiter);
424 zone->build_upper_levels(set_callbacks);
425 return zone->get_iface();
429 } // namespace simgrid