Logo AND Algorithmique Numérique Distribuée

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