Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move common part for selective_update to System class.
[simgrid.git] / src / kernel / resource / Model.cpp
1 /* Copyright (c) 2004-2022. 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(const std::string& name) : name_(name)
16 {
17 }
18
19 Model::~Model() =
20     default; // Don't move this declaration to the header, or it will break external projects such as SimGrid-FMI
21
22 Model* Model::set_update_algorithm(Model::UpdateAlgo algo)
23 {
24   update_algorithm_ = algo;
25   return this;
26 }
27
28 Action::ModifiedSet* Model::get_modified_set() const
29 {
30   return maxmin_system_->get_modified_action_set();
31 }
32
33 void Model::set_maxmin_system(lmm::System* system)
34 {
35   maxmin_system_.release(); // ugly...
36   maxmin_system_.reset(system);
37 }
38
39 double Model::next_occurring_event(double now)
40 {
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);
46   else
47     xbt_die("Invalid cpu update mechanism!");
48 }
49
50 double Model::next_occurring_event_lazy(double now)
51 {
52   XBT_DEBUG("Before share resources, the size of modified actions set is %zu",
53             maxmin_system_->get_modified_action_set()->size());
54   maxmin_system_->solve();
55   Action::ModifiedSet* modified_action_set = maxmin_system_->get_modified_action_set();
56   XBT_DEBUG("After share resources, The size of modified actions set is %zu", modified_action_set->size());
57
58   while (not modified_action_set->empty()) {
59     Action* action = &(modified_action_set->front());
60     modified_action_set->pop_front();
61     ActionHeap::Type action_type = ActionHeap::Type::normal;
62
63     if (action->get_state_set() != &started_action_set_)
64       continue;
65
66     /* bogus priority, skip it */
67     if (action->get_sharing_penalty() <= 0 || action->get_type() == ActionHeap::Type::latency)
68       continue;
69
70     action->update_remains_lazy(now);
71
72     double min   = -1;
73     double share = action->get_rate();
74
75     if (share > 0) {
76       double time_to_completion;
77       if (action->get_remains() > 0) {
78         time_to_completion = action->get_remains_no_update() / share;
79       } else {
80         time_to_completion = 0.0;
81       }
82       min = now + time_to_completion; // when the task will complete if nothing changes
83     }
84
85     if ((action->get_max_duration() != NO_MAX_DURATION) &&
86         (min <= -1 || action->get_start_time() + action->get_max_duration() < min)) {
87       // when the task will complete anyway because of the deadline if any
88       min         = action->get_start_time() + action->get_max_duration();
89       action_type = ActionHeap::Type::max_duration;
90     }
91
92     XBT_DEBUG("Action(%p) corresponds to variable %d", action, action->get_variable()->rank_);
93
94     XBT_DEBUG("Action(%p) Start %f. May finish at %f (got a share of %f). Max_duration %f", action,
95               action->get_start_time(), min, share, action->get_max_duration());
96
97     if (min > -1) {
98       action_heap_.update(action, min, action_type);
99       XBT_DEBUG("Insert at heap action(%p) min %f now %f", action, min, now);
100     } else
101       DIE_IMPOSSIBLE;
102   }
103
104   // hereafter must have already the min value for this resource model
105   if (not action_heap_.empty()) {
106     double min = action_heap_.top_date() - now;
107     XBT_DEBUG("minimum with the HEAP %f", min);
108     return min;
109   } else {
110     XBT_DEBUG("The HEAP is empty, thus returning -1");
111     return -1;
112   }
113 }
114
115 double Model::next_occurring_event_full(double /*now*/)
116 {
117   maxmin_system_->solve();
118
119   double min = -1;
120
121   for (Action& action : *get_started_action_set()) {
122     double value = action.get_rate();
123     if (value > 0) {
124       if (action.get_remains() > 0)
125         value = action.get_remains_no_update() / value;
126       else
127         value = 0.0;
128       if (min < 0 || value < min) {
129         min = value;
130         XBT_DEBUG("Updating min (value) with %p: %f", &action, min);
131       }
132     }
133     if ((action.get_max_duration() >= 0) && (min < 0 || action.get_max_duration() < min)) {
134       min = action.get_max_duration();
135       XBT_DEBUG("Updating min (duration) with %p: %f", &action, min);
136     }
137   }
138   XBT_DEBUG("min value : %f", min);
139
140   return min;
141 }
142
143 void Model::update_actions_state(double now, double delta)
144 {
145   if (update_algorithm_ == Model::UpdateAlgo::FULL)
146     update_actions_state_full(now, delta);
147   else if (update_algorithm_ == Model::UpdateAlgo::LAZY)
148     update_actions_state_lazy(now, delta);
149   else
150     xbt_die("Invalid cpu update mechanism!");
151 }
152
153 /** Pops and returns the first action of that state set (or nullptr if none exist) */
154 Action* Model::extract_action(Action::StateSet* list)
155 {
156   if (list->empty())
157     return nullptr;
158   Action* res = &list->front();
159   list->pop_front();
160   return res;
161 }
162
163 /** Pops and returns the first finished action (or nullptr if none exist) */
164 Action* Model::extract_done_action()
165 {
166   return extract_action(get_finished_action_set());
167 }
168
169 /** Pops and returns the failed finished action (or nullptr if none exist) */
170 Action* Model::extract_failed_action()
171 {
172   return extract_action(get_failed_action_set());
173 }
174
175 void Model::update_actions_state_lazy(double /*now*/, double /*delta*/)
176 {
177   THROW_UNIMPLEMENTED;
178 }
179
180 void Model::update_actions_state_full(double /*now*/, double /*delta*/)
181 {
182   THROW_UNIMPLEMENTED;
183 }
184
185 } // namespace resource
186 } // namespace kernel
187 } // namespace simgrid