Logo AND Algorithmique Numérique Distribuée

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