1 /* Copyright (c) 2004-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. */
6 #include "simgrid/kernel/resource/Model.hpp"
7 #include "src/kernel/lmm/maxmin.hpp"
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(resource);
15 Model::Model(Model::UpdateAlgo algo) : update_algorithm_(algo) {}
19 delete ready_action_set_;
20 delete running_action_set_;
21 delete failed_action_set_;
22 delete done_action_set_;
23 delete maxmin_system_;
26 Action::ModifiedSet* Model::get_modified_set() const
28 return maxmin_system_->modified_set_;
31 double Model::next_occuring_event(double now)
33 // FIXME: set the good function once and for all
34 if (update_algorithm_ == Model::UpdateAlgo::Lazy)
35 return next_occuring_event_lazy(now);
36 else if (update_algorithm_ == Model::UpdateAlgo::Full)
37 return next_occuring_event_full(now);
39 xbt_die("Invalid cpu update mechanism!");
42 double Model::next_occuring_event_lazy(double now)
44 XBT_DEBUG("Before share resources, the size of modified actions set is %zu", maxmin_system_->modified_set_->size());
45 maxmin_system_->lmm_solve();
46 XBT_DEBUG("After share resources, The size of modified actions set is %zu", maxmin_system_->modified_set_->size());
48 while (not maxmin_system_->modified_set_->empty()) {
49 Action* action = &(maxmin_system_->modified_set_->front());
50 maxmin_system_->modified_set_->pop_front();
51 bool max_duration_flag = false;
53 if (action->get_state_set() != running_action_set_)
56 /* bogus priority, skip it */
57 if (action->get_priority() <= 0 || action->get_type() == ActionHeap::Type::latency)
60 action->update_remains_lazy(now);
63 double share = action->get_variable()->get_value();
66 double time_to_completion;
67 if (action->get_remains() > 0) {
68 time_to_completion = action->get_remains_no_update() / share;
70 time_to_completion = 0.0;
72 min = now + time_to_completion; // when the task will complete if nothing changes
75 if ((action->get_max_duration() > NO_MAX_DURATION) &&
76 (min <= -1 || action->get_start_time() + action->get_max_duration() < min)) {
77 // when the task will complete anyway because of the deadline if any
78 min = action->get_start_time() + action->get_max_duration();
79 max_duration_flag = true;
82 XBT_DEBUG("Action(%p) corresponds to variable %d", action, action->get_variable()->id_int);
84 XBT_DEBUG("Action(%p) Start %f. May finish at %f (got a share of %f). Max_duration %f", action,
85 action->get_start_time(), min, share, action->get_max_duration());
88 action_heap_.update(action, min, max_duration_flag ? ActionHeap::Type::max_duration : ActionHeap::Type::normal);
89 XBT_DEBUG("Insert at heap action(%p) min %f now %f", action, min, now);
94 // hereafter must have already the min value for this resource model
95 if (not action_heap_.empty()) {
96 double min = action_heap_.top_date() - now;
97 XBT_DEBUG("minimum with the HEAP %f", min);
100 XBT_DEBUG("The HEAP is empty, thus returning -1");
105 double Model::next_occuring_event_full(double /*now*/)
107 maxmin_system_->solve();
111 for (Action& action : *get_running_action_set()) {
112 double value = action.get_variable()->get_value();
114 if (action.get_remains() > 0)
115 value = action.get_remains_no_update() / value;
118 if (min < 0 || value < min) {
120 XBT_DEBUG("Updating min (value) with %p: %f", &action, min);
123 if ((action.get_max_duration() >= 0) && (min < 0 || action.get_max_duration() < min)) {
124 min = action.get_max_duration();
125 XBT_DEBUG("Updating min (duration) with %p: %f", &action, min);
128 XBT_DEBUG("min value : %f", min);
133 void Model::update_actions_state(double now, double delta)
135 if (update_algorithm_ == Model::UpdateAlgo::Full)
136 update_actions_state_full(now, delta);
137 else if (update_algorithm_ == Model::UpdateAlgo::Lazy)
138 update_actions_state_lazy(now, delta);
140 xbt_die("Invalid cpu update mechanism!");
143 void Model::update_actions_state_lazy(double /*now*/, double /*delta*/)
148 void Model::update_actions_state_full(double /*now*/, double /*delta*/)
154 } // namespace simgrid
155 } // namespace simgrid