Logo AND Algorithmique Numérique Distribuée

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