1 /* Copyright (c) 2004-2023. 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/internal_config.h"
7 #include "src/kernel/lmm/fair_bottleneck.hpp"
8 #include "src/kernel/lmm/maxmin.hpp"
9 #include "src/simgrid/math_utils.h"
10 #if SIMGRID_HAVE_EIGEN3
11 #include "src/kernel/lmm/bmf.hpp"
14 #include <boost/core/demangle.hpp>
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_lmm, kernel, "Kernel Linear Max-Min solver");
19 double sg_precision_workamount = 1E-5; /* Change this with --cfg=precision/work-amount:VALUE */
20 double sg_precision_timing = 1E-9; /* Change this with --cfg=precision/timing:VALUE */
21 int sg_concurrency_limit = -1; /* Change this with --cfg=maxmin/concurrency-limit:VALUE */
23 namespace simgrid::kernel::lmm {
25 int Variable::next_rank_ = 1;
26 int Constraint::next_rank_ = 1;
28 Element::Element(Constraint* constraint, Variable* variable, double cweight)
29 : constraint(constraint), variable(variable), consumption_weight(cweight), max_consumption_weight(cweight)
33 int Element::get_concurrency() const
35 // just to try having the computation of the concurrency
36 if (constraint->get_sharing_policy() == Constraint::SharingPolicy::WIFI) {
40 // Ignore element with weight less than one (e.g. cross-traffic)
41 return (consumption_weight >= 1) ? 1 : 0;
42 // There are other alternatives, but they will change the behavior of the model..
43 // So do not use it unless you want to make a new model.
44 // If you do, remember to change the variables concurrency share to reflect it.
45 // Potential examples are:
46 // return (elem->weight>0)?1:0;//Include element as soon as weight is non-zero
47 // return (int)ceil(elem->weight);//Include element as the rounded-up integer value of the element weight
50 void Element::decrease_concurrency()
52 xbt_assert(constraint->concurrency_current_ >= get_concurrency());
53 constraint->concurrency_current_ -= get_concurrency();
56 void Element::increase_concurrency(bool check_limit)
58 constraint->concurrency_current_ += get_concurrency();
60 if (constraint->concurrency_current_ > constraint->concurrency_maximum_)
61 constraint->concurrency_maximum_ = constraint->concurrency_current_;
63 xbt_assert(not check_limit || constraint->get_concurrency_limit() < 0 ||
64 constraint->concurrency_current_ <= constraint->get_concurrency_limit(),
65 "Concurrency limit overflow!");
68 System* System::build(std::string_view solver_name, bool selective_update)
70 System* system = nullptr;
71 if (solver_name == "bmf") {
72 #if SIMGRID_HAVE_EIGEN3
73 system = new BmfSystem(selective_update);
75 } else if (solver_name == "fairbottleneck") {
76 system = new FairBottleneck(selective_update);
78 system = new MaxMin(selective_update);
83 void System::validate_solver(const std::string& solver_name)
85 static const std::vector<std::string> opts{"bmf", "maxmin", "fairbottleneck"};
86 if (solver_name == "bmf") {
87 #if !SIMGRID_HAVE_EIGEN3
88 xbt_die("Cannot use the BMF solver without installing Eigen3.");
91 if (std::find(opts.begin(), opts.end(), solver_name) == std::end(opts)) {
92 xbt_die("Invalid system solver, it should be one of: \"maxmin\", \"fairbottleneck\" or \"bmf\"");
96 void System::check_concurrency() const
98 // These checks are very expensive, so do them only if we want to debug the LMM
99 if (not XBT_LOG_ISENABLED(ker_lmm, xbt_log_priority_debug))
102 for (Constraint const& cnst : constraint_set) {
104 for (Element const& elem : cnst.enabled_element_set_) {
105 xbt_assert(elem.variable->sharing_penalty_ > 0);
106 concurrency += elem.get_concurrency();
109 for (Element const& elem : cnst.disabled_element_set_) {
110 // We should have staged variables only if concurrency is reached in some constraint
111 xbt_assert(cnst.get_concurrency_limit() < 0 || elem.variable->staged_sharing_penalty_ == 0 ||
112 elem.variable->get_min_concurrency_slack() == 0,
113 "should not have staged variable!");
116 xbt_assert(cnst.get_concurrency_limit() < 0 || cnst.get_concurrency_limit() >= concurrency,
117 "concurrency check failed!");
118 xbt_assert(cnst.concurrency_current_ == concurrency, "concurrency_current is out-of-date!");
121 // Check that for each variable, all corresponding elements are in the same state (i.e. same element sets)
122 for (Variable const& var : variable_set) {
123 if (var.cnsts_.empty())
126 const Element& elem = var.cnsts_[0];
127 bool belong_to_enabled = elem.enabled_element_set_hook.is_linked();
128 bool belong_to_disabled = elem.disabled_element_set_hook.is_linked();
129 bool belong_to_active = elem.active_element_set_hook.is_linked();
131 for (Element const& elem2 : var.cnsts_) {
132 xbt_assert(belong_to_enabled == elem2.enabled_element_set_hook.is_linked(),
133 "Variable inconsistency (1): enabled_element_set");
134 xbt_assert(belong_to_disabled == elem2.disabled_element_set_hook.is_linked(),
135 "Variable inconsistency (2): disabled_element_set");
136 xbt_assert(belong_to_active == elem2.active_element_set_hook.is_linked(),
137 "Variable inconsistency (3): active_element_set");
142 void System::var_free(Variable* var)
144 XBT_IN("(sys=%p, var=%p)", this, var);
147 // TODOLATER Can do better than that by leaving only the variable in only one enabled_element_set, call
148 // update_modified_set, and then remove it..
149 update_modified_cnst_set_from_variable(var);
151 for (Element& elem : var->cnsts_) {
152 if (var->sharing_penalty_ > 0)
153 elem.decrease_concurrency();
154 if (elem.enabled_element_set_hook.is_linked())
155 simgrid::xbt::intrusive_erase(elem.constraint->enabled_element_set_, elem);
156 if (elem.disabled_element_set_hook.is_linked())
157 simgrid::xbt::intrusive_erase(elem.constraint->disabled_element_set_, elem);
158 if (elem.active_element_set_hook.is_linked())
159 simgrid::xbt::intrusive_erase(elem.constraint->active_element_set_, elem);
160 if (elem.constraint->enabled_element_set_.empty() && elem.constraint->disabled_element_set_.empty())
161 make_constraint_inactive(elem.constraint);
163 on_disabled_var(elem.constraint);
170 xbt_mallocator_release(variable_mallocator_, var);
174 System::System(bool selective_update) : selective_update_active(selective_update)
176 XBT_DEBUG("Setting selective_update_active flag to %d", selective_update_active);
178 if (selective_update)
179 modified_set_ = std::make_unique<kernel::resource::Action::ModifiedSet>();
184 while (Variable* var = extract_variable()) {
185 const char* name = var->id_ ? typeid(*var->id_).name() : "(unidentified)";
186 boost::core::scoped_demangled_name demangled(name);
187 XBT_WARN("Probable bug: a %s variable (#%d) not removed before the LMM system destruction.",
188 demangled.get() ? demangled.get() : name, var->rank_);
191 while (Constraint* cnst = extract_constraint())
194 xbt_mallocator_free(variable_mallocator_);
197 void System::cnst_free(Constraint* cnst)
199 make_constraint_inactive(cnst);
203 Constraint::Constraint(resource::Resource* id_value, double bound_value) : bound_(bound_value), id_(id_value)
205 rank_ = next_rank_++;
208 Constraint* System::constraint_new(resource::Resource* id, double bound_value)
210 auto* cnst = new Constraint(id, bound_value);
211 insert_constraint(cnst);
215 void* System::variable_mallocator_new_f()
220 void System::variable_mallocator_free_f(void* var)
222 delete static_cast<Variable*>(var);
225 Variable* System::variable_new(resource::Action* id, double sharing_penalty, double bound, size_t number_of_constraints)
227 XBT_IN("(sys=%p, id=%p, penalty=%f, bound=%f, num_cons =%zu)", this, id, sharing_penalty, bound,
228 number_of_constraints);
230 auto* var = static_cast<Variable*>(xbt_mallocator_get(variable_mallocator_));
231 var->initialize(id, sharing_penalty, bound, number_of_constraints, visited_counter_ - 1);
232 if (sharing_penalty > 0)
233 variable_set.push_front(*var);
235 variable_set.push_back(*var);
237 XBT_OUT(" returns %p", var);
241 void System::variable_free(Variable* var)
243 remove_variable(var);
247 void System::variable_free_all()
249 while (Variable* var = extract_variable())
253 Element& System::expand_create_elem(Constraint* cnst, Variable* var, double consumption_weight)
255 xbt_assert(var->cnsts_.size() < var->cnsts_.capacity(), "Too much constraints");
257 var->cnsts_.emplace_back(cnst, var, consumption_weight);
258 Element& elem = var->cnsts_.back();
260 if (var->sharing_penalty_ != 0.0) {
261 elem.constraint->enabled_element_set_.push_front(elem);
263 elem.constraint->disabled_element_set_.push_back(elem);
265 if (elem.consumption_weight > 0 || var->sharing_penalty_ > 0) {
266 make_constraint_active(cnst);
271 Element& System::expand_add_to_elem(Element& elem, const Constraint* cnst, double consumption_weight) const
273 elem.max_consumption_weight = std::max(elem.max_consumption_weight, consumption_weight);
274 if (cnst->sharing_policy_ != Constraint::SharingPolicy::FATPIPE)
275 elem.consumption_weight += consumption_weight;
277 elem.consumption_weight = std::max(elem.consumption_weight, consumption_weight);
281 void System::expand(Constraint* cnst, Variable* var, double consumption_weight, bool force_creation)
286 std::find_if(begin(var->cnsts_), end(var->cnsts_), [&cnst](Element const& x) { return x.constraint == cnst; });
288 bool reuse_elem = elem_it != end(var->cnsts_) && not force_creation;
289 if (reuse_elem && var->sharing_penalty_ != 0.0) {
290 /* before changing it, decreases concurrency on constraint, it'll be added back later */
291 elem_it->decrease_concurrency();
293 Element& elem = reuse_elem ? expand_add_to_elem(*elem_it, cnst, consumption_weight)
294 : expand_create_elem(cnst, var, consumption_weight);
296 // Check if we need to disable the variable
297 if (var->sharing_penalty_ != 0) {
298 /* increase concurrency in constraint that this element uses.
299 * as we don't check if constraint has reached its limit before increasing,
300 * we can't check the correct state at increase_concurrency, anyway
301 * it'll check if the slack is smaller than 0 just below */
302 elem.increase_concurrency(false);
303 if (cnst->get_concurrency_slack() < 0) {
304 double penalty = var->sharing_penalty_;
306 for (Element const& elem2 : var->cnsts_)
307 on_disabled_var(elem2.constraint);
308 var->staged_sharing_penalty_ = penalty;
309 xbt_assert(not var->sharing_penalty_);
313 /* update modified constraint set accordingly */
314 if (elem.consumption_weight > 0 || var->sharing_penalty_ > 0)
315 update_modified_cnst_set(cnst);
320 Variable* Constraint::get_variable(const Element** elem) const
322 if (*elem == nullptr) {
323 // That is the first call, pick the first element among enabled_element_set (or disabled_element_set if
324 // enabled_element_set is empty)
325 if (not enabled_element_set_.empty())
326 *elem = &enabled_element_set_.front();
327 else if (not disabled_element_set_.empty())
328 *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, size_t* 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();
369 // elem is not null, so we carry on
370 if ((*elem)->enabled_element_set_hook.is_linked()) {
371 // Look at enabled_element_set, and jump to disabled_element_set when finished
372 auto iter = std::next(enabled_element_set_.iterator_to(**elem));
373 if (iter != std::end(enabled_element_set_))
375 else if (not disabled_element_set_.empty())
376 *nextelem = &disabled_element_set_.front();
380 auto iter = std::next(disabled_element_set_.iterator_to(**elem));
381 *nextelem = iter != std::end(disabled_element_set_) ? &*iter : nullptr;
383 return (*elem)->variable;
388 template <class ElemList>
389 static void format_element_list(const ElemList& elem_list, Constraint::SharingPolicy sharing_policy, double& sum,
392 for (Element const& elem : elem_list) {
393 buf += std::to_string(elem.consumption_weight) + ".'" + std::to_string(elem.variable->rank_) + "'(" +
394 std::to_string(elem.variable->value_) + ")" +
395 (sharing_policy != Constraint::SharingPolicy::FATPIPE ? " + " : " , ");
396 if (sharing_policy != Constraint::SharingPolicy::FATPIPE)
397 sum += elem.consumption_weight * elem.variable->value_;
399 sum = std::max(sum, elem.consumption_weight * elem.variable->value_);
403 void System::print() const
405 std::string buf = "MAX-MIN ( ";
407 /* Printing Objective */
408 for (Variable const& var : variable_set)
409 buf += "'" + std::to_string(var.rank_) + "'(" + std::to_string(var.sharing_penalty_) + ") ";
411 XBT_DEBUG("%20s", buf.c_str());
414 XBT_DEBUG("Constraints");
415 /* Printing Constraints */
416 for (Constraint const& cnst : active_constraint_set) {
418 // Show the enabled variables
420 buf += cnst.sharing_policy_ != Constraint::SharingPolicy::FATPIPE ? "(" : "max(";
421 format_element_list(cnst.enabled_element_set_, cnst.sharing_policy_, sum, buf);
422 // TODO: Adding disabled elements only for test compatibility, but do we really want them to be printed?
423 format_element_list(cnst.disabled_element_set_, cnst.sharing_policy_, sum, buf);
425 buf += "0) <= " + std::to_string(cnst.bound_) + " ('" + std::to_string(cnst.rank_) + "')";
427 if (cnst.sharing_policy_ == Constraint::SharingPolicy::FATPIPE) {
428 buf += " [MAX-Constraint]";
430 XBT_DEBUG("%s", buf.c_str());
432 xbt_assert(not double_positive(sum - cnst.bound_, cnst.bound_ * sg_precision_workamount),
433 "Incorrect value (%f is not smaller than %f): %g", sum, cnst.bound_, sum - cnst.bound_);
436 XBT_DEBUG("Variables");
437 /* Printing Result */
438 for (Variable const& var : variable_set) {
439 if (var.bound_ > 0) {
440 XBT_DEBUG("'%d'(%f) : %f (<=%f)", var.rank_, var.sharing_penalty_, var.value_, var.bound_);
441 xbt_assert(not double_positive(var.value_ - var.bound_, var.bound_ * sg_precision_workamount),
442 "Incorrect value (%f is not smaller than %f", var.value_, var.bound_);
444 XBT_DEBUG("'%d'(%f) : %f", var.rank_, var.sharing_penalty_, var.value_);
449 resource::Action::ModifiedSet* System::get_modified_action_set() const
451 return modified_set_.get();
462 if (selective_update_active) {
463 /* update list of modified variables */
464 for (const Constraint& cnst : modified_constraint_set) {
465 for (const Element& elem : cnst.enabled_element_set_) {
466 if (elem.consumption_weight > 0) {
467 resource::Action* action = elem.variable->id_;
468 if (not action->is_within_modified_set())
469 modified_set_->push_back(*action);
473 /* clear list of modified constraint */
474 remove_all_modified_cnst_set();
477 if (XBT_LOG_ISENABLED(ker_lmm, xbt_log_priority_debug)) {
484 /** @brief Attribute the value bound to var->bound.
486 * @param var the Variable*
487 * @param bound the new bound to associate with var
489 * Makes var->bound equal to bound. Whenever this function is called a change is signed in the system. To
490 * avoid false system changing detection it is a good idea to test (bound != 0) before calling it.
492 void System::update_variable_bound(Variable* var, double bound)
497 if (not var->cnsts_.empty()) {
498 for (Element const& elem : var->cnsts_) {
499 update_modified_cnst_set(elem.constraint);
504 void Variable::initialize(resource::Action* id_value, double sharing_penalty, double bound_value,
505 size_t number_of_constraints, unsigned visited_value)
508 rank_ = next_rank_++;
509 cnsts_.reserve(number_of_constraints);
510 sharing_penalty_ = sharing_penalty;
511 staged_sharing_penalty_ = 0.0;
512 bound_ = bound_value;
514 visited_ = visited_value;
517 xbt_assert(not variable_set_hook_.is_linked());
518 xbt_assert(not saturated_variable_set_hook_.is_linked());
521 int Variable::get_min_concurrency_slack() const
523 int minslack = std::numeric_limits<int>::max();
524 for (Element const& elem : cnsts_) {
525 int slack = elem.constraint->get_concurrency_slack();
526 if (slack < minslack) {
527 // This is only an optimization, to avoid looking at more constraints when slack is already zero
536 // Small remark: In this implementation of System::enable_var() and System::disable_var(), we will meet multiple times
537 // with var when running System::update_modified_cnst_set().
538 // A priori not a big performance issue, but we might do better by calling System::update_modified_cnst_set() within the
539 // for loops (after doing the first for enabling==1, and before doing the last for disabling==1)
540 void System::enable_var(Variable* var)
542 xbt_assert(not XBT_LOG_ISENABLED(ker_lmm, xbt_log_priority_debug) || var->can_enable());
544 var->sharing_penalty_ = var->staged_sharing_penalty_;
545 var->staged_sharing_penalty_ = 0;
547 // Enabling the variable, move var to list head. Subtlety is: here, we need to call update_modified_cnst_set AFTER
548 // moving at least one element of var.
550 simgrid::xbt::intrusive_erase(variable_set, *var);
551 variable_set.push_front(*var);
552 for (Element& elem : var->cnsts_) {
553 simgrid::xbt::intrusive_erase(elem.constraint->disabled_element_set_, elem);
554 elem.constraint->enabled_element_set_.push_front(elem);
555 elem.increase_concurrency();
557 update_modified_cnst_set_from_variable(var);
559 // When used within on_disabled_var, we would get an assertion fail, because transiently there can be variables
560 // that are staged and could be activated.
561 // Anyway, caller functions all call check_concurrency() in the end.
564 void System::disable_var(Variable* var)
566 xbt_assert(not var->staged_sharing_penalty_, "Staged penalty should have been cleared");
567 // Disabling the variable, move to var to list tail. Subtlety is: here, we need to call update_modified_cnst_set
568 // BEFORE moving the last element of var.
569 simgrid::xbt::intrusive_erase(variable_set, *var);
570 variable_set.push_back(*var);
571 update_modified_cnst_set_from_variable(var);
572 for (Element& elem : var->cnsts_) {
573 simgrid::xbt::intrusive_erase(elem.constraint->enabled_element_set_, elem);
574 elem.constraint->disabled_element_set_.push_back(elem);
575 if (elem.active_element_set_hook.is_linked())
576 simgrid::xbt::intrusive_erase(elem.constraint->active_element_set_, elem);
577 elem.decrease_concurrency();
580 var->sharing_penalty_ = 0.0;
581 var->staged_sharing_penalty_ = 0.0;
586 /* /brief Find variables that can be enabled and enable them.
588 * Assuming that the variable has already been removed from non-zero penalties
589 * Can we find a staged variable to add?
590 * If yes, check that none of the constraints that this variable is involved in is at the limit of its concurrency
591 * And then add it to enabled variables
593 void System::on_disabled_var(Constraint* cnstr)
595 if (cnstr->get_concurrency_limit() < 0)
598 size_t numelem = cnstr->disabled_element_set_.size();
602 Element* elem = &cnstr->disabled_element_set_.front();
604 // Cannot use foreach loop, because System::enable_var() will modify disabled_element_set.. within the loop
605 while (numelem-- && elem) {
607 if (elem->disabled_element_set_hook.is_linked()) {
608 auto iter = std::next(cnstr->disabled_element_set_.iterator_to(*elem));
609 nextelem = iter != std::end(cnstr->disabled_element_set_) ? &*iter : nullptr;
614 if (elem->variable->staged_sharing_penalty_ > 0 && elem->variable->can_enable()) {
615 // Found a staged variable
616 // TODOLATER: Add random timing function to model reservation protocol fuzziness? Then how to make sure that
617 // staged variables will eventually be called?
618 enable_var(elem->variable);
621 xbt_assert(cnstr->concurrency_current_ <= cnstr->get_concurrency_limit(), "Concurrency overflow!");
622 if (cnstr->concurrency_current_ == cnstr->get_concurrency_limit())
628 // We could get an assertion fail, because transiently there can be variables that are staged and could be activated.
629 // And we need to go through all constraints of the disabled var before getting back a coherent state.
630 // Anyway, caller functions all call check_concurrency() in the end.
633 /** @brief update the penalty of a variable (disable it by passing 0 as a penalty) */
634 void System::update_variable_penalty(Variable* var, double penalty)
636 xbt_assert(penalty >= 0, "Variable penalty should not be negative!");
637 if (penalty == var->sharing_penalty_)
640 bool enabling_var = (penalty > 0 && var->sharing_penalty_ <= 0);
641 bool disabling_var = (penalty <= 0 && var->sharing_penalty_ > 0);
643 XBT_IN("(sys=%p, var=%p, var->sharing_penalty = %f, penalty=%f)", this, var, var->sharing_penalty_, penalty);
647 // Are we enabling this variable?
649 var->staged_sharing_penalty_ = penalty;
650 int minslack = var->get_min_concurrency_slack();
652 XBT_DEBUG("Staging var (instead of enabling) because min concurrency slack is 0");
655 XBT_DEBUG("Enabling var with min concurrency slack %i", minslack);
657 } else if (disabling_var) {
660 var->sharing_penalty_ = penalty;
661 update_modified_cnst_set_from_variable(var);
669 void System::update_constraint_bound(Constraint* cnst, double bound)
672 update_modified_cnst_set(cnst);
673 cnst->bound_ = bound;
676 /** @brief Update the constraint set propagating recursively to other constraints so the system should not be entirely
679 * @param cnst the Constraint* affected by the change
681 * A recursive algorithm to optimize the system recalculation selecting only constraints that have changed. Each
682 * constraint change is propagated to the list of constraints for each variable.
684 void System::update_modified_cnst_set_rec(const Constraint* cnst)
686 for (Element const& elem : cnst->enabled_element_set_) {
687 Variable* var = elem.variable;
688 for (Element const& elem2 : var->cnsts_) {
689 if (var->visited_ == visited_counter_)
691 if (elem2.constraint != cnst && not elem2.constraint->modified_constraint_set_hook_.is_linked()) {
692 modified_constraint_set.push_back(*elem2.constraint);
693 update_modified_cnst_set_rec(elem2.constraint);
696 // var will be ignored in later visits as long as sys->visited_counter does not move
697 var->visited_ = visited_counter_;
701 void System::update_modified_cnst_set_from_variable(const Variable* var)
703 /* nothing to update in these cases:
704 * - selective update not active, all variables are active
705 * - variable doesn't use any constraint
706 * - variable is disabled (sharing penalty <= 0): we iterate only through the enabled_variables in
707 * update_modified_cnst_set_rec */
708 if (not selective_update_active || var->cnsts_.empty() || var->sharing_penalty_ <= 0)
711 /* Normally, if the conditions above are true, specially variable is enabled, we can call
712 * modified_set over the first contraint only, since the recursion in update_modified_cnst_set_rec
713 * will iterate over the other constraints of this variable */
714 update_modified_cnst_set(var->cnsts_[0].constraint);
717 void System::update_modified_cnst_set(Constraint* cnst)
719 /* nothing to do if selective update isn't active */
720 if (selective_update_active && not cnst->modified_constraint_set_hook_.is_linked()) {
721 modified_constraint_set.push_back(*cnst);
722 update_modified_cnst_set_rec(cnst);
726 void System::remove_all_modified_cnst_set()
728 // We cleverly un-flag all variables just by incrementing visited_counter
729 // In effect, the var->visited value will no more be equal to visited counter
730 // To be clean, when visited counter has wrapped around, we force these var->visited values so that variables that
731 // were in the modified a long long time ago are not wrongly skipped here, which would lead to very nasty bugs
732 // (i.e. not readily reproducible, and requiring a lot of run time before happening).
733 if (++visited_counter_ == 1) {
734 /* the counter wrapped around, reset each variable->visited */
735 for (Variable& var : variable_set)
738 modified_constraint_set.clear();
742 * Returns resource load (in flop per second, or byte per second, or similar)
744 * If the resource is shared (the default case), the load is sum of resource usage made by
745 * every variables located on this resource.
747 * If the resource is not shared (ie in FATPIPE mode), then the load is the max (not the sum)
748 * of all resource usages located on this resource.
750 double Constraint::get_load() const
753 if (sharing_policy_ != SharingPolicy::FATPIPE) {
754 for (Element const& elem : enabled_element_set_)
755 if (elem.consumption_weight > 0)
756 result += elem.consumption_weight * elem.variable->value_;
758 for (Element const& elem : enabled_element_set_)
759 if (elem.consumption_weight > 0)
760 result = std::max(result, elem.consumption_weight * elem.variable->value_);
765 void Constraint::set_sharing_policy(SharingPolicy policy, const s4u::NonLinearResourceCb& cb)
767 xbt_assert(policy == SharingPolicy::NONLINEAR || policy == SharingPolicy::WIFI || not cb,
768 "Invalid sharing policy for constraint. Callback should be used with NONLINEAR sharing policy");
769 sharing_policy_ = policy;
770 dyn_constraint_cb_ = cb;
773 } // namespace simgrid::kernel::lmm