Logo AND Algorithmique Numérique Distribuée

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