1 /* Copyright (c) 2016-2021. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_S4U_NETZONE_HPP
7 #define SIMGRID_S4U_NETZONE_HPP
9 #include <simgrid/forward.h>
10 #include <simgrid/s4u/Link.hpp>
11 #include <xbt/graph.h>
12 #include <xbt/signal.hpp>
16 #include <unordered_map>
23 /** @brief Networking Zones
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
29 class XBT_PUBLIC NetZone {
30 kernel::routing::NetZoneImpl* const pimpl_;
33 friend kernel::routing::NetZoneImpl;
35 explicit NetZone(kernel::routing::NetZoneImpl* impl) : pimpl_(impl) {}
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;
43 XBT_ATTRIB_DEPRECATED_v331("Please use get_parent()") NetZone* get_father();
44 NetZone* get_parent() const;
45 NetZone* set_parent(const NetZone* parent);
47 std::vector<Host*> get_all_hosts() const;
48 int get_host_count() const;
50 kernel::routing::NetZoneImpl* get_impl() const { return pimpl_; }
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);
58 std::vector<NetZone*> get_children() const;
59 XBT_ATTRIB_DEPRECATED_v332("Please use set_parent() to manage NetZone's relationship") NetZone* add_child(
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);
65 /* Add content to the netzone, at parsing time. It should be sealed afterward. */
66 int add_component(kernel::routing::NetPoint* elm); /* A host, a router or a netzone, whatever */
69 * @brief Add a route between 2 netpoints
72 * - route between 2 hosts/routers in same netzone, no gateway is needed
73 * - route between 2 netzones, connecting 2 gateways.
75 * @param src Source netzone's netpoint
76 * @param dst Destination netzone' netpoint
77 * @param src_gw Netpoint of the gateway in the source netzone
78 * @param dst_gw Netpoint of the gateway in the destination netzone
79 * @param link_list List of links used in this communication
80 * @param symmetrical Bi-directional communication
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<Link*>& link_list, bool symmetrical = true);
85 XBT_ATTRIB_DEPRECATED_v332("Please use add_route() method which uses s4u::Link instead of LinkImpl") void add_route(
86 kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst, kernel::routing::NetPoint* gw_src,
87 kernel::routing::NetPoint* gw_dst, const std::vector<kernel::resource::LinkImpl*>& link_list, bool symmetrical);
88 void add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
89 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
90 std::vector<kernel::resource::LinkImpl*>& link_list, bool symmetrical);
92 /*** Called on each newly created regular route (not on bypass routes) */
93 static xbt::signal<void(bool symmetrical, kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
94 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
95 std::vector<kernel::resource::LinkImpl*> const& link_list)>
97 static xbt::signal<void(NetZone const&)> on_creation;
98 static xbt::signal<void(NetZone const&)> on_seal;
101 * @brief Create a host
103 * @param name Host name
104 * @param speed_per_state Vector of CPU's speeds
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);
109 * @brief Create a Host (string version)
111 * @throw std::invalid_argument if speed format is incorrect.
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);
117 * @brief Create a link
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.
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);
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);
131 * @brief Make a router within that NetZone
133 * @param name Router name
135 kernel::routing::NetPoint* create_router(const std::string& name);
137 /** @brief Seal this netzone configuration */
141 /** @brief Auxiliary function to get list of LinkImpl */
142 static std::vector<kernel::resource::LinkImpl*> get_link_list_impl(const std::vector<Link*> link_list);
145 // External constructors so that the types (and the types of their content) remain hidden
146 XBT_PUBLIC NetZone* create_full_zone(const std::string& name);
147 XBT_PUBLIC NetZone* create_cluster_zone(const std::string& name);
148 XBT_PUBLIC NetZone* create_star_zone(const std::string& name);
149 XBT_PUBLIC NetZone* create_dijkstra_zone(const std::string& name, bool cache);
150 XBT_PUBLIC NetZone* create_dragonfly_zone(const std::string& name);
151 XBT_PUBLIC NetZone* create_empty_zone(const std::string& name);
152 XBT_PUBLIC NetZone* create_fatTree_zone(const std::string& name);
153 XBT_PUBLIC NetZone* create_floyd_zone(const std::string& name);
155 * @brief Callback used to set the netpoint and gateway located at some leaf of the torus
157 * The netpoint can be either a host, router or another netzone.
158 * Gateway must be non-null if netpoint is a netzone
160 * @param zone: The newly create Torus zone, needed for creating new resources (hosts, links)
161 * @param coord: the coordinates of the element in the torus, eg. 0,1,1
162 * @param id: Internal identifier of the element
163 * @return pair<NetPoint*, NetPoint*>: returns a pair of netpoint and gateway.
165 typedef std::pair<kernel::routing::NetPoint*, kernel::routing::NetPoint*>
166 TorusNetPointCb(NetZone* zone, const std::vector<unsigned int>& coord, int id);
168 * @brief Callback used to set the links for some leaf of the torus
170 * @param zone: The newly create Torus zone, needed for creating new resources (hosts, links)
171 * @param coord: the coordinates of the element in the torus, eg. 0,1,1
172 * @param id: Internal identifier of the element
173 * @return Pointer to the Link
175 typedef Link* TorusLinkCb(NetZone* zone, const std::vector<unsigned int>& coord, int id);
177 * @brief Create a torus zone
179 * Torus clusters are characterized by:
180 * - dimensions, eg. {3,3,3} creates a torus with X = 3 elements, Y = 3 and Z = 3. In total, this cluster have 27
182 * - inter-node communication: (bandwidth, latency, sharing_policy) the elements are connected through regular links
183 * with these characteristics
185 * Moreover, this method accepts 3 callbacks to populate the cluster: set_netpoint, set_loopback and set_limiter .
187 * Note that the all elements in Torus cluster must have (or not) the same elements (loopback and limiter)
189 * @param name NetZone's name
190 * @param parent Pointer to parent's netzone (nullptr if rootnetzone). Needed to be able to create the resources inside
192 * @param dimensions List of positive integers (> 0) which determines the torus' dimensions
193 * @param bandwidth Characteristics of the inter-nodes link
194 * @param latency Characteristics of the inter-nodes link
195 * @param sharing_policy Characteristics of the inter-nodes link
196 * @param set_netpoint Callback to set the netpoint of an element in the torus
197 * @param set_loopback Callback to set the loopback
198 * @param set_limiter Callback to set the limiter
199 * @return Pointer to new netzone
201 NetZone* create_torus_zone(const std::string& name, const NetZone* parent, const std::vector<unsigned int>& dimensions,
202 double bandwidth, double latency, Link::SharingPolicy sharing_policy,
203 const std::function<TorusNetPointCb>& set_netpoint,
204 const std::function<TorusLinkCb>& set_loopback = {},
205 const std::function<TorusLinkCb>& set_limiter = {});
206 XBT_PUBLIC NetZone* create_vivaldi_zone(const std::string& name);
207 XBT_PUBLIC NetZone* create_wifi_zone(const std::string& name);
210 } // namespace simgrid
212 #endif /* SIMGRID_S4U_NETZONE_HPP */