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();
62 void extract_xbt_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
63 std::map<std::string, xbt_edge_t, std::less<>>* edges);
65 /* Add content to the netzone, at parsing time. It should be sealed afterward. */
66 unsigned long add_component(kernel::routing::NetPoint* elm); /* A host, a router or a netzone, whatever */
69 * @brief Add a route between 2 netpoints
72 * - route between 2 hosts/routers in same netzone, no gateway is needed
73 * - route between 2 netzones, connecting 2 gateways.
75 * @param src Source netzone's netpoint
76 * @param dst Destination netzone' netpoint
77 * @param gw_src Netpoint of the gateway in the source netzone
78 * @param gw_dst Netpoint of the gateway in the destination netzone
79 * @param link_list List of links and their direction used in this communication
80 * @param symmetrical Bi-directional communication
82 void add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst, kernel::routing::NetPoint* gw_src,
83 kernel::routing::NetPoint* gw_dst, const std::vector<LinkInRoute>& link_list, bool symmetrical = true);
85 * @brief Add a route between 2 netpoints, and same in other direction
88 * - route between 2 hosts/routers in same netzone, no gateway is needed
89 * - route between 2 netzones, connecting 2 gateways.
91 * @param src Source netzone's netpoint
92 * @param dst Destination netzone' netpoint
93 * @param gw_src Netpoint of the gateway in the source netzone
94 * @param gw_dst Netpoint of the gateway in the destination netzone
95 * @param link_list List of links
97 void add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst, kernel::routing::NetPoint* gw_src,
98 kernel::routing::NetPoint* gw_dst, const std::vector<const Link*>& links);
101 * @brief Add a route between 2 hosts
103 * @param src Source host
104 * @param dst Destination host
105 * @param link_list List of links and their direction used in this communication
106 * @param symmetrical Bi-directional communication
108 void add_route(const Host* src, const Host* dst, const std::vector<LinkInRoute>& link_list, bool symmetrical = true);
110 * @brief Add a route between 2 hosts
112 * @param src Source host
113 * @param dst Destination host
114 * @param link_list List of links. The UP direction will be used on src->dst and DOWN direction on dst->src
116 void add_route(const Host* src, const Host* dst, const std::vector<const Link*>& links);
118 void add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
119 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
120 const std::vector<LinkInRoute>& link_list);
124 static xbt::signal<void(NetZone const&)> on_creation;
125 static xbt::signal<void(NetZone const&)> on_seal;
129 /** \static Add a callback fired on each newly created NetZone */
130 static void on_creation_cb(const std::function<void(NetZone const&)>& cb) { on_creation.connect(cb); }
131 /** \static Add a callback fired on each newly sealed NetZone */
132 static void on_seal_cb(const std::function<void(NetZone const&)>& cb) { on_seal.connect(cb); }
135 * @brief Create a host
137 * @param name Host name
138 * @param speed_per_pstate Vector of CPU's speeds
140 s4u::Host* create_host(const std::string& name, const std::vector<double>& speed_per_pstate);
141 s4u::Host* create_host(const std::string& name, double speed);
143 * @brief Create a Host (string version)
145 * @throw std::invalid_argument if speed format is incorrect.
147 s4u::Host* create_host(const std::string& name, const std::vector<std::string>& speed_per_pstate);
148 s4u::Host* create_host(const std::string& name, const std::string& speed);
151 * @brief Create a link
153 * @param name Link name
154 * @param bandwidths Link's speed (vector for wifi links)
155 * @throw std::invalid_argument if bandwidth format is incorrect.
157 s4u::Link* create_link(const std::string& name, const std::vector<double>& bandwidths);
158 s4u::Link* create_link(const std::string& name, double bandwidth);
160 /** @brief Create a link (string version) */
161 s4u::Link* create_link(const std::string& name, const std::vector<std::string>& bandwidths);
162 s4u::Link* create_link(const std::string& name, const std::string& bandwidth);
165 * @brief Create a split-duplex link
167 * In SimGrid, split-duplex links are a composition of 2 regular (shared) links (up/down).
169 * This function eases its utilization by creating the 2 links for you. We append a suffix
170 * "_UP" and "_DOWN" to your link name to identify each of them.
172 * Both up/down links have exactly the same bandwidth
174 * @param name Name of the link
175 * @param bandwidth Speed
177 s4u::SplitDuplexLink* create_split_duplex_link(const std::string& name, const std::string& bandwidth);
178 s4u::SplitDuplexLink* create_split_duplex_link(const std::string& name, double bandwidth);
180 kernel::resource::NetworkModel* get_network_model() const;
183 * @brief Make a router within that NetZone
185 * @param name Router name
187 kernel::routing::NetPoint* create_router(const std::string& name);
189 /** @brief Seal this netzone configuration */
193 set_latency_factor_cb(std::function<double(double size, const s4u::Host* src, const s4u::Host* dst,
194 const std::vector<s4u::Link*>& /*links*/,
195 const std::unordered_set<s4u::NetZone*>& /*netzones*/)> const& cb) const;
197 set_bandwidth_factor_cb(std::function<double(double size, const s4u::Host* src, const s4u::Host* dst,
198 const std::vector<s4u::Link*>& /*links*/,
199 const std::unordered_set<s4u::NetZone*>& /*netzones*/)> const& cb) const;
202 // External constructors so that the types (and the types of their content) remain hidden
203 XBT_PUBLIC NetZone* create_full_zone(const std::string& name);
204 XBT_PUBLIC NetZone* create_star_zone(const std::string& name);
205 XBT_PUBLIC NetZone* create_dijkstra_zone(const std::string& name, bool cache);
206 XBT_PUBLIC NetZone* create_empty_zone(const std::string& name);
207 XBT_PUBLIC NetZone* create_floyd_zone(const std::string& name);
208 XBT_PUBLIC NetZone* create_vivaldi_zone(const std::string& name);
209 XBT_PUBLIC NetZone* create_wifi_zone(const std::string& name);
211 // Extra data structure for complex constructors
213 /** @brief Aggregates the callbacks used to build clusters netzones (Torus/Dragronfly/Fat-Tree) */
214 struct ClusterCallbacks {
216 * @brief Callback used to set the netpoint and gateway located at some leaf of clusters (Torus, FatTree, etc)
218 * The netpoint can be either a host, router or another netzone.
219 * Gateway must be non-null if netpoint is a netzone
221 * @param zone: The newly create zone, needed for creating new resources (hosts, links)
222 * @param coord: the coordinates of the element
223 * @param id: Internal identifier of the element
224 * @return pair<NetPoint*, NetPoint*>: returns a pair of netpoint and gateway.
226 using ClusterNetPointCb = std::pair<kernel::routing::NetPoint*, kernel::routing::NetPoint*>(
227 NetZone* zone, const std::vector<unsigned long>& coord, unsigned long id);
229 * @brief Callback used to set the links for some leaf of the cluster (Torus, FatTree, etc)
231 * The coord parameter depends on the cluster being created:
232 * - Torus: Direct translation of the Torus' dimensions, e.g. (0, 0, 0) for a 3-D Torus
233 * - Fat-Tree: A pair (level in the tree, id), e.g. (0, 0): first leaf and (1,0): first switch at level 1.
234 * - Dragonfly: a tuple (group, chassis, blades/routers, nodes), e.g. (0, 0, 0, 0) for first node in the cluster.
235 * Important: To identify the router inside a "group, chassis, blade", we use MAX_UINT in the last parameter (e.g. 0,
238 * @param zone: The newly create zone, needed for creating new resources (hosts, links)
239 * @param coord: the coordinates of the element
240 * @param id: Internal identifier of the element
241 * @return Pointer to the Link
243 using ClusterLinkCb = Link*(NetZone* zone, const std::vector<unsigned long>& coord, unsigned long id);
245 std::function<ClusterNetPointCb> netpoint;
246 std::function<ClusterLinkCb> loopback = {};
247 std::function<ClusterLinkCb> limiter = {};
248 explicit ClusterCallbacks(const std::function<ClusterNetPointCb>& set_netpoint)
249 : netpoint(set_netpoint){/*nothing to do */};
250 ClusterCallbacks(const std::function<ClusterNetPointCb>& set_netpoint,
251 const std::function<ClusterLinkCb>& set_loopback, const std::function<ClusterLinkCb>& set_limiter)
252 : netpoint(set_netpoint), loopback(set_loopback), limiter(set_limiter){/*nothing to do */};
255 * @brief Create a torus zone
257 * Torus clusters are characterized by:
258 * - dimensions, eg. {3,3,3} creates a torus with X = 3 elements, Y = 3 and Z = 3. In total, this cluster have 27
260 * - inter-node communication: (bandwidth, latency, sharing_policy) the elements are connected through regular links
261 * with these characteristics
262 * More details in: <a href="https://simgrid.org/doc/latest/Platform_examples.html?highlight=torus#torus-cluster">Torus
265 * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
267 * Note that the all elements in a Torus cluster must have (or not) the same elements (loopback and limiter)
269 * @param name NetZone's name
270 * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
272 * @param dimensions List of positive integers (> 0) which determines the torus' dimensions
273 * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
274 * @param bandwidth Characteristics of the inter-nodes link
275 * @param latency Characteristics of the inter-nodes link
276 * @param sharing_policy Characteristics of the inter-nodes link
277 * @return Pointer to new netzone
279 XBT_PUBLIC NetZone* create_torus_zone(const std::string& name, const NetZone* parent,
280 const std::vector<unsigned long>& dimensions,
281 const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
282 Link::SharingPolicy sharing_policy);
284 /** @brief Aggregates the parameters necessary to build a Fat-tree zone */
285 struct XBT_PUBLIC FatTreeParams {
287 std::vector<unsigned int> down;
288 std::vector<unsigned int> up;
289 std::vector<unsigned int> number;
290 FatTreeParams(unsigned int n_levels, const std::vector<unsigned int>& down_links,
291 const std::vector<unsigned int>& up_links, const std::vector<unsigned int>& links_number);
294 * @brief Create a Fat-Tree zone
296 * Fat-Tree clusters are characterized by:
297 * - levels: number of levels in the cluster, e.g. 2 (the final tree will have n+1 levels)
298 * - downlinks: for each level, how many connections between elements below them, e.g. 2, 3: level 1 nodes are connected
299 * to 2 nodes in level 2, which are connected to 3 nodes below. Determines the number total of leaves in the tree.
300 * - uplinks: for each level, how nodes are connected, e.g. 1, 2
301 * - link count: for each level, number of links connecting the nodes, e.g. 1, 1
303 * The best way to understand it is looking to the doc available in: <a
304 * href="https://simgrid.org/doc/latest/Platform_examples.html#fat-tree-cluster">Fat Tree Cluster</a>
306 * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
308 * Note that the all elements in a Fat-Tree cluster must have (or not) the same elements (loopback and limiter)
310 * @param name NetZone's name
311 * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
313 * @param parameters Characteristics of this Fat-Tree
314 * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
315 * @param bandwidth Characteristics of the inter-nodes link
316 * @param latency Characteristics of the inter-nodes link
317 * @param sharing_policy Characteristics of the inter-nodes link
318 * @return Pointer to new netzone
320 XBT_PUBLIC NetZone* create_fatTree_zone(const std::string& name, const NetZone* parent, const FatTreeParams& parameters,
321 const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
322 Link::SharingPolicy sharing_policy);
324 /** @brief Aggregates the parameters necessary to build a Dragonfly zone */
325 struct XBT_PUBLIC DragonflyParams {
326 std::pair<unsigned int, unsigned int> groups;
327 std::pair<unsigned int, unsigned int> chassis;
328 std::pair<unsigned int, unsigned int> routers;
330 DragonflyParams(const std::pair<unsigned int, unsigned int>& groups,
331 const std::pair<unsigned int, unsigned int>& chassis,
332 const std::pair<unsigned int, unsigned int>& routers, unsigned int nodes);
335 * @brief Create a Dragonfly zone
337 * Dragonfly clusters are characterized by:
338 * - groups: number of groups and links between each group, e.g. 2,2.
339 * - chassis: number of chassis in each group and the number of links used to connect the chassis, e.g. 2,3
340 * - routers: number of routers in each chassis and their links, e.g. 3,1
341 * - nodes: number of nodes connected to each router using a single link, e.g. 2
343 * In total, the cluster will have groups * chassis * routers * nodes elements/leaves.
345 * The best way to understand it is looking to the doc available in: <a
346 * href="https://simgrid.org/doc/latest/Platform_examples.html#dragonfly-cluster">Dragonfly Cluster</a>
348 * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
350 * Note that the all elements in a Dragonfly cluster must have (or not) the same elements (loopback and limiter)
352 * @param name NetZone's name
353 * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
355 * @param parameters Characteristics of this Dragonfly
356 * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
357 * @param bandwidth Characteristics of the inter-nodes link
358 * @param latency Characteristics of the inter-nodes link
359 * @param sharing_policy Characteristics of the inter-nodes link
360 * @return Pointer to new netzone
362 XBT_PUBLIC NetZone* create_dragonfly_zone(const std::string& name, const NetZone* parent,
363 const DragonflyParams& parameters, const ClusterCallbacks& set_callbacks,
364 double bandwidth, double latency, Link::SharingPolicy sharing_policy);
366 } // namespace simgrid::s4u
368 #endif /* SIMGRID_S4U_NETZONE_HPP */