1 /* Copyright (c) 2016-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 #ifndef SIMGRID_S4U_NETZONE_HPP
7 #define SIMGRID_S4U_NETZONE_HPP
9 #include "simgrid/s4u/Host.hpp"
10 #include <simgrid/forward.h>
11 #include <simgrid/s4u/Link.hpp>
12 #include <xbt/graph.h>
13 #include <xbt/signal.hpp>
17 #include <unordered_map>
18 #include <unordered_set>
22 namespace simgrid::s4u {
24 /** @brief Networking Zones
26 * A netzone is a network container, in charge of routing information between elements (hosts) and to the nearby
27 * netzones. In SimGrid, there is a hierarchy of netzones, with a unique root zone (that you can retrieve from the
30 class XBT_PUBLIC NetZone {
32 friend kernel::routing::NetZoneImpl;
35 kernel::routing::NetZoneImpl* const pimpl_;
38 explicit NetZone(kernel::routing::NetZoneImpl* impl) : pimpl_(impl) {}
41 /** @brief Retrieves the name of that netzone as a C++ string */
42 const std::string& get_name() const;
43 /** @brief Retrieves the name of that netzone as a C string */
44 const char* get_cname() const;
46 NetZone* get_parent() const;
47 NetZone* set_parent(const NetZone* parent);
48 std::vector<NetZone*> get_children() const;
50 std::vector<Host*> get_all_hosts() const;
51 size_t get_host_count() const;
53 kernel::routing::NetZoneImpl* get_impl() const { return pimpl_; }
55 /** Get the properties assigned to a netzone */
56 const std::unordered_map<std::string, std::string>* get_properties() const;
57 /** Retrieve the property value (or nullptr if not set) */
58 const char* get_property(const std::string& key) const;
59 void set_property(const std::string& key, const std::string& value);
60 /** @brief Get the netpoint associated to this netzone */
61 kernel::routing::NetPoint* get_netpoint() const;
62 /** @brief Get the gateway associated to this netzone */
63 kernel::routing::NetPoint* get_gateway() const;
64 kernel::routing::NetPoint* get_gateway(const std::string& name) const;
65 void set_gateway(const s4u::Host* router) { set_gateway(router->get_netpoint()); }
66 void set_gateway(kernel::routing::NetPoint* router);
67 void set_gateway(const std::string& name, kernel::routing::NetPoint* router);
69 void extract_xbt_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
70 std::map<std::string, xbt_edge_t, std::less<>>* edges);
72 /* Add content to the netzone, at parsing time. It should be sealed afterward. */
73 unsigned long add_component(kernel::routing::NetPoint* elm); /* A host, a router or a netzone, whatever */
76 * @brief Add a route between 2 netzones, and same in other direction
77 * @param src Source netzone
78 * @param dst Destination netzone
79 * @param links List of links
81 void add_route(const NetZone* src, const NetZone* dst, const std::vector<const Link*>& links);
84 * @brief Add a route between 2 netzones, and same in other direction
85 * @param src Source netzone
86 * @param dst Destination netzone
87 * @param link_list List of links and their direction used in this communication
88 * @param symmetrical Bi-directional communication
90 void add_route(const NetZone* src, const NetZone* dst, const std::vector<LinkInRoute>& link_list, bool symmetrical = true);
94 * @brief Add a route between 2 netpoints
97 * - route between 2 hosts/routers in same netzone, no gateway is needed
98 * - route between 2 netzones, connecting 2 gateways.
100 * @param src Source netzone's netpoint
101 * @param dst Destination netzone' netpoint
102 * @param gw_src Netpoint of the gateway in the source netzone
103 * @param gw_dst Netpoint of the gateway in the destination netzone
104 * @param link_list List of links and their direction used in this communication
105 * @param symmetrical Bi-directional communication
107 //(we should first remove the Python binding in v3.35) XBT_ATTRIB_DEPRECATED_v339("Please call add_route either from
108 // Host to Host or NetZone to NetZone")
109 void add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst, kernel::routing::NetPoint* gw_src,
110 kernel::routing::NetPoint* gw_dst, const std::vector<LinkInRoute>& link_list, bool symmetrical = true);
112 * @brief Add a route between 2 netpoints, and same in other direction
115 * - route between 2 hosts/routers in same netzone, no gateway is needed
116 * - route between 2 netzones, connecting 2 gateways.
118 * @param src Source netzone's netpoint
119 * @param dst Destination netzone' netpoint
120 * @param gw_src Netpoint of the gateway in the source netzone
121 * @param gw_dst Netpoint of the gateway in the destination netzone
122 * @param link_list List of links
124 XBT_ATTRIB_DEPRECATED_v339("Please call add_route either from Host to Host or NetZone to NetZone") void add_route(
125 kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst, kernel::routing::NetPoint* gw_src,
126 kernel::routing::NetPoint* gw_dst, const std::vector<const Link*>& links);
130 * @brief Add a route between 2 hosts
132 * @param src Source host
133 * @param dst Destination host
134 * @param link_list List of links and their direction used in this communication
135 * @param symmetrical Bi-directional communication
137 void add_route(const Host* src, const Host* dst, const std::vector<LinkInRoute>& link_list, bool symmetrical = true);
139 * @brief Add a route between 2 hosts
141 * @param src Source host
142 * @param dst Destination host
143 * @param links List of links. The UP direction will be used on src->dst and DOWN direction on dst->src
145 void add_route(const Host* src, const Host* dst, const std::vector<const Link*>& links);
147 void add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
148 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
149 const std::vector<LinkInRoute>& link_list);
153 static xbt::signal<void(NetZone const&)> on_creation;
154 static xbt::signal<void(NetZone const&)> on_seal;
158 /** \static Add a callback fired on each newly created NetZone */
159 static void on_creation_cb(const std::function<void(NetZone const&)>& cb) { on_creation.connect(cb); }
160 /** \static Add a callback fired on each newly sealed NetZone */
161 static void on_seal_cb(const std::function<void(NetZone const&)>& cb) { on_seal.connect(cb); }
164 * @brief Create a host
166 * @param name Host name
167 * @param speed_per_pstate Vector of CPU's speeds
169 s4u::Host* create_host(const std::string& name, const std::vector<double>& speed_per_pstate);
170 s4u::Host* create_host(const std::string& name, double speed);
172 * @brief Create a Host (string version)
174 * @throw std::invalid_argument if speed format is incorrect.
176 s4u::Host* create_host(const std::string& name, const std::vector<std::string>& speed_per_pstate);
177 s4u::Host* create_host(const std::string& name, const std::string& speed);
180 * @brief Create a link
182 * @param name Link name
183 * @param bandwidths Link's speed (vector for wifi links)
184 * @throw std::invalid_argument if bandwidth format is incorrect.
186 s4u::Link* create_link(const std::string& name, const std::vector<double>& bandwidths);
187 s4u::Link* create_link(const std::string& name, double bandwidth);
189 /** @brief Create a link (string version) */
190 s4u::Link* create_link(const std::string& name, const std::vector<std::string>& bandwidths);
191 s4u::Link* create_link(const std::string& name, const std::string& bandwidth);
194 * @brief Create a split-duplex link
196 * In SimGrid, split-duplex links are a composition of 2 regular (shared) links (up/down).
198 * This function eases its utilization by creating the 2 links for you. We append a suffix
199 * "_UP" and "_DOWN" to your link name to identify each of them.
201 * Both up/down links have exactly the same bandwidth
203 * @param name Name of the link
204 * @param bandwidth Speed
206 s4u::SplitDuplexLink* create_split_duplex_link(const std::string& name, const std::string& bandwidth);
207 s4u::SplitDuplexLink* create_split_duplex_link(const std::string& name, double bandwidth);
209 kernel::resource::NetworkModel* get_network_model() const;
212 * @brief Make a router within that NetZone
214 * @param name Router name
216 kernel::routing::NetPoint* create_router(const std::string& name);
218 /** @brief Seal this netzone configuration */
222 set_latency_factor_cb(std::function<double(double size, const s4u::Host* src, const s4u::Host* dst,
223 const std::vector<s4u::Link*>& /*links*/,
224 const std::unordered_set<s4u::NetZone*>& /*netzones*/)> const& cb) const;
226 set_bandwidth_factor_cb(std::function<double(double size, const s4u::Host* src, const s4u::Host* dst,
227 const std::vector<s4u::Link*>& /*links*/,
228 const std::unordered_set<s4u::NetZone*>& /*netzones*/)> const& cb) const;
231 // External constructors so that the types (and the types of their content) remain hidden
232 XBT_PUBLIC NetZone* create_full_zone(const std::string& name);
233 XBT_PUBLIC NetZone* create_star_zone(const std::string& name);
234 XBT_PUBLIC NetZone* create_dijkstra_zone(const std::string& name, bool cache);
235 XBT_PUBLIC NetZone* create_empty_zone(const std::string& name);
236 XBT_PUBLIC NetZone* create_floyd_zone(const std::string& name);
237 XBT_PUBLIC NetZone* create_vivaldi_zone(const std::string& name);
238 XBT_PUBLIC NetZone* create_wifi_zone(const std::string& name);
240 // Extra data structure for complex constructors
242 /** @brief Aggregates the callbacks used to build clusters netzones (Torus/Dragronfly/Fat-Tree) */
243 struct ClusterCallbacks {
245 * @brief Callback used to set the netpoint and gateway located at some leaf of clusters (Torus, FatTree, etc)
247 * The netpoint can be either a host, router or another netzone.
248 * Gateway must be non-null if netpoint is a netzone
250 * @param zone: The newly create zone, needed for creating new resources (hosts, links)
251 * @param coord: the coordinates of the element
252 * @param id: Internal identifier of the element
253 * @return pair<NetPoint*, NetPoint*>: returns a pair of netpoint and gateway.
255 // XBT_ATTRIB_DEPRECATED_v339
256 using ClusterNetPointCb = std::pair<kernel::routing::NetPoint*, kernel::routing::NetPoint*>(
257 NetZone* zone, const std::vector<unsigned long>& coord, unsigned long id);
260 * @brief Callback used to set the NetZone located at some leaf of clusters (Torus, FatTree, etc)
262 * @param zone: The parent zone, needed for creating new resources (hosts, links)
263 * @param coord: the coordinates of the element
264 * @param id: Internal identifier of the element
265 * @return NetZone*: returns newly created netzone
267 using ClusterNetZoneCb = NetZone*(NetZone* zone, const std::vector<unsigned long>& coord, unsigned long id);
269 * @brief Callback used to set the Host located at some leaf of clusters (Torus, FatTree, etc)
271 * @param zone: The parent zone, needed for creating new resources (hosts, links)
272 * @param coord: the coordinates of the element
273 * @param id: Internal identifier of the element
274 * @return Host*: returns newly created host
276 using ClusterHostCb = Host*(NetZone* zone, const std::vector<unsigned long>& coord, unsigned long id);
279 * @brief Callback used to set the links for some leaf of the cluster (Torus, FatTree, etc)
281 * The coord parameter depends on the cluster being created:
282 * - Torus: Direct translation of the Torus' dimensions, e.g. (0, 0, 0) for a 3-D Torus
283 * - Fat-Tree: A pair (level in the tree, id), e.g. (0, 0): first leaf and (1,0): first switch at level 1.
284 * - Dragonfly: a tuple (group, chassis, blades/routers, nodes), e.g. (0, 0, 0, 0) for first node in the cluster.
285 * Important: To identify the router inside a "group, chassis, blade", we use MAX_UINT in the last parameter (e.g. 0,
288 * @param zone: The newly create zone, needed for creating new resources (hosts, links)
289 * @param coord: the coordinates of the element
290 * @param id: Internal identifier of the element
291 * @return Pointer to the Link
293 using ClusterLinkCb = Link*(NetZone* zone, const std::vector<unsigned long>& coord, unsigned long id);
295 bool by_netzone_ = false;
296 bool is_by_netzone() const { return by_netzone_; }
297 bool by_netpoint_ = false; // XBT_ATTRIB_DEPRECATED_v339
298 bool is_by_netpoint() const { return by_netpoint_; } // XBT_ATTRIB_DEPRECATED_v339
299 std::function<ClusterNetPointCb> netpoint; // XBT_ATTRIB_DEPRECATED_v339
300 std::function<ClusterHostCb> host;
301 std::function<ClusterNetZoneCb> netzone;
302 std::function<ClusterLinkCb> loopback = {};
303 std::function<ClusterLinkCb> limiter = {};
304 explicit ClusterCallbacks(const std::function<ClusterNetZoneCb>& set_netzone)
305 : by_netzone_(true), netzone(set_netzone){/* nothing to do */};
307 ClusterCallbacks(const std::function<ClusterNetZoneCb>& set_netzone,
308 const std::function<ClusterLinkCb>& set_loopback, const std::function<ClusterLinkCb>& set_limiter)
309 : by_netzone_(true), netzone(set_netzone), loopback(set_loopback), limiter(set_limiter){/* nothing to do */};
311 explicit ClusterCallbacks(const std::function<ClusterHostCb>& set_host)
312 : host(set_host) {/* nothing to do */};
314 ClusterCallbacks(const std::function<ClusterHostCb>& set_host,
315 const std::function<ClusterLinkCb>& set_loopback, const std::function<ClusterLinkCb>& set_limiter)
316 : host(set_host), loopback(set_loopback), limiter(set_limiter){/* nothing to do */};
318 XBT_ATTRIB_DEPRECATED_v339("Please use callback with either a Host/NetZone creation function as first parameter")
319 explicit ClusterCallbacks(const std::function<ClusterNetPointCb>& set_netpoint)
320 : by_netpoint_(true), netpoint(set_netpoint){/* nothing to do */};
321 XBT_ATTRIB_DEPRECATED_v339("Please use callback with either a Host/NetZone creation function as first parameter")
322 ClusterCallbacks(const std::function<ClusterNetPointCb>& set_netpoint,
323 const std::function<ClusterLinkCb>& set_loopback, const std::function<ClusterLinkCb>& set_limiter)
324 : by_netpoint_(true), netpoint(set_netpoint), loopback(set_loopback), limiter(set_limiter){/* nothing to do */};
327 * @brief Create a torus zone
329 * Torus clusters are characterized by:
330 * - dimensions, eg. {3,3,3} creates a torus with X = 3 elements, Y = 3 and Z = 3. In total, this cluster have 27
332 * - inter-node communication: (bandwidth, latency, sharing_policy) the elements are connected through regular links
333 * with these characteristics
334 * More details in: <a href="https://simgrid.org/doc/latest/Platform_examples.html?highlight=torus#torus-cluster">Torus
337 * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
339 * Note that the all elements in a Torus cluster must have (or not) the same elements (loopback and limiter)
341 * @param name NetZone's name
342 * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
344 * @param dimensions List of positive integers (> 0) which determines the torus' dimensions
345 * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
346 * @param bandwidth Characteristics of the inter-nodes link
347 * @param latency Characteristics of the inter-nodes link
348 * @param sharing_policy Characteristics of the inter-nodes link
349 * @return Pointer to new netzone
351 XBT_PUBLIC NetZone* create_torus_zone(const std::string& name, const NetZone* parent,
352 const std::vector<unsigned long>& dimensions,
353 const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
354 Link::SharingPolicy sharing_policy);
356 /** @brief Aggregates the parameters necessary to build a Fat-tree zone */
357 struct XBT_PUBLIC FatTreeParams {
359 std::vector<unsigned int> down;
360 std::vector<unsigned int> up;
361 std::vector<unsigned int> number;
362 FatTreeParams(unsigned int n_levels, const std::vector<unsigned int>& down_links,
363 const std::vector<unsigned int>& up_links, const std::vector<unsigned int>& links_number);
366 * @brief Create a Fat-Tree zone
368 * Fat-Tree clusters are characterized by:
369 * - levels: number of levels in the cluster, e.g. 2 (the final tree will have n+1 levels)
370 * - downlinks: for each level, how many connections between elements below them, e.g. 2, 3: level 1 nodes are connected
371 * to 2 nodes in level 2, which are connected to 3 nodes below. Determines the number total of leaves in the tree.
372 * - uplinks: for each level, how nodes are connected, e.g. 1, 2
373 * - link count: for each level, number of links connecting the nodes, e.g. 1, 1
375 * The best way to understand it is looking to the doc available in: <a
376 * href="https://simgrid.org/doc/latest/Platform_examples.html#fat-tree-cluster">Fat Tree Cluster</a>
378 * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
380 * Note that the all elements in a Fat-Tree cluster must have (or not) the same elements (loopback and limiter)
382 * @param name NetZone's name
383 * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
385 * @param parameters Characteristics of this Fat-Tree
386 * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
387 * @param bandwidth Characteristics of the inter-nodes link
388 * @param latency Characteristics of the inter-nodes link
389 * @param sharing_policy Characteristics of the inter-nodes link
390 * @return Pointer to new netzone
392 XBT_PUBLIC NetZone* create_fatTree_zone(const std::string& name, const NetZone* parent, const FatTreeParams& parameters,
393 const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
394 Link::SharingPolicy sharing_policy);
396 /** @brief Aggregates the parameters necessary to build a Dragonfly zone */
397 struct XBT_PUBLIC DragonflyParams {
398 std::pair<unsigned int, unsigned int> groups;
399 std::pair<unsigned int, unsigned int> chassis;
400 std::pair<unsigned int, unsigned int> routers;
402 DragonflyParams(const std::pair<unsigned int, unsigned int>& groups,
403 const std::pair<unsigned int, unsigned int>& chassis,
404 const std::pair<unsigned int, unsigned int>& routers, unsigned int nodes);
407 * @brief Create a Dragonfly zone
409 * Dragonfly clusters are characterized by:
410 * - groups: number of groups and links between each group, e.g. 2,2.
411 * - chassis: number of chassis in each group and the number of links used to connect the chassis, e.g. 2,3
412 * - routers: number of routers in each chassis and their links, e.g. 3,1
413 * - nodes: number of nodes connected to each router using a single link, e.g. 2
415 * In total, the cluster will have groups * chassis * routers * nodes elements/leaves.
417 * The best way to understand it is looking to the doc available in: <a
418 * href="https://simgrid.org/doc/latest/Platform_examples.html#dragonfly-cluster">Dragonfly Cluster</a>
420 * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
422 * Note that the all elements in a Dragonfly cluster must have (or not) the same elements (loopback and limiter)
424 * @param name NetZone's name
425 * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
427 * @param parameters Characteristics of this Dragonfly
428 * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
429 * @param bandwidth Characteristics of the inter-nodes link
430 * @param latency Characteristics of the inter-nodes link
431 * @param sharing_policy Characteristics of the inter-nodes link
432 * @return Pointer to new netzone
434 XBT_PUBLIC NetZone* create_dragonfly_zone(const std::string& name, const NetZone* parent,
435 const DragonflyParams& parameters, const ClusterCallbacks& set_callbacks,
436 double bandwidth, double latency, Link::SharingPolicy sharing_policy);
438 } // namespace simgrid::s4u
440 #endif /* SIMGRID_S4U_NETZONE_HPP */