Logo AND Algorithmique Numérique Distribuée

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