1 /* Copyright (c) 2004-2018. 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 /* \file callbacks.h */
8 #include "src/kernel/lmm/maxmin.hpp"
9 #include "xbt/backtrace.hpp"
11 #include "xbt/mallocator.h"
12 #include "xbt/sysdep.h"
13 #include "xbt/utility.hpp"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_maxmin, surf, "Logging specific to SURF (maxmin)");
23 double sg_maxmin_precision = 0.00001; /* Change this with --cfg=maxmin/precision:VALUE */
24 double sg_surf_precision = 0.00001; /* Change this with --cfg=surf/precision:VALUE */
25 int sg_concurrency_limit = -1; /* Change this with --cfg=maxmin/concurrency-limit:VALUE */
31 typedef std::vector<int> dyn_light_t;
33 int Variable::Global_debug_id = 1;
34 int Constraint::Global_debug_id = 1;
36 System* make_new_maxmin_system(bool selective_update)
38 return new System(selective_update);
41 int Element::get_concurrency() const
43 // Ignore element with weight less than one (e.g. cross-traffic)
44 return (consumption_weight >= 1) ? 1 : 0;
45 // There are other alternatives, but they will change the behaviour of the model..
46 // So do not use it unless you want to make a new model.
47 // If you do, remember to change the variables concurrency share to reflect it.
48 // Potential examples are:
49 // return (elem->weight>0)?1:0;//Include element as soon as weight is non-zero
50 // return (int)ceil(elem->weight);//Include element as the rounded-up integer value of the element weight
53 void Element::decrease_concurrency()
55 xbt_assert(constraint->concurrency_current >= get_concurrency());
56 constraint->concurrency_current -= get_concurrency();
59 void Element::increase_concurrency()
61 constraint->concurrency_current += get_concurrency();
63 if (constraint->concurrency_current > constraint->concurrency_maximum)
64 constraint->concurrency_maximum = constraint->concurrency_current;
66 xbt_assert(constraint->get_concurrency_limit() < 0 ||
67 constraint->concurrency_current <= constraint->get_concurrency_limit(),
68 "Concurrency limit overflow!");
71 void System::check_concurrency() const
73 // These checks are very expensive, so do them only if we want to debug SURF LMM
74 if (not XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug))
77 for (Constraint const& cnst : constraint_set) {
79 for (Element const& elem : cnst.enabled_element_set) {
80 xbt_assert(elem.variable->sharing_weight > 0);
81 concurrency += elem.get_concurrency();
84 for (Element const& elem : cnst.disabled_element_set) {
85 // We should have staged variables only if concurrency is reached in some constraint
86 xbt_assert(cnst.get_concurrency_limit() < 0 || elem.variable->staged_weight == 0 ||
87 elem.variable->get_min_concurrency_slack() < elem.variable->concurrency_share,
88 "should not have staged variable!");
91 xbt_assert(cnst.get_concurrency_limit() < 0 || cnst.get_concurrency_limit() >= concurrency,
92 "concurrency check failed!");
93 xbt_assert(cnst.concurrency_current == concurrency, "concurrency_current is out-of-date!");
96 // Check that for each variable, all corresponding elements are in the same state (i.e. same element sets)
97 for (Variable const& var : variable_set) {
98 if (var.cnsts.empty())
101 const Element& elem = var.cnsts[0];
102 int belong_to_enabled = elem.enabled_element_set_hook.is_linked();
103 int belong_to_disabled = elem.disabled_element_set_hook.is_linked();
104 int belong_to_active = elem.active_element_set_hook.is_linked();
106 for (Element const& elem2 : var.cnsts) {
107 xbt_assert(belong_to_enabled == elem2.enabled_element_set_hook.is_linked(),
108 "Variable inconsistency (1): enabled_element_set");
109 xbt_assert(belong_to_disabled == elem2.disabled_element_set_hook.is_linked(),
110 "Variable inconsistency (2): disabled_element_set");
111 xbt_assert(belong_to_active == elem2.active_element_set_hook.is_linked(),
112 "Variable inconsistency (3): active_element_set");
117 void System::var_free(Variable* var)
119 XBT_IN("(sys=%p, var=%p)", this, var);
122 // TODOLATER Can do better than that by leaving only the variable in only one enabled_element_set, call
123 // update_modified_set, and then remove it..
124 if (not var->cnsts.empty())
125 update_modified_set(var->cnsts[0].constraint);
127 for (Element& elem : var->cnsts) {
128 if (var->sharing_weight > 0)
129 elem.decrease_concurrency();
130 if (elem.enabled_element_set_hook.is_linked())
131 simgrid::xbt::intrusive_erase(elem.constraint->enabled_element_set, elem);
132 if (elem.disabled_element_set_hook.is_linked())
133 simgrid::xbt::intrusive_erase(elem.constraint->disabled_element_set, elem);
134 if (elem.active_element_set_hook.is_linked())
135 simgrid::xbt::intrusive_erase(elem.constraint->active_element_set, elem);
136 int nelements = elem.constraint->enabled_element_set.size() + elem.constraint->disabled_element_set.size();
138 make_constraint_inactive(elem.constraint);
140 on_disabled_var(elem.constraint);
147 xbt_mallocator_release(variable_mallocator_, var);
151 System::System(bool selective_update) : selective_update_active(selective_update)
153 XBT_DEBUG("Setting selective_update_active flag to %d", selective_update_active);
161 while ((var = extract_variable())) {
162 auto demangled = simgrid::xbt::demangle(typeid(*var->id).name());
163 XBT_WARN("Probable bug: a %s variable (#%d) not removed before the LMM system destruction.", demangled.get(),
167 while ((cnst = extract_constraint()))
170 xbt_mallocator_free(variable_mallocator_);
171 delete modified_set_;
174 void System::cnst_free(Constraint* cnst)
176 make_constraint_inactive(cnst);
180 Constraint::Constraint(void* id_value, double bound_value) : bound(bound_value), id(id_value)
182 id_int = Global_debug_id++;
186 concurrency_limit = sg_concurrency_limit;
187 concurrency_current = 0;
188 concurrency_maximum = 0;
189 sharing_policy = 1; /* FIXME: don't hardcode the value */
193 cnst_light = nullptr;
196 Constraint* System::constraint_new(void* id, double bound_value)
198 Constraint* cnst = new Constraint(id, bound_value);
199 insert_constraint(cnst);
203 void* System::variable_mallocator_new_f()
208 void System::variable_mallocator_free_f(void* var)
210 delete static_cast<Variable*>(var);
213 Variable* System::variable_new(simgrid::kernel::resource::Action* id, double sharing_weight, double bound,
214 int number_of_constraints)
216 XBT_IN("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)", this, id, sharing_weight, bound, number_of_constraints);
218 Variable* var = static_cast<Variable*>(xbt_mallocator_get(variable_mallocator_));
219 var->initialize(id, sharing_weight, bound, number_of_constraints, visited_counter_ - 1);
221 variable_set.push_front(*var);
223 variable_set.push_back(*var);
225 XBT_OUT(" returns %p", var);
229 void System::variable_free(Variable* var)
231 remove_variable(var);
235 void System::expand(Constraint* cnst, Variable* var, double consumption_weight)
239 // Check if this variable already has an active element in this constraint
240 // If it does, substract it from the required slack
241 int current_share = 0;
242 if (var->concurrency_share > 1) {
243 for (Element& elem : var->cnsts) {
244 if (elem.constraint == cnst && elem.enabled_element_set_hook.is_linked())
245 current_share += elem.get_concurrency();
249 // Check if we need to disable the variable
250 if (var->sharing_weight > 0 && var->concurrency_share - current_share > cnst->get_concurrency_slack()) {
251 double weight = var->sharing_weight;
253 for (Element const& elem : var->cnsts)
254 on_disabled_var(elem.constraint);
255 consumption_weight = 0;
256 var->staged_weight = weight;
257 xbt_assert(not var->sharing_weight);
260 xbt_assert(var->cnsts.size() < var->cnsts.capacity(), "Too much constraints");
262 var->cnsts.resize(var->cnsts.size() + 1);
263 Element& elem = var->cnsts.back();
265 elem.consumption_weight = consumption_weight;
266 elem.constraint = cnst;
269 if (var->sharing_weight) {
270 elem.constraint->enabled_element_set.push_front(elem);
271 elem.increase_concurrency();
273 elem.constraint->disabled_element_set.push_back(elem);
275 if (not selective_update_active) {
276 make_constraint_active(cnst);
277 } else if (elem.consumption_weight > 0 || var->sharing_weight > 0) {
278 make_constraint_active(cnst);
279 update_modified_set(cnst);
280 // TODOLATER: Why do we need this second call?
281 if (var->cnsts.size() > 1)
282 update_modified_set(var->cnsts[0].constraint);
288 void System::expand_add(Constraint* cnst, Variable* var, double value)
294 // BEWARE: In case you have multiple elements in one constraint, this will always add value to the first element.
296 std::find_if(begin(var->cnsts), end(var->cnsts), [&cnst](Element const& x) { return x.constraint == cnst; });
297 if (elem_it != end(var->cnsts)) {
298 Element& elem = *elem_it;
299 if (var->sharing_weight)
300 elem.decrease_concurrency();
302 if (cnst->sharing_policy)
303 elem.consumption_weight += value;
305 elem.consumption_weight = std::max(elem.consumption_weight, value);
307 // We need to check that increasing value of the element does not cross the concurrency limit
308 if (var->sharing_weight) {
309 if (cnst->get_concurrency_slack() < elem.get_concurrency()) {
310 double weight = var->sharing_weight;
312 for (Element const& elem2 : var->cnsts)
313 on_disabled_var(elem2.constraint);
314 var->staged_weight = weight;
315 xbt_assert(not var->sharing_weight);
317 elem.increase_concurrency();
319 update_modified_set(cnst);
321 expand(cnst, var, value);
326 Variable* Constraint::get_variable(const_lmm_element_t* elem) const
328 if (*elem == nullptr) {
329 // That is the first call, pick the first element among enabled_element_set (or disabled_element_set if
330 // enabled_element_set is empty)
331 if (not enabled_element_set.empty())
332 *elem = &enabled_element_set.front();
333 else if (not disabled_element_set.empty())
334 *elem = &disabled_element_set.front();
338 // elem is not null, so we carry on
339 if ((*elem)->enabled_element_set_hook.is_linked()) {
340 // Look at enabled_element_set, and jump to disabled_element_set when finished
341 auto iter = std::next(enabled_element_set.iterator_to(**elem));
342 if (iter != std::end(enabled_element_set))
344 else if (not disabled_element_set.empty())
345 *elem = &disabled_element_set.front();
349 auto iter = std::next(disabled_element_set.iterator_to(**elem));
350 *elem = iter != std::end(disabled_element_set) ? &*iter : nullptr;
354 return (*elem)->variable;
359 // if we modify the list between calls, normal version may loop forever
360 // this safe version ensures that we browse the list elements only once
361 Variable* Constraint::get_variable_safe(const_lmm_element_t* elem, const_lmm_element_t* nextelem, int* numelem) const
363 if (*elem == nullptr) {
364 *numelem = enabled_element_set.size() + disabled_element_set.size() - 1;
365 if (not enabled_element_set.empty())
366 *elem = &enabled_element_set.front();
367 else if (not disabled_element_set.empty())
368 *elem = &disabled_element_set.front();
379 // elem is not null, so we carry on
380 if ((*elem)->enabled_element_set_hook.is_linked()) {
381 // Look at enabled_element_set, and jump to disabled_element_set when finished
382 auto iter = std::next(enabled_element_set.iterator_to(**elem));
383 if (iter != std::end(enabled_element_set))
385 else if (not disabled_element_set.empty())
386 *nextelem = &disabled_element_set.front();
390 auto iter = std::next(disabled_element_set.iterator_to(**elem));
391 *nextelem = iter != std::end(disabled_element_set) ? &*iter : nullptr;
393 return (*elem)->variable;
398 static inline void saturated_constraints_update(double usage, int cnst_light_num, dyn_light_t& saturated_constraints,
401 xbt_assert(usage > 0, "Impossible");
403 if (*min_usage < 0 || *min_usage > usage) {
405 XBT_HERE(" min_usage=%f (cnst->remaining / cnst->usage =%f)", *min_usage, usage);
406 saturated_constraints.assign(1, cnst_light_num);
407 } else if (*min_usage == usage) {
408 saturated_constraints.emplace_back(cnst_light_num);
412 static inline void saturated_variable_set_update(ConstraintLight* cnst_light_tab,
413 const dyn_light_t& saturated_constraints, System* sys)
415 /* Add active variables (i.e. variables that need to be set) from the set of constraints to saturate
417 for (int const& saturated_cnst : saturated_constraints) {
418 ConstraintLight& cnst = cnst_light_tab[saturated_cnst];
419 for (Element const& elem : cnst.cnst->active_element_set) {
420 // Visiting active_element_set, so, by construction, should never get a zero weight, correct?
421 xbt_assert(elem.variable->sharing_weight > 0);
422 if (elem.consumption_weight > 0 && not elem.variable->saturated_variable_set_hook.is_linked())
423 sys->saturated_variable_set.push_back(*elem.variable);
428 template <class ElemList>
429 static void format_element_list(const ElemList& elem_list, int sharing_policy, double& sum, std::string& buf)
431 for (Element const& elem : elem_list) {
432 buf += std::to_string(elem.consumption_weight) + ".'" + std::to_string(elem.variable->id_int) + "'(" +
433 std::to_string(elem.variable->value) + ")" + (sharing_policy ? " + " : " , ");
435 sum += elem.consumption_weight * elem.variable->value;
437 sum = std::max(sum, elem.consumption_weight * elem.variable->value);
441 void System::print() const
443 std::string buf = std::string("MAX-MIN ( ");
445 /* Printing Objective */
446 for (Variable const& var : variable_set)
447 buf += "'" + std::to_string(var.id_int) + "'(" + std::to_string(var.sharing_weight) + ") ";
449 XBT_DEBUG("%20s", buf.c_str());
452 XBT_DEBUG("Constraints");
453 /* Printing Constraints */
454 for (Constraint const& cnst : active_constraint_set) {
456 // Show the enabled variables
458 buf += cnst.sharing_policy ? "(" : "max(";
459 format_element_list(cnst.enabled_element_set, cnst.sharing_policy, sum, buf);
460 // TODO: Adding disabled elements only for test compatibility, but do we really want them to be printed?
461 format_element_list(cnst.disabled_element_set, cnst.sharing_policy, sum, buf);
463 buf += "0) <= " + std::to_string(cnst.bound) + " ('" + std::to_string(cnst.id_int) + "')";
465 if (not cnst.sharing_policy) {
466 buf += " [MAX-Constraint]";
468 XBT_DEBUG("%s", buf.c_str());
470 xbt_assert(not double_positive(sum - cnst.bound, cnst.bound * sg_maxmin_precision),
471 "Incorrect value (%f is not smaller than %f): %g", sum, cnst.bound, sum - cnst.bound);
474 XBT_DEBUG("Variables");
475 /* Printing Result */
476 for (Variable const& var : variable_set) {
478 XBT_DEBUG("'%d'(%f) : %f (<=%f)", var.id_int, var.sharing_weight, var.value, var.bound);
479 xbt_assert(not double_positive(var.value - var.bound, var.bound * sg_maxmin_precision),
480 "Incorrect value (%f is not smaller than %f", var.value, var.bound);
482 XBT_DEBUG("'%d'(%f) : %f", var.id_int, var.sharing_weight, var.value);
487 void System::lmm_solve()
490 XBT_IN("(sys=%p)", this);
491 /* Compute Usage and store the variables that reach the maximum. If selective_update_active is true, only
492 * constraints that changed are considered. Otherwise all constraints with active actions are considered.
494 if (selective_update_active)
495 lmm_solve(modified_constraint_set);
497 lmm_solve(active_constraint_set);
502 template <class CnstList> void System::lmm_solve(CnstList& cnst_list)
504 double min_usage = -1;
505 double min_bound = -1;
507 XBT_DEBUG("Active constraints : %zu", cnst_list.size());
508 /* Init: Only modified code portions: reset the value of active variables */
509 for (Constraint const& cnst : cnst_list) {
510 for (Element const& elem : cnst.enabled_element_set) {
511 xbt_assert(elem.variable->sharing_weight > 0.0);
512 elem.variable->value = 0.0;
516 ConstraintLight* cnst_light_tab = new ConstraintLight[cnst_list.size()]();
517 int cnst_light_num = 0;
518 dyn_light_t saturated_constraints;
520 for (Constraint& cnst : cnst_list) {
521 /* INIT: Collect constraints that actually need to be saturated (i.e remaining and usage are strictly positive)
522 * into cnst_light_tab. */
523 cnst.remaining = cnst.bound;
524 if (not double_positive(cnst.remaining, cnst.bound * sg_maxmin_precision))
527 for (Element& elem : cnst.enabled_element_set) {
528 xbt_assert(elem.variable->sharing_weight > 0);
529 if (elem.consumption_weight > 0) {
530 if (cnst.sharing_policy)
531 cnst.usage += elem.consumption_weight / elem.variable->sharing_weight;
532 else if (cnst.usage < elem.consumption_weight / elem.variable->sharing_weight)
533 cnst.usage = elem.consumption_weight / elem.variable->sharing_weight;
536 simgrid::kernel::resource::Action* action = static_cast<simgrid::kernel::resource::Action*>(elem.variable->id);
537 if (modified_set_ && not action->is_within_modified_set())
538 modified_set_->push_back(*action);
541 XBT_DEBUG("Constraint '%d' usage: %f remaining: %f concurrency: %i<=%i<=%i", cnst.id_int, cnst.usage,
542 cnst.remaining, cnst.concurrency_current, cnst.concurrency_maximum, cnst.get_concurrency_limit());
543 /* Saturated constraints update */
545 if (cnst.usage > 0) {
546 cnst_light_tab[cnst_light_num].cnst = &cnst;
547 cnst.cnst_light = &cnst_light_tab[cnst_light_num];
548 cnst_light_tab[cnst_light_num].remaining_over_usage = cnst.remaining / cnst.usage;
549 saturated_constraints_update(cnst_light_tab[cnst_light_num].remaining_over_usage, cnst_light_num,
550 saturated_constraints, &min_usage);
551 xbt_assert(not cnst.active_element_set.empty(),
552 "There is no sense adding a constraint that has no active element!");
557 saturated_variable_set_update(cnst_light_tab, saturated_constraints, this);
559 /* Saturated variables update */
561 /* Fix the variables that have to be */
562 auto& var_list = saturated_variable_set;
563 for (Variable const& var : var_list) {
564 if (var.sharing_weight <= 0.0)
566 /* First check if some of these variables could reach their upper bound and update min_bound accordingly. */
567 XBT_DEBUG("var=%d, var.bound=%f, var.weight=%f, min_usage=%f, var.bound*var.weight=%f", var.id_int, var.bound,
568 var.sharing_weight, min_usage, var.bound * var.sharing_weight);
569 if ((var.bound > 0) && (var.bound * var.sharing_weight < min_usage)) {
571 min_bound = var.bound * var.sharing_weight;
573 min_bound = std::min(min_bound, (var.bound * var.sharing_weight));
574 XBT_DEBUG("Updated min_bound=%f", min_bound);
578 while (not var_list.empty()) {
579 Variable& var = var_list.front();
581 // If no variable could reach its bound, deal iteratively the constraints usage ( at worst one constraint is
582 // saturated at each cycle)
583 var.value = min_usage / var.sharing_weight;
584 XBT_DEBUG("Setting var (%d) value to %f\n", var.id_int, var.value);
586 // If there exist a variable that can reach its bound, only update it (and other with the same bound) for now.
587 if (double_equals(min_bound, var.bound * var.sharing_weight, sg_maxmin_precision)) {
588 var.value = var.bound;
589 XBT_DEBUG("Setting %p (%d) value to %f\n", &var, var.id_int, var.value);
591 // Variables which bound is different are not considered for this cycle, but they will be afterwards.
592 XBT_DEBUG("Do not consider %p (%d) \n", &var, var.id_int);
593 var_list.pop_front();
597 XBT_DEBUG("Min usage: %f, Var(%d).weight: %f, Var(%d).value: %f ", min_usage, var.id_int, var.sharing_weight,
598 var.id_int, var.value);
600 /* Update the usage of contraints where this variable is involved */
601 for (Element& elem : var.cnsts) {
602 Constraint* cnst = elem.constraint;
603 if (cnst->sharing_policy) {
604 // Remember: shared constraints require that sum(elem.value * var.value) < cnst->bound
605 double_update(&(cnst->remaining), elem.consumption_weight * var.value, cnst->bound * sg_maxmin_precision);
606 double_update(&(cnst->usage), elem.consumption_weight / var.sharing_weight, sg_maxmin_precision);
607 // If the constraint is saturated, remove it from the set of active constraints (light_tab)
608 if (not double_positive(cnst->usage, sg_maxmin_precision) ||
609 not double_positive(cnst->remaining, cnst->bound * sg_maxmin_precision)) {
610 if (cnst->cnst_light) {
611 int index = (cnst->cnst_light - cnst_light_tab);
612 XBT_DEBUG("index: %d \t cnst_light_num: %d \t || usage: %f remaining: %f bound: %f ", index,
613 cnst_light_num, cnst->usage, cnst->remaining, cnst->bound);
614 cnst_light_tab[index] = cnst_light_tab[cnst_light_num - 1];
615 cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
617 cnst->cnst_light = nullptr;
620 cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
622 elem.make_inactive();
624 // Remember: non-shared constraints only require that max(elem.value * var.value) < cnst->bound
626 elem.make_inactive();
627 for (Element& elem2 : cnst->enabled_element_set) {
628 xbt_assert(elem2.variable->sharing_weight > 0);
629 if (elem2.variable->value > 0)
631 if (elem2.consumption_weight > 0)
632 cnst->usage = std::max(cnst->usage, elem2.consumption_weight / elem2.variable->sharing_weight);
634 // If the constraint is saturated, remove it from the set of active constraints (light_tab)
635 if (not double_positive(cnst->usage, sg_maxmin_precision) ||
636 not double_positive(cnst->remaining, cnst->bound * sg_maxmin_precision)) {
637 if (cnst->cnst_light) {
638 int index = (cnst->cnst_light - cnst_light_tab);
639 XBT_DEBUG("index: %d \t cnst_light_num: %d \t || \t cnst: %p \t cnst->cnst_light: %p "
640 "\t cnst_light_tab: %p usage: %f remaining: %f bound: %f ",
641 index, cnst_light_num, cnst, cnst->cnst_light, cnst_light_tab, cnst->usage, cnst->remaining,
643 cnst_light_tab[index] = cnst_light_tab[cnst_light_num - 1];
644 cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
646 cnst->cnst_light = nullptr;
649 cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
650 xbt_assert(not cnst->active_element_set.empty(),
651 "Should not keep a maximum constraint that has no active"
652 " element! You want to check the maxmin precision and possible rounding effects.");
656 var_list.pop_front();
659 /* Find out which variables reach the maximum */
662 saturated_constraints.clear();
664 for (pos = 0; pos < cnst_light_num; pos++) {
665 xbt_assert(not cnst_light_tab[pos].cnst->active_element_set.empty(),
666 "Cannot saturate more a constraint that has"
667 " no active element! You may want to change the maxmin precision (--cfg=maxmin/precision:<new_value>)"
668 " because of possible rounding effects.\n\tFor the record, the usage of this constraint is %g while "
669 "the maxmin precision to which it is compared is %g.\n\tThe usage of the previous constraint is %g.",
670 cnst_light_tab[pos].cnst->usage, sg_maxmin_precision, cnst_light_tab[pos - 1].cnst->usage);
671 saturated_constraints_update(cnst_light_tab[pos].remaining_over_usage, pos, saturated_constraints, &min_usage);
674 saturated_variable_set_update(cnst_light_tab, saturated_constraints, this);
676 } while (cnst_light_num > 0);
679 if (selective_update_active)
680 remove_all_modified_set();
682 if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
688 delete[] cnst_light_tab;
691 /** \brief Attribute the value bound to var->bound.
693 * \param var the Variable*
694 * \param bound the new bound to associate with var
696 * Makes var->bound equal to bound. Whenever this function is called a change is signed in the system. To
697 * avoid false system changing detection it is a good idea to test (bound != 0) before calling it.
699 void System::update_variable_bound(Variable* var, double bound)
704 if (not var->cnsts.empty())
705 update_modified_set(var->cnsts[0].constraint);
708 void Variable::initialize(simgrid::kernel::resource::Action* id_value, double sharing_weight_value, double bound_value,
709 int number_of_constraints, unsigned visited_value)
712 id_int = Global_debug_id++;
713 cnsts.reserve(number_of_constraints);
714 sharing_weight = sharing_weight_value;
717 concurrency_share = 1;
719 visited = visited_value;
723 xbt_assert(not variable_set_hook.is_linked());
724 xbt_assert(not saturated_variable_set_hook.is_linked());
727 int Variable::get_min_concurrency_slack() const
729 int minslack = std::numeric_limits<int>::max();
730 for (Element const& elem : cnsts) {
731 int slack = elem.constraint->get_concurrency_slack();
732 if (slack < minslack) {
733 // This is only an optimization, to avoid looking at more constraints when slack is already zero
742 // Small remark: In this implementation of System::enable_var() and System::disable_var(), we will meet multiple times
743 // with var when running System::update_modified_set().
744 // A priori not a big performance issue, but we might do better by calling System::update_modified_set() within the for
745 // loops (after doing the first for enabling==1, and before doing the last for disabling==1)
746 void System::enable_var(Variable* var)
748 xbt_assert(not XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug) || var->can_enable());
750 var->sharing_weight = var->staged_weight;
751 var->staged_weight = 0;
753 // Enabling the variable, move var to list head. Subtlety is: here, we need to call update_modified_set AFTER
754 // moving at least one element of var.
756 simgrid::xbt::intrusive_erase(variable_set, *var);
757 variable_set.push_front(*var);
758 for (Element& elem : var->cnsts) {
759 simgrid::xbt::intrusive_erase(elem.constraint->disabled_element_set, elem);
760 elem.constraint->enabled_element_set.push_front(elem);
761 elem.increase_concurrency();
763 if (not var->cnsts.empty())
764 update_modified_set(var->cnsts[0].constraint);
766 // When used within on_disabled_var, we would get an assertion fail, because transiently there can be variables
767 // that are staged and could be activated.
768 // Anyway, caller functions all call check_concurrency() in the end.
771 void System::disable_var(Variable* var)
773 xbt_assert(not var->staged_weight, "Staged weight should have been cleared");
774 // Disabling the variable, move to var to list tail. Subtlety is: here, we need to call update_modified_set
775 // BEFORE moving the last element of var.
776 simgrid::xbt::intrusive_erase(variable_set, *var);
777 variable_set.push_back(*var);
778 if (not var->cnsts.empty())
779 update_modified_set(var->cnsts[0].constraint);
780 for (Element& elem : var->cnsts) {
781 simgrid::xbt::intrusive_erase(elem.constraint->enabled_element_set, elem);
782 elem.constraint->disabled_element_set.push_back(elem);
783 if (elem.active_element_set_hook.is_linked())
784 simgrid::xbt::intrusive_erase(elem.constraint->active_element_set, elem);
785 elem.decrease_concurrency();
788 var->sharing_weight = 0.0;
789 var->staged_weight = 0.0;
794 /* /brief Find variables that can be enabled and enable them.
796 * Assuming that the variable has already been removed from non-zero weights
797 * Can we find a staged variable to add?
798 * If yes, check that none of the constraints that this variable is involved in is at the limit of its concurrency
799 * And then add it to enabled variables
801 void System::on_disabled_var(Constraint* cnstr)
803 if (cnstr->get_concurrency_limit() < 0)
806 int numelem = cnstr->disabled_element_set.size();
810 Element* elem = &cnstr->disabled_element_set.front();
812 // Cannot use foreach loop, because System::enable_var() will modify disabled_element_set.. within the loop
813 while (numelem-- && elem) {
816 if (elem->disabled_element_set_hook.is_linked()) {
817 auto iter = std::next(cnstr->disabled_element_set.iterator_to(*elem));
818 nextelem = iter != std::end(cnstr->disabled_element_set) ? &*iter : nullptr;
823 if (elem->variable->staged_weight > 0 && elem->variable->can_enable()) {
824 // Found a staged variable
825 // TODOLATER: Add random timing function to model reservation protocol fuzziness? Then how to make sure that
826 // staged variables will eventually be called?
827 enable_var(elem->variable);
830 xbt_assert(cnstr->concurrency_current <= cnstr->get_concurrency_limit(), "Concurrency overflow!");
831 if (cnstr->concurrency_current == cnstr->get_concurrency_limit())
837 // We could get an assertion fail, because transiently there can be variables that are staged and could be activated.
838 // And we need to go through all constraints of the disabled var before getting back a coherent state.
839 // Anyway, caller functions all call check_concurrency() in the end.
842 /* \brief update the weight of a variable, and enable/disable it.
843 * @return Returns whether a change was made
845 void System::update_variable_weight(Variable* var, double weight)
847 xbt_assert(weight >= 0, "Variable weight should not be negative!");
849 if (weight == var->sharing_weight)
852 int enabling_var = (weight > 0 && var->sharing_weight <= 0);
853 int disabling_var = (weight <= 0 && var->sharing_weight > 0);
855 XBT_IN("(sys=%p, var=%p, weight=%f)", this, var, weight);
859 // Are we enabling this variable?
861 var->staged_weight = weight;
862 int minslack = var->get_min_concurrency_slack();
863 if (minslack < var->concurrency_share) {
864 XBT_DEBUG("Staging var (instead of enabling) because min concurrency slack %i, with weight %f and concurrency"
866 minslack, weight, var->concurrency_share);
869 XBT_DEBUG("Enabling var with min concurrency slack %i", minslack);
871 } else if (disabling_var) {
872 // Are we disabling this variable?
875 var->sharing_weight = weight;
883 void System::update_constraint_bound(Constraint* cnst, double bound)
886 update_modified_set(cnst);
890 /** \brief Update the constraint set propagating recursively to other constraints so the system should not be entirely
893 * \param cnst the Constraint* affected by the change
895 * A recursive algorithm to optimize the system recalculation selecting only constraints that have changed. Each
896 * constraint change is propagated to the list of constraints for each variable.
898 void System::update_modified_set_rec(Constraint* cnst)
900 for (Element const& elem : cnst->enabled_element_set) {
901 Variable* var = elem.variable;
902 for (Element const& elem2 : var->cnsts) {
903 if (var->visited == visited_counter_)
905 if (elem2.constraint != cnst && not elem2.constraint->modified_constraint_set_hook.is_linked()) {
906 modified_constraint_set.push_back(*elem2.constraint);
907 update_modified_set_rec(elem2.constraint);
910 // var will be ignored in later visits as long as sys->visited_counter does not move
911 var->visited = visited_counter_;
915 void System::update_modified_set(Constraint* cnst)
917 /* nothing to do if selective update isn't active */
918 if (selective_update_active && not cnst->modified_constraint_set_hook.is_linked()) {
919 modified_constraint_set.push_back(*cnst);
920 update_modified_set_rec(cnst);
924 void System::remove_all_modified_set()
926 // We cleverly un-flag all variables just by incrementing visited_counter
927 // In effect, the var->visited value will no more be equal to visited counter
928 // To be clean, when visited counter has wrapped around, we force these var->visited values so that variables that
929 // were in the modified a long long time ago are not wrongly skipped here, which would lead to very nasty bugs
930 // (i.e. not readibily reproducible, and requiring a lot of run time before happening).
931 if (++visited_counter_ == 1) {
932 /* the counter wrapped around, reset each variable->visited */
933 for (Variable& var : variable_set)
936 modified_constraint_set.clear();
940 * Returns resource load (in flop per second, or byte per second, or similar)
942 * If the resource is shared (the default case), the load is sum of resource usage made by every variables located on
945 * If the resource is not shared (ie in FATPIPE mode), then the load is the max (not the sum) of all resource usages
946 * located on this resource.
948 double Constraint::get_usage() const
951 if (sharing_policy) {
952 for (Element const& elem : enabled_element_set)
953 if (elem.consumption_weight > 0)
954 result += elem.consumption_weight * elem.variable->value;
956 for (Element const& elem : enabled_element_set)
957 if (elem.consumption_weight > 0)
958 result = std::max(result, elem.consumption_weight * elem.variable->value);
963 int Constraint::get_variable_amount() const
965 return std::count_if(std::begin(enabled_element_set), std::end(enabled_element_set),
966 [](const Element& elem) { return elem.consumption_weight > 0; });