Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / include / simgrid / kernel / routing / NetZoneImpl.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_ROUTING_NETZONEIMPL_HPP
7 #define SIMGRID_ROUTING_NETZONEIMPL_HPP
8
9 #include <simgrid/forward.h>
10 #include <simgrid/s4u/Link.hpp>
11 #include <simgrid/s4u/NetZone.hpp>
12 #include <xbt/PropertyHolder.hpp>
13 #include <xbt/graph.h>
14
15 #include <map>
16 #include <vector>
17
18 namespace simgrid {
19 namespace kernel {
20 namespace routing {
21
22 class BypassRoute {
23 public:
24   explicit BypassRoute(NetPoint* gwSrc, NetPoint* gwDst) : gw_src(gwSrc), gw_dst(gwDst) {}
25   NetPoint* gw_src;
26   NetPoint* gw_dst;
27   std::vector<resource::LinkImpl*> links;
28 };
29
30 /** @ingroup ROUTING_API
31  *  @brief Private implementation of the Networking Zones
32  *
33  * A netzone is a network container, in charge of routing information between elements (hosts and sub-netzones)
34  * and to the nearby netzones. In SimGrid, there is a hierarchy of netzones, ie a tree with a unique root
35  * NetZone, that you can retrieve with simgrid::s4u::Engine::netRoot().
36  *
37  * The purpose of the kernel::routing module is to retrieve the routing path between two points in a time- and
38  * space-efficient manner. This is done by NetZoneImpl::getGlobalRoute(), called when creating a communication to
39  * retrieve both the list of links that the create communication will use, and the summed latency that these
40  * links represent.
41  *
42  * The network model could recompute the latency by itself from the list, but it would require an additional
43  * traversal of the link set. This operation being on the critical path of SimGrid, the routing computes the
44  * latency on the behalf of the network while constructing the link set.
45  *
46  * Finding the path between two nodes is rather complex because we navigate a hierarchy of netzones, each of them
47  * being a full network. In addition, the routing can declare shortcuts (called bypasses), either within a NetZone
48  * at the route level or directly between NetZones. Also, each NetZone can use a differing routing algorithm, depending
49  * on its class. @ref FullZone have a full matrix giving explicitly the path between any pair of their
50  * contained nodes, while @ref DijkstraZone or @ref FloydZone rely on a shortest path algorithm. @ref VivaldiZone
51  * does not even have any link but only use only coordinate information to compute the latency.
52  *
53  * So NetZoneImpl::getGlobalRoute builds the path recursively asking its specific information to each traversed NetZone
54  * with NetZoneImpl::getLocalRoute, that is redefined in each sub-class.
55  * The algorithm for that is explained in http://hal.inria.fr/hal-00650233/ (but for historical reasons, NetZones are
56  * called Autonomous Systems in this article).
57  *
58  */
59 class XBT_PUBLIC NetZoneImpl : public xbt::PropertyHolder {
60   friend EngineImpl; // it destroys netRoot_
61   s4u::NetZone piface_;
62
63   // our content, as known to our graph routing algorithm (maps vertex_id -> vertex)
64   std::vector<kernel::routing::NetPoint*> vertices_;
65
66   NetZoneImpl* father_ = nullptr;
67   std::vector<NetZoneImpl*> children_; // sub-netzones
68   std::string name_;
69   bool sealed_ = false; // We cannot add more content when sealed
70
71   std::map<std::pair<NetPoint*, NetPoint*>, BypassRoute*> bypass_routes_; // src x dst -> route
72   routing::NetPoint* netpoint_ = nullptr;                                 // Our representative in the father NetZone
73
74 protected:
75   explicit NetZoneImpl(NetZoneImpl* father, const std::string& name, resource::NetworkModel* network_model);
76   NetZoneImpl(const NetZoneImpl&) = delete;
77   NetZoneImpl& operator=(const NetZoneImpl&) = delete;
78   virtual ~NetZoneImpl();
79
80   /**
81    * @brief Probe the routing path between two points that are local to the called NetZone.
82    *
83    * @param src where from
84    * @param dst where to
85    * @param into Container into which the traversed links and gateway information should be pushed
86    * @param latency Accumulator in which the latencies should be added (caller must set it to 0)
87    */
88   virtual void get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* into, double* latency) = 0;
89   /** @brief retrieves the list of all routes of size 1 (of type src x dst x Link) */
90   /* returns whether we found a bypass path */
91   bool get_bypass_route(routing::NetPoint* src, routing::NetPoint* dst,
92                         /* OUT */ std::vector<resource::LinkImpl*>& links, double* latency);
93
94 public:
95   enum class RoutingMode {
96     unset = 0, /**< Undefined type                                   */
97     base,      /**< Base case: use simple link lists for routing     */
98     recursive  /**< Recursive case: also return gateway information  */
99   };
100
101   /* FIXME: protect the following fields once the construction madness is sorted out */
102   RoutingMode hierarchy_ = RoutingMode::unset;
103
104   resource::NetworkModel* network_model_;
105
106   const s4u::NetZone* get_iface() const { return &piface_; }
107   s4u::NetZone* get_iface() { return &piface_; }
108   unsigned int get_table_size() const { return vertices_.size(); }
109   std::vector<kernel::routing::NetPoint*> get_vertices() const { return vertices_; }
110   NetZoneImpl* get_father() const { return father_; }
111   /** @brief Returns the list of direct children (no grand-children). This returns the internal data, no copy.
112    * Don't mess with it.*/
113   std::vector<NetZoneImpl*>* get_children() { return &children_; }
114   /** @brief Retrieves the name of that netzone as a C++ string */
115   const std::string& get_name() const { return name_; }
116   /** @brief Retrieves the name of that netzone as a C string */
117   const char* get_cname() const { return name_.c_str(); };
118
119   std::vector<s4u::Host*> get_all_hosts() const;
120   int get_host_count() const;
121
122   /** @brief Make a host within that NetZone */
123   s4u::Host* create_host(const std::string& name, const std::vector<double>& speed_per_pstate, int core_count);
124   /** @brief Make a link within that NetZone */
125   virtual s4u::Link* create_link(const std::string& name, const std::vector<double>& bandwidths, double latency,
126                                  s4u::Link::SharingPolicy policy);
127   /** @brief Creates a new route in this NetZone */
128   virtual void add_bypass_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
129                                 std::vector<resource::LinkImpl*>& link_list, bool symmetrical);
130
131   /** @brief Seal your netzone once you're done adding content, and before routing stuff through it */
132   virtual void seal() { sealed_ = true; };
133   virtual int add_component(kernel::routing::NetPoint* elm); /* A host, a router or a netzone, whatever */
134   virtual void add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
135                          kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
136                          std::vector<kernel::resource::LinkImpl*>& link_list, bool symmetrical);
137
138   /* @brief get the route between two nodes in the full platform
139    *
140    * @param src where from
141    * @param dst where to
142    * @param links Accumulator in which all traversed links should be pushed (caller must empty it)
143    * @param latency Accumulator in which the latencies should be added (caller must set it to 0)
144    */
145   static void get_global_route(routing::NetPoint* src, routing::NetPoint* dst,
146                                /* OUT */ std::vector<resource::LinkImpl*>& links, double* latency);
147
148   virtual void get_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
149                          std::map<std::string, xbt_edge_t, std::less<>>* edges) = 0;
150 };
151 } // namespace routing
152 } // namespace kernel
153 } // namespace simgrid
154
155 #endif /* SIMGRID_ROUTING_NETZONEIMPL_HPP */