Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename NetworkModelIntf into NetworkModelFactors
[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 set_latency_factor_cb(std::function<double(double size, const s4u::Host* src, const s4u::Host* dst,
159                                                   const std::vector<s4u::Link*>& /*links*/,
160                                                   const std::unordered_set<s4u::NetZone*>& /*netzones*/)> const& cb);
161   void set_bandwidth_factor_cb(std::function<double(double size, const s4u::Host* src, const s4u::Host* dst,
162                                                     const std::vector<s4u::Link*>& /*links*/,
163                                                     const std::unordered_set<s4u::NetZone*>& /*netzones*/)> const& cb);
164 };
165
166 // External constructors so that the types (and the types of their content) remain hidden
167 XBT_PUBLIC NetZone* create_full_zone(const std::string& name);
168 XBT_PUBLIC NetZone* create_star_zone(const std::string& name);
169 XBT_PUBLIC NetZone* create_dijkstra_zone(const std::string& name, bool cache);
170 XBT_PUBLIC NetZone* create_empty_zone(const std::string& name);
171 XBT_PUBLIC NetZone* create_floyd_zone(const std::string& name);
172 XBT_PUBLIC NetZone* create_vivaldi_zone(const std::string& name);
173 XBT_PUBLIC NetZone* create_wifi_zone(const std::string& name);
174
175 // Extra data structure for complex constructors
176
177 /** @brief Aggregates the callbacks used to build clusters netzones (Torus/Dragronfly/Fat-Tree) */
178 struct ClusterCallbacks {
179   /**
180    * @brief Callback used to set the netpoint and gateway located at some leaf of clusters (Torus, FatTree, etc)
181    *
182    * The netpoint can be either a host, router or another netzone.
183    * Gateway must be non-null if netpoint is a netzone
184    *
185    * @param zone: The newly create zone, needed for creating new resources (hosts, links)
186    * @param coord: the coordinates of the element
187    * @param id: Internal identifier of the element
188    * @return pair<NetPoint*, NetPoint*>: returns a pair of netpoint and gateway.
189    */
190   using ClusterNetPointCb = std::pair<kernel::routing::NetPoint*, kernel::routing::NetPoint*>(
191       NetZone* zone, const std::vector<unsigned long>& coord, unsigned long id);
192   /**
193    * @brief Callback used to set the links for some leaf of the cluster (Torus, FatTree, etc)
194    *
195    * The coord parameter depends on the cluster being created:
196    * - Torus: Direct translation of the Torus' dimensions, e.g. (0, 0, 0) for a 3-D Torus
197    * - Fat-Tree: A pair (level in the tree, id), e.g. (0, 0): first leaf and (1,0): first switch at level 1.
198    * - Dragonfly: a tuple (group, chassis, blades/routers, nodes), e.g. (0, 0, 0, 0) for first node in the cluster.
199    * Important: To identify the router inside a "group, chassis, blade", we use MAX_UINT in the last parameter (e.g. 0,
200    * 0, 0, 4294967295).
201    *
202    * @param zone: The newly create zone, needed for creating new resources (hosts, links)
203    * @param coord: the coordinates of the element
204    * @param id: Internal identifier of the element
205    * @return Pointer to the Link
206    */
207   using ClusterLinkCb = Link*(NetZone* zone, const std::vector<unsigned long>& coord, unsigned long id);
208
209   std::function<ClusterNetPointCb> netpoint;
210   std::function<ClusterLinkCb> loopback = {};
211   std::function<ClusterLinkCb> limiter  = {};
212   explicit ClusterCallbacks(const std::function<ClusterNetPointCb>& set_netpoint)
213       : netpoint(set_netpoint){/*nothing to do */};
214   ClusterCallbacks(const std::function<ClusterNetPointCb>& set_netpoint,
215                    const std::function<ClusterLinkCb>& set_loopback, const std::function<ClusterLinkCb>& set_limiter)
216       : netpoint(set_netpoint), loopback(set_loopback), limiter(set_limiter){/*nothing to do */};
217 };
218 /**
219  * @brief Create a torus zone
220  *
221  * Torus clusters are characterized by:
222  * - dimensions, eg. {3,3,3} creates a torus with X = 3 elements, Y = 3 and Z = 3. In total, this cluster have 27
223  * elements
224  * - inter-node communication: (bandwidth, latency, sharing_policy) the elements are connected through regular links
225  * with these characteristics
226  * More details in: <a href="https://simgrid.org/doc/latest/Platform_examples.html?highlight=torus#torus-cluster">Torus
227  * Cluster</a>
228  *
229  * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
230  *
231  * Note that the all elements in a Torus cluster must have (or not) the same elements (loopback and limiter)
232  *
233  * @param name NetZone's name
234  * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
235  * the netzone
236  * @param dimensions List of positive integers (> 0) which determines the torus' dimensions
237  * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
238  * @param bandwidth Characteristics of the inter-nodes link
239  * @param latency Characteristics of the inter-nodes link
240  * @param sharing_policy Characteristics of the inter-nodes link
241  * @return Pointer to new netzone
242  */
243 XBT_PUBLIC NetZone* create_torus_zone(const std::string& name, const NetZone* parent,
244                                       const std::vector<unsigned long>& dimensions,
245                                       const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
246                                       Link::SharingPolicy sharing_policy);
247
248 /** @brief Aggregates the parameters necessary to build a Fat-tree zone */
249 struct XBT_PUBLIC FatTreeParams {
250   unsigned int levels;
251   std::vector<unsigned int> down;
252   std::vector<unsigned int> up;
253   std::vector<unsigned int> number;
254   FatTreeParams(unsigned int n_levels, const std::vector<unsigned int>& down_links,
255                 const std::vector<unsigned int>& up_links, const std::vector<unsigned int>& links_number);
256 };
257 /**
258  * @brief Create a Fat-Tree zone
259  *
260  * Fat-Tree clusters are characterized by:
261  * - levels: number of levels in the cluster, e.g. 2 (the final tree will have n+1 levels)
262  * - downlinks: for each level, how many connections between elements below them, e.g. 2, 3: level 1 nodes are connected
263  * to 2 nodes in level 2, which are connected to 3 nodes below. Determines the number total of leaves in the tree.
264  * - uplinks: for each level, how nodes are connected, e.g. 1, 2
265  * - link count: for each level, number of links connecting the nodes, e.g. 1, 1
266  *
267  * The best way to understand it is looking to the doc available in: <a
268  * href="https://simgrid.org/doc/latest/Platform_examples.html#fat-tree-cluster">Fat Tree Cluster</a>
269  *
270  * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
271  *
272  * Note that the all elements in a Fat-Tree cluster must have (or not) the same elements (loopback and limiter)
273  *
274  * @param name NetZone's name
275  * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
276  * the netzone
277  * @param parameters Characteristics of this Fat-Tree
278  * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
279  * @param bandwidth Characteristics of the inter-nodes link
280  * @param latency Characteristics of the inter-nodes link
281  * @param sharing_policy Characteristics of the inter-nodes link
282  * @return Pointer to new netzone
283  */
284 XBT_PUBLIC NetZone* create_fatTree_zone(const std::string& name, const NetZone* parent, const FatTreeParams& parameters,
285                                         const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
286                                         Link::SharingPolicy sharing_policy);
287
288 /** @brief Aggregates the parameters necessary to build a Dragonfly zone */
289 struct XBT_PUBLIC DragonflyParams {
290   std::pair<unsigned int, unsigned int> groups;
291   std::pair<unsigned int, unsigned int> chassis;
292   std::pair<unsigned int, unsigned int> routers;
293   unsigned int nodes;
294   DragonflyParams(const std::pair<unsigned int, unsigned int>& groups,
295                   const std::pair<unsigned int, unsigned int>& chassis,
296                   const std::pair<unsigned int, unsigned int>& routers, unsigned int nodes);
297 };
298 /**
299  * @brief Create a Dragonfly zone
300  *
301  * Dragonfly clusters are characterized by:
302  * - groups: number of groups and links between each group, e.g. 2,2.
303  * - chassis: number of chassis in each group and the number of links used to connect the chassis, e.g. 2,3
304  * - routers: number of routers in each chassis and their links, e.g. 3,1
305  * - nodes: number of nodes connected to each router using a single link, e.g. 2
306  *
307  * In total, the cluster will have groups * chassis * routers * nodes elements/leaves.
308  *
309  * The best way to understand it is looking to the doc available in: <a
310  * href="https://simgrid.org/doc/latest/Platform_examples.html#dragonfly-cluster">Dragonfly Cluster</a>
311  *
312  * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
313  *
314  * Note that the all elements in a Dragonfly cluster must have (or not) the same elements (loopback and limiter)
315  *
316  * @param name NetZone's name
317  * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
318  * the netzone
319  * @param parameters Characteristics of this Dragonfly
320  * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
321  * @param bandwidth Characteristics of the inter-nodes link
322  * @param latency Characteristics of the inter-nodes link
323  * @param sharing_policy Characteristics of the inter-nodes link
324  * @return Pointer to new netzone
325  */
326 XBT_PUBLIC NetZone* create_dragonfly_zone(const std::string& name, const NetZone* parent,
327                                           const DragonflyParams& parameters, const ClusterCallbacks& set_callbacks,
328                                           double bandwidth, double latency, Link::SharingPolicy sharing_policy);
329
330 } // namespace s4u
331 } // namespace simgrid
332
333 #endif /* SIMGRID_S4U_NETZONE_HPP */