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/forward.h>
10 #include <simgrid/s4u/Link.hpp>
11 #include <xbt/graph.h>
12 #include <xbt/signal.hpp>
16 #include <unordered_map>
17 #include <unordered_set>
21 namespace simgrid::s4u {
23 /** @brief Networking Zones
25 * A netzone is a network container, in charge of routing information between elements (hosts) and to the nearby
26 * netzones. In SimGrid, there is a hierarchy of netzones, with a unique root zone (that you can retrieve from the
29 class XBT_PUBLIC NetZone {
31 friend kernel::routing::NetZoneImpl;
34 kernel::routing::NetZoneImpl* const pimpl_;
37 explicit NetZone(kernel::routing::NetZoneImpl* impl) : pimpl_(impl) {}
40 /** @brief Retrieves the name of that netzone as a C++ string */
41 const std::string& get_name() const;
42 /** @brief Retrieves the name of that netzone as a C string */
43 const char* get_cname() const;
45 NetZone* get_parent() const;
46 NetZone* set_parent(const NetZone* parent);
47 std::vector<NetZone*> get_children() const;
49 std::vector<Host*> get_all_hosts() const;
50 size_t get_host_count() const;
52 kernel::routing::NetZoneImpl* get_impl() const { return pimpl_; }
54 /** Get the properties assigned to a netzone */
55 const std::unordered_map<std::string, std::string>* get_properties() const;
56 /** Retrieve the property value (or nullptr if not set) */
57 const char* get_property(const std::string& key) const;
58 void set_property(const std::string& key, const std::string& value);
59 /** @brief Get the netpoint associated to this netzone */
60 kernel::routing::NetPoint* get_netpoint() const;
61 /** @brief Get the gateway associated to this netzone */
62 kernel::routing::NetPoint* get_gateway() const;
63 kernel::routing::NetPoint* get_gateway(const std::string& name) const;
64 void set_gateway(kernel::routing::NetPoint* router);
65 void set_gateway(const std::string& name, kernel::routing::NetPoint* router);
67 void extract_xbt_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
68 std::map<std::string, xbt_edge_t, std::less<>>* edges);
70 /* Add content to the netzone, at parsing time. It should be sealed afterward. */
71 unsigned long add_component(kernel::routing::NetPoint* elm); /* A host, a router or a netzone, whatever */
75 * @brief Add a route between 2 netzones, and same in other direction
76 * @param src Source netzone
77 * @param dst Destination netzone
78 * @param link_list List of links
80 void add_route(const NetZone* src, const NetZone* dst, const std::vector<const Link*>& links);
83 * @brief Add a route between 2 netzones, and same in other direction
84 * @param src Source netzone
85 * @param dst Destination netzone
86 * @param link_list List of links and their direction used in this communication
87 * @param symmetrical Bi-directional communication
89 void add_route(const NetZone* src, const NetZone* dst, const std::vector<LinkInRoute>& link_list, bool symmetrical = true);
92 * @brief Add a route between 2 netpoints
95 * - route between 2 hosts/routers in same netzone, no gateway is needed
96 * - route between 2 netzones, connecting 2 gateways.
98 * @param src Source netzone's netpoint
99 * @param dst Destination netzone' netpoint
100 * @param gw_src Netpoint of the gateway in the source netzone
101 * @param gw_dst Netpoint of the gateway in the destination netzone
102 * @param link_list List of links and their direction used in this communication
103 * @param symmetrical Bi-directional communication
105 //XBT_ATTRIB_DEPRECATED_v339("Please call add_route either from Host to Host or NetZone to NetZone")
106 void add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst, kernel::routing::NetPoint* gw_src,
107 kernel::routing::NetPoint* gw_dst, const std::vector<LinkInRoute>& link_list, bool symmetrical = true);
109 * @brief Add a route between 2 netpoints, and same in other direction
112 * - route between 2 hosts/routers in same netzone, no gateway is needed
113 * - route between 2 netzones, connecting 2 gateways.
115 * @param src Source netzone's netpoint
116 * @param dst Destination netzone' netpoint
117 * @param gw_src Netpoint of the gateway in the source netzone
118 * @param gw_dst Netpoint of the gateway in the destination netzone
119 * @param link_list List of links
121 //XBT_ATTRIB_DEPRECATED_v339("Please call add_route either from Host to Host or NetZone to NetZone")
122 void add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst, kernel::routing::NetPoint* gw_src,
123 kernel::routing::NetPoint* gw_dst, const std::vector<const Link*>& links);
126 * @brief Add a route between 2 hosts
128 * @param src Source host
129 * @param dst Destination host
130 * @param link_list List of links and their direction used in this communication
131 * @param symmetrical Bi-directional communication
133 void add_route(const Host* src, const Host* dst, const std::vector<LinkInRoute>& link_list, bool symmetrical = true);
135 * @brief Add a route between 2 hosts
137 * @param src Source host
138 * @param dst Destination host
139 * @param link_list List of links. The UP direction will be used on src->dst and DOWN direction on dst->src
141 void add_route(const Host* src, const Host* dst, const std::vector<const Link*>& links);
143 void add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
144 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
145 const std::vector<LinkInRoute>& link_list);
149 static xbt::signal<void(NetZone const&)> on_creation;
150 static xbt::signal<void(NetZone const&)> on_seal;
154 /** \static Add a callback fired on each newly created NetZone */
155 static void on_creation_cb(const std::function<void(NetZone const&)>& cb) { on_creation.connect(cb); }
156 /** \static Add a callback fired on each newly sealed NetZone */
157 static void on_seal_cb(const std::function<void(NetZone const&)>& cb) { on_seal.connect(cb); }
160 * @brief Create a host
162 * @param name Host name
163 * @param speed_per_pstate Vector of CPU's speeds
165 s4u::Host* create_host(const std::string& name, const std::vector<double>& speed_per_pstate);
166 s4u::Host* create_host(const std::string& name, double speed);
168 * @brief Create a Host (string version)
170 * @throw std::invalid_argument if speed format is incorrect.
172 s4u::Host* create_host(const std::string& name, const std::vector<std::string>& speed_per_pstate);
173 s4u::Host* create_host(const std::string& name, const std::string& speed);
176 * @brief Create a link
178 * @param name Link name
179 * @param bandwidths Link's speed (vector for wifi links)
180 * @throw std::invalid_argument if bandwidth format is incorrect.
182 s4u::Link* create_link(const std::string& name, const std::vector<double>& bandwidths);
183 s4u::Link* create_link(const std::string& name, double bandwidth);
185 /** @brief Create a link (string version) */
186 s4u::Link* create_link(const std::string& name, const std::vector<std::string>& bandwidths);
187 s4u::Link* create_link(const std::string& name, const std::string& bandwidth);
190 * @brief Create a split-duplex link
192 * In SimGrid, split-duplex links are a composition of 2 regular (shared) links (up/down).
194 * This function eases its utilization by creating the 2 links for you. We append a suffix
195 * "_UP" and "_DOWN" to your link name to identify each of them.
197 * Both up/down links have exactly the same bandwidth
199 * @param name Name of the link
200 * @param bandwidth Speed
202 s4u::SplitDuplexLink* create_split_duplex_link(const std::string& name, const std::string& bandwidth);
203 s4u::SplitDuplexLink* create_split_duplex_link(const std::string& name, double bandwidth);
205 kernel::resource::NetworkModel* get_network_model() const;
208 * @brief Make a router within that NetZone
210 * @param name Router name
212 kernel::routing::NetPoint* create_router(const std::string& name);
214 /** @brief Seal this netzone configuration */
218 set_latency_factor_cb(std::function<double(double size, const s4u::Host* src, const s4u::Host* dst,
219 const std::vector<s4u::Link*>& /*links*/,
220 const std::unordered_set<s4u::NetZone*>& /*netzones*/)> const& cb) const;
222 set_bandwidth_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;
227 // External constructors so that the types (and the types of their content) remain hidden
228 XBT_PUBLIC NetZone* create_full_zone(const std::string& name);
229 XBT_PUBLIC NetZone* create_star_zone(const std::string& name);
230 XBT_PUBLIC NetZone* create_dijkstra_zone(const std::string& name, bool cache);
231 XBT_PUBLIC NetZone* create_empty_zone(const std::string& name);
232 XBT_PUBLIC NetZone* create_floyd_zone(const std::string& name);
233 XBT_PUBLIC NetZone* create_vivaldi_zone(const std::string& name);
234 XBT_PUBLIC NetZone* create_wifi_zone(const std::string& name);
236 // Extra data structure for complex constructors
238 /** @brief Aggregates the callbacks used to build clusters netzones (Torus/Dragronfly/Fat-Tree) */
239 struct ClusterCallbacks {
241 * @brief Callback used to set the netpoint and gateway located at some leaf of clusters (Torus, FatTree, etc)
243 * The netpoint can be either a host, router or another netzone.
244 * Gateway must be non-null if netpoint is a netzone
246 * @param zone: The newly create zone, needed for creating new resources (hosts, links)
247 * @param coord: the coordinates of the element
248 * @param id: Internal identifier of the element
249 * @return pair<NetPoint*, NetPoint*>: returns a pair of netpoint and gateway.
251 // XBT_ATTRIB_DEPRECATED_v339
252 using ClusterNetPointCb = std::pair<kernel::routing::NetPoint*, kernel::routing::NetPoint*>(
253 NetZone* zone, const std::vector<unsigned long>& coord, unsigned long id);
256 * @brief Callback used to set the NetZone located at some leaf of clusters (Torus, FatTree, etc)
258 * @param zone: The parent zone, needed for creating new resources (hosts, links)
259 * @param coord: the coordinates of the element
260 * @param id: Internal identifier of the element
261 * @return NetZone*: returns newly created netzone
263 using ClusterNetZoneCb = NetZone*(NetZone* zone, const std::vector<unsigned long>& coord, unsigned long id);
265 * @brief Callback used to set the Host located at some leaf of clusters (Torus, FatTree, etc)
267 * @param zone: The parent zone, needed for creating new resources (hosts, links)
268 * @param coord: the coordinates of the element
269 * @param id: Internal identifier of the element
270 * @return Host*: returns newly created host
272 using ClusterHostCb = Host*(NetZone* zone, const std::vector<unsigned long>& coord, unsigned long id);
275 * @brief Callback used to set the links for some leaf of the cluster (Torus, FatTree, etc)
277 * The coord parameter depends on the cluster being created:
278 * - Torus: Direct translation of the Torus' dimensions, e.g. (0, 0, 0) for a 3-D Torus
279 * - Fat-Tree: A pair (level in the tree, id), e.g. (0, 0): first leaf and (1,0): first switch at level 1.
280 * - Dragonfly: a tuple (group, chassis, blades/routers, nodes), e.g. (0, 0, 0, 0) for first node in the cluster.
281 * Important: To identify the router inside a "group, chassis, blade", we use MAX_UINT in the last parameter (e.g. 0,
284 * @param zone: The newly create zone, needed for creating new resources (hosts, links)
285 * @param coord: the coordinates of the element
286 * @param id: Internal identifier of the element
287 * @return Pointer to the Link
289 using ClusterLinkCb = Link*(NetZone* zone, const std::vector<unsigned long>& coord, unsigned long id);
291 bool by_netzone_ = false;
292 bool is_by_netzone() const { return by_netzone_; }
293 bool by_netpoint_ = false; // XBT_ATTRIB_DEPRECATED_v339
294 bool is_by_netpoint() const { return by_netpoint_; } // XBT_ATTRIB_DEPRECATED_v339
295 std::function<ClusterNetPointCb> netpoint; // XBT_ATTRIB_DEPRECATED_v339
296 std::function<ClusterHostCb> host;
297 std::function<ClusterNetZoneCb> netzone;
298 std::function<ClusterLinkCb> loopback = {};
299 std::function<ClusterLinkCb> limiter = {};
300 explicit ClusterCallbacks(const std::function<ClusterNetZoneCb>& set_netzone)
301 : by_netzone_(true), netzone(set_netzone){/* nothing to do */};
303 ClusterCallbacks(const std::function<ClusterNetZoneCb>& set_netzone,
304 const std::function<ClusterLinkCb>& set_loopback, const std::function<ClusterLinkCb>& set_limiter)
305 : by_netzone_(true), netzone(set_netzone), loopback(set_loopback), limiter(set_limiter){/* nothing to do */};
307 explicit ClusterCallbacks(const std::function<ClusterHostCb>& set_host)
308 : host(set_host) {/* nothing to do */};
310 ClusterCallbacks(const std::function<ClusterHostCb>& set_host,
311 const std::function<ClusterLinkCb>& set_loopback, const std::function<ClusterLinkCb>& set_limiter)
312 : host(set_host), loopback(set_loopback), limiter(set_limiter){/* nothing to do */};
314 XBT_ATTRIB_DEPRECATED_v339("Please use callback with either a Host/NetZone creation function as first parameter")
315 explicit ClusterCallbacks(const std::function<ClusterNetPointCb>& set_netpoint)
316 : by_netpoint_(true), netpoint(set_netpoint){/* nothing to do */};
317 XBT_ATTRIB_DEPRECATED_v339("Please use callback with either a Host/NetZone creation function as first parameter")
318 ClusterCallbacks(const std::function<ClusterNetPointCb>& set_netpoint,
319 const std::function<ClusterLinkCb>& set_loopback, const std::function<ClusterLinkCb>& set_limiter)
320 : by_netpoint_(true), netpoint(set_netpoint), loopback(set_loopback), limiter(set_limiter){/* nothing to do */};
323 * @brief Create a torus zone
325 * Torus clusters are characterized by:
326 * - dimensions, eg. {3,3,3} creates a torus with X = 3 elements, Y = 3 and Z = 3. In total, this cluster have 27
328 * - inter-node communication: (bandwidth, latency, sharing_policy) the elements are connected through regular links
329 * with these characteristics
330 * More details in: <a href="https://simgrid.org/doc/latest/Platform_examples.html?highlight=torus#torus-cluster">Torus
333 * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
335 * Note that the all elements in a Torus cluster must have (or not) the same elements (loopback and limiter)
337 * @param name NetZone's name
338 * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
340 * @param dimensions List of positive integers (> 0) which determines the torus' dimensions
341 * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
342 * @param bandwidth Characteristics of the inter-nodes link
343 * @param latency Characteristics of the inter-nodes link
344 * @param sharing_policy Characteristics of the inter-nodes link
345 * @return Pointer to new netzone
347 XBT_PUBLIC NetZone* create_torus_zone(const std::string& name, const NetZone* parent,
348 const std::vector<unsigned long>& dimensions,
349 const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
350 Link::SharingPolicy sharing_policy);
352 /** @brief Aggregates the parameters necessary to build a Fat-tree zone */
353 struct XBT_PUBLIC FatTreeParams {
355 std::vector<unsigned int> down;
356 std::vector<unsigned int> up;
357 std::vector<unsigned int> number;
358 FatTreeParams(unsigned int n_levels, const std::vector<unsigned int>& down_links,
359 const std::vector<unsigned int>& up_links, const std::vector<unsigned int>& links_number);
362 * @brief Create a Fat-Tree zone
364 * Fat-Tree clusters are characterized by:
365 * - levels: number of levels in the cluster, e.g. 2 (the final tree will have n+1 levels)
366 * - downlinks: for each level, how many connections between elements below them, e.g. 2, 3: level 1 nodes are connected
367 * to 2 nodes in level 2, which are connected to 3 nodes below. Determines the number total of leaves in the tree.
368 * - uplinks: for each level, how nodes are connected, e.g. 1, 2
369 * - link count: for each level, number of links connecting the nodes, e.g. 1, 1
371 * The best way to understand it is looking to the doc available in: <a
372 * href="https://simgrid.org/doc/latest/Platform_examples.html#fat-tree-cluster">Fat Tree Cluster</a>
374 * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
376 * Note that the all elements in a Fat-Tree cluster must have (or not) the same elements (loopback and limiter)
378 * @param name NetZone's name
379 * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
381 * @param parameters Characteristics of this Fat-Tree
382 * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
383 * @param bandwidth Characteristics of the inter-nodes link
384 * @param latency Characteristics of the inter-nodes link
385 * @param sharing_policy Characteristics of the inter-nodes link
386 * @return Pointer to new netzone
388 XBT_PUBLIC NetZone* create_fatTree_zone(const std::string& name, const NetZone* parent, const FatTreeParams& parameters,
389 const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
390 Link::SharingPolicy sharing_policy);
392 /** @brief Aggregates the parameters necessary to build a Dragonfly zone */
393 struct XBT_PUBLIC DragonflyParams {
394 std::pair<unsigned int, unsigned int> groups;
395 std::pair<unsigned int, unsigned int> chassis;
396 std::pair<unsigned int, unsigned int> routers;
398 DragonflyParams(const std::pair<unsigned int, unsigned int>& groups,
399 const std::pair<unsigned int, unsigned int>& chassis,
400 const std::pair<unsigned int, unsigned int>& routers, unsigned int nodes);
403 * @brief Create a Dragonfly zone
405 * Dragonfly clusters are characterized by:
406 * - groups: number of groups and links between each group, e.g. 2,2.
407 * - chassis: number of chassis in each group and the number of links used to connect the chassis, e.g. 2,3
408 * - routers: number of routers in each chassis and their links, e.g. 3,1
409 * - nodes: number of nodes connected to each router using a single link, e.g. 2
411 * In total, the cluster will have groups * chassis * routers * nodes elements/leaves.
413 * The best way to understand it is looking to the doc available in: <a
414 * href="https://simgrid.org/doc/latest/Platform_examples.html#dragonfly-cluster">Dragonfly Cluster</a>
416 * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
418 * Note that the all elements in a Dragonfly cluster must have (or not) the same elements (loopback and limiter)
420 * @param name NetZone's name
421 * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
423 * @param parameters Characteristics of this Dragonfly
424 * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
425 * @param bandwidth Characteristics of the inter-nodes link
426 * @param latency Characteristics of the inter-nodes link
427 * @param sharing_policy Characteristics of the inter-nodes link
428 * @return Pointer to new netzone
430 XBT_PUBLIC NetZone* create_dragonfly_zone(const std::string& name, const NetZone* parent,
431 const DragonflyParams& parameters, const ClusterCallbacks& set_callbacks,
432 double bandwidth, double latency, Link::SharingPolicy sharing_policy);
434 } // namespace simgrid::s4u
436 #endif /* SIMGRID_S4U_NETZONE_HPP */