Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Assert message fix
[simgrid.git] / src / kernel / lmm / maxmin.cpp
1 /* Copyright (c) 2004-2023. 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 "src/kernel/lmm/maxmin.hpp"
7 #include "src/simgrid/math_utils.h"
8 #include "xbt/ex.h"
9
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ker_lmm);
11
12 namespace simgrid::kernel::lmm {
13
14 using dyn_light_t = std::vector<int>;
15
16 static inline void saturated_constraints_update(double usage, int cnst_light_num, dyn_light_t& saturated_constraints,
17                                                 double* min_usage)
18 {
19   xbt_assert(usage > 0, "Impossible");
20
21   if (*min_usage < 0 || *min_usage > usage) {
22     *min_usage = usage;
23     XBT_HERE(" min_usage=%f (cnst->remaining / cnst->usage =%f)", *min_usage, usage);
24     saturated_constraints.assign(1, cnst_light_num);
25   } else if (*min_usage == usage) {
26     saturated_constraints.emplace_back(cnst_light_num);
27   }
28 }
29
30 static inline void saturated_variable_set_update(const ConstraintLight* cnst_light_tab,
31                                                  const dyn_light_t& saturated_constraints, System* sys)
32 {
33   /* Add active variables (i.e. variables that need to be set) from the set of constraints to saturate
34    * (cnst_light_tab)*/
35   for (int const& saturated_cnst : saturated_constraints) {
36     const ConstraintLight& cnst = cnst_light_tab[saturated_cnst];
37     for (Element const& elem : cnst.cnst->active_element_set_) {
38       xbt_assert(elem.variable->sharing_penalty_ > 0); // All elements of active_element_set should be active
39       if (elem.consumption_weight > 0 && not elem.variable->saturated_variable_set_hook_.is_linked())
40         sys->saturated_variable_set.push_back(*elem.variable);
41     }
42   }
43 }
44
45 void MaxMin::do_solve()
46 {
47   XBT_IN("(sys=%p)", this);
48   /* Compute Usage and store the variables that reach the maximum. If selective_update_active is true, only
49    * constraints that changed are considered. Otherwise all constraints with active actions are considered.
50    */
51   if (selective_update_active)
52     maxmin_solve(modified_constraint_set);
53   else
54     maxmin_solve(active_constraint_set);
55   XBT_OUT();
56 }
57
58 template <class CnstList> void MaxMin::maxmin_solve(CnstList& cnst_list)
59 {
60   double min_usage = -1;
61   double min_bound = -1;
62
63   XBT_DEBUG("Active constraints : %zu", cnst_list.size());
64   cnst_light_vec.reserve(cnst_list.size());
65   ConstraintLight* cnst_light_tab = cnst_light_vec.data();
66   int cnst_light_num              = 0;
67
68   for (Constraint& cnst : cnst_list) {
69     /* INIT: Collect constraints that actually need to be saturated (i.e remaining  and usage are strictly positive)
70      * into cnst_light_tab. */
71     cnst.dynamic_bound_ = cnst.bound_;
72     if ((cnst.get_sharing_policy() == Constraint::SharingPolicy::NONLINEAR || cnst.get_sharing_policy() == Constraint::SharingPolicy::WIFI) && cnst.dyn_constraint_cb_) {
73       cnst.dynamic_bound_ = cnst.dyn_constraint_cb_(cnst.bound_, cnst.concurrency_current_);
74     }
75     cnst.remaining_ = cnst.dynamic_bound_;
76     if (not double_positive(cnst.remaining_, cnst.dynamic_bound_ * sg_precision_workamount))
77       continue;
78     cnst.usage_ = 0;
79     for (Element& elem : cnst.enabled_element_set_) {
80       xbt_assert(elem.variable->sharing_penalty_ > 0.0);
81       elem.variable->value_ = 0.0;
82       if (elem.consumption_weight > 0) {
83         if (cnst.sharing_policy_ != Constraint::SharingPolicy::FATPIPE)
84           cnst.usage_ += elem.consumption_weight / elem.variable->sharing_penalty_;
85         else if (cnst.usage_ < elem.consumption_weight / elem.variable->sharing_penalty_)
86           cnst.usage_ = elem.consumption_weight / elem.variable->sharing_penalty_;
87
88         elem.make_active();
89       }
90     }
91     XBT_DEBUG("Constraint '%d' usage: %f remaining: %f concurrency: %i<=%i<=%i", cnst.rank_, cnst.usage_,
92               cnst.remaining_, cnst.concurrency_current_, cnst.concurrency_maximum_, cnst.get_concurrency_limit());
93     /* Saturated constraints update */
94
95     if (cnst.usage_ > 0) {
96       cnst_light_tab[cnst_light_num].cnst                 = &cnst;
97       cnst.cnst_light_                                    = &cnst_light_tab[cnst_light_num];
98       cnst_light_tab[cnst_light_num].remaining_over_usage = cnst.remaining_ / cnst.usage_;
99       saturated_constraints_update(cnst_light_tab[cnst_light_num].remaining_over_usage, cnst_light_num,
100                                    saturated_constraints, &min_usage);
101       xbt_assert(not cnst.active_element_set_.empty(),
102                  "There is no sense adding a constraint that has no active element!");
103       cnst_light_num++;
104     }
105   }
106
107   saturated_variable_set_update(cnst_light_tab, saturated_constraints, this);
108
109   /* Saturated variables update */
110   do {
111     /* Fix the variables that have to be */
112     auto& var_list = saturated_variable_set;
113     for (Variable const& var : var_list) {
114       if (var.sharing_penalty_ <= 0.0)
115         DIE_IMPOSSIBLE;
116       /* First check if some of these variables could reach their upper bound and update min_bound accordingly. */
117       XBT_DEBUG("var=%d, var.bound=%f, var.penalty=%f, min_usage=%f, var.bound*var.penalty=%f", var.rank_, var.bound_,
118                 var.sharing_penalty_, min_usage, var.bound_ * var.sharing_penalty_);
119       if ((var.bound_ > 0) && (var.bound_ * var.sharing_penalty_ < min_usage)) {
120         if (min_bound < 0)
121           min_bound = var.bound_ * var.sharing_penalty_;
122         else
123           min_bound = std::min(min_bound, (var.bound_ * var.sharing_penalty_));
124         XBT_DEBUG("Updated min_bound=%f", min_bound);
125       }
126     }
127
128     while (not var_list.empty()) {
129       Variable& var = var_list.front();
130       if (min_bound < 0) {
131         // If no variable could reach its bound, deal iteratively the constraints usage ( at worst one constraint is
132         // saturated at each cycle)
133         var.value_ = min_usage / var.sharing_penalty_;
134         XBT_DEBUG("Setting var (%d) value to %f\n", var.rank_, var.value_);
135       } else {
136         // If there exist a variable that can reach its bound, only update it (and other with the same bound) for now.
137         if (double_equals(min_bound, var.bound_ * var.sharing_penalty_, sg_precision_workamount)) {
138           var.value_ = var.bound_;
139           XBT_DEBUG("Setting %p (%d) value to %f\n", &var, var.rank_, var.value_);
140         } else {
141           // Variables which bound is different are not considered for this cycle, but they will be afterwards.
142           XBT_DEBUG("Do not consider %p (%d)\n", &var, var.rank_);
143           var_list.pop_front();
144           continue;
145         }
146       }
147       XBT_DEBUG("Min usage: %f, Var(%d).penalty: %f, Var(%d).value: %f", min_usage, var.rank_, var.sharing_penalty_,
148                 var.rank_, var.value_);
149
150       /* Update the usage of constraints where this variable is involved */
151       for (Element& elem : var.cnsts_) {
152         Constraint* cnst = elem.constraint;
153         if (cnst->sharing_policy_ != Constraint::SharingPolicy::FATPIPE) {
154           // Remember: shared constraints require that sum(elem.value * var.value) < cnst->bound
155           double_update(&(cnst->remaining_), elem.consumption_weight * var.value_,
156                         cnst->dynamic_bound_ * sg_precision_workamount);
157           double_update(&(cnst->usage_), elem.consumption_weight / var.sharing_penalty_, sg_precision_workamount);
158           // If the constraint is saturated, remove it from the set of active constraints (light_tab)
159           if (not double_positive(cnst->usage_, sg_precision_workamount) ||
160               not double_positive(cnst->remaining_, cnst->dynamic_bound_ * sg_precision_workamount)) {
161             if (cnst->cnst_light_) {
162               size_t index = (cnst->cnst_light_ - cnst_light_tab);
163               XBT_DEBUG("index: %zu \t cnst_light_num: %d \t || usage: %f remaining: %f bound: %f", index,
164                         cnst_light_num, cnst->usage_, cnst->remaining_, cnst->dynamic_bound_);
165               cnst_light_tab[index]                   = cnst_light_tab[cnst_light_num - 1];
166               cnst_light_tab[index].cnst->cnst_light_ = &cnst_light_tab[index];
167               cnst_light_num--;
168               cnst->cnst_light_ = nullptr;
169             }
170           } else {
171             if (cnst->cnst_light_) {
172               cnst->cnst_light_->remaining_over_usage = cnst->remaining_ / cnst->usage_;
173             }
174           }
175           elem.make_inactive();
176         } else {
177           // Remember: non-shared constraints only require that max(elem.value * var.value) < cnst->bound
178           cnst->usage_ = 0.0;
179           elem.make_inactive();
180           for (const Element& elem2 : cnst->enabled_element_set_) {
181             xbt_assert(elem2.variable->sharing_penalty_ > 0);
182             if (elem2.variable->value_ > 0)
183               continue;
184             if (elem2.consumption_weight > 0)
185               cnst->usage_ = std::max(cnst->usage_, elem2.consumption_weight / elem2.variable->sharing_penalty_);
186           }
187           // If the constraint is saturated, remove it from the set of active constraints (light_tab)
188           if (not double_positive(cnst->usage_, sg_precision_workamount) ||
189               not double_positive(cnst->remaining_, cnst->dynamic_bound_ * sg_precision_workamount)) {
190             if (cnst->cnst_light_) {
191               size_t index = (cnst->cnst_light_ - cnst_light_tab);
192               XBT_DEBUG("index: %zu \t cnst_light_num: %d \t || \t cnst: %p \t cnst->cnst_light: %p "
193                         "\t cnst_light_tab: %p usage: %f remaining: %f bound: %f",
194                         index, cnst_light_num, cnst, cnst->cnst_light_, cnst_light_tab, cnst->usage_, cnst->remaining_,
195                         cnst->dynamic_bound_);
196               cnst_light_tab[index]                   = cnst_light_tab[cnst_light_num - 1];
197               cnst_light_tab[index].cnst->cnst_light_ = &cnst_light_tab[index];
198               cnst_light_num--;
199               cnst->cnst_light_ = nullptr;
200             }
201           } else {
202             if (cnst->cnst_light_) {
203               cnst->cnst_light_->remaining_over_usage = cnst->remaining_ / cnst->usage_;
204               xbt_assert(not cnst->active_element_set_.empty(),
205                          "Should not keep a maximum constraint that has no active"
206                          " element! You want to check the maxmin precision and possible rounding effects.");
207             }
208           }
209         }
210       }
211       var_list.pop_front();
212     }
213
214     /* Find out which variables reach the maximum */
215     min_usage = -1;
216     min_bound = -1;
217     saturated_constraints.clear();
218     for (int pos = 0; pos < cnst_light_num; pos++) {
219       xbt_assert(
220           not cnst_light_tab[pos].cnst->active_element_set_.empty(),
221           "Cannot saturate more a constraint that has"
222           " no active element! You may want to change the work amount precision (--cfg=maxmin/precision:<new_value>)"
223           " because of possible rounding effects.\n\tFor the record, the usage of this constraint is %g while "
224           "the maxmin precision to which it is compared is %g.\n\tThe usage of the previous constraint is %g.",
225           cnst_light_tab[pos].cnst->usage_, sg_precision_workamount, cnst_light_tab[pos - 1].cnst->usage_);
226       saturated_constraints_update(cnst_light_tab[pos].remaining_over_usage, pos, saturated_constraints, &min_usage);
227     }
228
229     saturated_variable_set_update(cnst_light_tab, saturated_constraints, this);
230   } while (cnst_light_num > 0);
231 }
232
233 } // namespace simgrid::kernel::lmm