Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1dc5eebf2cb747a36ebe91b1a9e3465ad4acc4f5
[simgrid.git] / src / kernel / resource / models / cpu_ti.cpp
1 /* Copyright (c) 2013-2023. 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 "cpu_ti.hpp"
7 #include "simgrid/kernel/routing/NetZoneImpl.hpp"
8 #include "simgrid/s4u/Engine.hpp"
9 #include "src/kernel/EngineImpl.hpp"
10 #include "src/kernel/resource/profile/Event.hpp"
11 #include "src/kernel/resource/profile/Profile.hpp"
12 #include "xbt/asserts.h"
13
14 #include <algorithm>
15 #include <memory>
16
17 constexpr double EPSILON = 0.000000001;
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(cpu_ti, res_cpu, "CPU resource, Trace Integration model");
20
21 namespace simgrid::kernel::resource {
22
23 /*********
24  * Trace *
25  *********/
26
27 CpuTiProfile::CpuTiProfile(const profile::Profile* profile)
28 {
29   double integral                                = 0;
30   double time                                    = 0;
31   double prev_value                              = 1;
32   const std::vector<profile::DatedValue>& events = profile->get_event_list();
33   xbt_assert(not events.empty());
34   unsigned long nb_points = events.size() + 1;
35   time_points_.reserve(nb_points);
36   integral_.reserve(nb_points);
37   for (auto const& val : events) {
38     time += val.date_;
39     integral += val.date_ * prev_value;
40     time_points_.push_back(time);
41     integral_.push_back(integral);
42     prev_value = val.value_;
43   }
44
45   double delay = profile->get_repeat_delay() + events.at(0).date_;
46
47   xbt_assert(events.back().value_ == prev_value, "Profiles need to end as they start");
48   time += delay;
49   integral += delay * prev_value;
50
51   time_points_.push_back(time);
52   integral_.push_back(integral);
53 }
54
55 /**
56  * @brief Integrate trace
57  *
58  * Wrapper around profile_->integrate_simple() to get
59  * the cyclic effect.
60  *
61  * @param a      Begin of interval
62  * @param b      End of interval
63  * @return the integrate value. -1 if an error occurs.
64  */
65 double CpuTiTmgr::integrate(double a, double b) const
66 {
67   xbt_assert(a >= 0.0 && a <= b,
68              "Error, invalid integration interval [%.2f,%.2f]. You probably have a task executing with negative "
69              "computation amount. Check your code.",
70              a, b);
71   if (fabs(a - b) < EPSILON)
72     return 0.0;
73
74   if (type_ == Type::FIXED) {
75     return (b - a) * value_;
76   }
77
78   double a_index;
79   if (fabs(ceil(a / last_time_) - a / last_time_) < EPSILON)
80     a_index = 1 + ceil(a / last_time_);
81   else
82     a_index = ceil(a / last_time_);
83   double b_index = floor(b / last_time_);
84
85   if (a_index > b_index) { /* Same chunk */
86     return profile_->integrate_simple(a - (a_index - 1) * last_time_, b - b_index * last_time_);
87   }
88
89   double first_chunk  = profile_->integrate_simple(a - (a_index - 1) * last_time_, last_time_);
90   double middle_chunk = (b_index - a_index) * total_;
91   double last_chunk   = profile_->integrate_simple(0.0, b - b_index * last_time_);
92
93   XBT_DEBUG("first_chunk=%.2f  middle_chunk=%.2f  last_chunk=%.2f\n", first_chunk, middle_chunk, last_chunk);
94
95   return (first_chunk + middle_chunk + last_chunk);
96 }
97
98 /**
99  * @brief Auxiliary function to compute the integral between a and b.
100  *     It simply computes the integrals at point a and b and returns the difference between them.
101  * @param a  Initial point
102  * @param b  Final point
103  */
104 double CpuTiProfile::integrate_simple(double a, double b) const
105 {
106   return integrate_simple_point(b) - integrate_simple_point(a);
107 }
108
109 /**
110  * @brief Auxiliary function to compute the integral at point a.
111  * @param a        point
112  */
113 double CpuTiProfile::integrate_simple_point(double a) const
114 {
115   double integral = 0;
116   double a_aux    = a;
117   long ind        = binary_search(time_points_, a);
118   integral += integral_[ind];
119
120   XBT_DEBUG("a %f ind %ld integral %f ind + 1 %f ind %f time +1 %f time %f", a, ind, integral, integral_[ind + 1],
121             integral_[ind], time_points_[ind + 1], time_points_[ind]);
122   double_update(&a_aux, time_points_[ind], sg_precision_workamount * sg_precision_timing);
123   if (a_aux > 0)
124     integral +=
125         ((integral_[ind + 1] - integral_[ind]) / (time_points_[ind + 1] - time_points_[ind])) * (a - time_points_[ind]);
126   XBT_DEBUG("Integral a %f = %f", a, integral);
127
128   return integral;
129 }
130
131 /**
132  * @brief Computes the time needed to execute "amount" on cpu.
133  *
134  * Here, amount can span multiple trace periods
135  *
136  * @param a        Initial time
137  * @param amount  Amount to be executed
138  * @return  End time
139  */
140 double CpuTiTmgr::solve(double a, double amount) const
141 {
142   /* Fix very small negative numbers */
143   if ((a < 0.0) && (a > -EPSILON)) {
144     a = 0.0;
145   }
146   if ((amount < 0.0) && (amount > -EPSILON)) {
147     amount = 0.0;
148   }
149
150   /* Sanity checks */
151   xbt_assert(a >= 0.0 && amount >= 0.0,
152              "Error, invalid parameters [a = %.2f, amount = %.2f]. "
153              "You probably have a task executing with negative computation amount. Check your code.",
154              a, amount);
155
156   /* At this point, a and amount are positive */
157   if (amount < EPSILON)
158     return a;
159
160   /* Is the trace fixed ? */
161   if (type_ == Type::FIXED) {
162     return (a + (amount / value_));
163   }
164
165   XBT_DEBUG("amount %f total %f", amount, total_);
166   /* Reduce the problem to one where amount <= trace_total */
167   double quotient       = floor(amount / total_);
168   double reduced_amount = total_ * ((amount / total_) - floor(amount / total_));
169   double reduced_a      = a - last_time_ * static_cast<int>(floor(a / last_time_));
170
171   XBT_DEBUG("Quotient: %g reduced_amount: %f reduced_a: %f", quotient, reduced_amount, reduced_a);
172
173   /* Now solve for new_amount which is <= trace_total */
174   XBT_DEBUG("Solve integral: [%.2f, amount=%.2f]", reduced_a, reduced_amount);
175
176   double amount_till_end = integrate(reduced_a, last_time_);
177   double reduced_b       = amount_till_end > reduced_amount
178                                ? profile_->solve_simple(reduced_a, reduced_amount)
179                                : last_time_ + profile_->solve_simple(0.0, reduced_amount - amount_till_end);
180
181   /* Re-map to the original b and amount */
182   return last_time_ * floor(a / last_time_) + (quotient * last_time_) + reduced_b;
183 }
184
185 /**
186  * @brief Auxiliary function to solve integral.
187  *  It returns the date when the requested amount of flops is available
188  * @param a        Initial point
189  * @param amount  Amount of flops
190  * @return The date when amount is available.
191  */
192 double CpuTiProfile::solve_simple(double a, double amount) const
193 {
194   double integral_a = integrate_simple_point(a);
195   long ind          = binary_search(integral_, integral_a + amount);
196   double time       = time_points_[ind];
197   time += (integral_a + amount - integral_[ind]) /
198           ((integral_[ind + 1] - integral_[ind]) / (time_points_[ind + 1] - time_points_[ind]));
199
200   return time;
201 }
202
203 /**
204  * @brief Auxiliary function to update the CPU speed scale.
205  *
206  *  This function uses the trace structure to return the speed scale at the determined time a.
207  * @param a        Time
208  * @return CPU speed scale
209  */
210 double CpuTiTmgr::get_power_scale(double a) const
211 {
212   double reduced_a        = a - floor(a / last_time_) * last_time_;
213   long point              = CpuTiProfile::binary_search(profile_->get_time_points(), reduced_a);
214   profile::DatedValue val = speed_profile_->get_event_list().at(point);
215   return val.value_;
216 }
217
218 /**
219  * @brief Creates a new integration trace from a tmgr_trace_t
220  *
221  * @param  speed_trace    CPU availability trace
222  * @param  value          Percentage of CPU speed available (useful to fixed tracing)
223  * @return  Integration trace structure
224  */
225 CpuTiTmgr::CpuTiTmgr(kernel::profile::Profile* speed_profile, double value) : speed_profile_(speed_profile)
226 {
227   double total_time = 0.0;
228   profile_.reset(nullptr);
229
230   /* no availability file, fixed trace */
231   if (not speed_profile) {
232     value_ = value;
233     XBT_DEBUG("No availability trace. Constant value = %f", value);
234     return;
235   }
236
237   xbt_assert(speed_profile->is_repeating());
238
239   /* only one point available, fixed trace */
240   if (speed_profile->get_event_list().size() == 1) {
241     value_ = speed_profile->get_event_list().front().value_;
242     return;
243   }
244
245   type_ = Type::DYNAMIC;
246
247   /* count the total time of trace file */
248   for (auto const& val : speed_profile->get_event_list())
249     total_time += val.date_;
250   total_time += speed_profile->get_repeat_delay();
251
252   profile_   = std::make_unique<CpuTiProfile>(speed_profile);
253   last_time_ = total_time;
254   total_     = profile_->integrate_simple(0, total_time);
255
256   XBT_DEBUG("Total integral %f, last_time %f ", total_, last_time_);
257 }
258
259 /**
260  * @brief Binary search in array.
261  *  It returns the last point of the interval in which "a" is.
262  * @param array    Array
263  * @param a        Value to search
264  * @return Index of point
265  */
266 long CpuTiProfile::binary_search(const std::vector<double>& array, double a)
267 {
268   if (array[0] > a)
269     return 0;
270   auto pos = std::upper_bound(begin(array), end(array), a);
271   return std::distance(begin(array), pos) - 1;
272 }
273
274 /*********
275  * Model *
276  *********/
277
278 void CpuTiModel::create_pm_models()
279 {
280   auto cpu_model_pm = std::make_shared<CpuTiModel>("Cpu_TI");
281   auto* engine      = EngineImpl::get_instance();
282   engine->add_model(cpu_model_pm);
283   engine->get_netzone_root()->set_cpu_pm_model(cpu_model_pm);
284 }
285
286 CpuImpl* CpuTiModel::create_cpu(s4u::Host* host, const std::vector<double>& speed_per_pstate)
287 {
288   return (new CpuTi(host, speed_per_pstate))->set_model(this);
289 }
290
291 double CpuTiModel::next_occurring_event(double now)
292 {
293   double min_action_duration = -1;
294
295   /* iterates over modified cpus to update share resources */
296   for (auto it = std::begin(modified_cpus_); it != std::end(modified_cpus_);) {
297     CpuTi& cpu = *it;
298     ++it; // increment iterator here since the following call to ti.update_actions_finish_time() may invalidate it
299     cpu.update_actions_finish_time(now);
300   }
301
302   /* get the min next event if heap not empty */
303   if (not get_action_heap().empty())
304     min_action_duration = get_action_heap().top_date() - now;
305
306   XBT_DEBUG("Share resources, min next event date: %f", min_action_duration);
307
308   return min_action_duration;
309 }
310
311 void CpuTiModel::update_actions_state(double now, double /*delta*/)
312 {
313   while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_precision_timing)) {
314     auto* action = static_cast<CpuTiAction*>(get_action_heap().pop());
315     XBT_DEBUG("Action %p: finish", action);
316     action->finish(Action::State::FINISHED);
317     /* update remaining amount of all actions */
318     action->cpu_->update_remaining_amount(EngineImpl::get_clock());
319   }
320 }
321
322 /************
323  * Resource *
324  ************/
325 CpuTi::CpuTi(s4u::Host* host, const std::vector<double>& speed_per_pstate) : CpuImpl(host, speed_per_pstate)
326 {
327   speed_.peak = speed_per_pstate.front();
328   XBT_DEBUG("CPU create: peak=%f", speed_.peak);
329
330   speed_integrated_trace_ = new CpuTiTmgr(nullptr, 1 /*scale*/);
331 }
332
333 CpuTi::~CpuTi()
334 {
335   set_modified(false);
336   delete speed_integrated_trace_;
337 }
338
339 CpuImpl* CpuTi::set_speed_profile(kernel::profile::Profile* profile)
340 {
341   delete speed_integrated_trace_;
342   speed_integrated_trace_ = new CpuTiTmgr(profile, speed_.scale);
343
344   /* add a fake trace event if periodicity == 0 */
345   if (profile && profile->get_event_list().size() > 1) {
346     kernel::profile::DatedValue val = profile->get_event_list().back();
347     if (val.date_ < 1e-12) {
348       auto* prof   = profile::ProfileBuilder::from_void();
349       speed_.event = prof->schedule(&profile::future_evt_set, this);
350     }
351   }
352   return this;
353 }
354
355 void CpuTi::apply_event(kernel::profile::Event* event, double value)
356 {
357   if (event == speed_.event) {
358     XBT_DEBUG("Speed changed in trace! New fixed value: %f", value);
359
360     /* update remaining of actions and put in modified cpu list */
361     update_remaining_amount(EngineImpl::get_clock());
362
363     set_modified(true);
364
365     delete speed_integrated_trace_;
366     speed_integrated_trace_ = new CpuTiTmgr(value);
367
368     speed_.scale = value;
369     tmgr_trace_event_unref(&speed_.event);
370
371   } else if (event == get_state_event()) {
372     if (value > 0) {
373       if (not is_on()) {
374         XBT_VERB("Restart actors on host %s", get_iface()->get_cname());
375         get_iface()->turn_on();
376       }
377     } else {
378       get_iface()->turn_off();
379       double date = EngineImpl::get_clock();
380
381       /* put all action running on cpu to failed */
382       for (CpuTiAction& action : action_set_) {
383         if (action.get_state() == Action::State::INITED || action.get_state() == Action::State::STARTED ||
384             action.get_state() == Action::State::IGNORED) {
385           action.set_finish_time(date);
386           action.set_state(Action::State::FAILED);
387           get_model()->get_action_heap().remove(&action);
388         }
389       }
390     }
391     unref_state_event();
392
393   } else {
394     xbt_die("Unknown event!\n");
395   }
396 }
397
398 /** Update the actions that are running on this CPU (which was modified recently) */
399 void CpuTi::update_actions_finish_time(double now)
400 {
401   /* update remaining amount of actions */
402   update_remaining_amount(now);
403
404   /* Compute the sum of priorities for the actions running on that CPU */
405   sum_priority_ = 0.0;
406   for (CpuTiAction const& action : action_set_) {
407     /* action not running, skip it */
408     if (action.get_state_set() != get_model()->get_started_action_set())
409       continue;
410
411     /* bogus priority, skip it */
412     if (action.get_sharing_penalty() <= 0)
413       continue;
414
415     /* action suspended, skip it */
416     if (not action.is_running())
417       continue;
418
419     sum_priority_ += 1.0 / action.get_sharing_penalty();
420   }
421
422   for (CpuTiAction& action : action_set_) {
423     double min_finish = NO_MAX_DURATION;
424     /* action not running, skip it */
425     if (action.get_state_set() != get_model()->get_started_action_set())
426       continue;
427
428     /* verify if the action is really running on cpu */
429     if (action.is_running() && action.get_sharing_penalty() > 0) {
430       /* total area needed to finish the action. Used in trace integration */
431       double total_area = (action.get_remains() * sum_priority_ * action.get_sharing_penalty()) / speed_.peak;
432
433       action.set_finish_time(speed_integrated_trace_->solve(now, total_area));
434       /* verify which event will happen before (max_duration or finish time) */
435       if (action.get_max_duration() != NO_MAX_DURATION &&
436           action.get_start_time() + action.get_max_duration() < action.get_finish_time())
437         min_finish = action.get_start_time() + action.get_max_duration();
438       else
439         min_finish = action.get_finish_time();
440     } else {
441       /* put the max duration time on heap */
442       if (action.get_max_duration() != NO_MAX_DURATION)
443         min_finish = action.get_start_time() + action.get_max_duration();
444     }
445     /* add in action heap */
446     if (min_finish != NO_MAX_DURATION)
447       get_model()->get_action_heap().update(&action, min_finish, ActionHeap::Type::unset);
448     else
449       get_model()->get_action_heap().remove(&action);
450
451     XBT_DEBUG("Update finish time: Cpu(%s) Action: %p, Start Time: %f Finish Time: %f Max duration %f", get_cname(),
452               &action, action.get_start_time(), action.get_finish_time(), action.get_max_duration());
453   }
454   /* remove from modified cpu */
455   set_modified(false);
456 }
457
458 bool CpuTi::is_used() const
459 {
460   return not action_set_.empty();
461 }
462
463 double CpuTi::get_speed_ratio()
464 {
465   speed_.scale = speed_integrated_trace_->get_power_scale(EngineImpl::get_clock());
466   return CpuImpl::get_speed_ratio();
467 }
468
469 /** @brief Update the remaining amount of actions */
470 void CpuTi::update_remaining_amount(double now)
471 {
472   /* already up to date */
473   if (last_update_ >= now)
474     return;
475
476   /* compute the integration area */
477   double area_total = speed_integrated_trace_->integrate(last_update_, now) * speed_.peak;
478   XBT_DEBUG("Flops total: %f, Last update %f", area_total, last_update_);
479   for (CpuTiAction& action : action_set_) {
480     /* action not running, skip it */
481     if (action.get_state_set() != get_model()->get_started_action_set())
482       continue;
483
484     /* bogus priority, skip it */
485     if (action.get_sharing_penalty() <= 0)
486       continue;
487
488     /* action suspended, skip it */
489     if (not action.is_running())
490       continue;
491
492     /* action don't need update */
493     if (action.get_start_time() >= now)
494       continue;
495
496     /* skip action that are finishing now */
497     if (action.get_finish_time() >= 0 && action.get_finish_time() <= now)
498       continue;
499
500     /* update remaining */
501     action.update_remains(area_total / (sum_priority_ * action.get_sharing_penalty()));
502     XBT_DEBUG("Update remaining action(%p) remaining %f", &action, action.get_remains_no_update());
503   }
504   last_update_ = now;
505 }
506
507 CpuAction* CpuTi::execution_start(double size, double user_bound)
508 {
509   XBT_IN("(%s,%g)", get_cname(), size);
510   xbt_assert(user_bound <= 0, "Invalid user bound (%lf) in CPU TI model", user_bound);
511   auto* action = new CpuTiAction(this, size);
512
513   action_set_.push_back(*action); // Actually start the action
514
515   XBT_OUT();
516   return action;
517 }
518
519 CpuAction* CpuTi::sleep(double duration)
520 {
521   if (duration > 0)
522     duration = std::max(duration, sg_precision_timing);
523
524   XBT_IN("(%s,%g)", get_cname(), duration);
525   auto* action = new CpuTiAction(this, 1.0);
526
527   action->set_max_duration(duration);
528   action->set_suspend_state(Action::SuspendStates::SLEEPING);
529   if (duration == NO_MAX_DURATION)
530     action->set_state(Action::State::IGNORED);
531
532   action_set_.push_back(*action);
533
534   XBT_OUT();
535   return action;
536 }
537
538 void CpuTi::set_modified(bool modified)
539 {
540   CpuTiList& modified_cpus = static_cast<CpuTiModel*>(get_model())->modified_cpus_;
541   if (modified) {
542     if (not cpu_ti_hook.is_linked()) {
543       modified_cpus.push_back(*this);
544     }
545   } else {
546     if (cpu_ti_hook.is_linked())
547       xbt::intrusive_erase(modified_cpus, *this);
548   }
549 }
550
551 /**********
552  * Action *
553  **********/
554
555 CpuTiAction::CpuTiAction(CpuTi* cpu, double cost) : CpuAction(cpu->get_model(), cost, not cpu->is_on()), cpu_(cpu)
556 {
557   cpu_->set_modified(true);
558 }
559 CpuTiAction::~CpuTiAction()
560 {
561   /* remove from action_set */
562   if (action_ti_hook.is_linked())
563     xbt::intrusive_erase(cpu_->action_set_, *this);
564   /* remove from heap */
565   get_model()->get_action_heap().remove(this);
566   cpu_->set_modified(true);
567 }
568
569 void CpuTiAction::set_state(Action::State state)
570 {
571   CpuAction::set_state(state);
572   cpu_->set_modified(true);
573 }
574
575 void CpuTiAction::cancel()
576 {
577   this->set_state(Action::State::FAILED);
578   get_model()->get_action_heap().remove(this);
579   cpu_->set_modified(true);
580 }
581
582 void CpuTiAction::suspend()
583 {
584   XBT_IN("(%p)", this);
585   if (is_running()) {
586     set_suspend_state(Action::SuspendStates::SUSPENDED);
587     get_model()->get_action_heap().remove(this);
588     cpu_->set_modified(true);
589   }
590   XBT_OUT();
591 }
592
593 void CpuTiAction::resume()
594 {
595   XBT_IN("(%p)", this);
596   if (is_suspended()) {
597     set_suspend_state(Action::SuspendStates::RUNNING);
598     cpu_->set_modified(true);
599   }
600   XBT_OUT();
601 }
602
603 void CpuTiAction::set_sharing_penalty(double sharing_penalty)
604 {
605   XBT_IN("(%p,%g)", this, sharing_penalty);
606   set_sharing_penalty_no_update(sharing_penalty);
607   cpu_->set_modified(true);
608   XBT_OUT();
609 }
610
611 double CpuTiAction::get_remains()
612 {
613   XBT_IN("(%p)", this);
614   cpu_->update_remaining_amount(EngineImpl::get_clock());
615   XBT_OUT();
616   return get_remains_no_update();
617 }
618
619 } // namespace simgrid::kernel::resource