Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Separate NetworkModel from LinkImpl.
[simgrid.git] / src / kernel / resource / NetworkModel.hpp
1 /* Copyright (c) 2004-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_KERNEL_RESOURCE_NETWORKMODEL_HPP
7 #define SIMGRID_KERNEL_RESOURCE_NETWORKMODEL_HPP
8
9 #include "simgrid/kernel/resource/Model.hpp"
10 #include "simgrid/kernel/resource/NetworkModelIntf.hpp"
11 #include "src/kernel/resource/StandardLinkImpl.hpp"
12
13 #include <list>
14
15 namespace simgrid {
16 namespace kernel {
17 namespace resource {
18
19 /*********
20  * Model *
21  *********/
22
23 /** @ingroup SURF_network_interface
24  * @brief SURF network model interface class
25  * @details A model is an object which handles the interactions between its Resources and its Actions
26  */
27 class NetworkModel : public Model, public NetworkModelIntf {
28 public:
29   static config::Flag<double> cfg_tcp_gamma;
30   static config::Flag<bool> cfg_crosstraffic;
31
32   using Model::Model;
33   NetworkModel(const NetworkModel&) = delete;
34   NetworkModel& operator=(const NetworkModel&) = delete;
35   ~NetworkModel() override;
36
37   /**
38    * @brief Create a [WiFi]Link
39    *
40    * @param name The name of the Link
41    * @param bandwidths The vector of bandwidths of the Link in bytes per second
42    */
43   virtual StandardLinkImpl* create_link(const std::string& name, const std::vector<double>& bandwidths) = 0;
44
45   virtual StandardLinkImpl* create_wifi_link(const std::string& name, const std::vector<double>& bandwidths) = 0;
46
47   /**
48    * @brief Create a communication between two hosts.
49    * @details It makes calls to the routing part, and execute the communication between the two end points.
50    *
51    * @param src The source of the communication
52    * @param dst The destination of the communication
53    * @param size The size of the communication in bytes
54    * @param rate Allows to limit the transfer rate. Negative value means unlimited.
55    * @return The action representing the communication
56    */
57   virtual Action* communicate(s4u::Host* src, s4u::Host* dst, double size, double rate) = 0;
58
59   /**
60    * @brief Get the right multiplicative factor for the latency.
61    * @details Depending on the model, the effective latency when sending a message might be different from the
62    * theoretical latency of the link, in function of the message size. In order to account for this, this function gets
63    * this factor.
64    *
65    * @param size The size of the message.
66    * @return The latency factor.
67    */
68   virtual double get_latency_factor(double /* size */) { return sg_latency_factor; }
69
70   /**
71    * @brief Get the right multiplicative factor for the bandwidth.
72    * @details Depending on the model, the effective bandwidth when sending a message might be different from the
73    * theoretical bandwidth of the link, in function of the message size. In order to account for this, this function
74    * gets this factor.
75    *
76    * @param size The size of the message.
77    * @return The bandwidth factor.
78    */
79   virtual double get_bandwidth_factor(double /* size*/) { return sg_bandwidth_factor; }
80
81   double next_occurring_event_full(double now) override;
82
83   void set_lat_factor_cb(const std::function<NetworkFactorCb>& cb) override { THROW_UNIMPLEMENTED; }
84   void set_bw_factor_cb(const std::function<NetworkFactorCb>& cb) override { THROW_UNIMPLEMENTED; }
85
86   std::unique_ptr<StandardLinkImpl, StandardLinkImpl::Deleter> loopback_;
87 };
88
89 /**********
90  * Action *
91  **********/
92 /** @ingroup SURF_network_interface
93  * @brief SURF network action interface class
94  * @details A NetworkAction represents a communication between two [hosts](@ref HostImpl)
95  */
96 class NetworkAction : public Action {
97   s4u::Host& src_;
98   s4u::Host& dst_;
99
100 public:
101   /** @brief Constructor
102    *
103    * @param model The NetworkModel associated to this NetworkAction
104    * @param cost The cost of this  NetworkAction in [TODO]
105    * @param failed [description]
106    */
107   NetworkAction(Model* model, s4u::Host& src, s4u::Host& dst, double cost, bool failed)
108       : Action(model, cost, failed), src_(src), dst_(dst)
109   {
110   }
111
112   /**
113    * @brief NetworkAction constructor
114    *
115    * @param model The NetworkModel associated to this NetworkAction
116    * @param cost The cost of this  NetworkAction in bytes
117    * @param failed Actions can be created in a failed state
118    * @param var The lmm variable associated to this Action if it is part of a LMM component
119    */
120   NetworkAction(Model* model, s4u::Host& src, s4u::Host& dst, double cost, bool failed, lmm::Variable* var)
121       : Action(model, cost, failed, var), src_(src), dst_(dst){};
122
123   void set_state(Action::State state) override;
124   virtual std::list<StandardLinkImpl*> get_links() const;
125
126   double latency_         = 0.; // Delay before the action starts
127   double lat_current_     = 0.; // Used to compute the communication RTT, and accordingly limit the communication rate
128   double sharing_penalty_ = {};
129
130   s4u::Host& get_src() const { return src_; }
131   s4u::Host& get_dst() const { return dst_; }
132 };
133
134 /* Insert link(s) at the end of vector `result' (at the beginning, and reversed, for insert_link_latency()), and add
135  * link->get_latency() to *latency when latency is not null
136  */
137 void add_link_latency(std::vector<StandardLinkImpl*>& result, StandardLinkImpl* link, double* latency);
138 void add_link_latency(std::vector<StandardLinkImpl*>& result, const std::vector<StandardLinkImpl*>& links,
139                       double* latency);
140 void insert_link_latency(std::vector<StandardLinkImpl*>& result, const std::vector<StandardLinkImpl*>& links,
141                          double* latency);
142
143 } // namespace resource
144 } // namespace kernel
145 } // namespace simgrid
146
147 #endif /* SIMGRID_KERNEL_RESOURCE_NETWORKMODEL_HPP */