1 /* Copyright (c) 2004-2022. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/kernel/lmm/maxmin.hpp"
8 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ker_lmm);
14 using dyn_light_t = std::vector<int>;
16 static inline void saturated_constraints_update(double usage, int cnst_light_num, dyn_light_t& saturated_constraints,
19 xbt_assert(usage > 0, "Impossible");
21 if (*min_usage < 0 || *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);
30 static inline void saturated_variable_set_update(const ConstraintLight* cnst_light_tab,
31 const dyn_light_t& saturated_constraints, System* sys)
33 /* Add active variables (i.e. variables that need to be set) from the set of constraints to saturate
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);
45 void MaxMin::do_solve()
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.
51 if (selective_update_active)
52 maxmin_solve(modified_constraint_set);
54 maxmin_solve(active_constraint_set);
58 template <class CnstList> void MaxMin::maxmin_solve(CnstList& cnst_list)
60 double min_usage = -1;
61 double min_bound = -1;
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;
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.dyn_constraint_cb_) {
73 cnst.dynamic_bound_ = cnst.dyn_constraint_cb_(cnst.bound_, cnst.concurrency_current_);
75 cnst.remaining_ = cnst.dynamic_bound_;
76 if (not double_positive(cnst.remaining_, cnst.dynamic_bound_ * sg_maxmin_precision))
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_;
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 */
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!");
107 saturated_variable_set_update(cnst_light_tab, saturated_constraints, this);
109 /* Saturated variables update */
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)
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)) {
121 min_bound = var.bound_ * var.sharing_penalty_;
123 min_bound = std::min(min_bound, (var.bound_ * var.sharing_penalty_));
124 XBT_DEBUG("Updated min_bound=%f", min_bound);
128 while (not var_list.empty()) {
129 Variable& var = var_list.front();
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_);
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_maxmin_precision)) {
138 var.value_ = var.bound_;
139 XBT_DEBUG("Setting %p (%d) value to %f\n", &var, var.rank_, var.value_);
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();
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_);
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_maxmin_precision);
157 double_update(&(cnst->usage_), elem.consumption_weight / var.sharing_penalty_, sg_maxmin_precision);
158 // If the constraint is saturated, remove it from the set of active constraints (light_tab)
159 if (not double_positive(cnst->usage_, sg_maxmin_precision) ||
160 not double_positive(cnst->remaining_, cnst->dynamic_bound_ * sg_maxmin_precision)) {
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];
168 cnst->cnst_light_ = nullptr;
171 if (cnst->cnst_light_) {
172 cnst->cnst_light_->remaining_over_usage = cnst->remaining_ / cnst->usage_;
175 elem.make_inactive();
177 // Remember: non-shared constraints only require that max(elem.value * var.value) < cnst->bound
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)
184 if (elem2.consumption_weight > 0)
185 cnst->usage_ = std::max(cnst->usage_, elem2.consumption_weight / elem2.variable->sharing_penalty_);
187 // If the constraint is saturated, remove it from the set of active constraints (light_tab)
188 if (not double_positive(cnst->usage_, sg_maxmin_precision) ||
189 not double_positive(cnst->remaining_, cnst->dynamic_bound_ * sg_maxmin_precision)) {
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];
199 cnst->cnst_light_ = nullptr;
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.");
211 var_list.pop_front();
214 /* Find out which variables reach the maximum */
217 saturated_constraints.clear();
218 for (int pos = 0; pos < cnst_light_num; pos++) {
219 xbt_assert(not cnst_light_tab[pos].cnst->active_element_set_.empty(),
220 "Cannot saturate more a constraint that has"
221 " no active element! You may want to change the maxmin precision (--cfg=maxmin/precision:<new_value>)"
222 " because of possible rounding effects.\n\tFor the record, the usage of this constraint is %g while "
223 "the maxmin precision to which it is compared is %g.\n\tThe usage of the previous constraint is %g.",
224 cnst_light_tab[pos].cnst->usage_, sg_maxmin_precision, cnst_light_tab[pos - 1].cnst->usage_);
225 saturated_constraints_update(cnst_light_tab[pos].remaining_over_usage, pos, saturated_constraints, &min_usage);
228 saturated_variable_set_update(cnst_light_tab, saturated_constraints, this);
229 } while (cnst_light_num > 0);
233 } // namespace kernel
234 } // namespace simgrid