Logo AND Algorithmique Numérique Distribuée

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