Logo AND Algorithmique Numérique Distribuée

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