Logo AND Algorithmique Numérique Distribuée

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