1 /* Copyright (c) 2016-2021. 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>
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 XBT_ATTRIB_DEPRECATED_v331("Please use get_parent()") NetZone* get_father();
46 NetZone* get_parent() const;
47 NetZone* set_parent(const NetZone* parent);
49 std::vector<Host*> get_all_hosts() const;
50 int 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 std::vector<NetZone*> get_children() const;
63 XBT_ATTRIB_DEPRECATED_v332("Please use set_parent() to manage NetZone's relationship") NetZone* add_child(
66 void extract_xbt_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
67 std::map<std::string, xbt_edge_t, std::less<>>* edges);
69 /* Add content to the netzone, at parsing time. It should be sealed afterward. */
70 int add_component(kernel::routing::NetPoint* elm); /* A host, a router or a netzone, whatever */
73 * @brief Add a route between 2 netpoints
76 * - route between 2 hosts/routers in same netzone, no gateway is needed
77 * - route between 2 netzones, connecting 2 gateways.
79 * @param src Source netzone's netpoint
80 * @param dst Destination netzone' netpoint
81 * @param gw_src Netpoint of the gateway in the source netzone
82 * @param gw_dst Netpoint of the gateway in the destination netzone
83 * @param link_list List of links and their direction used in this communication
84 * @param symmetrical Bi-directional communication
86 void add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst, kernel::routing::NetPoint* gw_src,
87 kernel::routing::NetPoint* gw_dst, const std::vector<LinkInRoute>& link_list, bool symmetrical = true);
90 XBT_ATTRIB_DEPRECATED_v332("Please use add_route() method which uses s4u::LinkInRoute instead of "
91 "LinkImpl") void add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
92 kernel::routing::NetPoint* gw_src,
93 kernel::routing::NetPoint* gw_dst,
94 const std::vector<kernel::resource::LinkImpl*>& link_list,
97 XBT_ATTRIB_DEPRECATED_v332("Please use add_bypass_route() method which uses s4u::LinkInRoute instead of "
98 "LinkImpl") void add_bypass_route(kernel::routing::NetPoint* src,
99 kernel::routing::NetPoint* dst,
100 kernel::routing::NetPoint* gw_src,
101 kernel::routing::NetPoint* gw_dst,
102 std::vector<kernel::resource::LinkImpl*>& link_list,
103 bool /*symmetrical*/);
106 void add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
107 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
108 const std::vector<LinkInRoute>& link_list);
111 /*** Called on each newly created regular route (not on bypass routes) */
112 static xbt::signal<void(bool symmetrical, kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
113 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
114 std::vector<kernel::resource::LinkImpl*> const& link_list)>
115 on_route_creation; // XBT_ATTRIB_DEPRECATED_v332 : should not be used by users, used by ns3.. if necessary,
116 // signal shouldn't use LinkImpl*
119 static xbt::signal<void(NetZone const&)> on_creation;
120 static xbt::signal<void(NetZone const&)> on_seal;
123 * @brief Create a host
125 * @param name Host name
126 * @param speed_per_pstate Vector of CPU's speeds
128 s4u::Host* create_host(const std::string& name, const std::vector<double>& speed_per_pstate);
129 s4u::Host* create_host(const std::string& name, double speed);
131 * @brief Create a Host (string version)
133 * @throw std::invalid_argument if speed format is incorrect.
135 s4u::Host* create_host(const std::string& name, const std::vector<std::string>& speed_per_pstate);
136 s4u::Host* create_host(const std::string& name, const std::string& speed);
139 * @brief Create a link
141 * @param name Link name
142 * @param bandwidths Link's speed (vector for wifi links)
143 * @throw std::invalid_argument if bandwidth format is incorrect.
145 s4u::Link* create_link(const std::string& name, const std::vector<double>& bandwidths);
146 s4u::Link* create_link(const std::string& name, double bandwidth);
148 /** @brief Create a link (string version) */
149 s4u::Link* create_link(const std::string& name, const std::vector<std::string>& bandwidths);
150 s4u::Link* create_link(const std::string& name, const std::string& bandwidth);
153 * @brief Create a split-duplex link
155 * In SimGrid, split-duplex links are a composition of 2 regular (shared) links (up/down).
157 * This function eases its utilization by creating the 2 links for you. We append a suffix
158 * "_UP" and "_DOWN" to your link name to identify each of them.
160 * Both up/down links have exactly the same bandwidth
162 * @param name Name of the link
163 * @param bandwidth Speed
165 s4u::SplitDuplexLink* create_split_duplex_link(const std::string& name, const std::string& bandwidth);
166 s4u::SplitDuplexLink* create_split_duplex_link(const std::string& name, double bandwidth);
168 kernel::resource::NetworkModelIntf* get_network_model() const;
171 * @brief Make a router within that NetZone
173 * @param name Router name
175 kernel::routing::NetPoint* create_router(const std::string& name);
177 /** @brief Seal this netzone configuration */
182 /** @brief XBT_ATTRIB_DEPRECATED_v332 Auxiliary function to convert types */
183 static std::vector<LinkInRoute> convert_to_linkInRoute(const std::vector<kernel::resource::LinkImpl*>& link_list);
187 // External constructors so that the types (and the types of their content) remain hidden
188 XBT_PUBLIC NetZone* create_full_zone(const std::string& name);
189 XBT_PUBLIC NetZone* create_star_zone(const std::string& name);
190 XBT_PUBLIC NetZone* create_dijkstra_zone(const std::string& name, bool cache);
191 XBT_PUBLIC NetZone* create_empty_zone(const std::string& name);
192 XBT_PUBLIC NetZone* create_floyd_zone(const std::string& name);
193 XBT_PUBLIC NetZone* create_vivaldi_zone(const std::string& name);
194 XBT_PUBLIC NetZone* create_wifi_zone(const std::string& name);
196 // Extra data structure for complex constructors
198 /** @brief Aggregates the callbacks used to build clusters netzones (Torus/Dragronfly/Fat-Tree) */
199 struct ClusterCallbacks {
201 * @brief Callback used to set the netpoint and gateway located at some leaf of clusters (Torus, FatTree, etc)
203 * The netpoint can be either a host, router or another netzone.
204 * Gateway must be non-null if netpoint is a netzone
206 * @param zone: The newly create zone, needed for creating new resources (hosts, links)
207 * @param coord: the coordinates of the element
208 * @param id: Internal identifier of the element
209 * @return pair<NetPoint*, NetPoint*>: returns a pair of netpoint and gateway.
211 using ClusterNetPointCb = std::pair<kernel::routing::NetPoint*, kernel::routing::NetPoint*>(
212 NetZone* zone, const std::vector<unsigned int>& coord, int id);
214 * @brief Callback used to set the links for some leaf of the cluster (Torus, FatTree, etc)
216 * The coord parameter depends on the cluster being created:
217 * - Torus: Direct translation of the Torus' dimensions, e.g. (0, 0, 0) for a 3-D Torus
218 * - Fat-Tree: A pair (level in the tree, id), e.g. (0, 0): first leaf and (1,0): first switch at level 1.
219 * - Dragonfly: a tuple (group, chassis, blades/routers, nodes), e.g. (0, 0, 0, 0) for first node in the cluster.
220 * Important: To identify the router inside a "group, chassis, blade", we use MAX_UINT in the last parameter (e.g. 0,
223 * @param zone: The newly create zone, needed for creating new resources (hosts, links)
224 * @param coord: the coordinates of the element
225 * @param id: Internal identifier of the element
226 * @return Pointer to the Link
228 using ClusterLinkCb = Link*(NetZone* zone, const std::vector<unsigned int>& coord, int id);
230 std::function<ClusterNetPointCb> netpoint;
231 std::function<ClusterLinkCb> loopback = {};
232 std::function<ClusterLinkCb> limiter = {};
233 explicit ClusterCallbacks(const std::function<ClusterNetPointCb>& set_netpoint)
234 : netpoint(set_netpoint){/*nothing to do */};
235 ClusterCallbacks(const std::function<ClusterNetPointCb>& set_netpoint,
236 const std::function<ClusterLinkCb>& set_loopback, const std::function<ClusterLinkCb>& set_limiter)
237 : netpoint(set_netpoint), loopback(set_loopback), limiter(set_limiter){/*nothing to do */};
240 * @brief Create a torus zone
242 * Torus clusters are characterized by:
243 * - dimensions, eg. {3,3,3} creates a torus with X = 3 elements, Y = 3 and Z = 3. In total, this cluster have 27
245 * - inter-node communication: (bandwidth, latency, sharing_policy) the elements are connected through regular links
246 * with these characteristics
247 * More details in: <a href="https://simgrid.org/doc/latest/Platform_examples.html?highlight=torus#torus-cluster">Torus
250 * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
252 * Note that the all elements in a Torus cluster must have (or not) the same elements (loopback and limiter)
254 * @param name NetZone's name
255 * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
257 * @param dimensions List of positive integers (> 0) which determines the torus' dimensions
258 * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
259 * @param bandwidth Characteristics of the inter-nodes link
260 * @param latency Characteristics of the inter-nodes link
261 * @param sharing_policy Characteristics of the inter-nodes link
262 * @return Pointer to new netzone
264 XBT_PUBLIC NetZone* create_torus_zone(const std::string& name, const NetZone* parent,
265 const std::vector<unsigned int>& dimensions,
266 const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
267 Link::SharingPolicy sharing_policy);
269 /** @brief Aggregates the parameters necessary to build a Fat-tree zone */
270 struct FatTreeParams {
272 std::vector<unsigned int> down;
273 std::vector<unsigned int> up;
274 std::vector<unsigned int> number;
275 FatTreeParams(unsigned int n_levels, const std::vector<unsigned int>& down_links,
276 const std::vector<unsigned int>& up_links, const std::vector<unsigned int>& links_number);
279 * @brief Create a Fat-Tree zone
281 * Fat-Tree clusters are characterized by:
282 * - levels: number of levels in the cluster, e.g. 2 (the final tree will have n+1 levels)
283 * - downlinks: for each level, how many connections between elements below them, e.g. 2, 3: level 1 nodes are connected
284 * to 2 nodes in level 2, which are connected to 3 nodes below. Determines the number total of leaves in the tree.
285 * - uplinks: for each level, how nodes are connected, e.g. 1, 2
286 * - link count: for each level, number of links connecting the nodes, e.g. 1, 1
288 * The best way to understand it is looking to the doc available in: <a
289 * href="https://simgrid.org/doc/latest/Platform_examples.html#fat-tree-cluster">Fat Tree Cluster</a>
291 * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
293 * Note that the all elements in a Fat-Tree cluster must have (or not) the same elements (loopback and limiter)
295 * @param name NetZone's name
296 * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
298 * @param parameters Characteristics of this Fat-Tree
299 * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
300 * @param bandwidth Characteristics of the inter-nodes link
301 * @param latency Characteristics of the inter-nodes link
302 * @param sharing_policy Characteristics of the inter-nodes link
303 * @return Pointer to new netzone
305 XBT_PUBLIC NetZone* create_fatTree_zone(const std::string& name, const NetZone* parent, const FatTreeParams& parameters,
306 const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
307 Link::SharingPolicy sharing_policy);
309 /** @brief Aggregates the parameters necessary to build a Dragonfly zone */
310 struct DragonflyParams {
311 std::pair<unsigned int, unsigned int> groups;
312 std::pair<unsigned int, unsigned int> chassis;
313 std::pair<unsigned int, unsigned int> routers;
315 DragonflyParams(const std::pair<unsigned int, unsigned int>& groups,
316 const std::pair<unsigned int, unsigned int>& chassis,
317 const std::pair<unsigned int, unsigned int>& routers, unsigned int nodes);
320 * @brief Create a Dragonfly zone
322 * Dragonfly clusters are characterized by:
323 * - groups: number of groups and links between each group, e.g. 2,2.
324 * - chassis: number of chassis in each group and the number of links used to connect the chassis, e.g. 2,3
325 * - routers: number of routers in each chassis and their links, e.g. 3,1
326 * - nodes: number of nodes connected to each router using a single link, e.g. 2
328 * In total, the cluster will have groups * chassis * routers * nodes elements/leaves.
330 * The best way to understand it is looking to the doc available in: <a
331 * href="https://simgrid.org/doc/latest/Platform_examples.html#dragonfly-cluster">Dragonfly Cluster</a>
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 Dragonfly 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 parameters Characteristics of this Dragonfly
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_dragonfly_zone(const std::string& name, const NetZone* parent,
348 const DragonflyParams& parameters, const ClusterCallbacks& set_callbacks,
349 double bandwidth, double latency, Link::SharingPolicy sharing_policy);
352 } // namespace simgrid
354 #endif /* SIMGRID_S4U_NETZONE_HPP */