1 /* Copyright (c) 2004-2019. 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"
7 #include "src/surf/surf_interface.hpp"
8 #include "xbt/backtrace.hpp"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_maxmin, surf, "Logging specific to SURF (maxmin)");
12 double sg_maxmin_precision = 0.00001; /* Change this with --cfg=maxmin/precision:VALUE */
13 double sg_surf_precision = 0.00001; /* Change this with --cfg=surf/precision:VALUE */
14 int sg_concurrency_limit = -1; /* Change this with --cfg=maxmin/concurrency-limit:VALUE */
20 typedef std::vector<int> dyn_light_t;
22 int Variable::next_rank_ = 1;
23 int Constraint::next_rank_ = 1;
25 System* make_new_maxmin_system(bool selective_update)
27 return new System(selective_update);
30 int Element::get_concurrency() const
32 // Ignore element with weight less than one (e.g. cross-traffic)
33 return (consumption_weight >= 1) ? 1 : 0;
34 // There are other alternatives, but they will change the behaviour of the model..
35 // So do not use it unless you want to make a new model.
36 // If you do, remember to change the variables concurrency share to reflect it.
37 // Potential examples are:
38 // return (elem->weight>0)?1:0;//Include element as soon as weight is non-zero
39 // return (int)ceil(elem->weight);//Include element as the rounded-up integer value of the element weight
42 void Element::decrease_concurrency()
44 xbt_assert(constraint->concurrency_current_ >= get_concurrency());
45 constraint->concurrency_current_ -= get_concurrency();
48 void Element::increase_concurrency()
50 constraint->concurrency_current_ += get_concurrency();
52 if (constraint->concurrency_current_ > constraint->concurrency_maximum_)
53 constraint->concurrency_maximum_ = constraint->concurrency_current_;
55 xbt_assert(constraint->get_concurrency_limit() < 0 ||
56 constraint->concurrency_current_ <= constraint->get_concurrency_limit(),
57 "Concurrency limit overflow!");
60 void System::check_concurrency() const
62 // These checks are very expensive, so do them only if we want to debug SURF LMM
63 if (not XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug))
66 for (Constraint const& cnst : constraint_set) {
68 for (Element const& elem : cnst.enabled_element_set_) {
69 xbt_assert(elem.variable->sharing_penalty_ > 0);
70 concurrency += elem.get_concurrency();
73 for (Element const& elem : cnst.disabled_element_set_) {
74 // We should have staged variables only if concurrency is reached in some constraint
75 xbt_assert(cnst.get_concurrency_limit() < 0 || elem.variable->staged_penalty_ == 0 ||
76 elem.variable->get_min_concurrency_slack() < elem.variable->concurrency_share_,
77 "should not have staged variable!");
80 xbt_assert(cnst.get_concurrency_limit() < 0 || cnst.get_concurrency_limit() >= concurrency,
81 "concurrency check failed!");
82 xbt_assert(cnst.concurrency_current_ == concurrency, "concurrency_current is out-of-date!");
85 // Check that for each variable, all corresponding elements are in the same state (i.e. same element sets)
86 for (Variable const& var : variable_set) {
87 if (var.cnsts_.empty())
90 const Element& elem = var.cnsts_[0];
91 int belong_to_enabled = elem.enabled_element_set_hook.is_linked();
92 int belong_to_disabled = elem.disabled_element_set_hook.is_linked();
93 int belong_to_active = elem.active_element_set_hook.is_linked();
95 for (Element const& elem2 : var.cnsts_) {
96 xbt_assert(belong_to_enabled == elem2.enabled_element_set_hook.is_linked(),
97 "Variable inconsistency (1): enabled_element_set");
98 xbt_assert(belong_to_disabled == elem2.disabled_element_set_hook.is_linked(),
99 "Variable inconsistency (2): disabled_element_set");
100 xbt_assert(belong_to_active == elem2.active_element_set_hook.is_linked(),
101 "Variable inconsistency (3): active_element_set");
106 void System::var_free(Variable* var)
108 XBT_IN("(sys=%p, var=%p)", this, var);
111 // TODOLATER Can do better than that by leaving only the variable in only one enabled_element_set, call
112 // update_modified_set, and then remove it..
113 if (not var->cnsts_.empty())
114 update_modified_set(var->cnsts_[0].constraint);
116 for (Element& elem : var->cnsts_) {
117 if (var->sharing_penalty_ > 0)
118 elem.decrease_concurrency();
119 if (elem.enabled_element_set_hook.is_linked())
120 simgrid::xbt::intrusive_erase(elem.constraint->enabled_element_set_, elem);
121 if (elem.disabled_element_set_hook.is_linked())
122 simgrid::xbt::intrusive_erase(elem.constraint->disabled_element_set_, elem);
123 if (elem.active_element_set_hook.is_linked())
124 simgrid::xbt::intrusive_erase(elem.constraint->active_element_set_, elem);
125 int nelements = elem.constraint->enabled_element_set_.size() + elem.constraint->disabled_element_set_.size();
127 make_constraint_inactive(elem.constraint);
129 on_disabled_var(elem.constraint);
136 xbt_mallocator_release(variable_mallocator_, var);
140 System::System(bool selective_update) : selective_update_active(selective_update)
142 XBT_DEBUG("Setting selective_update_active flag to %d", selective_update_active);
144 if (selective_update)
145 modified_set_ = new kernel::resource::Action::ModifiedSet();
153 while ((var = extract_variable())) {
154 auto demangled = simgrid::xbt::demangle(typeid(*var->id_).name());
155 XBT_WARN("Probable bug: a %s variable (#%d) not removed before the LMM system destruction.", demangled.get(),
159 while ((cnst = extract_constraint()))
162 xbt_mallocator_free(variable_mallocator_);
163 delete modified_set_;
166 void System::cnst_free(Constraint* cnst)
168 make_constraint_inactive(cnst);
172 Constraint::Constraint(void* id_value, double bound_value) : bound_(bound_value), id_(id_value)
174 rank_ = next_rank_++;
178 concurrency_limit_ = sg_concurrency_limit;
179 concurrency_current_ = 0;
180 concurrency_maximum_ = 0;
181 sharing_policy_ = s4u::Link::SharingPolicy::SHARED;
185 cnst_light_ = nullptr;
188 Constraint* System::constraint_new(void* id, double bound_value)
190 Constraint* cnst = new Constraint(id, bound_value);
191 insert_constraint(cnst);
195 void* System::variable_mallocator_new_f()
200 void System::variable_mallocator_free_f(void* var)
202 delete static_cast<Variable*>(var);
205 Variable* System::variable_new(resource::Action* id, double sharing_penalty, double bound, size_t number_of_constraints)
207 XBT_IN("(sys=%p, id=%p, penalty=%f, bound=%f, num_cons =%zu)", this, id, sharing_penalty, bound,
208 number_of_constraints);
210 Variable* var = static_cast<Variable*>(xbt_mallocator_get(variable_mallocator_));
211 var->initialize(id, sharing_penalty, bound, number_of_constraints, visited_counter_ - 1);
212 if (sharing_penalty > 0)
213 variable_set.push_front(*var);
215 variable_set.push_back(*var);
217 XBT_OUT(" returns %p", var);
221 void System::variable_free(Variable* var)
223 remove_variable(var);
227 void System::expand(Constraint* cnst, Variable* var, double consumption_weight)
231 // Check if this variable already has an active element in this constraint
232 // If it does, substract it from the required slack
233 int current_share = 0;
234 if (var->concurrency_share_ > 1) {
235 for (Element& elem : var->cnsts_) {
236 if (elem.constraint == cnst && elem.enabled_element_set_hook.is_linked())
237 current_share += elem.get_concurrency();
241 // Check if we need to disable the variable
242 if (var->sharing_penalty_ > 0 && var->concurrency_share_ - current_share > cnst->get_concurrency_slack()) {
243 double penalty = var->sharing_penalty_;
245 for (Element const& elem : var->cnsts_)
246 on_disabled_var(elem.constraint);
247 consumption_weight = 0;
248 var->staged_penalty_ = penalty;
249 xbt_assert(not var->sharing_penalty_);
252 xbt_assert(var->cnsts_.size() < var->cnsts_.capacity(), "Too much constraints");
254 var->cnsts_.resize(var->cnsts_.size() + 1);
255 Element& elem = var->cnsts_.back();
257 elem.consumption_weight = consumption_weight;
258 elem.constraint = cnst;
261 if (var->sharing_penalty_) {
262 elem.constraint->enabled_element_set_.push_front(elem);
263 elem.increase_concurrency();
265 elem.constraint->disabled_element_set_.push_back(elem);
267 if (not selective_update_active) {
268 make_constraint_active(cnst);
269 } else if (elem.consumption_weight > 0 || var->sharing_penalty_ > 0) {
270 make_constraint_active(cnst);
271 update_modified_set(cnst);
272 // TODOLATER: Why do we need this second call?
273 if (var->cnsts_.size() > 1)
274 update_modified_set(var->cnsts_[0].constraint);
280 void System::expand_add(Constraint* cnst, Variable* var, double value)
286 // BEWARE: In case you have multiple elements in one constraint, this will always add value to the first element.
288 std::find_if(begin(var->cnsts_), end(var->cnsts_), [&cnst](Element const& x) { return x.constraint == cnst; });
289 if (elem_it != end(var->cnsts_)) {
290 Element& elem = *elem_it;
291 if (var->sharing_penalty_)
292 elem.decrease_concurrency();
294 if (cnst->sharing_policy_ != s4u::Link::SharingPolicy::FATPIPE)
295 elem.consumption_weight += value;
297 elem.consumption_weight = std::max(elem.consumption_weight, value);
299 // We need to check that increasing value of the element does not cross the concurrency limit
300 if (var->sharing_penalty_) {
301 if (cnst->get_concurrency_slack() < elem.get_concurrency()) {
302 double penalty = var->sharing_penalty_;
304 for (Element const& elem2 : var->cnsts_)
305 on_disabled_var(elem2.constraint);
306 var->staged_penalty_ = penalty;
307 xbt_assert(not var->sharing_penalty_);
309 elem.increase_concurrency();
311 update_modified_set(cnst);
313 expand(cnst, var, value);
318 Variable* Constraint::get_variable(const Element** elem) const
320 if (*elem == nullptr) {
321 // That is the first call, pick the first element among enabled_element_set (or disabled_element_set if
322 // enabled_element_set is empty)
323 if (not enabled_element_set_.empty())
324 *elem = &enabled_element_set_.front();
325 else if (not disabled_element_set_.empty())
326 *elem = &disabled_element_set_.front();
330 // elem is not null, so we carry on
331 if ((*elem)->enabled_element_set_hook.is_linked()) {
332 // Look at enabled_element_set, and jump to disabled_element_set when finished
333 auto iter = std::next(enabled_element_set_.iterator_to(**elem));
334 if (iter != std::end(enabled_element_set_))
336 else if (not disabled_element_set_.empty())
337 *elem = &disabled_element_set_.front();
341 auto iter = std::next(disabled_element_set_.iterator_to(**elem));
342 *elem = iter != std::end(disabled_element_set_) ? &*iter : nullptr;
346 return (*elem)->variable;
351 // if we modify the list between calls, normal version may loop forever
352 // this safe version ensures that we browse the list elements only once
353 Variable* Constraint::get_variable_safe(const Element** elem, const Element** nextelem, int* numelem) const
355 if (*elem == nullptr) {
356 *numelem = enabled_element_set_.size() + disabled_element_set_.size() - 1;
357 if (not enabled_element_set_.empty())
358 *elem = &enabled_element_set_.front();
359 else if (not disabled_element_set_.empty())
360 *elem = &disabled_element_set_.front();
371 // elem is not null, so we carry on
372 if ((*elem)->enabled_element_set_hook.is_linked()) {
373 // Look at enabled_element_set, and jump to disabled_element_set when finished
374 auto iter = std::next(enabled_element_set_.iterator_to(**elem));
375 if (iter != std::end(enabled_element_set_))
377 else if (not disabled_element_set_.empty())
378 *nextelem = &disabled_element_set_.front();
382 auto iter = std::next(disabled_element_set_.iterator_to(**elem));
383 *nextelem = iter != std::end(disabled_element_set_) ? &*iter : nullptr;
385 return (*elem)->variable;
390 static inline void saturated_constraints_update(double usage, int cnst_light_num, dyn_light_t& saturated_constraints,
393 xbt_assert(usage > 0, "Impossible");
395 if (*min_usage < 0 || *min_usage > usage) {
397 XBT_HERE(" min_usage=%f (cnst->remaining / cnst->usage =%f)", *min_usage, usage);
398 saturated_constraints.assign(1, cnst_light_num);
399 } else if (*min_usage == usage) {
400 saturated_constraints.emplace_back(cnst_light_num);
404 static inline void saturated_variable_set_update(ConstraintLight* cnst_light_tab,
405 const dyn_light_t& saturated_constraints, System* sys)
407 /* Add active variables (i.e. variables that need to be set) from the set of constraints to saturate
409 for (int const& saturated_cnst : saturated_constraints) {
410 ConstraintLight& cnst = cnst_light_tab[saturated_cnst];
411 for (Element const& elem : cnst.cnst->active_element_set_) {
412 xbt_assert(elem.variable->sharing_penalty_ > 0); // All elements of active_element_set should be active
413 if (elem.consumption_weight > 0 && not elem.variable->saturated_variable_set_hook_.is_linked())
414 sys->saturated_variable_set.push_back(*elem.variable);
419 template <class ElemList>
420 static void format_element_list(const ElemList& elem_list, s4u::Link::SharingPolicy sharing_policy, double& sum,
423 for (Element const& elem : elem_list) {
424 buf += std::to_string(elem.consumption_weight) + ".'" + std::to_string(elem.variable->rank_) + "'(" +
425 std::to_string(elem.variable->value_) + ")" +
426 (sharing_policy != s4u::Link::SharingPolicy::FATPIPE ? " + " : " , ");
427 if (sharing_policy != s4u::Link::SharingPolicy::FATPIPE)
428 sum += elem.consumption_weight * elem.variable->value_;
430 sum = std::max(sum, elem.consumption_weight * elem.variable->value_);
434 void System::print() const
436 std::string buf = std::string("MAX-MIN ( ");
438 /* Printing Objective */
439 for (Variable const& var : variable_set)
440 buf += "'" + std::to_string(var.rank_) + "'(" + std::to_string(var.sharing_penalty_) + ") ";
442 XBT_DEBUG("%20s", buf.c_str());
445 XBT_DEBUG("Constraints");
446 /* Printing Constraints */
447 for (Constraint const& cnst : active_constraint_set) {
449 // Show the enabled variables
451 buf += cnst.sharing_policy_ != s4u::Link::SharingPolicy::FATPIPE ? "(" : "max(";
452 format_element_list(cnst.enabled_element_set_, cnst.sharing_policy_, sum, buf);
453 // TODO: Adding disabled elements only for test compatibility, but do we really want them to be printed?
454 format_element_list(cnst.disabled_element_set_, cnst.sharing_policy_, sum, buf);
456 buf += "0) <= " + std::to_string(cnst.bound_) + " ('" + std::to_string(cnst.rank_) + "')";
458 if (cnst.sharing_policy_ == s4u::Link::SharingPolicy::FATPIPE) {
459 buf += " [MAX-Constraint]";
461 XBT_DEBUG("%s", buf.c_str());
463 xbt_assert(not double_positive(sum - cnst.bound_, cnst.bound_ * sg_maxmin_precision),
464 "Incorrect value (%f is not smaller than %f): %g", sum, cnst.bound_, sum - cnst.bound_);
467 XBT_DEBUG("Variables");
468 /* Printing Result */
469 for (Variable const& var : variable_set) {
470 if (var.bound_ > 0) {
471 XBT_DEBUG("'%d'(%f) : %f (<=%f)", var.rank_, var.sharing_penalty_, var.value_, var.bound_);
472 xbt_assert(not double_positive(var.value_ - var.bound_, var.bound_ * sg_maxmin_precision),
473 "Incorrect value (%f is not smaller than %f", var.value_, var.bound_);
475 XBT_DEBUG("'%d'(%f) : %f", var.rank_, var.sharing_penalty_, var.value_);
480 void System::lmm_solve()
483 XBT_IN("(sys=%p)", this);
484 /* Compute Usage and store the variables that reach the maximum. If selective_update_active is true, only
485 * constraints that changed are considered. Otherwise all constraints with active actions are considered.
487 if (selective_update_active)
488 lmm_solve(modified_constraint_set);
490 lmm_solve(active_constraint_set);
495 template <class CnstList> void System::lmm_solve(CnstList& cnst_list)
497 double min_usage = -1;
498 double min_bound = -1;
500 XBT_DEBUG("Active constraints : %zu", cnst_list.size());
501 /* Init: Only modified code portions: reset the value of active variables */
502 for (Constraint const& cnst : cnst_list) {
503 for (Element const& elem : cnst.enabled_element_set_) {
504 xbt_assert(elem.variable->sharing_penalty_ > 0.0);
505 elem.variable->value_ = 0.0;
509 ConstraintLight* cnst_light_tab = new ConstraintLight[cnst_list.size()]();
510 int cnst_light_num = 0;
511 dyn_light_t saturated_constraints;
513 for (Constraint& cnst : cnst_list) {
514 /* INIT: Collect constraints that actually need to be saturated (i.e remaining and usage are strictly positive)
515 * into cnst_light_tab. */
516 cnst.remaining_ = cnst.bound_;
517 if (not double_positive(cnst.remaining_, cnst.bound_ * sg_maxmin_precision))
520 for (Element& elem : cnst.enabled_element_set_) {
521 xbt_assert(elem.variable->sharing_penalty_ > 0);
522 if (elem.consumption_weight > 0) {
523 if (cnst.sharing_policy_ != s4u::Link::SharingPolicy::FATPIPE)
524 cnst.usage_ += elem.consumption_weight / elem.variable->sharing_penalty_;
525 else if (cnst.usage_ < elem.consumption_weight / elem.variable->sharing_penalty_)
526 cnst.usage_ = elem.consumption_weight / elem.variable->sharing_penalty_;
529 resource::Action* action = static_cast<resource::Action*>(elem.variable->id_);
530 if (modified_set_ && not action->is_within_modified_set())
531 modified_set_->push_back(*action);
534 XBT_DEBUG("Constraint '%d' usage: %f remaining: %f concurrency: %i<=%i<=%i", cnst.rank_, cnst.usage_,
535 cnst.remaining_, cnst.concurrency_current_, cnst.concurrency_maximum_, cnst.get_concurrency_limit());
536 /* Saturated constraints update */
538 if (cnst.usage_ > 0) {
539 cnst_light_tab[cnst_light_num].cnst = &cnst;
540 cnst.cnst_light_ = &cnst_light_tab[cnst_light_num];
541 cnst_light_tab[cnst_light_num].remaining_over_usage = cnst.remaining_ / cnst.usage_;
542 saturated_constraints_update(cnst_light_tab[cnst_light_num].remaining_over_usage, cnst_light_num,
543 saturated_constraints, &min_usage);
544 xbt_assert(not cnst.active_element_set_.empty(),
545 "There is no sense adding a constraint that has no active element!");
550 saturated_variable_set_update(cnst_light_tab, saturated_constraints, this);
552 /* Saturated variables update */
554 /* Fix the variables that have to be */
555 auto& var_list = saturated_variable_set;
556 for (Variable const& var : var_list) {
557 if (var.sharing_penalty_ <= 0.0)
559 /* First check if some of these variables could reach their upper bound and update min_bound accordingly. */
560 XBT_DEBUG("var=%d, var.bound=%f, var.penalty=%f, min_usage=%f, var.bound*var.penalty=%f", var.rank_, var.bound_,
561 var.sharing_penalty_, min_usage, var.bound_ * var.sharing_penalty_);
562 if ((var.bound_ > 0) && (var.bound_ * var.sharing_penalty_ < min_usage)) {
564 min_bound = var.bound_ * var.sharing_penalty_;
566 min_bound = std::min(min_bound, (var.bound_ * var.sharing_penalty_));
567 XBT_DEBUG("Updated min_bound=%f", min_bound);
571 while (not var_list.empty()) {
572 Variable& var = var_list.front();
574 // If no variable could reach its bound, deal iteratively the constraints usage ( at worst one constraint is
575 // saturated at each cycle)
576 var.value_ = min_usage / var.sharing_penalty_;
577 XBT_DEBUG("Setting var (%d) value to %f\n", var.rank_, var.value_);
579 // If there exist a variable that can reach its bound, only update it (and other with the same bound) for now.
580 if (double_equals(min_bound, var.bound_ * var.sharing_penalty_, sg_maxmin_precision)) {
581 var.value_ = var.bound_;
582 XBT_DEBUG("Setting %p (%d) value to %f\n", &var, var.rank_, var.value_);
584 // Variables which bound is different are not considered for this cycle, but they will be afterwards.
585 XBT_DEBUG("Do not consider %p (%d) \n", &var, var.rank_);
586 var_list.pop_front();
590 XBT_DEBUG("Min usage: %f, Var(%d).penalty: %f, Var(%d).value: %f ", min_usage, var.rank_, var.sharing_penalty_,
591 var.rank_, var.value_);
593 /* Update the usage of contraints where this variable is involved */
594 for (Element& elem : var.cnsts_) {
595 Constraint* cnst = elem.constraint;
596 if (cnst->sharing_policy_ != s4u::Link::SharingPolicy::FATPIPE) {
597 // Remember: shared constraints require that sum(elem.value * var.value) < cnst->bound
598 double_update(&(cnst->remaining_), elem.consumption_weight * var.value_, cnst->bound_ * sg_maxmin_precision);
599 double_update(&(cnst->usage_), elem.consumption_weight / var.sharing_penalty_, sg_maxmin_precision);
600 // If the constraint is saturated, remove it from the set of active constraints (light_tab)
601 if (not double_positive(cnst->usage_, sg_maxmin_precision) ||
602 not double_positive(cnst->remaining_, cnst->bound_ * sg_maxmin_precision)) {
603 if (cnst->cnst_light_) {
604 int index = (cnst->cnst_light_ - cnst_light_tab);
605 XBT_DEBUG("index: %d \t cnst_light_num: %d \t || usage: %f remaining: %f bound: %f ", index,
606 cnst_light_num, cnst->usage_, cnst->remaining_, cnst->bound_);
607 cnst_light_tab[index] = cnst_light_tab[cnst_light_num - 1];
608 cnst_light_tab[index].cnst->cnst_light_ = &cnst_light_tab[index];
610 cnst->cnst_light_ = nullptr;
613 cnst->cnst_light_->remaining_over_usage = cnst->remaining_ / cnst->usage_;
615 elem.make_inactive();
617 // Remember: non-shared constraints only require that max(elem.value * var.value) < cnst->bound
619 elem.make_inactive();
620 for (Element& elem2 : cnst->enabled_element_set_) {
621 xbt_assert(elem2.variable->sharing_penalty_ > 0);
622 if (elem2.variable->value_ > 0)
624 if (elem2.consumption_weight > 0)
625 cnst->usage_ = std::max(cnst->usage_, elem2.consumption_weight / elem2.variable->sharing_penalty_);
627 // If the constraint is saturated, remove it from the set of active constraints (light_tab)
628 if (not double_positive(cnst->usage_, sg_maxmin_precision) ||
629 not double_positive(cnst->remaining_, cnst->bound_ * sg_maxmin_precision)) {
630 if (cnst->cnst_light_) {
631 int index = (cnst->cnst_light_ - cnst_light_tab);
632 XBT_DEBUG("index: %d \t cnst_light_num: %d \t || \t cnst: %p \t cnst->cnst_light: %p "
633 "\t cnst_light_tab: %p usage: %f remaining: %f bound: %f ",
634 index, cnst_light_num, cnst, cnst->cnst_light_, cnst_light_tab, cnst->usage_, cnst->remaining_,
636 cnst_light_tab[index] = cnst_light_tab[cnst_light_num - 1];
637 cnst_light_tab[index].cnst->cnst_light_ = &cnst_light_tab[index];
639 cnst->cnst_light_ = nullptr;
642 cnst->cnst_light_->remaining_over_usage = cnst->remaining_ / cnst->usage_;
643 xbt_assert(not cnst->active_element_set_.empty(),
644 "Should not keep a maximum constraint that has no active"
645 " element! You want to check the maxmin precision and possible rounding effects.");
649 var_list.pop_front();
652 /* Find out which variables reach the maximum */
655 saturated_constraints.clear();
657 for (pos = 0; pos < cnst_light_num; pos++) {
658 xbt_assert(not cnst_light_tab[pos].cnst->active_element_set_.empty(),
659 "Cannot saturate more a constraint that has"
660 " no active element! You may want to change the maxmin precision (--cfg=maxmin/precision:<new_value>)"
661 " because of possible rounding effects.\n\tFor the record, the usage of this constraint is %g while "
662 "the maxmin precision to which it is compared is %g.\n\tThe usage of the previous constraint is %g.",
663 cnst_light_tab[pos].cnst->usage_, sg_maxmin_precision, cnst_light_tab[pos - 1].cnst->usage_);
664 saturated_constraints_update(cnst_light_tab[pos].remaining_over_usage, pos, saturated_constraints, &min_usage);
667 saturated_variable_set_update(cnst_light_tab, saturated_constraints, this);
669 } while (cnst_light_num > 0);
672 if (selective_update_active)
673 remove_all_modified_set();
675 if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
681 delete[] cnst_light_tab;
684 /** @brief Attribute the value bound to var->bound.
686 * @param var the Variable*
687 * @param bound the new bound to associate with var
689 * Makes var->bound equal to bound. Whenever this function is called a change is signed in the system. To
690 * avoid false system changing detection it is a good idea to test (bound != 0) before calling it.
692 void System::update_variable_bound(Variable* var, double bound)
697 if (not var->cnsts_.empty())
698 update_modified_set(var->cnsts_[0].constraint);
701 void Variable::initialize(resource::Action* id_value, double sharing_penalty, double bound_value,
702 int number_of_constraints, unsigned visited_value)
705 rank_ = next_rank_++;
706 cnsts_.reserve(number_of_constraints);
707 sharing_penalty_ = sharing_penalty;
708 staged_penalty_ = 0.0;
709 bound_ = bound_value;
710 concurrency_share_ = 1;
712 visited_ = visited_value;
715 xbt_assert(not variable_set_hook_.is_linked());
716 xbt_assert(not saturated_variable_set_hook_.is_linked());
719 int Variable::get_min_concurrency_slack() const
721 int minslack = std::numeric_limits<int>::max();
722 for (Element const& elem : cnsts_) {
723 int slack = elem.constraint->get_concurrency_slack();
724 if (slack < minslack) {
725 // This is only an optimization, to avoid looking at more constraints when slack is already zero
734 // Small remark: In this implementation of System::enable_var() and System::disable_var(), we will meet multiple times
735 // with var when running System::update_modified_set().
736 // A priori not a big performance issue, but we might do better by calling System::update_modified_set() within the for
737 // loops (after doing the first for enabling==1, and before doing the last for disabling==1)
738 void System::enable_var(Variable* var)
740 xbt_assert(not XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug) || var->can_enable());
742 var->sharing_penalty_ = var->staged_penalty_;
743 var->staged_penalty_ = 0;
745 // Enabling the variable, move var to list head. Subtlety is: here, we need to call update_modified_set AFTER
746 // moving at least one element of var.
748 simgrid::xbt::intrusive_erase(variable_set, *var);
749 variable_set.push_front(*var);
750 for (Element& elem : var->cnsts_) {
751 simgrid::xbt::intrusive_erase(elem.constraint->disabled_element_set_, elem);
752 elem.constraint->enabled_element_set_.push_front(elem);
753 elem.increase_concurrency();
755 if (not var->cnsts_.empty())
756 update_modified_set(var->cnsts_[0].constraint);
758 // When used within on_disabled_var, we would get an assertion fail, because transiently there can be variables
759 // that are staged and could be activated.
760 // Anyway, caller functions all call check_concurrency() in the end.
763 void System::disable_var(Variable* var)
765 xbt_assert(not var->staged_penalty_, "Staged penalty should have been cleared");
766 // Disabling the variable, move to var to list tail. Subtlety is: here, we need to call update_modified_set
767 // BEFORE moving the last element of var.
768 simgrid::xbt::intrusive_erase(variable_set, *var);
769 variable_set.push_back(*var);
770 if (not var->cnsts_.empty())
771 update_modified_set(var->cnsts_[0].constraint);
772 for (Element& elem : var->cnsts_) {
773 simgrid::xbt::intrusive_erase(elem.constraint->enabled_element_set_, elem);
774 elem.constraint->disabled_element_set_.push_back(elem);
775 if (elem.active_element_set_hook.is_linked())
776 simgrid::xbt::intrusive_erase(elem.constraint->active_element_set_, elem);
777 elem.decrease_concurrency();
780 var->sharing_penalty_ = 0.0;
781 var->staged_penalty_ = 0.0;
786 /* /brief Find variables that can be enabled and enable them.
788 * Assuming that the variable has already been removed from non-zero penalties
789 * Can we find a staged variable to add?
790 * If yes, check that none of the constraints that this variable is involved in is at the limit of its concurrency
791 * And then add it to enabled variables
793 void System::on_disabled_var(Constraint* cnstr)
795 if (cnstr->get_concurrency_limit() < 0)
798 int numelem = cnstr->disabled_element_set_.size();
802 Element* elem = &cnstr->disabled_element_set_.front();
804 // Cannot use foreach loop, because System::enable_var() will modify disabled_element_set.. within the loop
805 while (numelem-- && elem) {
808 if (elem->disabled_element_set_hook.is_linked()) {
809 auto iter = std::next(cnstr->disabled_element_set_.iterator_to(*elem));
810 nextelem = iter != std::end(cnstr->disabled_element_set_) ? &*iter : nullptr;
815 if (elem->variable->staged_penalty_ > 0 && elem->variable->can_enable()) {
816 // Found a staged variable
817 // TODOLATER: Add random timing function to model reservation protocol fuzziness? Then how to make sure that
818 // staged variables will eventually be called?
819 enable_var(elem->variable);
822 xbt_assert(cnstr->concurrency_current_ <= cnstr->get_concurrency_limit(), "Concurrency overflow!");
823 if (cnstr->concurrency_current_ == cnstr->get_concurrency_limit())
829 // We could get an assertion fail, because transiently there can be variables that are staged and could be activated.
830 // And we need to go through all constraints of the disabled var before getting back a coherent state.
831 // Anyway, caller functions all call check_concurrency() in the end.
834 /** @brief update the penalty of a variable (disable it by passing 0 as a penalty) */
835 void System::update_variable_penalty(Variable* var, double penalty)
837 xbt_assert(penalty >= 0, "Variable penalty should not be negative!");
839 if (penalty == var->sharing_penalty_)
842 int enabling_var = (penalty > 0 && var->sharing_penalty_ <= 0);
843 int disabling_var = (penalty <= 0 && var->sharing_penalty_ > 0);
845 XBT_IN("(sys=%p, var=%p, penalty=%f)", this, var, penalty);
849 // Are we enabling this variable?
851 var->staged_penalty_ = penalty;
852 int minslack = var->get_min_concurrency_slack();
853 if (minslack < var->concurrency_share_) {
854 XBT_DEBUG("Staging var (instead of enabling) because min concurrency slack %i, with penalty %f and concurrency"
856 minslack, penalty, var->concurrency_share_);
859 XBT_DEBUG("Enabling var with min concurrency slack %i", minslack);
861 } else if (disabling_var) {
864 var->sharing_penalty_ = penalty;
872 void System::update_constraint_bound(Constraint* cnst, double bound)
875 update_modified_set(cnst);
876 cnst->bound_ = bound;
879 /** @brief Update the constraint set propagating recursively to other constraints so the system should not be entirely
882 * @param cnst the Constraint* affected by the change
884 * A recursive algorithm to optimize the system recalculation selecting only constraints that have changed. Each
885 * constraint change is propagated to the list of constraints for each variable.
887 void System::update_modified_set_rec(Constraint* cnst)
889 for (Element const& elem : cnst->enabled_element_set_) {
890 Variable* var = elem.variable;
891 for (Element const& elem2 : var->cnsts_) {
892 if (var->visited_ == visited_counter_)
894 if (elem2.constraint != cnst && not elem2.constraint->modified_constraint_set_hook_.is_linked()) {
895 modified_constraint_set.push_back(*elem2.constraint);
896 update_modified_set_rec(elem2.constraint);
899 // var will be ignored in later visits as long as sys->visited_counter does not move
900 var->visited_ = visited_counter_;
904 void System::update_modified_set(Constraint* cnst)
906 /* nothing to do if selective update isn't active */
907 if (selective_update_active && not cnst->modified_constraint_set_hook_.is_linked()) {
908 modified_constraint_set.push_back(*cnst);
909 update_modified_set_rec(cnst);
913 void System::remove_all_modified_set()
915 // We cleverly un-flag all variables just by incrementing visited_counter
916 // In effect, the var->visited value will no more be equal to visited counter
917 // To be clean, when visited counter has wrapped around, we force these var->visited values so that variables that
918 // were in the modified a long long time ago are not wrongly skipped here, which would lead to very nasty bugs
919 // (i.e. not readibily reproducible, and requiring a lot of run time before happening).
920 if (++visited_counter_ == 1) {
921 /* the counter wrapped around, reset each variable->visited */
922 for (Variable& var : variable_set)
925 modified_constraint_set.clear();
929 * Returns resource load (in flop per second, or byte per second, or similar)
931 * If the resource is shared (the default case), the load is sum of resource usage made by
932 * every variables located on this resource.
934 * If the resource is not shared (ie in FATPIPE mode), then the load is the max (not the sum)
935 * of all resource usages located on this resource.
937 double Constraint::get_usage() const
940 if (sharing_policy_ != s4u::Link::SharingPolicy::FATPIPE) {
941 for (Element const& elem : enabled_element_set_)
942 if (elem.consumption_weight > 0)
943 result += elem.consumption_weight * elem.variable->value_;
945 for (Element const& elem : enabled_element_set_)
946 if (elem.consumption_weight > 0)
947 result = std::max(result, elem.consumption_weight * elem.variable->value_);
952 int Constraint::get_variable_amount() const
954 return std::count_if(std::begin(enabled_element_set_), std::end(enabled_element_set_),
955 [](const Element& elem) { return elem.consumption_weight > 0; });