Logo AND Algorithmique Numérique Distribuée

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