Logo AND Algorithmique Numérique Distribuée

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