Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
clang-format before starting changing files
[simgrid.git] / src / surf / ptask_L07.cpp
1 /* Copyright (c) 2007-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 #include "ptask_L07.hpp"
7 #include "src/kernel/resource/profile/Event.hpp"
8 #include "surf/surf.hpp"
9 #include "xbt/config.hpp"
10
11 #include <unordered_set>
12
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_host);
14 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
15
16 /**************************************/
17 /*** Resource Creation & Destruction **/
18 /**************************************/
19 void surf_host_model_init_ptask_L07()
20 {
21   XBT_CINFO(xbt_cfg, "Switching to the L07 model to handle parallel tasks.");
22   xbt_assert(not surf_cpu_model_pm, "Cannot switch to ptasks: CPU model already defined");
23   xbt_assert(not surf_network_model, "Cannot switch to ptasks: network model already defined");
24
25   surf_host_model = new simgrid::surf::HostL07Model();
26   all_existing_models.push_back(surf_host_model);
27 }
28
29 namespace simgrid {
30 namespace surf {
31
32 HostL07Model::HostL07Model() : HostModel()
33 {
34   auto* maxmin_system = new simgrid::kernel::lmm::FairBottleneck(true /* selective update */);
35   set_maxmin_system(maxmin_system);
36   surf_network_model = new NetworkL07Model(this, maxmin_system);
37   surf_cpu_model_pm  = new CpuL07Model(this, maxmin_system);
38 }
39
40 HostL07Model::~HostL07Model()
41 {
42   delete surf_network_model;
43   delete surf_cpu_model_pm;
44 }
45
46 CpuL07Model::CpuL07Model(HostL07Model* hmodel, kernel::lmm::System* sys)
47     : CpuModel(Model::UpdateAlgo::FULL), hostModel_(hmodel)
48 {
49   set_maxmin_system(sys);
50 }
51
52 CpuL07Model::~CpuL07Model()
53 {
54   set_maxmin_system(nullptr);
55 }
56
57 NetworkL07Model::NetworkL07Model(HostL07Model* hmodel, kernel::lmm::System* sys)
58     : NetworkModel(Model::UpdateAlgo::FULL), hostModel_(hmodel)
59 {
60   set_maxmin_system(sys);
61   loopback_ = NetworkL07Model::create_link(
62                   "__loopback__", std::vector<double>{simgrid::config::get_value<double>("network/loopback-bw")},
63                   s4u::Link::SharingPolicy::FATPIPE)
64                   ->set_latency(simgrid::config::get_value<double>("network/loopback-lat"));
65   loopback_->seal();
66 }
67
68 NetworkL07Model::~NetworkL07Model()
69 {
70   set_maxmin_system(nullptr);
71 }
72
73 double HostL07Model::next_occurring_event(double now)
74 {
75   double min = HostModel::next_occurring_event_full(now);
76   for (kernel::resource::Action const& action : *get_started_action_set()) {
77     const auto& net_action = static_cast<const L07Action&>(action);
78     if (net_action.get_latency() > 0 && (min < 0 || net_action.get_latency() < min)) {
79       min = net_action.get_latency();
80       XBT_DEBUG("Updating min with %p (start %f): %f", &net_action, net_action.get_start_time(), min);
81     }
82   }
83   XBT_DEBUG("min value: %f", min);
84
85   return min;
86 }
87
88 void HostL07Model::update_actions_state(double /*now*/, double delta)
89 {
90   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
91     auto& action = static_cast<L07Action&>(*it);
92     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
93     if (action.get_latency() > 0) {
94       if (action.get_latency() > delta) {
95         action.update_latency(delta, sg_surf_precision);
96       } else {
97         action.set_latency(0.0);
98       }
99       if ((action.get_latency() <= 0.0) && (action.is_suspended() == 0)) {
100         action.updateBound();
101         get_maxmin_system()->update_variable_penalty(action.get_variable(), 1.0);
102         action.set_last_update();
103       }
104     }
105     XBT_DEBUG("Action (%p) : remains (%g) updated by %g.", &action, action.get_remains(),
106               action.get_variable()->get_value() * delta);
107     action.update_remains(action.get_variable()->get_value() * delta);
108     action.update_max_duration(delta);
109
110     XBT_DEBUG("Action (%p) : remains (%g).", &action, action.get_remains());
111
112     /* In the next if cascade, the action can be finished either because:
113      *  - The amount of remaining work reached 0
114      *  - The max duration was reached
115      * If it's not done, it may have failed.
116      */
117
118     if (((action.get_remains() <= 0) && (action.get_variable()->get_penalty() > 0)) ||
119         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
120       action.finish(kernel::resource::Action::State::FINISHED);
121       continue;
122     }
123
124     /* Need to check that none of the model has failed */
125     int i                               = 0;
126     const kernel::lmm::Constraint* cnst = action.get_variable()->get_constraint(i);
127     while (cnst != nullptr) {
128       i++;
129       const kernel::resource::Resource* constraint_id = cnst->get_id();
130       if (not constraint_id->is_on()) {
131         XBT_DEBUG("Action (%p) Failed!!", &action);
132         action.finish(kernel::resource::Action::State::FAILED);
133         break;
134       }
135       cnst = action.get_variable()->get_constraint(i);
136     }
137   }
138 }
139
140 kernel::resource::CpuAction* HostL07Model::execute_parallel(const std::vector<s4u::Host*>& host_list,
141                                                             const double* flops_amount, const double* bytes_amount,
142                                                             double rate)
143 {
144   return new L07Action(this, host_list, flops_amount, bytes_amount, rate);
145 }
146
147 L07Action::L07Action(kernel::resource::Model* model, const std::vector<s4u::Host*>& host_list,
148                      const double* flops_amount, const double* bytes_amount, double rate)
149     : CpuAction(model, 1.0, false), computationAmount_(flops_amount), communicationAmount_(bytes_amount), rate_(rate)
150 {
151   size_t link_nb      = 0;
152   size_t used_host_nb = 0; /* Only the hosts with something to compute (>0 flops) are counted) */
153   double latency      = 0.0;
154   this->set_last_update();
155
156   hostList_.insert(hostList_.end(), host_list.begin(), host_list.end());
157
158   if (flops_amount != nullptr)
159     used_host_nb += std::count_if(flops_amount, flops_amount + host_list.size(), [](double x) { return x > 0.0; });
160
161   /* Compute the number of affected resources... */
162   if (bytes_amount != nullptr) {
163     std::unordered_set<const char*> affected_links;
164
165     for (size_t k = 0; k < host_list.size() * host_list.size(); k++) {
166       if (bytes_amount[k] <= 0)
167         continue;
168
169       double lat = 0.0;
170       std::vector<kernel::resource::LinkImpl*> route;
171       hostList_[k / host_list.size()]->route_to(hostList_[k % host_list.size()], route, &lat);
172       latency = std::max(latency, lat);
173
174       for (auto const& link : route)
175         affected_links.insert(link->get_cname());
176     }
177
178     link_nb = affected_links.size();
179   }
180
181   XBT_DEBUG("Creating a parallel task (%p) with %zu hosts and %zu unique links.", this, host_list.size(), link_nb);
182   latency_ = latency;
183
184   set_variable(
185       model->get_maxmin_system()->variable_new(this, 1.0, (rate > 0 ? rate : -1.0), host_list.size() + link_nb));
186
187   if (latency_ > 0)
188     model->get_maxmin_system()->update_variable_penalty(get_variable(), 0.0);
189
190   /* Expand it for the CPUs even if there is nothing to compute, to make sure that it gets expended even if there is no
191    * communication either */
192   double bound = std::numeric_limits<double>::max();
193   for (size_t i = 0; i < host_list.size(); i++) {
194     model->get_maxmin_system()->expand(host_list[i]->pimpl_cpu->get_constraint(), get_variable(),
195                                        (flops_amount == nullptr ? 0.0 : flops_amount[i]));
196     if (flops_amount && flops_amount[i] > 0)
197       bound = std::min(bound, host_list[i]->pimpl_cpu->get_speed(1.0) * host_list[i]->pimpl_cpu->get_speed_ratio() /
198                                   flops_amount[i]);
199   }
200   if (bound < std::numeric_limits<double>::max())
201     model->get_maxmin_system()->update_variable_bound(get_variable(), bound);
202
203   if (bytes_amount != nullptr) {
204     for (size_t k = 0; k < host_list.size() * host_list.size(); k++) {
205       if (bytes_amount[k] <= 0.0)
206         continue;
207       std::vector<kernel::resource::LinkImpl*> route;
208       hostList_[k / host_list.size()]->route_to(hostList_[k % host_list.size()], route, nullptr);
209
210       for (auto const& link : route)
211         model->get_maxmin_system()->expand_add(link->get_constraint(), this->get_variable(), bytes_amount[k]);
212     }
213   }
214
215   if (link_nb + used_host_nb == 0) {
216     this->set_cost(1.0);
217     this->set_remains(0.0);
218   }
219 }
220
221 kernel::resource::Action* NetworkL07Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
222 {
223   std::vector<s4u::Host*> host_list = {src, dst};
224   const auto* flops_amount          = new double[2]();
225   auto* bytes_amount                = new double[4]();
226
227   bytes_amount[1] = size;
228
229   kernel::resource::Action* res = hostModel_->execute_parallel(host_list, flops_amount, bytes_amount, rate);
230   static_cast<L07Action*>(res)->free_arrays_ = true;
231   return res;
232 }
233
234 kernel::resource::Cpu* CpuL07Model::create_cpu(s4u::Host* host, const std::vector<double>& speed_per_pstate)
235 {
236   return (new CpuL07(host, speed_per_pstate))->set_model(this);
237 }
238
239 kernel::resource::LinkImpl* NetworkL07Model::create_link(const std::string& name, const std::vector<double>& bandwidths,
240                                                          s4u::Link::SharingPolicy policy)
241 {
242   xbt_assert(bandwidths.size() == 1, "Non WIFI link must have only 1 bandwidth.");
243   return new LinkL07(name, bandwidths[0], policy, get_maxmin_system());
244 }
245
246 /************
247  * Resource *
248  ************/
249
250 kernel::resource::CpuAction* CpuL07::execution_start(double size)
251 {
252   std::vector<s4u::Host*> host_list = {get_iface()};
253
254   auto* flops_amount = new double[host_list.size()]();
255   flops_amount[0]    = size;
256
257   kernel::resource::CpuAction* res =
258       static_cast<CpuL07Model*>(get_model())->hostModel_->execute_parallel(host_list, flops_amount, nullptr, -1);
259   static_cast<L07Action*>(res)->free_arrays_ = true;
260   return res;
261 }
262
263 kernel::resource::CpuAction* CpuL07::sleep(double duration)
264 {
265   auto* action = static_cast<L07Action*>(execution_start(1.0));
266   action->set_max_duration(duration);
267   action->set_suspend_state(kernel::resource::Action::SuspendStates::SLEEPING);
268   get_model()->get_maxmin_system()->update_variable_penalty(action->get_variable(), 0.0);
269
270   return action;
271 }
272
273 bool CpuL07::is_used() const
274 {
275   return get_model()->get_maxmin_system()->constraint_used(get_constraint());
276 }
277
278 /** @brief take into account changes of speed (either load or max) */
279 void CpuL07::on_speed_change()
280 {
281   const kernel::lmm::Variable* var;
282   const kernel::lmm::Element* elem = nullptr;
283
284   get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(), speed_.peak * speed_.scale);
285   while ((var = get_constraint()->get_variable(&elem))) {
286     const kernel::resource::Action* action = var->get_id();
287
288     get_model()->get_maxmin_system()->update_variable_bound(action->get_variable(), speed_.scale * speed_.peak);
289   }
290
291   Cpu::on_speed_change();
292 }
293
294 LinkL07::LinkL07(const std::string& name, double bandwidth, s4u::Link::SharingPolicy policy,
295                  kernel::lmm::System* system)
296     : LinkImpl(name)
297 {
298   this->set_constraint(system->constraint_new(this, bandwidth));
299   bandwidth_.peak = bandwidth;
300
301   if (policy == s4u::Link::SharingPolicy::FATPIPE)
302     get_constraint()->unshare();
303 }
304
305 bool LinkL07::is_used() const
306 {
307   return get_model()->get_maxmin_system()->constraint_used(get_constraint());
308 }
309
310 void CpuL07::apply_event(kernel::profile::Event* triggered, double value)
311 {
312   XBT_DEBUG("Updating cpu %s (%p) with value %g", get_cname(), this, value);
313   if (triggered == speed_.event) {
314     speed_.scale = value;
315     on_speed_change();
316     tmgr_trace_event_unref(&speed_.event);
317
318   } else if (triggered == state_event_) {
319     if (value > 0) {
320       if (not is_on()) {
321         XBT_VERB("Restart actors on host %s", get_iface()->get_cname());
322         get_iface()->turn_on();
323       }
324     } else
325       get_iface()->turn_off();
326     tmgr_trace_event_unref(&state_event_);
327
328   } else {
329     xbt_die("Unknown event!\n");
330   }
331 }
332
333 void LinkL07::apply_event(kernel::profile::Event* triggered, double value)
334 {
335   XBT_DEBUG("Updating link %s (%p) with value=%f", get_cname(), this, value);
336   if (triggered == bandwidth_.event) {
337     set_bandwidth(value);
338     tmgr_trace_event_unref(&bandwidth_.event);
339
340   } else if (triggered == latency_.event) {
341     set_latency(value);
342     tmgr_trace_event_unref(&latency_.event);
343
344   } else if (triggered == state_event_) {
345     if (value > 0)
346       turn_on();
347     else
348       turn_off();
349     tmgr_trace_event_unref(&state_event_);
350
351   } else {
352     xbt_die("Unknown event ! \n");
353   }
354 }
355
356 void LinkL07::set_bandwidth(double value)
357 {
358   bandwidth_.peak = value;
359   LinkImpl::on_bandwidth_change();
360
361   get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(), bandwidth_.peak * bandwidth_.scale);
362 }
363
364 kernel::resource::LinkImpl* LinkL07::set_latency(double value)
365 {
366   latency_check(value);
367   const kernel::lmm::Variable* var;
368   L07Action* action;
369   const kernel::lmm::Element* elem = nullptr;
370
371   latency_.peak = value;
372   while ((var = get_constraint()->get_variable(&elem))) {
373     action = static_cast<L07Action*>(var->get_id());
374     action->updateBound();
375   }
376   return this;
377 }
378 LinkL07::~LinkL07() = default;
379
380 /**********
381  * Action *
382  **********/
383
384 L07Action::~L07Action()
385 {
386   if (free_arrays_) {
387     delete[] computationAmount_;
388     delete[] communicationAmount_;
389   }
390 }
391
392 void L07Action::updateBound()
393 {
394   double lat_current = 0.0;
395
396   size_t host_count = hostList_.size();
397
398   if (communicationAmount_ != nullptr) {
399     for (size_t i = 0; i < host_count; i++) {
400       for (size_t j = 0; j < host_count; j++) {
401         if (communicationAmount_[i * host_count + j] > 0) {
402           double lat = 0.0;
403           std::vector<kernel::resource::LinkImpl*> route;
404           hostList_.at(i)->route_to(hostList_.at(j), route, &lat);
405
406           lat_current = std::max(lat_current, lat * communicationAmount_[i * host_count + j]);
407         }
408       }
409     }
410   }
411   double lat_bound = kernel::resource::NetworkModel::cfg_tcp_gamma / (2.0 * lat_current);
412   XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
413   if ((latency_ <= 0.0) && is_running()) {
414     if (rate_ < 0)
415       get_model()->get_maxmin_system()->update_variable_bound(get_variable(), lat_bound);
416     else
417       get_model()->get_maxmin_system()->update_variable_bound(get_variable(), std::min(rate_, lat_bound));
418   }
419 }
420
421 } // namespace surf
422 } // namespace simgrid