Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
setter function only need a simcall in MC or with parallel execs
[simgrid.git] / src / surf / sio_S22.cpp
1 /* Copyright (c) 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 #include <simgrid/kernel/routing/NetZoneImpl.hpp>
7 #include <simgrid/s4u/Engine.hpp>
8 #include <xbt/config.hpp>
9
10 #include "simgrid/config.h"
11 #include "src/kernel/EngineImpl.hpp"
12 #if SIMGRID_HAVE_EIGEN3
13 #include "src/kernel/lmm/bmf.hpp"
14 #endif
15 #include "src/kernel/resource/profile/Event.hpp"
16 #include "src/surf/sio_S22.hpp"
17
18 #include <unordered_set>
19
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_host);
21 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
22
23 /***********
24  * Options *
25  ***********/
26  static simgrid::config::Flag<std::string> cfg_sio_solver("host/sio_solver",
27                                                            "Set linear equations solver used by sio model",
28                                                            "fairbottleneck",
29                                                            &simgrid::kernel::lmm::System::validate_solver);
30
31 /**************************************/
32 /*** Resource Creation & Destruction **/
33 /**************************************/
34 void surf_host_model_init_sio_S22()
35 {
36   XBT_CINFO(xbt_cfg, "Switching to the S22 model to handle streaming I/Os.");
37   xbt_assert(cfg_sio_solver != "maxmin", "Invalid configuration. Cannot use maxmin solver with streaming I/Os.");
38
39   auto* system    = simgrid::kernel::lmm::System::build(cfg_sio_solver.get(), true /* selective update */);
40   auto host_model = std::make_shared<simgrid::kernel::resource::HostS22Model>("Host_Sio", system);
41   auto* engine    = simgrid::kernel::EngineImpl::get_instance();
42   engine->add_model(host_model);
43   engine->get_netzone_root()->set_host_model(host_model);
44 }
45
46 namespace simgrid::kernel::resource {
47
48 HostS22Model::HostS22Model(const std::string& name, lmm::System* sys) : HostModel(name)
49 {
50   set_maxmin_system(sys);
51
52   auto net_model = std::make_shared<NetworkS22Model>("Network_Sio", this, sys);
53   auto engine    = EngineImpl::get_instance();
54   engine->add_model(net_model);
55   engine->get_netzone_root()->set_network_model(net_model);
56
57   auto disk_model = std::make_shared<DiskS22Model>("Disk_Sio", this, sys);
58   engine->add_model(disk_model);
59   engine->get_netzone_root()->set_disk_model(disk_model);
60
61   surf_cpu_model_init_Cas01();
62 }
63
64 DiskS22Model::DiskS22Model(const std::string& name, HostS22Model* hmodel, lmm::System* sys)
65     : DiskModel(name), hostModel_(hmodel)
66 {
67   set_maxmin_system(sys);
68 }
69
70 DiskS22Model::~DiskS22Model()
71 {
72   set_maxmin_system(nullptr);
73 }
74
75 NetworkS22Model::NetworkS22Model(const std::string& name, HostS22Model* hmodel, lmm::System* sys)
76     : NetworkModel(name), hostModel_(hmodel)
77 {
78   set_maxmin_system(sys);
79   loopback_.reset(create_link("__loopback__", {simgrid::config::get_value<double>("network/loopback-bw")}));
80   loopback_->set_sharing_policy(s4u::Link::SharingPolicy::FATPIPE, {});
81   loopback_->set_latency(simgrid::config::get_value<double>("network/loopback-lat"));
82   loopback_->get_iface()->seal();
83 }
84
85 NetworkS22Model::~NetworkS22Model()
86 {
87   set_maxmin_system(nullptr);
88 }
89
90 double HostS22Model::next_occurring_event(double now)
91 {
92   double min = HostModel::next_occurring_event_full(now);
93   for (Action const& action : *get_started_action_set()) {
94     const auto& net_action = static_cast<const S22Action&>(action);
95     if (net_action.get_latency() > 0 && (min < 0 || net_action.get_latency() < min)) {
96       min = net_action.get_latency();
97       XBT_DEBUG("Updating min with %p (start %f): %f", &net_action, net_action.get_start_time(), min);
98     }
99   }
100   XBT_DEBUG("min value: %f", min);
101
102   return min;
103 }
104
105 void HostS22Model::update_actions_state(double /*now*/, double delta)
106 {
107   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
108     auto& action = static_cast<S22Action&>(*it);
109     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
110     if (action.get_latency() > 0) {
111       if (action.get_latency() > delta) {
112         action.update_latency(delta, sg_surf_precision);
113       } else {
114         action.set_latency(0.0);
115       }
116       if ((action.get_latency() <= 0.0) && (action.is_suspended() == 0)) {
117         action.update_bound();
118         get_maxmin_system()->update_variable_penalty(action.get_variable(), 1.0);
119         action.set_last_update();
120       }
121     }
122     XBT_DEBUG("Action (%p) : remains (%g) updated by %g.", &action, action.get_remains(), action.get_rate() * delta);
123     action.update_remains(action.get_rate() * delta);
124     action.update_max_duration(delta);
125
126     XBT_DEBUG("Action (%p) : remains (%g).", &action, action.get_remains());
127
128     /* In the next if cascade, the action can be finished either because:
129      *  - The amount of remaining work reached 0
130      *  - The max duration was reached
131      * If it's not done, it may have failed.
132      */
133
134     if (((action.get_remains() <= 0) && (action.get_variable()->get_penalty() > 0)) ||
135         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
136       action.finish(Action::State::FINISHED);
137       continue;
138     }
139
140     /* Need to check that none of the model has failed */
141     int i                               = 0;
142     const lmm::Constraint* cnst         = action.get_variable()->get_constraint(i);
143     while (cnst != nullptr) {
144       i++;
145       if (not cnst->get_id()->is_on()) {
146         XBT_DEBUG("Action (%p) Failed!!", &action);
147         action.finish(Action::State::FAILED);
148         break;
149       }
150       cnst = action.get_variable()->get_constraint(i);
151     }
152   }
153 }
154
155 S22Action::S22Action(Model* model, s4u::Host* src_host, DiskImpl* src_disk, s4u::Host* dst_host, DiskImpl* dst_disk, double size)
156     : DiskAction(model, 1, false)
157     , src_host_(src_host)
158     , src_disk_(src_disk)
159     , dst_host_(dst_host)
160     , dst_disk_(dst_disk)
161     , size_(size)
162 {
163   size_t disk_nb       = 0;
164   size_t link_nb       = 0;
165   double latency       = 0.0;
166   this->set_last_update();
167
168   if (src_disk_ != nullptr)
169     disk_nb++;
170   if (dst_disk_ != nullptr)
171     disk_nb++;
172
173   /* there should always be a route between src_host and dst_host (loopback_ for self communication at least) */
174   double lat = 0.0;
175   std::vector<StandardLinkImpl*> route;
176   src_host_->route_to(dst_host_, route, &lat);
177   latency = std::max(latency, lat);
178   link_nb = route.size();
179
180   XBT_DEBUG("Creating a stream io (%p) with %zu disk(s) and %zu unique link(s).", this, disk_nb, link_nb);
181   latency_ = latency;
182
183   set_variable(model->get_maxmin_system()->variable_new(this, 1.0, -1.0, disk_nb + link_nb));
184
185   if (latency_ > 0)
186     model->get_maxmin_system()->update_variable_penalty(get_variable(), 0.0);
187
188   /* Expand it for the disks even if there is nothing to read/write, to make sure that it gets expended even if there is no
189    * communication either */
190   if (src_disk_ != nullptr)
191     model->get_maxmin_system()->expand(src_disk_->get_constraint(), get_variable(), size, true);
192   if (dst_disk_ != nullptr)
193     model->get_maxmin_system()->expand(dst_disk_->get_constraint(), get_variable(), size, true);
194
195   if (link_nb > 0) {
196     std::vector<StandardLinkImpl*> route;
197     src_host_->route_to(dst_host_, route, nullptr);
198     for (auto const& link : route)
199       model->get_maxmin_system()->expand(link->get_constraint(), this->get_variable(), size_);
200   }
201
202   if (link_nb + disk_nb == 0) {
203     this->set_cost(1.0);
204     this->set_remains(0.0);
205   }
206
207   /* finally calculate the initial bound value */
208   update_bound();
209 }
210
211 DiskAction* HostS22Model::io_stream(s4u::Host* src_host, DiskImpl* src_disk, s4u::Host* dst_host, DiskImpl* dst_disk,
212                                    double size)
213 {
214   return new S22Action(this, src_host, src_disk, dst_host, dst_disk, size);
215 }
216
217 Action* NetworkS22Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double /*rate*/)
218 {
219   return hostModel_->io_stream(src, nullptr, dst, nullptr, size);
220 }
221
222 DiskImpl* DiskS22Model::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
223 {
224   return (new DiskS22(name, read_bandwidth, write_bandwidth))->set_model(this);
225 }
226
227 StandardLinkImpl* NetworkS22Model::create_link(const std::string& name, const std::vector<double>& bandwidths)
228 {
229   xbt_assert(bandwidths.size() == 1, "Non WIFI link must have only 1 bandwidth.");
230   auto link = new LinkS22(name, bandwidths[0], get_maxmin_system());
231   link->set_model(this);
232   return link;
233 }
234
235 StandardLinkImpl* NetworkS22Model::create_wifi_link(const std::string& name, const std::vector<double>& bandwidths)
236 {
237   THROW_UNIMPLEMENTED;
238 }
239
240 /************
241  * Resource *
242  ************/
243 DiskAction* DiskS22::io_start(sg_size_t size, s4u::Io::OpType type)
244 {
245   DiskAction* res;
246   switch (type) {
247     case s4u::Io::OpType::READ:
248       res = static_cast<DiskS22Model*>(get_model())->hostModel_->io_stream(get_host(), this, get_host(), nullptr, size);
249     break;
250     case s4u::Io::OpType::WRITE:
251       res = static_cast<DiskS22Model*>(get_model())->hostModel_->io_stream(get_host(), nullptr, get_host(), this, size);
252     break;
253     default:
254       THROW_UNIMPLEMENTED;
255   }
256
257    return res;
258 }
259
260 void DiskS22::apply_event(kernel::profile::Event* triggered, double value)
261 {
262   /* Find out which of my iterators was triggered, and react accordingly */
263   if (triggered == get_read_event()) {
264     set_read_bandwidth(value);
265     unref_read_event();
266   } else if (triggered == get_write_event()) {
267     set_write_bandwidth(value);
268     unref_write_event();
269   } else if (triggered == get_state_event()) {
270     if (value > 0)
271       turn_on();
272     else
273       turn_off();
274     unref_state_event();
275   } else {
276     xbt_die("Unknown event!\n");
277   }
278 }
279
280 LinkS22::LinkS22(const std::string& name, double bandwidth, lmm::System* system) : StandardLinkImpl(name)
281 {
282   this->set_constraint(system->constraint_new(this, bandwidth));
283   bandwidth_.peak = bandwidth;
284 }
285
286 void LinkS22::apply_event(profile::Event* triggered, double value)
287 {
288   XBT_DEBUG("Updating link %s (%p) with value=%f", get_cname(), this, value);
289   if (triggered == bandwidth_.event) {
290     set_bandwidth(value);
291     tmgr_trace_event_unref(&bandwidth_.event);
292
293   } else if (triggered == latency_.event) {
294     set_latency(value);
295     tmgr_trace_event_unref(&latency_.event);
296
297   } else if (triggered == get_state_event()) {
298     if (value > 0)
299       turn_on();
300     else
301       turn_off();
302     unref_state_event();
303   } else {
304     xbt_die("Unknown event ! \n");
305   }
306 }
307
308 void LinkS22::set_bandwidth(double value)
309 {
310   bandwidth_.peak = value;
311   StandardLinkImpl::on_bandwidth_change();
312
313   get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(), bandwidth_.peak * bandwidth_.scale);
314 }
315
316 void LinkS22::set_latency(double value)
317 {
318   latency_check(value);
319   const lmm::Element* elem = nullptr;
320
321   latency_.peak = value;
322   while (const auto* var = get_constraint()->get_variable(&elem)) {
323     const auto* action = static_cast<S22Action*>(var->get_id());
324     action->update_bound();
325   }
326 }
327
328 /**********
329  * Action *
330  **********/
331
332 double S22Action::calculate_io_read_bound() const
333 {
334   double io_read_bound = std::numeric_limits<double>::max();
335
336   if (src_disk_ == nullptr)
337     return io_read_bound;
338
339   if (size_ > 0)
340     io_read_bound = std::min(io_read_bound, src_disk_->get_read_bandwidth() / size_);
341
342   return io_read_bound;
343 }
344
345 double S22Action::calculate_io_write_bound() const
346 {
347   double io_write_bound = std::numeric_limits<double>::max();
348
349   if (dst_disk_ == nullptr)
350     return io_write_bound;
351
352   if (size_ > 0)
353     io_write_bound = std::min(io_write_bound, dst_disk_->get_write_bandwidth() / size_);
354
355   return io_write_bound;
356 }
357
358 double S22Action::calculate_network_bound() const
359 {
360   double lat = 0.0;
361   double lat_bound   = std::numeric_limits<double>::max();
362
363   if (src_host_ == dst_host_)
364     return lat_bound;
365
366   if (size_ <= 0){
367     std::vector<StandardLinkImpl*> route;
368     src_host_->route_to(dst_host_, route, &lat);
369   }
370
371   if (lat > 0)
372     lat_bound = NetworkModel::cfg_tcp_gamma / (2.0 * lat);
373
374   return lat_bound;
375 }
376
377 void S22Action::update_bound() const
378 {
379   double bound = std::min(std::min(calculate_io_read_bound(), calculate_io_write_bound()),
380                           calculate_network_bound());
381
382   XBT_DEBUG("action (%p) : bound = %g", this, bound);
383
384   /* latency has been paid (or no latency), we can set the appropriate bound for network limit */
385   if ((bound < std::numeric_limits<double>::max()) && (latency_ <= 0.0))
386     get_model()->get_maxmin_system()->update_variable_bound(get_variable(), bound);
387  }
388
389 } // namespace simgrid::kernel::resource