Logo AND Algorithmique Numérique Distribuée

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