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(const std::string& name) : name_(name)
20 default; // Don't move this declaration to the header, or it will break external projects such as SimGrid-FMI
22 Model* Model::set_update_algorithm(Model::UpdateAlgo algo)
24 update_algorithm_ = algo;
28 Action::ModifiedSet* Model::get_modified_set() const
30 return maxmin_system_->modified_set_.get();
33 void Model::set_maxmin_system(lmm::System* system)
35 maxmin_system_.release(); // ugly...
36 maxmin_system_.reset(system);
39 double Model::next_occurring_event(double now)
41 // FIXME: set the good function once and for all
42 if (update_algorithm_ == Model::UpdateAlgo::LAZY)
43 return next_occurring_event_lazy(now);
44 else if (update_algorithm_ == Model::UpdateAlgo::FULL)
45 return next_occurring_event_full(now);
47 xbt_die("Invalid cpu update mechanism!");
50 double Model::next_occurring_event_lazy(double now)
52 XBT_DEBUG("Before share resources, the size of modified actions set is %zu", maxmin_system_->modified_set_->size());
53 maxmin_system_->lmm_solve();
54 XBT_DEBUG("After share resources, The size of modified actions set is %zu", maxmin_system_->modified_set_->size());
56 while (not maxmin_system_->modified_set_->empty()) {
57 Action* action = &(maxmin_system_->modified_set_->front());
58 maxmin_system_->modified_set_->pop_front();
59 ActionHeap::Type action_type = ActionHeap::Type::normal;
61 if (action->get_state_set() != &started_action_set_)
64 /* bogus priority, skip it */
65 if (action->get_sharing_penalty() <= 0 || action->get_type() == ActionHeap::Type::latency)
68 action->update_remains_lazy(now);
71 double share = action->get_variable()->get_value();
74 double time_to_completion;
75 if (action->get_remains() > 0) {
76 time_to_completion = action->get_remains_no_update() / share;
78 time_to_completion = 0.0;
80 min = now + time_to_completion; // when the task will complete if nothing changes
83 if ((action->get_max_duration() != NO_MAX_DURATION) &&
84 (min <= -1 || action->get_start_time() + action->get_max_duration() < min)) {
85 // when the task will complete anyway because of the deadline if any
86 min = action->get_start_time() + action->get_max_duration();
87 action_type = ActionHeap::Type::max_duration;
90 XBT_DEBUG("Action(%p) corresponds to variable %d", action, action->get_variable()->rank_);
92 XBT_DEBUG("Action(%p) Start %f. May finish at %f (got a share of %f). Max_duration %f", action,
93 action->get_start_time(), min, share, action->get_max_duration());
96 action_heap_.update(action, min, action_type);
97 XBT_DEBUG("Insert at heap action(%p) min %f now %f", action, min, now);
102 // hereafter must have already the min value for this resource model
103 if (not action_heap_.empty()) {
104 double min = action_heap_.top_date() - now;
105 XBT_DEBUG("minimum with the HEAP %f", min);
108 XBT_DEBUG("The HEAP is empty, thus returning -1");
113 double Model::next_occurring_event_full(double /*now*/)
115 maxmin_system_->solve();
119 for (Action& action : *get_started_action_set()) {
120 double value = action.get_variable()->get_value();
122 if (action.get_remains() > 0)
123 value = action.get_remains_no_update() / value;
126 if (min < 0 || value < min) {
128 XBT_DEBUG("Updating min (value) with %p: %f", &action, min);
131 if ((action.get_max_duration() >= 0) && (min < 0 || action.get_max_duration() < min)) {
132 min = action.get_max_duration();
133 XBT_DEBUG("Updating min (duration) with %p: %f", &action, min);
136 XBT_DEBUG("min value : %f", min);
141 void Model::update_actions_state(double now, double delta)
143 if (update_algorithm_ == Model::UpdateAlgo::FULL)
144 update_actions_state_full(now, delta);
145 else if (update_algorithm_ == Model::UpdateAlgo::LAZY)
146 update_actions_state_lazy(now, delta);
148 xbt_die("Invalid cpu update mechanism!");
151 /** Pops and returns the first action of that state set (or nullptr if none exist) */
152 Action* Model::extract_action(Action::StateSet* list)
156 Action* res = &list->front();
161 /** Pops and returns the first finished action (or nullptr if none exist) */
162 Action* Model::extract_done_action()
164 return extract_action(get_finished_action_set());
167 /** Pops and returns the failed finished action (or nullptr if none exist) */
168 Action* Model::extract_failed_action()
170 return extract_action(get_failed_action_set());
173 void Model::update_actions_state_lazy(double /*now*/, double /*delta*/)
178 void Model::update_actions_state_full(double /*now*/, double /*delta*/)
183 } // namespace resource
184 } // namespace kernel
185 } // namespace simgrid