Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
this belongs to the Impl not a specific model
[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 #include "src/surf/sio_S22.hpp"
13
14 #include <unordered_set>
15
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_host);
17 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
18
19 /***********
20  * Options *
21  ***********/
22  static simgrid::config::Flag<std::string> cfg_sio_solver("host/sio_solver",
23                                                            "Set linear equations solver used by sio model",
24                                                            "maxmin",
25                                                            &simgrid::kernel::lmm::System::validate_solver);
26
27 /**************************************/
28 /*** Resource Creation & Destruction **/
29 /**************************************/
30 void surf_host_model_init_sio_S22()
31 {
32   XBT_CINFO(xbt_cfg, "Switching to the S22 model to handle streaming I/Os.");
33   simgrid::config::set_default<bool>("network/crosstraffic", true);
34   auto host_model = std::make_shared<simgrid::kernel::resource::HostS22Model>("Host_Sio");
35   surf_network_model_init_LegrandVelho();
36   surf_cpu_model_init_Cas01();
37   surf_disk_model_init_S19();
38
39   auto* engine    = simgrid::kernel::EngineImpl::get_instance();
40   engine->add_model(host_model);
41   engine->get_netzone_root()->set_host_model(host_model);
42 }
43
44 namespace simgrid::kernel::resource {
45
46 HostS22Model::HostS22Model(const std::string& name) : HostModel(name)
47 {
48   set_maxmin_system(lmm::System::build(cfg_sio_solver.get(), true /* selective update */));
49 }
50
51 double HostS22Model::next_occurring_event(double now)
52 {
53   double min = HostModel::next_occurring_event_full(now);
54   for (Action const& action : *get_started_action_set()) {
55     const auto& net_action = static_cast<const S22Action&>(action);
56     if (net_action.get_latency() > 0 && (min < 0 || net_action.get_latency() < min)) {
57       min = net_action.get_latency();
58       XBT_DEBUG("Updating min with %p (start %f): %f", &net_action, net_action.get_start_time(), min);
59     }
60   }
61   XBT_DEBUG("min value: %f", min);
62
63   return min;
64 }
65
66 void HostS22Model::update_actions_state(double /*now*/, double delta)
67 {
68   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
69     auto& action = static_cast<S22Action&>(*it);
70     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
71     if (action.get_latency() > 0) {
72       if (action.get_latency() > delta) {
73         action.update_latency(delta, sg_surf_precision);
74       } else {
75         action.set_latency(0.0);
76       }
77       if ((action.get_latency() <= 0.0) && (action.is_suspended() == 0)) {
78         action.update_bound();
79         get_maxmin_system()->update_variable_penalty(action.get_variable(), 1.0);
80         action.set_last_update();
81       }
82     }
83     XBT_DEBUG("Action (%p) : remains (%g) updated by %g.", &action, action.get_remains(), action.get_rate() * delta);
84     action.update_remains(action.get_rate() * delta);
85     action.update_max_duration(delta);
86
87     XBT_DEBUG("Action (%p) : remains (%g).", &action, action.get_remains());
88
89     if (((action.get_remains() <= 0) && (action.get_variable()->get_penalty() > 0)) ||
90         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
91       action.finish(Action::State::FINISHED);
92       continue;
93     }
94   }
95 }
96
97 DiskAction* HostS22Model::io_stream(s4u::Host* src_host, DiskImpl* src_disk, s4u::Host* dst_host, DiskImpl* dst_disk,
98                                    double size)
99 {
100   return new S22Action(this, src_host, src_disk, dst_host, dst_disk, size);
101 }
102
103 Action* HostS22Model::execute_thread(const s4u::Host* host, double flops_amount, int thread_count)
104 {
105   /* Create a single action whose cost is thread_count * flops_amount and that requests thread_count cores. */
106   return host->get_cpu()->execution_start(thread_count * flops_amount, thread_count, -1);
107 }
108
109 /**********
110  * Action *
111  **********/
112 void S22Action::update_bound() const
113 {
114   double bound = std::numeric_limits<double>::max();
115   if (src_disk_)
116     bound = std::min(bound, src_disk_->get_read_bandwidth());
117   if (dst_disk_)
118     bound = std::min(bound, dst_disk_->get_write_bandwidth());
119   if (src_host_ != dst_host_) {
120     double lat       = 0.0;
121     std::vector<StandardLinkImpl*> route;
122     src_host_->route_to(dst_host_, route, &lat);
123     if (lat > 0)
124       bound = std::min(bound,NetworkModel::cfg_tcp_gamma / (2.0 * lat));
125   }
126
127   XBT_DEBUG("action (%p) : bound = %g", this, bound);
128
129   /* latency has been paid (or no latency), we can set the appropriate bound for network limit */
130   if ((bound < std::numeric_limits<double>::max()) && (latency_ <= 0.0))
131     get_model()->get_maxmin_system()->update_variable_bound(get_variable(), bound);
132  }
133
134 S22Action::S22Action(Model* model, s4u::Host* src_host, DiskImpl* src_disk, s4u::Host* dst_host, DiskImpl* dst_disk, double size)
135     : DiskAction(model, size, false)
136     , src_host_(src_host)
137     , src_disk_(src_disk)
138     , dst_host_(dst_host)
139     , dst_disk_(dst_disk)
140     , size_(size)
141 {
142   this->set_last_update();
143
144   size_t disk_nb       = 0;
145   if (src_disk_ != nullptr)
146     disk_nb++;
147   if (dst_disk_ != nullptr)
148     disk_nb++;
149
150   /* there should always be a route between src_host and dst_host (loopback_ for self communication at least) */
151   std::vector<StandardLinkImpl*> route;
152   src_host_->route_to(dst_host_, route, &latency_);
153   size_t link_nb = route.size();
154
155   XBT_DEBUG("Creating a stream io (%p) with %zu disk(s) and %zu unique link(s).", this, disk_nb, link_nb);
156
157   set_variable(model->get_maxmin_system()->variable_new(this, 1.0, -1.0, 2 * disk_nb + link_nb));
158
159   if (latency_ > 0)
160     model->get_maxmin_system()->update_variable_penalty(get_variable(), 0.0);
161
162   /* 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
163    * communication either */
164   if (src_disk_ != nullptr){
165     model->get_maxmin_system()->expand(src_disk_->get_constraint(), get_variable(), 1);
166     model->get_maxmin_system()->expand(src_disk->get_read_constraint(), get_variable(), 1);
167   }
168   if (dst_disk_ != nullptr){
169     model->get_maxmin_system()->expand(dst_disk_->get_constraint(), get_variable(), 1);
170     model->get_maxmin_system()->expand(dst_disk_->get_write_constraint(), get_variable(), 1);
171   }
172
173   for (auto const& link : route)
174     model->get_maxmin_system()->expand(link->get_constraint(), get_variable(), 1);
175
176   if (size <= 0) {
177     this->set_cost(1.0);
178     this->set_remains(0.0);
179   }
180
181   /* finally calculate the initial bound value */
182   update_bound();
183 }
184 } // namespace simgrid::kernel::resource