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);
16 default; // Don't move this declaration to the header, or it will break external projects such as SimGrid-FMI
18 Model* Model::set_update_algorithm(Model::UpdateAlgo algo)
20 update_algorithm_ = algo;
24 Action::ModifiedSet* Model::get_modified_set() const
26 return maxmin_system_->modified_set_.get();
29 void Model::set_maxmin_system(lmm::System* system)
31 maxmin_system_.release(); // ugly...
32 maxmin_system_.reset(system);
35 double Model::next_occurring_event(double now)
37 // FIXME: set the good function once and for all
38 if (update_algorithm_ == Model::UpdateAlgo::LAZY)
39 return next_occurring_event_lazy(now);
40 else if (update_algorithm_ == Model::UpdateAlgo::FULL)
41 return next_occurring_event_full(now);
43 xbt_die("Invalid cpu update mechanism!");
46 double Model::next_occurring_event_lazy(double now)
48 XBT_DEBUG("Before share resources, the size of modified actions set is %zu", maxmin_system_->modified_set_->size());
49 maxmin_system_->lmm_solve();
50 XBT_DEBUG("After share resources, The size of modified actions set is %zu", maxmin_system_->modified_set_->size());
52 while (not maxmin_system_->modified_set_->empty()) {
53 Action* action = &(maxmin_system_->modified_set_->front());
54 maxmin_system_->modified_set_->pop_front();
55 ActionHeap::Type action_type = ActionHeap::Type::normal;
57 if (action->get_state_set() != &started_action_set_)
60 /* bogus priority, skip it */
61 if (action->get_sharing_penalty() <= 0 || action->get_type() == ActionHeap::Type::latency)
64 action->update_remains_lazy(now);
67 double share = action->get_variable()->get_value();
70 double time_to_completion;
71 if (action->get_remains() > 0) {
72 time_to_completion = action->get_remains_no_update() / share;
74 time_to_completion = 0.0;
76 min = now + time_to_completion; // when the task will complete if nothing changes
79 if ((action->get_max_duration() != NO_MAX_DURATION) &&
80 (min <= -1 || action->get_start_time() + action->get_max_duration() < min)) {
81 // when the task will complete anyway because of the deadline if any
82 min = action->get_start_time() + action->get_max_duration();
83 action_type = ActionHeap::Type::max_duration;
86 XBT_DEBUG("Action(%p) corresponds to variable %d", action, action->get_variable()->rank_);
88 XBT_DEBUG("Action(%p) Start %f. May finish at %f (got a share of %f). Max_duration %f", action,
89 action->get_start_time(), min, share, action->get_max_duration());
92 action_heap_.update(action, min, action_type);
93 XBT_DEBUG("Insert at heap action(%p) min %f now %f", action, min, now);
98 // hereafter must have already the min value for this resource model
99 if (not action_heap_.empty()) {
100 double min = action_heap_.top_date() - now;
101 XBT_DEBUG("minimum with the HEAP %f", min);
104 XBT_DEBUG("The HEAP is empty, thus returning -1");
109 double Model::next_occurring_event_full(double /*now*/)
111 maxmin_system_->solve();
115 for (Action& action : *get_started_action_set()) {
116 double value = action.get_variable()->get_value();
118 if (action.get_remains() > 0)
119 value = action.get_remains_no_update() / value;
122 if (min < 0 || value < min) {
124 XBT_DEBUG("Updating min (value) with %p: %f", &action, min);
127 if ((action.get_max_duration() >= 0) && (min < 0 || action.get_max_duration() < min)) {
128 min = action.get_max_duration();
129 XBT_DEBUG("Updating min (duration) with %p: %f", &action, min);
132 XBT_DEBUG("min value : %f", min);
137 void Model::update_actions_state(double now, double delta)
139 if (update_algorithm_ == Model::UpdateAlgo::FULL)
140 update_actions_state_full(now, delta);
141 else if (update_algorithm_ == Model::UpdateAlgo::LAZY)
142 update_actions_state_lazy(now, delta);
144 xbt_die("Invalid cpu update mechanism!");
147 /** Pops and returns the first action of that state set (or nullptr if none exist) */
148 Action* Model::extract_action(Action::StateSet* list)
152 Action* res = &list->front();
157 /** Pops and returns the first finished action (or nullptr if none exist) */
158 Action* Model::extract_done_action()
160 return extract_action(get_finished_action_set());
163 /** Pops and returns the failed finished action (or nullptr if none exist) */
164 Action* Model::extract_failed_action()
166 return extract_action(get_failed_action_set());
169 void Model::update_actions_state_lazy(double /*now*/, double /*delta*/)
174 void Model::update_actions_state_full(double /*now*/, double /*delta*/)
179 Model* Model::set_name(const std::string& name)
185 } // namespace resource
186 } // namespace kernel
187 } // namespace simgrid