Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a add_route for hosts, without gateways
[simgrid.git] / include / simgrid / s4u / NetZone.hpp
1 /* Copyright (c) 2016-2023. The SimGrid Team. All rights reserved.               */
2
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. */
5
6 #ifndef SIMGRID_S4U_NETZONE_HPP
7 #define SIMGRID_S4U_NETZONE_HPP
8
9 #include <simgrid/forward.h>
10 #include <simgrid/s4u/Link.hpp>
11 #include <xbt/graph.h>
12 #include <xbt/signal.hpp>
13
14 #include <map>
15 #include <string>
16 #include <unordered_map>
17 #include <unordered_set>
18 #include <utility>
19 #include <vector>
20
21 namespace simgrid::s4u {
22
23 /** @brief Networking Zones
24  *
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
27  * s4u::Engine).
28  */
29 class XBT_PUBLIC NetZone {
30 #ifndef DOXYGEN
31   friend kernel::routing::NetZoneImpl;
32 #endif
33
34   kernel::routing::NetZoneImpl* const pimpl_;
35
36 protected:
37   explicit NetZone(kernel::routing::NetZoneImpl* impl) : pimpl_(impl) {}
38
39 public:
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;
44
45   NetZone* get_parent() const;
46   NetZone* set_parent(const NetZone* parent);
47   std::vector<NetZone*> get_children() const;
48
49   std::vector<Host*> get_all_hosts() const;
50   size_t get_host_count() const;
51
52   kernel::routing::NetZoneImpl* get_impl() const { return pimpl_; }
53
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();
61
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);
64
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 */
67
68   /**
69    * @brief Add a route between 2 netpoints
70    *
71    * Create a route:
72    * - route between 2 hosts/routers in same netzone, no gateway is needed
73    * - route between 2 netzones, connecting 2 gateways.
74    *
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
81    */
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);
84   /**
85    * @brief Add a route between 2 hosts
86    *
87    * @param src Source host
88    * @param dst Destination host
89    * @param link_list List of links and their direction used in this communication
90    * @param symmetrical Bi-directional communication
91    */
92   void add_route(const Host* src, const Host* dst, const std::vector<LinkInRoute>& link_list, bool symmetrical = true);
93
94   void add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
95                         kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
96                         const std::vector<LinkInRoute>& link_list);
97
98 private:
99 #ifndef DOXYGEN
100   static xbt::signal<void(NetZone const&)> on_creation;
101   static xbt::signal<void(NetZone const&)> on_seal;
102 #endif
103
104 public:
105   /** \static Add a callback fired on each newly created NetZone */
106   static void on_creation_cb(const std::function<void(NetZone const&)>& cb) { on_creation.connect(cb); }
107   /** \static Add a callback fired on each newly sealed NetZone */
108   static void on_seal_cb(const std::function<void(NetZone const&)>& cb) { on_seal.connect(cb); }
109
110   /**
111    * @brief Create a host
112    *
113    * @param name Host name
114    * @param speed_per_pstate Vector of CPU's speeds
115    */
116   s4u::Host* create_host(const std::string& name, const std::vector<double>& speed_per_pstate);
117   s4u::Host* create_host(const std::string& name, double speed);
118   /**
119    * @brief Create a Host (string version)
120    *
121    * @throw std::invalid_argument if speed format is incorrect.
122    */
123   s4u::Host* create_host(const std::string& name, const std::vector<std::string>& speed_per_pstate);
124   s4u::Host* create_host(const std::string& name, const std::string& speed);
125
126   /**
127    * @brief Create a link
128    *
129    * @param name Link name
130    * @param bandwidths Link's speed (vector for wifi links)
131    * @throw std::invalid_argument if bandwidth format is incorrect.
132    */
133   s4u::Link* create_link(const std::string& name, const std::vector<double>& bandwidths);
134   s4u::Link* create_link(const std::string& name, double bandwidth);
135
136   /** @brief Create a link (string version) */
137   s4u::Link* create_link(const std::string& name, const std::vector<std::string>& bandwidths);
138   s4u::Link* create_link(const std::string& name, const std::string& bandwidth);
139
140   /**
141    * @brief Create a split-duplex link
142    *
143    * In SimGrid, split-duplex links are a composition of 2 regular (shared) links (up/down).
144    *
145    * This function eases its utilization by creating the 2 links for you. We append a suffix
146    * "_UP" and "_DOWN" to your link name to identify each of them.
147    *
148    * Both up/down links have exactly the same bandwidth
149    *
150    * @param name Name of the link
151    * @param bandwidth Speed
152    */
153   s4u::SplitDuplexLink* create_split_duplex_link(const std::string& name, const std::string& bandwidth);
154   s4u::SplitDuplexLink* create_split_duplex_link(const std::string& name, double bandwidth);
155
156   kernel::resource::NetworkModel* get_network_model() const;
157
158   /**
159    * @brief Make a router within that NetZone
160    *
161    * @param name Router name
162    */
163   kernel::routing::NetPoint* create_router(const std::string& name);
164
165   /** @brief Seal this netzone configuration */
166   NetZone* seal();
167
168   void
169   set_latency_factor_cb(std::function<double(double size, const s4u::Host* src, const s4u::Host* dst,
170                                              const std::vector<s4u::Link*>& /*links*/,
171                                              const std::unordered_set<s4u::NetZone*>& /*netzones*/)> const& cb) const;
172   void
173   set_bandwidth_factor_cb(std::function<double(double size, const s4u::Host* src, const s4u::Host* dst,
174                                                const std::vector<s4u::Link*>& /*links*/,
175                                                const std::unordered_set<s4u::NetZone*>& /*netzones*/)> const& cb) const;
176 };
177
178 // External constructors so that the types (and the types of their content) remain hidden
179 XBT_PUBLIC NetZone* create_full_zone(const std::string& name);
180 XBT_PUBLIC NetZone* create_star_zone(const std::string& name);
181 XBT_PUBLIC NetZone* create_dijkstra_zone(const std::string& name, bool cache);
182 XBT_PUBLIC NetZone* create_empty_zone(const std::string& name);
183 XBT_PUBLIC NetZone* create_floyd_zone(const std::string& name);
184 XBT_PUBLIC NetZone* create_vivaldi_zone(const std::string& name);
185 XBT_PUBLIC NetZone* create_wifi_zone(const std::string& name);
186
187 // Extra data structure for complex constructors
188
189 /** @brief Aggregates the callbacks used to build clusters netzones (Torus/Dragronfly/Fat-Tree) */
190 struct ClusterCallbacks {
191   /**
192    * @brief Callback used to set the netpoint and gateway located at some leaf of clusters (Torus, FatTree, etc)
193    *
194    * The netpoint can be either a host, router or another netzone.
195    * Gateway must be non-null if netpoint is a netzone
196    *
197    * @param zone: The newly create zone, needed for creating new resources (hosts, links)
198    * @param coord: the coordinates of the element
199    * @param id: Internal identifier of the element
200    * @return pair<NetPoint*, NetPoint*>: returns a pair of netpoint and gateway.
201    */
202   using ClusterNetPointCb = std::pair<kernel::routing::NetPoint*, kernel::routing::NetPoint*>(
203       NetZone* zone, const std::vector<unsigned long>& coord, unsigned long id);
204   /**
205    * @brief Callback used to set the links for some leaf of the cluster (Torus, FatTree, etc)
206    *
207    * The coord parameter depends on the cluster being created:
208    * - Torus: Direct translation of the Torus' dimensions, e.g. (0, 0, 0) for a 3-D Torus
209    * - Fat-Tree: A pair (level in the tree, id), e.g. (0, 0): first leaf and (1,0): first switch at level 1.
210    * - Dragonfly: a tuple (group, chassis, blades/routers, nodes), e.g. (0, 0, 0, 0) for first node in the cluster.
211    * Important: To identify the router inside a "group, chassis, blade", we use MAX_UINT in the last parameter (e.g. 0,
212    * 0, 0, 4294967295).
213    *
214    * @param zone: The newly create zone, needed for creating new resources (hosts, links)
215    * @param coord: the coordinates of the element
216    * @param id: Internal identifier of the element
217    * @return Pointer to the Link
218    */
219   using ClusterLinkCb = Link*(NetZone* zone, const std::vector<unsigned long>& coord, unsigned long id);
220
221   std::function<ClusterNetPointCb> netpoint;
222   std::function<ClusterLinkCb> loopback = {};
223   std::function<ClusterLinkCb> limiter  = {};
224   explicit ClusterCallbacks(const std::function<ClusterNetPointCb>& set_netpoint)
225       : netpoint(set_netpoint){/*nothing to do */};
226   ClusterCallbacks(const std::function<ClusterNetPointCb>& set_netpoint,
227                    const std::function<ClusterLinkCb>& set_loopback, const std::function<ClusterLinkCb>& set_limiter)
228       : netpoint(set_netpoint), loopback(set_loopback), limiter(set_limiter){/*nothing to do */};
229 };
230 /**
231  * @brief Create a torus zone
232  *
233  * Torus clusters are characterized by:
234  * - dimensions, eg. {3,3,3} creates a torus with X = 3 elements, Y = 3 and Z = 3. In total, this cluster have 27
235  * elements
236  * - inter-node communication: (bandwidth, latency, sharing_policy) the elements are connected through regular links
237  * with these characteristics
238  * More details in: <a href="https://simgrid.org/doc/latest/Platform_examples.html?highlight=torus#torus-cluster">Torus
239  * Cluster</a>
240  *
241  * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
242  *
243  * Note that the all elements in a Torus cluster must have (or not) the same elements (loopback and limiter)
244  *
245  * @param name NetZone's name
246  * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
247  * the netzone
248  * @param dimensions List of positive integers (> 0) which determines the torus' dimensions
249  * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
250  * @param bandwidth Characteristics of the inter-nodes link
251  * @param latency Characteristics of the inter-nodes link
252  * @param sharing_policy Characteristics of the inter-nodes link
253  * @return Pointer to new netzone
254  */
255 XBT_PUBLIC NetZone* create_torus_zone(const std::string& name, const NetZone* parent,
256                                       const std::vector<unsigned long>& dimensions,
257                                       const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
258                                       Link::SharingPolicy sharing_policy);
259
260 /** @brief Aggregates the parameters necessary to build a Fat-tree zone */
261 struct XBT_PUBLIC FatTreeParams {
262   unsigned int levels;
263   std::vector<unsigned int> down;
264   std::vector<unsigned int> up;
265   std::vector<unsigned int> number;
266   FatTreeParams(unsigned int n_levels, const std::vector<unsigned int>& down_links,
267                 const std::vector<unsigned int>& up_links, const std::vector<unsigned int>& links_number);
268 };
269 /**
270  * @brief Create a Fat-Tree zone
271  *
272  * Fat-Tree clusters are characterized by:
273  * - levels: number of levels in the cluster, e.g. 2 (the final tree will have n+1 levels)
274  * - downlinks: for each level, how many connections between elements below them, e.g. 2, 3: level 1 nodes are connected
275  * to 2 nodes in level 2, which are connected to 3 nodes below. Determines the number total of leaves in the tree.
276  * - uplinks: for each level, how nodes are connected, e.g. 1, 2
277  * - link count: for each level, number of links connecting the nodes, e.g. 1, 1
278  *
279  * The best way to understand it is looking to the doc available in: <a
280  * href="https://simgrid.org/doc/latest/Platform_examples.html#fat-tree-cluster">Fat Tree Cluster</a>
281  *
282  * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
283  *
284  * Note that the all elements in a Fat-Tree cluster must have (or not) the same elements (loopback and limiter)
285  *
286  * @param name NetZone's name
287  * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
288  * the netzone
289  * @param parameters Characteristics of this Fat-Tree
290  * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
291  * @param bandwidth Characteristics of the inter-nodes link
292  * @param latency Characteristics of the inter-nodes link
293  * @param sharing_policy Characteristics of the inter-nodes link
294  * @return Pointer to new netzone
295  */
296 XBT_PUBLIC NetZone* create_fatTree_zone(const std::string& name, const NetZone* parent, const FatTreeParams& parameters,
297                                         const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
298                                         Link::SharingPolicy sharing_policy);
299
300 /** @brief Aggregates the parameters necessary to build a Dragonfly zone */
301 struct XBT_PUBLIC DragonflyParams {
302   std::pair<unsigned int, unsigned int> groups;
303   std::pair<unsigned int, unsigned int> chassis;
304   std::pair<unsigned int, unsigned int> routers;
305   unsigned int nodes;
306   DragonflyParams(const std::pair<unsigned int, unsigned int>& groups,
307                   const std::pair<unsigned int, unsigned int>& chassis,
308                   const std::pair<unsigned int, unsigned int>& routers, unsigned int nodes);
309 };
310 /**
311  * @brief Create a Dragonfly zone
312  *
313  * Dragonfly clusters are characterized by:
314  * - groups: number of groups and links between each group, e.g. 2,2.
315  * - chassis: number of chassis in each group and the number of links used to connect the chassis, e.g. 2,3
316  * - routers: number of routers in each chassis and their links, e.g. 3,1
317  * - nodes: number of nodes connected to each router using a single link, e.g. 2
318  *
319  * In total, the cluster will have groups * chassis * routers * nodes elements/leaves.
320  *
321  * The best way to understand it is looking to the doc available in: <a
322  * href="https://simgrid.org/doc/latest/Platform_examples.html#dragonfly-cluster">Dragonfly Cluster</a>
323  *
324  * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
325  *
326  * Note that the all elements in a Dragonfly cluster must have (or not) the same elements (loopback and limiter)
327  *
328  * @param name NetZone's name
329  * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
330  * the netzone
331  * @param parameters Characteristics of this Dragonfly
332  * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
333  * @param bandwidth Characteristics of the inter-nodes link
334  * @param latency Characteristics of the inter-nodes link
335  * @param sharing_policy Characteristics of the inter-nodes link
336  * @return Pointer to new netzone
337  */
338 XBT_PUBLIC NetZone* create_dragonfly_zone(const std::string& name, const NetZone* parent,
339                                           const DragonflyParams& parameters, const ClusterCallbacks& set_callbacks,
340                                           double bandwidth, double latency, Link::SharingPolicy sharing_policy);
341
342 } // namespace simgrid::s4u
343
344 #endif /* SIMGRID_S4U_NETZONE_HPP */