1 /* Copyright (c) 2013-2018. The SimGrid Team. All rights reserved. */
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. */
7 #include "src/surf/surf_interface.hpp"
8 #include "src/surf/trace_mgr.hpp"
9 #include "surf/surf.hpp"
11 #ifndef SURF_MODEL_CPUTI_H_
12 #define SURF_MODEL_CPUTI_H_
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_ti, surf_cpu, "Logging specific to the SURF CPU TRACE INTEGRATION module");
23 CpuTiTrace::CpuTiTrace(tmgr_trace_t speedTrace)
28 nb_points_ = speedTrace->event_list.size() + 1;
29 time_points_ = new double[nb_points_];
30 integral_ = new double[nb_points_];
31 for (auto const& val : speedTrace->event_list) {
32 time_points_[i] = time;
33 integral_[i] = integral;
34 integral += val.date_ * val.value_;
38 time_points_[i] = time;
39 integral_[i] = integral;
42 CpuTiTrace::~CpuTiTrace()
44 delete[] time_points_;
48 CpuTiTmgr::~CpuTiTmgr()
54 * \brief Integrate trace
56 * Wrapper around surf_cpu_integrate_trace_simple() to get
59 * \param a Begin of interval
60 * \param b End of interval
61 * \return the integrate value. -1 if an error occurs.
63 double CpuTiTmgr::integrate(double a, double b)
67 if ((a < 0.0) || (a > b)) {
68 xbt_die("Error, invalid integration interval [%.2f,%.2f]. "
69 "You probably have a task executing with negative computation amount. Check your code.", a, b);
71 if (fabs(a -b) < EPSILON)
74 if (type_ == TRACE_FIXED) {
75 return ((b - a) * value_);
78 if (fabs(ceil(a / last_time_) - a / last_time_) < EPSILON)
79 a_index = 1 + static_cast<int>(ceil(a / last_time_));
81 a_index = static_cast<int>(ceil(a / last_time_));
83 int b_index = static_cast<int>(floor(b / last_time_));
85 if (a_index > b_index) { /* Same chunk */
86 return trace_->integrate_simple(a - (a_index - 1) * last_time_, b - (b_index)*last_time_);
89 double first_chunk = trace_->integrate_simple(a - (a_index - 1) * last_time_, last_time_);
90 double middle_chunk = (b_index - a_index) * total_;
91 double last_chunk = trace_->integrate_simple(0.0, b - (b_index)*last_time_);
93 XBT_DEBUG("first_chunk=%.2f middle_chunk=%.2f last_chunk=%.2f\n", first_chunk, middle_chunk, last_chunk);
95 return (first_chunk + middle_chunk + last_chunk);
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
104 double CpuTiTrace::integrate_simple(double a, double b)
106 return integrate_simple_point(b) - integrate_simple_point(a);
110 * \brief Auxiliary function to compute the integral at point a.
113 double CpuTiTrace::integrate_simple_point(double a)
117 int ind = binary_search(time_points_, a, 0, nb_points_ - 1);
118 integral += integral_[ind];
120 XBT_DEBUG("a %f ind %d 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_maxmin_precision * sg_surf_precision);
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);
132 * \brief Computes the time needed to execute "amount" on cpu.
134 * Here, amount can span multiple trace periods
136 * \param a Initial time
137 * \param amount Amount to be executed
140 double CpuTiTmgr::solve(double a, double amount)
142 /* Fix very small negative numbers */
143 if ((a < 0.0) && (a > -EPSILON)) {
146 if ((amount < 0.0) && (amount > -EPSILON)) {
151 if ((a < 0.0) || (amount < 0.0)) {
152 XBT_CRITICAL ("Error, invalid parameters [a = %.2f, amount = %.2f]. "
153 "You probably have a task executing with negative computation amount. Check your code.", a, amount);
157 /* At this point, a and amount are positive */
158 if (amount < EPSILON)
161 /* Is the trace fixed ? */
162 if (type_ == TRACE_FIXED) {
163 return (a + (amount / value_));
166 XBT_DEBUG("amount %f total %f", amount, total_);
167 /* Reduce the problem to one where amount <= trace_total */
168 int quotient = static_cast<int>(floor(amount / total_));
169 double reduced_amount = (total_) * ((amount / total_) - floor(amount / total_));
170 double reduced_a = a - (last_time_) * static_cast<int>(floor(a / last_time_));
172 XBT_DEBUG("Quotient: %d reduced_amount: %f reduced_a: %f", quotient, reduced_amount, reduced_a);
174 /* Now solve for new_amount which is <= trace_total */
176 XBT_DEBUG("Solve integral: [%.2f, amount=%.2f]", reduced_a, reduced_amount);
177 double amount_till_end = integrate(reduced_a, last_time_);
179 if (amount_till_end > reduced_amount) {
180 reduced_b = trace_->solve_simple(reduced_a, reduced_amount);
182 reduced_b = last_time_ + trace_->solve_simple(0.0, reduced_amount - amount_till_end);
185 /* Re-map to the original b and amount */
186 return (last_time_) * static_cast<int>(floor(a / last_time_)) + (quotient * last_time_) + reduced_b;
190 * \brief Auxiliary function to solve integral.
191 * It returns the date when the requested amount of flops is available
192 * \param a Initial point
193 * \param amount Amount of flops
194 * \return The date when amount is available.
196 double CpuTiTrace::solve_simple(double a, double amount)
198 double integral_a = integrate_simple_point(a);
199 int ind = binary_search(integral_, integral_a + amount, 0, nb_points_ - 1);
200 double time = time_points_[ind];
201 time += (integral_a + amount - integral_[ind]) /
202 ((integral_[ind + 1] - integral_[ind]) / (time_points_[ind + 1] - time_points_[ind]));
208 * \brief Auxiliary function to update the CPU speed scale.
210 * This function uses the trace structure to return the speed scale at the determined time a.
212 * \return CPU speed scale
214 double CpuTiTmgr::get_power_scale(double a)
216 double reduced_a = a - floor(a / last_time_) * last_time_;
217 int point = trace_->binary_search(trace_->time_points_, reduced_a, 0, trace_->nb_points_ - 1);
218 trace_mgr::DatedValue val = speed_trace_->event_list.at(point);
223 * \brief Creates a new integration trace from a tmgr_trace_t
225 * \param speedTrace CPU availability trace
226 * \param value Percentage of CPU speed available (useful to fixed tracing)
227 * \return Integration trace structure
229 CpuTiTmgr::CpuTiTmgr(tmgr_trace_t speedTrace, double value) : speed_trace_(speedTrace)
231 double total_time = 0.0;
234 /* no availability file, fixed trace */
235 if (not speedTrace) {
238 XBT_DEBUG("No availability trace. Constant value = %f", value);
242 /* only one point available, fixed trace */
243 if (speedTrace->event_list.size() == 1) {
244 trace_mgr::DatedValue val = speedTrace->event_list.front();
250 type_ = TRACE_DYNAMIC;
252 /* count the total time of trace file */
253 for (auto const& val : speedTrace->event_list)
254 total_time += val.date_;
256 trace_ = new CpuTiTrace(speedTrace);
257 last_time_ = total_time;
258 total_ = trace_->integrate_simple(0, total_time);
260 XBT_DEBUG("Total integral %f, last_time %f ", total_, last_time_);
264 * \brief Binary search in array.
265 * It returns the first point of the interval in which "a" is.
267 * \param a Value to search
268 * \param low Low bound to search in array
269 * \param high Upper bound to search in array
270 * \return Index of point
272 int CpuTiTrace::binary_search(double* array, double a, int low, int high)
274 xbt_assert(low < high, "Wrong parameters: low (%d) should be smaller than high (%d)", low, high);
277 int mid = low + (high - low) / 2;
278 XBT_DEBUG("a %f low %d high %d mid %d value %f", a, low, high, mid, array[mid]);
285 while (low < high - 1);
297 void surf_cpu_model_init_ti()
299 xbt_assert(not surf_cpu_model_pm, "CPU model already initialized. This should not happen.");
300 xbt_assert(not surf_cpu_model_vm, "CPU model already initialized. This should not happen.");
302 surf_cpu_model_pm = new simgrid::surf::CpuTiModel();
303 all_existing_models->push_back(surf_cpu_model_pm);
305 surf_cpu_model_vm = new simgrid::surf::CpuTiModel();
306 all_existing_models->push_back(surf_cpu_model_vm);
312 CpuTiModel::~CpuTiModel()
314 surf_cpu_model_pm = nullptr;
317 Cpu *CpuTiModel::createCpu(simgrid::s4u::Host *host, std::vector<double>* speedPerPstate, int core)
319 return new CpuTi(this, host, speedPerPstate, core);
322 double CpuTiModel::next_occuring_event(double now)
324 double min_action_duration = -1;
326 /* iterates over modified cpus to update share resources */
327 for (auto it = std::begin(modified_cpus_); it != std::end(modified_cpus_);) {
329 ++it; // increment iterator here since the following call to ti.updateActionsFinishTime() may invalidate it
330 ti.update_actions_finish_time(now);
333 /* get the min next event if heap not empty */
334 if (not get_action_heap().empty())
335 min_action_duration = get_action_heap().top_date() - now;
337 XBT_DEBUG("Share resources, min next event date: %f", min_action_duration);
339 return min_action_duration;
342 void CpuTiModel::update_actions_state(double now, double /*delta*/)
344 while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_surf_precision)) {
345 CpuTiAction* action = static_cast<CpuTiAction*>(get_action_heap().pop());
346 XBT_DEBUG("Action %p: finish", action);
347 action->finish(kernel::resource::Action::State::done);
348 /* update remaining amount of all actions */
349 action->cpu_->update_remaining_amount(surf_get_clock());
356 CpuTi::CpuTi(CpuTiModel *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
357 : Cpu(model, host, speedPerPstate, core)
359 xbt_assert(core == 1, "Multi-core not handled by this model yet");
361 speed_.peak = speedPerPstate->front();
362 XBT_DEBUG("CPU create: peak=%f", speed_.peak);
364 speed_integrated_trace_ = new CpuTiTmgr(nullptr, 1 /*scale*/);
370 delete speed_integrated_trace_;
372 void CpuTi::set_speed_trace(tmgr_trace_t trace)
374 delete speed_integrated_trace_;
375 speed_integrated_trace_ = new CpuTiTmgr(trace, speed_.scale);
377 /* add a fake trace event if periodicity == 0 */
378 if (trace && trace->event_list.size() > 1) {
379 trace_mgr::DatedValue val = trace->event_list.back();
380 if (val.date_ < 1e-12)
381 speed_.event = future_evt_set->add_trace(new simgrid::trace_mgr::trace(), this);
385 void CpuTi::apply_event(tmgr_trace_event_t event, double value)
387 if (event == speed_.event) {
388 tmgr_trace_t speedTrace;
391 XBT_DEBUG("Finish trace date: value %f", value);
392 /* update remaining of actions and put in modified cpu list */
393 update_remaining_amount(surf_get_clock());
397 speedTrace = speed_integrated_trace_->speed_trace_;
398 trace_mgr::DatedValue val = speedTrace->event_list.back();
399 delete speed_integrated_trace_;
400 speed_.scale = val.value_;
402 trace = new CpuTiTmgr(TRACE_FIXED, val.value_);
403 XBT_DEBUG("value %f", val.value_);
405 speed_integrated_trace_ = trace;
407 tmgr_trace_event_unref(&speed_.event);
409 } else if (event == stateEvent_) {
412 host_that_restart.push_back(getHost());
416 double date = surf_get_clock();
418 /* put all action running on cpu to failed */
419 for (CpuTiAction& action : action_set_) {
420 if (action.get_state() == kernel::resource::Action::State::running ||
421 action.get_state() == kernel::resource::Action::State::ready ||
422 action.get_state() == kernel::resource::Action::State::not_in_the_system) {
423 action.set_finish_time(date);
424 action.set_state(kernel::resource::Action::State::failed);
425 get_model()->get_action_heap().remove(&action);
429 tmgr_trace_event_unref(&stateEvent_);
432 xbt_die("Unknown event!\n");
436 /** Update the actions that are running on this CPU (which was modified recently) */
437 void CpuTi::update_actions_finish_time(double now)
439 /* update remaining amount of actions */
440 update_remaining_amount(now);
442 /* Compute the sum of priorities for the actions running on that CPU */
444 for (CpuTiAction const& action : action_set_) {
445 /* action not running, skip it */
446 if (action.get_state_set() != surf_cpu_model_pm->get_running_action_set())
449 /* bogus priority, skip it */
450 if (action.get_priority() <= 0)
453 /* action suspended, skip it */
454 if (action.suspended_ != kernel::resource::Action::SuspendStates::not_suspended)
457 sum_priority_ += 1.0 / action.get_priority();
460 for (CpuTiAction& action : action_set_) {
461 double min_finish = -1;
462 /* action not running, skip it */
463 if (action.get_state_set() != surf_cpu_model_pm->get_running_action_set())
466 /* verify if the action is really running on cpu */
467 if (action.suspended_ == kernel::resource::Action::SuspendStates::not_suspended && action.get_priority() > 0) {
468 /* total area needed to finish the action. Used in trace integration */
469 double total_area = (action.get_remains() * sum_priority_ * action.get_priority()) / speed_.peak;
471 action.set_finish_time(speed_integrated_trace_->solve(now, total_area));
472 /* verify which event will happen before (max_duration or finish time) */
473 if (action.get_max_duration() > NO_MAX_DURATION &&
474 action.get_start_time() + action.get_max_duration() < action.get_finish_time())
475 min_finish = action.get_start_time() + action.get_max_duration();
477 min_finish = action.get_finish_time();
479 /* put the max duration time on heap */
480 if (action.get_max_duration() > NO_MAX_DURATION)
481 min_finish = action.get_start_time() + action.get_max_duration();
483 /* add in action heap */
484 if (min_finish > NO_MAX_DURATION)
485 get_model()->get_action_heap().update(&action, min_finish, kernel::resource::ActionHeap::Type::unset);
487 get_model()->get_action_heap().remove(&action);
489 XBT_DEBUG("Update finish time: Cpu(%s) Action: %p, Start Time: %f Finish Time: %f Max duration %f", get_cname(),
490 &action, action.get_start_time(), action.get_finish_time(), action.get_max_duration());
492 /* remove from modified cpu */
496 bool CpuTi::is_used()
498 return not action_set_.empty();
501 double CpuTi::get_available_speed()
503 speed_.scale = speed_integrated_trace_->get_power_scale(surf_get_clock());
504 return Cpu::get_available_speed();
507 /** @brief Update the remaining amount of actions */
508 void CpuTi::update_remaining_amount(double now)
511 /* already updated */
512 if (last_update_ >= now)
515 /* compute the integration area */
516 double area_total = speed_integrated_trace_->integrate(last_update_, now) * speed_.peak;
517 XBT_DEBUG("Flops total: %f, Last update %f", area_total, last_update_);
518 for (CpuTiAction& action : action_set_) {
519 /* action not running, skip it */
520 if (action.get_state_set() != get_model()->get_running_action_set())
523 /* bogus priority, skip it */
524 if (action.get_priority() <= 0)
527 /* action suspended, skip it */
528 if (action.suspended_ != kernel::resource::Action::SuspendStates::not_suspended)
531 /* action don't need update */
532 if (action.get_start_time() >= now)
535 /* skip action that are finishing now */
536 if (action.get_finish_time() >= 0 && action.get_finish_time() <= now)
539 /* update remaining */
540 action.update_remains(area_total / (sum_priority_ * action.get_priority()));
541 XBT_DEBUG("Update remaining action(%p) remaining %f", &action, action.get_remains_no_update());
546 CpuAction *CpuTi::execution_start(double size)
548 XBT_IN("(%s,%g)", get_cname(), size);
549 CpuTiAction* action = new CpuTiAction(static_cast<CpuTiModel*>(get_model()), size, is_off(), this);
551 action_set_.push_back(*action);
558 CpuAction *CpuTi::sleep(double duration)
561 duration = std::max(duration, sg_surf_precision);
563 XBT_IN("(%s,%g)", get_cname(), duration);
564 CpuTiAction* action = new CpuTiAction(static_cast<CpuTiModel*>(get_model()), 1.0, is_off(), this);
566 action->set_max_duration(duration);
567 action->suspended_ = kernel::resource::Action::SuspendStates::sleeping;
568 if (duration == NO_MAX_DURATION) {
569 /* Move to the *end* of the corresponding action set. This convention is used to speed up update_resource_state */
570 simgrid::xbt::intrusive_erase(*action->get_state_set(), *action);
571 action->state_set_ = &static_cast<CpuTiModel*>(get_model())->runningActionSetThatDoesNotNeedBeingChecked_;
572 action->get_state_set()->push_back(*action);
575 action_set_.push_back(*action);
581 void CpuTi::set_modified(bool modified)
583 CpuTiList& modifiedCpu = static_cast<CpuTiModel*>(get_model())->modified_cpus_;
585 if (not cpu_ti_hook.is_linked()) {
586 modifiedCpu.push_back(*this);
589 if (cpu_ti_hook.is_linked())
590 simgrid::xbt::intrusive_erase(modifiedCpu, *this);
598 CpuTiAction::CpuTiAction(CpuTiModel *model_, double cost, bool failed, CpuTi *cpu)
599 : CpuAction(model_, cost, failed)
602 cpu_->set_modified(true);
604 CpuTiAction::~CpuTiAction()
606 /* remove from action_set */
607 if (action_ti_hook.is_linked())
608 simgrid::xbt::intrusive_erase(cpu_->action_set_, *this);
609 /* remove from heap */
610 get_model()->get_action_heap().remove(this);
611 cpu_->set_modified(true);
614 void CpuTiAction::set_state(Action::State state)
616 CpuAction::set_state(state);
617 cpu_->set_modified(true);
620 void CpuTiAction::cancel()
622 this->set_state(Action::State::failed);
623 get_model()->get_action_heap().remove(this);
624 cpu_->set_modified(true);
627 void CpuTiAction::suspend()
629 XBT_IN("(%p)", this);
630 if (suspended_ != Action::SuspendStates::sleeping) {
631 suspended_ = Action::SuspendStates::suspended;
632 get_model()->get_action_heap().remove(this);
633 cpu_->set_modified(true);
638 void CpuTiAction::resume()
640 XBT_IN("(%p)", this);
641 if (suspended_ != Action::SuspendStates::sleeping) {
642 suspended_ = Action::SuspendStates::not_suspended;
643 cpu_->set_modified(true);
648 void CpuTiAction::set_max_duration(double duration)
652 XBT_IN("(%p,%g)", this, duration);
654 Action::set_max_duration(duration);
657 min_finish = (get_start_time() + get_max_duration()) < get_finish_time() ? (get_start_time() + get_max_duration())
660 min_finish = get_finish_time();
662 /* add in action heap */
663 get_model()->get_action_heap().update(this, min_finish, kernel::resource::ActionHeap::Type::unset);
668 void CpuTiAction::set_priority(double priority)
670 XBT_IN("(%p,%g)", this, priority);
671 set_priority_no_update(priority);
672 cpu_->set_modified(true);
676 double CpuTiAction::get_remains()
678 XBT_IN("(%p)", this);
679 cpu_->update_remaining_amount(surf_get_clock());
681 return get_remains_no_update();
687 #endif /* SURF_MODEL_CPUTI_H_ */