Logo AND Algorithmique Numérique Distribuée

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