Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
these functions always take exactly the same parameter, thus inlining it
[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() = default;
16
17 Model::~Model()
18 {
19   delete ready_action_set_;
20   delete running_action_set_;
21   delete failed_action_set_;
22   delete done_action_set_;
23   delete maxmin_system_;
24 }
25
26 Action* Model::actionHeapPop()
27 {
28   Action* action = action_heap_.top().second;
29   action_heap_.pop();
30   action->clearHeapHandle();
31   return action;
32 }
33
34 Action::ModifiedSet* Model::getModifiedSet() const
35 {
36   return maxmin_system_->modified_set_;
37 }
38
39 double Model::nextOccuringEvent(double now)
40 {
41   // FIXME: set the good function once and for all
42   if (update_mechanism_ == UM_LAZY)
43     return nextOccuringEventLazy(now);
44   else if (update_mechanism_ == UM_FULL)
45     return nextOccuringEventFull(now);
46   else
47     xbt_die("Invalid cpu update mechanism!");
48 }
49
50 double Model::nextOccuringEventLazy(double now)
51 {
52   XBT_DEBUG("Before share resources, the size of modified actions set is %zu", maxmin_system_->modified_set_->size());
53   lmm_solve(maxmin_system_);
54   XBT_DEBUG("After share resources, The size of modified actions set is %zu", maxmin_system_->modified_set_->size());
55
56   while (not maxmin_system_->modified_set_->empty()) {
57     Action* action = &(maxmin_system_->modified_set_->front());
58     maxmin_system_->modified_set_->pop_front();
59     bool max_dur_flag = false;
60
61     if (action->get_state_set() != running_action_set_)
62       continue;
63
64     /* bogus priority, skip it */
65     if (action->get_priority() <= 0 || action->get_type() == Action::Type::LATENCY)
66       continue;
67
68     action->update_remains_lazy(now);
69
70     double min   = -1;
71     double share = action->get_variable()->get_value();
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       max_dur_flag = true;
88     }
89
90     XBT_DEBUG("Action(%p) corresponds to variable %d", action, action->get_variable()->id_int);
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->heapUpdate(min, max_dur_flag ? Action::Type::MAX_DURATION : Action::Type::NORMAL);
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 actionHeapIsEmpty()) {
104     double min = actionHeapTopDate() - 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::nextOccuringEventFull(double /*now*/)
114 {
115   maxmin_system_->solve_fun(maxmin_system_);
116
117   double min = -1;
118
119   for (Action& action : *getRunningActionSet()) {
120     double value = action.get_variable()->get_value();
121     if (value > 0) {
122       if (action.get_remains() > 0)
123         value = action.get_remains_no_update() / value;
124       else
125         value = 0.0;
126       if (min < 0 || value < min) {
127         min = value;
128         XBT_DEBUG("Updating min (value) with %p: %f", &action, min);
129       }
130     }
131     if ((action.get_max_duration() >= 0) && (min < 0 || action.get_max_duration() < min)) {
132       min = action.get_max_duration();
133       XBT_DEBUG("Updating min (duration) with %p: %f", &action, min);
134     }
135   }
136   XBT_DEBUG("min value : %f", min);
137
138   return min;
139 }
140
141 void Model::updateActionsState(double now, double delta)
142 {
143   if (update_mechanism_ == UM_FULL)
144     updateActionsStateFull(now, delta);
145   else if (update_mechanism_ == UM_LAZY)
146     updateActionsStateLazy(now, delta);
147   else
148     xbt_die("Invalid cpu update mechanism!");
149 }
150
151 void Model::updateActionsStateLazy(double /*now*/, double /*delta*/)
152 {
153   THROW_UNIMPLEMENTED;
154 }
155
156 void Model::updateActionsStateFull(double /*now*/, double /*delta*/)
157 {
158   THROW_UNIMPLEMENTED;
159 }
160
161 } // namespace surf
162 } // namespace simgrid
163 } // namespace simgrid