Logo AND Algorithmique Numérique Distribuée

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