Logo AND Algorithmique Numérique Distribuée

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