Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[cppcheck] Pass parameters by const reference.
[simgrid.git] / include / simgrid / s4u / NetZone.hpp
1 /* Copyright (c) 2016-2021. 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   kernel::routing::NetZoneImpl* const pimpl_;
31
32 protected:
33   friend kernel::routing::NetZoneImpl;
34
35   explicit NetZone(kernel::routing::NetZoneImpl* impl) : pimpl_(impl) {}
36
37 public:
38   /** @brief Retrieves the name of that netzone as a C++ string */
39   const std::string& get_name() const;
40   /** @brief Retrieves the name of that netzone as a C string */
41   const char* get_cname() const;
42
43   XBT_ATTRIB_DEPRECATED_v331("Please use get_parent()") NetZone* get_father();
44   NetZone* get_parent() const;
45   NetZone* set_parent(const NetZone* parent);
46
47   std::vector<Host*> get_all_hosts() const;
48   int get_host_count() const;
49
50   kernel::routing::NetZoneImpl* get_impl() const { return pimpl_; }
51
52   /** Get the properties assigned to a netzone */
53   const std::unordered_map<std::string, std::string>* get_properties() const;
54   /** Retrieve the property value (or nullptr if not set) */
55   const char* get_property(const std::string& key) const;
56   void set_property(const std::string& key, const std::string& value);
57   /** @brief Get the netpoint associated to this netzone */
58   kernel::routing::NetPoint* get_netpoint();
59
60   std::vector<NetZone*> get_children() const;
61   XBT_ATTRIB_DEPRECATED_v332("Please use set_parent() to manage NetZone's relationship") NetZone* add_child(
62       NetZone* new_zone);
63
64   void extract_xbt_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
65                          std::map<std::string, xbt_edge_t, std::less<>>* edges);
66
67   /* Add content to the netzone, at parsing time. It should be sealed afterward. */
68   int add_component(kernel::routing::NetPoint* elm); /* A host, a router or a netzone, whatever */
69
70   /**
71    * @brief Add a route between 2 netpoints
72    *
73    * Create a route:
74    * - route between 2 hosts/routers in same netzone, no gateway is needed
75    * - route between 2 netzones, connecting 2 gateways.
76    *
77    * @param src Source netzone's netpoint
78    * @param dst Destination netzone' netpoint
79    * @param src_gw Netpoint of the gateway in the source netzone
80    * @param dst_gw Netpoint of the gateway in the destination netzone
81    * @param link_list List of links used in this communication
82    * @param symmetrical Bi-directional communication
83    */
84   void add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst, kernel::routing::NetPoint* gw_src,
85                  kernel::routing::NetPoint* gw_dst, const std::vector<Link*>& link_list, bool symmetrical = true);
86
87   XBT_ATTRIB_DEPRECATED_v332("Please use add_route() method which uses s4u::Link instead of LinkImpl") void add_route(
88       kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst, kernel::routing::NetPoint* gw_src,
89       kernel::routing::NetPoint* gw_dst, const std::vector<kernel::resource::LinkImpl*>& link_list, bool symmetrical);
90   void add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
91                         kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
92                         std::vector<kernel::resource::LinkImpl*>& link_list, bool symmetrical);
93
94   /*** Called on each newly created regular route (not on bypass routes) */
95   static xbt::signal<void(bool symmetrical, kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
96                           kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
97                           std::vector<kernel::resource::LinkImpl*> const& link_list)>
98       on_route_creation;
99   static xbt::signal<void(NetZone const&)> on_creation;
100   static xbt::signal<void(NetZone const&)> on_seal;
101
102   /**
103    * @brief Create a host
104    *
105    * @param name Host name
106    * @param speed_per_state Vector of CPU's speeds
107    */
108   s4u::Host* create_host(const std::string& name, const std::vector<double>& speed_per_pstate);
109   s4u::Host* create_host(const std::string& name, double speed);
110   /**
111    * @brief Create a Host (string version)
112    *
113    * @throw std::invalid_argument if speed format is incorrect.
114    */
115   s4u::Host* create_host(const std::string& name, const std::vector<std::string>& speed_per_pstate);
116   s4u::Host* create_host(const std::string& name, const std::string& speed);
117
118   /**
119    * @brief Create a link
120    *
121    * @param name Link name
122    * @param bandwidths Link's speed (vector for wifi links)
123    * @throw std::invalid_argument if bandwidth format is incorrect.
124    */
125   s4u::Link* create_link(const std::string& name, const std::vector<double>& bandwidths);
126   s4u::Link* create_link(const std::string& name, double bandwidth);
127
128   /** @brief Create a link (string version) */
129   s4u::Link* create_link(const std::string& name, const std::vector<std::string>& bandwidths);
130   s4u::Link* create_link(const std::string& name, const std::string& bandwidth);
131
132   /**
133    * @brief Make a router within that NetZone
134    *
135    * @param name Router name
136    */
137   kernel::routing::NetPoint* create_router(const std::string& name);
138
139   /** @brief Seal this netzone configuration */
140   NetZone* seal();
141
142 private:
143   /** @brief Auxiliary function to get list of LinkImpl */
144   static std::vector<kernel::resource::LinkImpl*> get_link_list_impl(const std::vector<Link*>& link_list);
145 };
146
147 // External constructors so that the types (and the types of their content) remain hidden
148 XBT_PUBLIC NetZone* create_full_zone(const std::string& name);
149 XBT_PUBLIC NetZone* create_star_zone(const std::string& name);
150 XBT_PUBLIC NetZone* create_dijkstra_zone(const std::string& name, bool cache);
151 XBT_PUBLIC NetZone* create_empty_zone(const std::string& name);
152 XBT_PUBLIC NetZone* create_floyd_zone(const std::string& name);
153 XBT_PUBLIC NetZone* create_vivaldi_zone(const std::string& name);
154 XBT_PUBLIC NetZone* create_wifi_zone(const std::string& name);
155
156 // Extra data structure for complex constructors
157
158 /** @brief Aggregates the callbacks used to build clusters netzones (Torus/Dragronfly/Fat-Tree) */
159 struct ClusterCallbacks {
160   /**
161    * @brief Callback used to set the netpoint and gateway located at some leaf of clusters (Torus, FatTree, etc)
162    *
163    * The netpoint can be either a host, router or another netzone.
164    * Gateway must be non-null if netpoint is a netzone
165    *
166    * @param zone: The newly create zone, needed for creating new resources (hosts, links)
167    * @param coord: the coordinates of the element
168    * @param id: Internal identifier of the element
169    * @return pair<NetPoint*, NetPoint*>: returns a pair of netpoint and gateway.
170    */
171   using ClusterNetPointCb = std::pair<kernel::routing::NetPoint*, kernel::routing::NetPoint*>(
172       NetZone* zone, const std::vector<unsigned int>& coord, int id);
173   /**
174    * @brief Callback used to set the links for some leaf of the cluster (Torus, FatTree, etc)
175    *
176    * The coord parameter depends on the cluster being created:
177    * - Torus: Direct translation of the Torus' dimensions, e.g. (0, 0, 0) for a 3-D Torus
178    * - Fat-Tree: A pair (level in the tree, id), e.g. (0, 0): first leaf and (1,0): first switch at level 1.
179    * - Dragonfly: a tuple (group, chassis, blades/routers, nodes), e.g. (0, 0, 0, 0) for first node in the cluster.
180    * Important: To identify the router inside a "group, chassis, blade", we use MAX_UINT in the last parameter (e.g. 0,
181    * 0, 0, 4294967295).
182    *
183    * @param zone: The newly create zone, needed for creating new resources (hosts, links)
184    * @param coord: the coordinates of the element
185    * @param id: Internal identifier of the element
186    * @return Pointer to the Link
187    */
188   using ClusterLinkCb = Link*(NetZone* zone, const std::vector<unsigned int>& coord, int id);
189
190   std::function<ClusterNetPointCb> netpoint;
191   std::function<ClusterLinkCb> loopback = {};
192   std::function<ClusterLinkCb> limiter  = {};
193   explicit ClusterCallbacks(const std::function<ClusterNetPointCb>& set_netpoint)
194       : netpoint(set_netpoint){/*nothing to do */};
195   ClusterCallbacks(const std::function<ClusterNetPointCb>& set_netpoint,
196                    const std::function<ClusterLinkCb>& set_loopback, const std::function<ClusterLinkCb>& set_limiter)
197       : netpoint(set_netpoint), loopback(set_loopback), limiter(set_limiter){/*nothing to do */};
198 };
199 /**
200  * @brief Create a torus zone
201  *
202  * Torus clusters are characterized by:
203  * - dimensions, eg. {3,3,3} creates a torus with X = 3 elements, Y = 3 and Z = 3. In total, this cluster have 27
204  * elements
205  * - inter-node communication: (bandwidth, latency, sharing_policy) the elements are connected through regular links
206  * with these characteristics
207  * More details in: <a href="https://simgrid.org/doc/latest/Platform_examples.html?highlight=torus#torus-cluster">Torus
208  * Cluster</a>
209  *
210  * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
211  *
212  * Note that the all elements in a Torus cluster must have (or not) the same elements (loopback and limiter)
213  *
214  * @param name NetZone's name
215  * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
216  * the netzone
217  * @param dimensions List of positive integers (> 0) which determines the torus' dimensions
218  * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
219  * @param bandwidth Characteristics of the inter-nodes link
220  * @param latency Characteristics of the inter-nodes link
221  * @param sharing_policy Characteristics of the inter-nodes link
222  * @return Pointer to new netzone
223  */
224 XBT_PUBLIC NetZone* create_torus_zone(const std::string& name, const NetZone* parent,
225                                       const std::vector<unsigned int>& dimensions,
226                                       const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
227                                       Link::SharingPolicy sharing_policy);
228
229 /** @brief Aggregates the parameters necessary to build a Fat-tree zone */
230 struct FatTreeParams {
231   unsigned int levels;
232   std::vector<unsigned int> down;
233   std::vector<unsigned int> up;
234   std::vector<unsigned int> number;
235   FatTreeParams(unsigned int n_levels, const std::vector<unsigned int>& down_links,
236                 const std::vector<unsigned int>& up_links, const std::vector<unsigned int>& links_number);
237 };
238 /**
239  * @brief Create a Fat-Tree zone
240  *
241  * Fat-Tree clusters are characterized by:
242  * - levels: number of levels in the cluster, e.g. 2 (the final tree will have n+1 levels)
243  * - downlinks: for each level, how many connections between elements below them, e.g. 2, 3: level 1 nodes are connected
244  * to 2 nodes in level 2, which are connected to 3 nodes below. Determines the number total of leaves in the tree.
245  * - uplinks: for each level, how nodes are connected, e.g. 1, 2
246  * - link count: for each level, number of links connecting the nodes, e.g. 1, 1
247  *
248  * The best way to understand it is looking to the doc available in: <a
249  * href="https://simgrid.org/doc/latest/Platform_examples.html#fat-tree-cluster">Fat Tree Cluster</a>
250  *
251  * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
252  *
253  * Note that the all elements in a Fat-Tree cluster must have (or not) the same elements (loopback and limiter)
254  *
255  * @param name NetZone's name
256  * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
257  * the netzone
258  * @param parameters Characteristics of this Fat-Tree
259  * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
260  * @param bandwidth Characteristics of the inter-nodes link
261  * @param latency Characteristics of the inter-nodes link
262  * @param sharing_policy Characteristics of the inter-nodes link
263  * @return Pointer to new netzone
264  */
265 XBT_PUBLIC NetZone* create_fatTree_zone(const std::string& name, const NetZone* parent, const FatTreeParams& parameters,
266                                         const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
267                                         Link::SharingPolicy sharing_policy);
268
269 /** @brief Aggregates the parameters necessary to build a Dragonfly zone */
270 struct DragonflyParams {
271   std::pair<unsigned int, unsigned int> groups;
272   std::pair<unsigned int, unsigned int> chassis;
273   std::pair<unsigned int, unsigned int> routers;
274   unsigned int nodes;
275   DragonflyParams(const std::pair<unsigned int, unsigned int>& groups,
276                   const std::pair<unsigned int, unsigned int>& chassis,
277                   const std::pair<unsigned int, unsigned int>& routers, unsigned int nodes);
278 };
279 /**
280  * @brief Create a Dragonfly zone
281  *
282  * Dragonfly clusters are characterized by:
283  * - groups: number of groups and links between each group, e.g. 2,2.
284  * - chassis: number of chassis in each group and the number of links used to connect the chassis, e.g. 2,3
285  * - routers: number of routers in each chassis and their links, e.g. 3,1
286  * - nodes: number of nodes connected to each router using a single link, e.g. 2
287  *
288  * In total, the cluster will have groups * chassis * routers * nodes elements/leaves.
289  *
290  * The best way to understand it is looking to the doc available in: <a
291  * href="https://simgrid.org/doc/latest/Platform_examples.html#dragonfly-cluster">Dragonfly Cluster</a>
292  *
293  * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
294  *
295  * Note that the all elements in a Dragonfly cluster must have (or not) the same elements (loopback and limiter)
296  *
297  * @param name NetZone's name
298  * @param parent Pointer to parent's netzone (nullptr if root netzone). Needed to be able to create the resources inside
299  * the netzone
300  * @param parameters Characteristics of this Dragonfly
301  * @param set_callbacks Callbacks to set properties from cluster elements (netpoint, loopback and limiter)
302  * @param bandwidth Characteristics of the inter-nodes link
303  * @param latency Characteristics of the inter-nodes link
304  * @param sharing_policy Characteristics of the inter-nodes link
305  * @return Pointer to new netzone
306  */
307 XBT_PUBLIC NetZone* create_dragonfly_zone(const std::string& name, const NetZone* parent,
308                                           const DragonflyParams& parameters, const ClusterCallbacks& set_callbacks,
309                                           double bandwidth, double latency, Link::SharingPolicy sharing_policy);
310
311 } // namespace s4u
312 } // namespace simgrid
313
314 #endif /* SIMGRID_S4U_NETZONE_HPP */