Logo AND Algorithmique Numérique Distribuée

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