Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
No more types for models.
[simgrid.git] / src / kernel / resource / Model.cpp
1 /* Copyright (c) 2004-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "simgrid/kernel/resource/Model.hpp"
7 #include "src/kernel/lmm/maxmin.hpp"
8
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ker_resource);
10
11 namespace simgrid {
12 namespace kernel {
13 namespace resource {
14
15 Model::~Model() =
16     default; // Don't move this declaration to the header, or it will break external projects such as SimGrid-FMI
17
18 Model* Model::set_update_algorithm(Model::UpdateAlgo algo)
19 {
20   update_algorithm_ = algo;
21   return this;
22 }
23
24 Action::ModifiedSet* Model::get_modified_set() const
25 {
26   return maxmin_system_->modified_set_.get();
27 }
28
29 void Model::set_maxmin_system(lmm::System* system)
30 {
31   maxmin_system_.release(); // ugly...
32   maxmin_system_.reset(system);
33 }
34
35 double Model::next_occurring_event(double now)
36 {
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);
42   else
43     xbt_die("Invalid cpu update mechanism!");
44 }
45
46 double Model::next_occurring_event_lazy(double now)
47 {
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());
51
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;
56
57     if (action->get_state_set() != &started_action_set_)
58       continue;
59
60     /* bogus priority, skip it */
61     if (action->get_sharing_penalty() <= 0 || action->get_type() == ActionHeap::Type::latency)
62       continue;
63
64     action->update_remains_lazy(now);
65
66     double min   = -1;
67     double share = action->get_variable()->get_value();
68
69     if (share > 0) {
70       double time_to_completion;
71       if (action->get_remains() > 0) {
72         time_to_completion = action->get_remains_no_update() / share;
73       } else {
74         time_to_completion = 0.0;
75       }
76       min = now + time_to_completion; // when the task will complete if nothing changes
77     }
78
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;
84     }
85
86     XBT_DEBUG("Action(%p) corresponds to variable %d", action, action->get_variable()->rank_);
87
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());
90
91     if (min > -1) {
92       action_heap_.update(action, min, action_type);
93       XBT_DEBUG("Insert at heap action(%p) min %f now %f", action, min, now);
94     } else
95       DIE_IMPOSSIBLE;
96   }
97
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);
102     return min;
103   } else {
104     XBT_DEBUG("The HEAP is empty, thus returning -1");
105     return -1;
106   }
107 }
108
109 double Model::next_occurring_event_full(double /*now*/)
110 {
111   maxmin_system_->solve();
112
113   double min = -1;
114
115   for (Action& action : *get_started_action_set()) {
116     double value = action.get_variable()->get_value();
117     if (value > 0) {
118       if (action.get_remains() > 0)
119         value = action.get_remains_no_update() / value;
120       else
121         value = 0.0;
122       if (min < 0 || value < min) {
123         min = value;
124         XBT_DEBUG("Updating min (value) with %p: %f", &action, min);
125       }
126     }
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);
130     }
131   }
132   XBT_DEBUG("min value : %f", min);
133
134   return min;
135 }
136
137 void Model::update_actions_state(double now, double delta)
138 {
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);
143   else
144     xbt_die("Invalid cpu update mechanism!");
145 }
146
147 /** Pops and returns the first action of that state set (or nullptr if none exist) */
148 Action* Model::extract_action(Action::StateSet* list)
149 {
150   if (list->empty())
151     return nullptr;
152   Action* res = &list->front();
153   list->pop_front();
154   return res;
155 }
156
157 /** Pops and returns the first finished action (or nullptr if none exist) */
158 Action* Model::extract_done_action()
159 {
160   return extract_action(get_finished_action_set());
161 }
162
163 /** Pops and returns the failed finished action (or nullptr if none exist) */
164 Action* Model::extract_failed_action()
165 {
166   return extract_action(get_failed_action_set());
167 }
168
169 void Model::update_actions_state_lazy(double /*now*/, double /*delta*/)
170 {
171   THROW_UNIMPLEMENTED;
172 }
173
174 void Model::update_actions_state_full(double /*now*/, double /*delta*/)
175 {
176   THROW_UNIMPLEMENTED;
177 }
178
179 Model* Model::set_name(const std::string& name)
180 {
181   name_ = name;
182   return this;
183 }
184
185 } // namespace resource
186 } // namespace kernel
187 } // namespace simgrid