1 /* Copyright (c) 2004-2021. 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(ker_resource);
15 Model::Model(Model::UpdateAlgo algo) : update_algorithm_(algo) {}
16 Model::~Model() = default; // Don't move this declaration to the header, or it will break external projects such as SimGrid-FMI
18 Action::ModifiedSet* Model::get_modified_set() const
20 return maxmin_system_->modified_set_.get();
23 void Model::set_maxmin_system(lmm::System* system)
25 maxmin_system_.release(); // ugly...
26 maxmin_system_.reset(system);
29 double Model::next_occurring_event(double now)
31 // FIXME: set the good function once and for all
32 if (update_algorithm_ == Model::UpdateAlgo::LAZY)
33 return next_occurring_event_lazy(now);
34 else if (update_algorithm_ == Model::UpdateAlgo::FULL)
35 return next_occurring_event_full(now);
37 xbt_die("Invalid cpu update mechanism!");
40 double Model::next_occurring_event_lazy(double now)
42 XBT_DEBUG("Before share resources, the size of modified actions set is %zu", maxmin_system_->modified_set_->size());
43 maxmin_system_->lmm_solve();
44 XBT_DEBUG("After share resources, The size of modified actions set is %zu", maxmin_system_->modified_set_->size());
46 while (not maxmin_system_->modified_set_->empty()) {
47 Action* action = &(maxmin_system_->modified_set_->front());
48 maxmin_system_->modified_set_->pop_front();
49 ActionHeap::Type action_type = ActionHeap::Type::normal;
51 if (action->get_state_set() != &started_action_set_)
54 /* bogus priority, skip it */
55 if (action->get_sharing_penalty() <= 0 || action->get_type() == ActionHeap::Type::latency)
58 action->update_remains_lazy(now);
61 double share = action->get_variable()->get_value();
64 double time_to_completion;
65 if (action->get_remains() > 0) {
66 time_to_completion = action->get_remains_no_update() / share;
68 time_to_completion = 0.0;
70 min = now + time_to_completion; // when the task will complete if nothing changes
73 if ((action->get_max_duration() != NO_MAX_DURATION) &&
74 (min <= -1 || action->get_start_time() + action->get_max_duration() < min)) {
75 // when the task will complete anyway because of the deadline if any
76 min = action->get_start_time() + action->get_max_duration();
77 action_type = ActionHeap::Type::max_duration;
80 XBT_DEBUG("Action(%p) corresponds to variable %d", action, action->get_variable()->rank_);
82 XBT_DEBUG("Action(%p) Start %f. May finish at %f (got a share of %f). Max_duration %f", action,
83 action->get_start_time(), min, share, action->get_max_duration());
86 action_heap_.update(action, min, action_type);
87 XBT_DEBUG("Insert at heap action(%p) min %f now %f", action, min, now);
92 // hereafter must have already the min value for this resource model
93 if (not action_heap_.empty()) {
94 double min = action_heap_.top_date() - now;
95 XBT_DEBUG("minimum with the HEAP %f", min);
98 XBT_DEBUG("The HEAP is empty, thus returning -1");
103 double Model::next_occurring_event_full(double /*now*/)
105 maxmin_system_->solve();
109 for (Action& action : *get_started_action_set()) {
110 double value = action.get_variable()->get_value();
112 if (action.get_remains() > 0)
113 value = action.get_remains_no_update() / value;
116 if (min < 0 || value < min) {
118 XBT_DEBUG("Updating min (value) with %p: %f", &action, min);
121 if ((action.get_max_duration() >= 0) && (min < 0 || action.get_max_duration() < min)) {
122 min = action.get_max_duration();
123 XBT_DEBUG("Updating min (duration) with %p: %f", &action, min);
126 XBT_DEBUG("min value : %f", min);
131 void Model::update_actions_state(double now, double delta)
133 if (update_algorithm_ == Model::UpdateAlgo::FULL)
134 update_actions_state_full(now, delta);
135 else if (update_algorithm_ == Model::UpdateAlgo::LAZY)
136 update_actions_state_lazy(now, delta);
138 xbt_die("Invalid cpu update mechanism!");
141 /** Pops and returns the first action of that state set (or nullptr if none exist) */
142 Action* Model::extract_action(Action::StateSet* list)
146 Action* res = &list->front();
151 /** Pops and returns the first finished action (or nullptr if none exist) */
152 Action* Model::extract_done_action()
154 return extract_action(get_finished_action_set());
157 /** Pops and returns the failed finished action (or nullptr if none exist) */
158 Action* Model::extract_failed_action()
160 return extract_action(get_failed_action_set());
163 void Model::update_actions_state_lazy(double /*now*/, double /*delta*/)
168 void Model::update_actions_state_full(double /*now*/, double /*delta*/)
173 } // namespace resource
174 } // namespace kernel
175 } // namespace simgrid