1 /* Copyright (c) 2004-2017. 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 "surf/maxmin.hpp"
9 #include "xbt/backtrace.hpp"
11 #include "xbt/mallocator.h"
12 #include "xbt/sysdep.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_maxmin, surf, "Logging specific to SURF (maxmin)");
22 typedef std::vector<int> dyn_light_t;
24 double sg_maxmin_precision = 0.00001; /* Change this with --cfg=maxmin/precision:VALUE */
25 double sg_surf_precision = 0.00001; /* Change this with --cfg=surf/precision:VALUE */
26 int sg_concurrency_limit = -1; /* Change this with --cfg=maxmin/concurrency-limit:VALUE */
28 int s_lmm_variable_t::Global_debug_id = 1;
29 int s_lmm_constraint_t::Global_debug_id = 1;
31 int s_lmm_element_t::get_concurrency() const
33 //Ignore element with weight less than one (e.g. cross-traffic)
34 return (consumption_weight >= 1) ? 1 : 0;
35 //There are other alternatives, but they will change the behaviour of the model..
36 //So do not use it unless you want to make a new model.
37 //If you do, remember to change the variables concurrency share to reflect it.
38 //Potential examples are:
39 //return (elem->weight>0)?1:0;//Include element as soon as weight is non-zero
40 //return (int)ceil(elem->weight);//Include element as the rounded-up integer value of the element weight
43 void s_lmm_element_t::decrease_concurrency()
45 xbt_assert(constraint->concurrency_current >= get_concurrency());
46 constraint->concurrency_current -= get_concurrency();
49 void s_lmm_element_t::increase_concurrency()
51 constraint->concurrency_current += get_concurrency();
53 if (constraint->concurrency_current > constraint->concurrency_maximum)
54 constraint->concurrency_maximum = constraint->concurrency_current;
56 xbt_assert(constraint->get_concurrency_limit() < 0 ||
57 constraint->concurrency_current <= constraint->get_concurrency_limit(),
58 "Concurrency limit overflow!");
61 void s_lmm_system_t::check_concurrency()
63 // These checks are very expensive, so do them only if we want to debug SURF LMM
64 if (not XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug))
68 xbt_swag_foreach(cnstIt, &constraint_set)
70 lmm_constraint_t cnst = (lmm_constraint_t)cnstIt;
73 xbt_swag_foreach(elemIt, &(cnst->enabled_element_set))
75 lmm_element_t elem = (lmm_element_t)elemIt;
76 xbt_assert(elem->variable->sharing_weight > 0);
77 concurrency += elem->get_concurrency();
80 xbt_swag_foreach(elemIt, &(cnst->disabled_element_set))
82 lmm_element_t elem = (lmm_element_t)elemIt;
83 // We should have staged variables only if concurrency is reached in some constraint
84 xbt_assert(cnst->get_concurrency_limit() < 0 || elem->variable->staged_weight == 0 ||
85 elem->variable->get_min_concurrency_slack() < elem->variable->concurrency_share,
86 "should not have staged variable!");
89 xbt_assert(cnst->get_concurrency_limit() < 0 || cnst->get_concurrency_limit() >= concurrency,
90 "concurrency check failed!");
91 xbt_assert(cnst->concurrency_current == concurrency, "concurrency_current is out-of-date!");
94 // Check that for each variable, all corresponding elements are in the same state (i.e. same element sets)
96 xbt_swag_foreach(varIt, &variable_set)
98 lmm_variable_t var = (lmm_variable_t)varIt;
100 if (var->cnsts.empty())
103 lmm_element_t elem = &var->cnsts[0];
104 int belong_to_enabled = xbt_swag_belongs(elem, &(elem->constraint->enabled_element_set));
105 int belong_to_disabled = xbt_swag_belongs(elem, &(elem->constraint->disabled_element_set));
106 int belong_to_active = xbt_swag_belongs(elem, &(elem->constraint->active_element_set));
108 for (s_lmm_element_t const& elem : var->cnsts) {
109 xbt_assert(belong_to_enabled == xbt_swag_belongs(&elem, &(elem.constraint->enabled_element_set)),
110 "Variable inconsistency (1): enabled_element_set");
111 xbt_assert(belong_to_disabled == xbt_swag_belongs(&elem, &(elem.constraint->disabled_element_set)),
112 "Variable inconsistency (2): disabled_element_set");
113 xbt_assert(belong_to_active == xbt_swag_belongs(&elem, &(elem.constraint->active_element_set)),
114 "Variable inconsistency (3): active_element_set");
119 void s_lmm_system_t::var_free(lmm_variable_t var)
121 XBT_IN("(sys=%p, var=%p)", this, var);
124 // TODOLATER Can do better than that by leaving only the variable in only one enabled_element_set, call
125 // update_modified_set, and then remove it..
126 if (not var->cnsts.empty())
127 update_modified_set(var->cnsts[0].constraint);
129 for (s_lmm_element_t& elem : var->cnsts) {
130 if (var->sharing_weight > 0)
131 elem.decrease_concurrency();
132 xbt_swag_remove(&elem, &(elem.constraint->enabled_element_set));
133 xbt_swag_remove(&elem, &(elem.constraint->disabled_element_set));
134 xbt_swag_remove(&elem, &(elem.constraint->active_element_set));
135 int nelements = xbt_swag_size(&(elem.constraint->enabled_element_set)) +
136 xbt_swag_size(&(elem.constraint->disabled_element_set));
138 make_constraint_inactive(elem.constraint);
140 on_disabled_var(elem.constraint);
147 xbt_mallocator_release(variable_mallocator, var);
151 s_lmm_system_t::s_lmm_system_t(bool selective_update)
153 s_lmm_variable_t var;
154 s_lmm_constraint_t cnst;
157 selective_update_active = selective_update;
160 XBT_DEBUG("Setting selective_update_active flag to %d", selective_update_active);
162 xbt_swag_init(&variable_set, xbt_swag_offset(var, variable_set_hookup));
163 xbt_swag_init(&constraint_set, xbt_swag_offset(cnst, constraint_set_hookup));
165 xbt_swag_init(&active_constraint_set, xbt_swag_offset(cnst, active_constraint_set_hookup));
167 xbt_swag_init(&modified_constraint_set, xbt_swag_offset(cnst, modified_constraint_set_hookup));
168 xbt_swag_init(&saturated_variable_set, xbt_swag_offset(var, saturated_variable_set_hookup));
169 xbt_swag_init(&saturated_constraint_set, xbt_swag_offset(cnst, saturated_constraint_set_hookup));
171 keep_track = nullptr;
172 variable_mallocator = xbt_mallocator_new(65536, s_lmm_system_t::variable_mallocator_new_f,
173 s_lmm_system_t::variable_mallocator_free_f, nullptr);
174 solve_fun = &lmm_solve;
177 s_lmm_system_t::~s_lmm_system_t()
180 lmm_constraint_t cnst;
182 while ((var = extract_variable())) {
183 auto demangled = simgrid::xbt::demangle(typeid(*var->id).name());
184 XBT_WARN("Probable bug: a %s variable (#%d) not removed before the LMM system destruction.", demangled.get(),
188 while ((cnst = extract_constraint()))
191 xbt_mallocator_free(variable_mallocator);
194 void s_lmm_system_t::cnst_free(lmm_constraint_t cnst)
196 make_constraint_inactive(cnst);
200 s_lmm_constraint_t::s_lmm_constraint_t(void* id_value, double bound_value)
202 s_lmm_element_t elem;
205 id_int = Global_debug_id++;
206 xbt_swag_init(&enabled_element_set, xbt_swag_offset(elem, enabled_element_set_hookup));
207 xbt_swag_init(&disabled_element_set, xbt_swag_offset(elem, disabled_element_set_hookup));
208 xbt_swag_init(&active_element_set, xbt_swag_offset(elem, active_element_set_hookup));
213 concurrency_limit = sg_concurrency_limit;
214 concurrency_current = 0;
215 concurrency_maximum = 0;
216 sharing_policy = 1; /* FIXME: don't hardcode the value */
220 cnst_light = nullptr;
223 lmm_constraint_t s_lmm_system_t::constraint_new(void* id, double bound_value)
225 lmm_constraint_t cnst = new s_lmm_constraint_t(id, bound_value);
226 insert_constraint(cnst);
230 void* s_lmm_system_t::variable_mallocator_new_f()
232 return new s_lmm_variable_t;
235 void s_lmm_system_t::variable_mallocator_free_f(void* var)
237 delete static_cast<lmm_variable_t>(var);
240 lmm_variable_t s_lmm_system_t::variable_new(simgrid::surf::Action* id, double sharing_weight, double bound,
241 int number_of_constraints)
243 XBT_IN("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)", this, id, sharing_weight, bound, number_of_constraints);
245 lmm_variable_t var = static_cast<lmm_variable_t>(xbt_mallocator_get(variable_mallocator));
246 var->initialize(id, sharing_weight, bound, number_of_constraints, visited_counter - 1);
248 xbt_swag_insert_at_head(var, &variable_set);
250 xbt_swag_insert_at_tail(var, &variable_set);
252 XBT_OUT(" returns %p", var);
256 void s_lmm_system_t::variable_free(lmm_variable_t var)
258 remove_variable(var);
262 void s_lmm_system_t::expand(lmm_constraint_t cnst, lmm_variable_t var, double consumption_weight)
266 //Check if this variable already has an active element in this constraint
267 //If it does, substract it from the required slack
268 int current_share = 0;
269 if(var->concurrency_share>1){
270 for (s_lmm_element_t& elem : var->cnsts) {
271 if (elem.constraint == cnst && xbt_swag_belongs(&elem, &(elem.constraint->enabled_element_set)))
272 current_share += elem.get_concurrency();
276 //Check if we need to disable the variable
277 if (var->sharing_weight > 0 && var->concurrency_share - current_share > cnst->get_concurrency_slack()) {
278 double weight = var->sharing_weight;
280 for (s_lmm_element_t const& elem : var->cnsts)
281 on_disabled_var(elem.constraint);
282 consumption_weight = 0;
283 var->staged_weight=weight;
284 xbt_assert(not var->sharing_weight);
287 xbt_assert(var->cnsts.size() < var->cnsts.capacity(), "Too much constraints");
289 var->cnsts.resize(var->cnsts.size() + 1);
290 s_lmm_element_t& elem = var->cnsts.back();
292 elem.consumption_weight = consumption_weight;
293 elem.constraint = cnst;
296 if (var->sharing_weight) {
297 xbt_swag_insert_at_head(&elem, &(elem.constraint->enabled_element_set));
298 elem.increase_concurrency();
300 xbt_swag_insert_at_tail(&elem, &(elem.constraint->disabled_element_set));
302 if (not selective_update_active) {
303 make_constraint_active(cnst);
304 } else if (elem.consumption_weight > 0 || var->sharing_weight > 0) {
305 make_constraint_active(cnst);
306 update_modified_set(cnst);
307 //TODOLATER: Why do we need this second call?
308 if (var->cnsts.size() > 1)
309 update_modified_set(var->cnsts[0].constraint);
315 void s_lmm_system_t::expand_add(lmm_constraint_t cnst, lmm_variable_t var, double value)
321 //BEWARE: In case you have multiple elements in one constraint, this will always add value to the first element.
322 auto elem_it = std::find_if(begin(var->cnsts), end(var->cnsts),
323 [&cnst](s_lmm_element_t const& x) { return x.constraint == cnst; });
324 if (elem_it != end(var->cnsts)) {
325 s_lmm_element_t& elem = *elem_it;
326 if (var->sharing_weight)
327 elem.decrease_concurrency();
329 if (cnst->sharing_policy)
330 elem.consumption_weight += value;
332 elem.consumption_weight = std::max(elem.consumption_weight, value);
334 //We need to check that increasing value of the element does not cross the concurrency limit
335 if (var->sharing_weight) {
336 if (cnst->get_concurrency_slack() < elem.get_concurrency()) {
337 double weight = var->sharing_weight;
339 for (s_lmm_element_t const& elem2 : var->cnsts)
340 on_disabled_var(elem2.constraint);
341 var->staged_weight=weight;
342 xbt_assert(not var->sharing_weight);
344 elem.increase_concurrency();
346 update_modified_set(cnst);
348 expand(cnst, var, value);
353 lmm_variable_t s_lmm_constraint_t::get_variable(lmm_element_t* elem) const
355 if (*elem == nullptr) {
356 // That is the first call, pick the first element among enabled_element_set (or disabled_element_set if
357 // enabled_element_set is empty)
358 *elem = (lmm_element_t)xbt_swag_getFirst(&enabled_element_set);
359 if (*elem == nullptr)
360 *elem = (lmm_element_t)xbt_swag_getFirst(&disabled_element_set);
362 //elem is not null, so we carry on
363 if (xbt_swag_belongs(*elem, &enabled_element_set)) {
364 //Look at enabled_element_set, and jump to disabled_element_set when finished
365 *elem = (lmm_element_t)xbt_swag_getNext(*elem, enabled_element_set.offset);
366 if (*elem == nullptr)
367 *elem = (lmm_element_t)xbt_swag_getFirst(&disabled_element_set);
369 *elem = (lmm_element_t)xbt_swag_getNext(*elem, disabled_element_set.offset);
373 return (*elem)->variable;
378 //if we modify the swag between calls, normal version may loop forever
379 //this safe version ensures that we browse the swag elements only once
380 lmm_variable_t s_lmm_constraint_t::get_variable_safe(lmm_element_t* elem, lmm_element_t* nextelem, int* numelem) const
382 if (*elem == nullptr) {
383 *elem = (lmm_element_t)xbt_swag_getFirst(&enabled_element_set);
384 *numelem = xbt_swag_size(&enabled_element_set) + xbt_swag_size(&disabled_element_set) - 1;
385 if (*elem == nullptr)
386 *elem = (lmm_element_t)xbt_swag_getFirst(&disabled_element_set);
395 //elem is not null, so we carry on
396 if (xbt_swag_belongs(*elem, &enabled_element_set)) {
397 //Look at enabled_element_set, and jump to disabled_element_set when finished
398 *nextelem = (lmm_element_t)xbt_swag_getNext(*elem, enabled_element_set.offset);
399 if (*nextelem == nullptr)
400 *nextelem = (lmm_element_t)xbt_swag_getFirst(&disabled_element_set);
402 *nextelem = (lmm_element_t)xbt_swag_getNext(*elem, disabled_element_set.offset);
404 return (*elem)->variable;
409 static inline void saturated_constraints_update(double usage, int cnst_light_num, dyn_light_t& saturated_constraints,
412 xbt_assert(usage > 0,"Impossible");
414 if (*min_usage < 0 || *min_usage > usage) {
416 XBT_HERE(" min_usage=%f (cnst->remaining / cnst->usage =%f)", *min_usage, usage);
417 saturated_constraints.assign(1, cnst_light_num);
418 } else if (*min_usage == usage) {
419 saturated_constraints.emplace_back(cnst_light_num);
423 static inline void saturated_variable_set_update(s_lmm_constraint_light_t* cnst_light_tab,
424 const dyn_light_t& saturated_constraints, lmm_system_t sys)
426 /* Add active variables (i.e. variables that need to be set) from the set of constraints to saturate (cnst_light_tab)*/
427 for (int const& saturated_cnst : saturated_constraints) {
428 lmm_constraint_light_t cnst = &cnst_light_tab[saturated_cnst];
430 xbt_swag_t elem_list = &(cnst->cnst->active_element_set);
431 xbt_swag_foreach(_elem, elem_list) {
432 lmm_element_t elem = (lmm_element_t)_elem;
433 //Visiting active_element_set, so, by construction, should never get a zero weight, correct?
434 xbt_assert(elem->variable->sharing_weight > 0);
435 if (elem->consumption_weight > 0)
436 xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
441 void s_lmm_system_t::print()
443 std::string buf = std::string("MAX-MIN ( ");
446 /* Printing Objective */
447 xbt_swag_t var_list = &variable_set;
448 xbt_swag_foreach(_var, var_list) {
449 lmm_variable_t var = (lmm_variable_t)_var;
450 buf = buf + "'" + std::to_string(var->id_int) + "'(" + std::to_string(var->sharing_weight) + ") ";
453 XBT_DEBUG("%20s", buf.c_str());
456 XBT_DEBUG("Constraints");
457 /* Printing Constraints */
459 xbt_swag_t cnst_list = &active_constraint_set;
460 xbt_swag_foreach(_cnst, cnst_list) {
461 lmm_constraint_t cnst = (lmm_constraint_t)_cnst;
463 //Show the enabled variables
465 xbt_swag_t elem_list = &(cnst->enabled_element_set);
467 buf += ((cnst->sharing_policy) ? "(" : "max(");
468 xbt_swag_foreach(_elem, elem_list) {
469 lmm_element_t elem = (lmm_element_t)_elem;
470 buf = buf + std::to_string(elem->consumption_weight) + ".'" + std::to_string(elem->variable->id_int) + "'(" +
471 std::to_string(elem->variable->value) + ")" + ((cnst->sharing_policy) ? " + " : " , ");
472 if(cnst->sharing_policy)
473 sum += elem->consumption_weight * elem->variable->value;
475 sum = std::max(sum, elem->consumption_weight * elem->variable->value);
477 //TODO: Adding disabled elements only for test compatibility, but do we really want them to be printed?
478 elem_list = &(cnst->disabled_element_set);
479 xbt_swag_foreach(_elem, elem_list) {
480 lmm_element_t elem = (lmm_element_t)_elem;
481 buf = buf + std::to_string(elem->consumption_weight) + ".'" + std::to_string(elem->variable->id_int) + "'(" +
482 std::to_string(elem->variable->value) + ")" + ((cnst->sharing_policy) ? " + " : " , ");
483 if(cnst->sharing_policy)
484 sum += elem->consumption_weight * elem->variable->value;
486 sum = std::max(sum, elem->consumption_weight * elem->variable->value);
489 buf = buf + "0) <= " + std::to_string(cnst->bound) + " ('" + std::to_string(cnst->id_int) + "')";
491 if (not cnst->sharing_policy) {
492 buf += " [MAX-Constraint]";
494 XBT_DEBUG("%s", buf.c_str());
496 xbt_assert(not double_positive(sum - cnst->bound, cnst->bound * sg_maxmin_precision),
497 "Incorrect value (%f is not smaller than %f): %g", sum, cnst->bound, sum - cnst->bound);
500 XBT_DEBUG("Variables");
501 /* Printing Result */
502 xbt_swag_foreach(_var, var_list) {
503 lmm_variable_t var = (lmm_variable_t)_var;
504 if (var->bound > 0) {
505 XBT_DEBUG("'%d'(%f) : %f (<=%f)", var->id_int, var->sharing_weight, var->value, var->bound);
506 xbt_assert(not double_positive(var->value - var->bound, var->bound * sg_maxmin_precision),
507 "Incorrect value (%f is not smaller than %f", var->value, var->bound);
509 XBT_DEBUG("'%d'(%f) : %f", var->id_int, var->sharing_weight, var->value);
514 void s_lmm_system_t::solve()
519 double min_usage = -1;
520 double min_bound = -1;
525 XBT_IN("(sys=%p)", this);
527 /* Compute Usage and store the variables that reach the maximum. If selective_update_active is true, only constraints
528 * that changed are considered. Otherwise all constraints with active actions are considered.
530 xbt_swag_t cnst_list = selective_update_active ? &modified_constraint_set : &active_constraint_set;
532 XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
533 /* Init: Only modified code portions: reset the value of active variables */
534 xbt_swag_foreach(_cnst, cnst_list) {
535 lmm_constraint_t cnst = (lmm_constraint_t)_cnst;
536 xbt_swag_t elem_list = &(cnst->enabled_element_set);
537 xbt_swag_foreach(_elem, elem_list) {
538 lmm_variable_t var = ((lmm_element_t)_elem)->variable;
539 xbt_assert(var->sharing_weight > 0.0);
544 s_lmm_constraint_light_t* cnst_light_tab = new s_lmm_constraint_light_t[xbt_swag_size(cnst_list)]();
545 int cnst_light_num = 0;
546 dyn_light_t saturated_constraints;
548 xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list) {
549 lmm_constraint_t cnst = (lmm_constraint_t)_cnst;
550 /* INIT: Collect constraints that actually need to be saturated (i.e remaining and usage are strictly positive)
551 * into cnst_light_tab. */
552 cnst->remaining = cnst->bound;
553 if (not double_positive(cnst->remaining, cnst->bound * sg_maxmin_precision))
556 xbt_swag_t elem_list = &(cnst->enabled_element_set);
557 xbt_swag_foreach(_elem, elem_list) {
558 lmm_element_t elem = (lmm_element_t)_elem;
559 xbt_assert(elem->variable->sharing_weight > 0);
560 if (elem->consumption_weight > 0) {
561 if (cnst->sharing_policy)
562 cnst->usage += elem->consumption_weight / elem->variable->sharing_weight;
563 else if (cnst->usage < elem->consumption_weight / elem->variable->sharing_weight)
564 cnst->usage = elem->consumption_weight / elem->variable->sharing_weight;
567 simgrid::surf::Action *action = static_cast<simgrid::surf::Action*>(elem->variable->id);
568 if (keep_track && not action->is_linked())
569 keep_track->push_back(*action);
572 XBT_DEBUG("Constraint '%d' usage: %f remaining: %f concurrency: %i<=%i<=%i", cnst->id_int, cnst->usage,
573 cnst->remaining, cnst->concurrency_current, cnst->concurrency_maximum, cnst->get_concurrency_limit());
574 /* Saturated constraints update */
576 if(cnst->usage > 0) {
577 cnst_light_tab[cnst_light_num].cnst = cnst;
578 cnst->cnst_light = &(cnst_light_tab[cnst_light_num]);
579 cnst_light_tab[cnst_light_num].remaining_over_usage = cnst->remaining / cnst->usage;
580 saturated_constraints_update(cnst_light_tab[cnst_light_num].remaining_over_usage, cnst_light_num,
581 saturated_constraints, &min_usage);
582 xbt_assert(cnst->active_element_set.count>0, "There is no sense adding a constraint that has no active element!");
587 saturated_variable_set_update(cnst_light_tab, saturated_constraints, this);
589 /* Saturated variables update */
591 /* Fix the variables that have to be */
592 xbt_swag_t var_list = &saturated_variable_set;
594 lmm_variable_t var = nullptr;
595 xbt_swag_foreach(_var, var_list) {
596 var = (lmm_variable_t)_var;
597 if (var->sharing_weight <= 0.0)
599 /* First check if some of these variables could reach their upper bound and update min_bound accordingly. */
600 XBT_DEBUG("var=%d, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f", var->id_int,
601 var->bound, var->sharing_weight, min_usage, var->bound * var->sharing_weight);
602 if ((var->bound > 0) && (var->bound * var->sharing_weight < min_usage)) {
604 min_bound = var->bound * var->sharing_weight;
606 min_bound = std::min(min_bound, (var->bound * var->sharing_weight));
607 XBT_DEBUG("Updated min_bound=%f", min_bound);
611 while ((var = (lmm_variable_t)xbt_swag_getFirst(var_list))) {
613 //If no variable could reach its bound, deal iteratively the constraints usage ( at worst one constraint is
614 // saturated at each cycle)
615 var->value = min_usage / var->sharing_weight;
616 XBT_DEBUG("Setting var (%d) value to %f\n", var->id_int, var->value);
618 //If there exist a variable that can reach its bound, only update it (and other with the same bound) for now.
619 if (double_equals(min_bound, var->bound * var->sharing_weight, sg_maxmin_precision)) {
620 var->value = var->bound;
621 XBT_DEBUG("Setting %p (%d) value to %f\n", var, var->id_int, var->value);
623 // Variables which bound is different are not considered for this cycle, but they will be afterwards.
624 XBT_DEBUG("Do not consider %p (%d) \n", var, var->id_int);
625 xbt_swag_remove(var, var_list);
629 XBT_DEBUG("Min usage: %f, Var(%d)->weight: %f, Var(%d)->value: %f ", min_usage, var->id_int, var->sharing_weight,
630 var->id_int, var->value);
632 /* Update the usage of contraints where this variable is involved */
633 for (s_lmm_element_t& elem : var->cnsts) {
634 lmm_constraint_t cnst = elem.constraint;
635 if (cnst->sharing_policy) {
636 // Remember: shared constraints require that sum(elem.value * var->value) < cnst->bound
637 double_update(&(cnst->remaining), elem.consumption_weight * var->value, cnst->bound * sg_maxmin_precision);
638 double_update(&(cnst->usage), elem.consumption_weight / var->sharing_weight, sg_maxmin_precision);
639 // If the constraint is saturated, remove it from the set of active constraints (light_tab)
640 if (not double_positive(cnst->usage, sg_maxmin_precision) ||
641 not double_positive(cnst->remaining, cnst->bound * sg_maxmin_precision)) {
642 if (cnst->cnst_light) {
643 int index = (cnst->cnst_light-cnst_light_tab);
644 XBT_DEBUG("index: %d \t cnst_light_num: %d \t || usage: %f remaining: %f bound: %f ",
645 index,cnst_light_num, cnst->usage, cnst->remaining, cnst->bound);
646 cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
647 cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
649 cnst->cnst_light = nullptr;
652 cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
654 elem.make_inactive();
656 // Remember: non-shared constraints only require that max(elem.value * var->value) < cnst->bound
658 elem.make_inactive();
659 xbt_swag_t elem_list = &(cnst->enabled_element_set);
660 xbt_swag_foreach(_elem, elem_list) {
661 lmm_element_t elem2 = static_cast<lmm_element_t>(_elem);
662 xbt_assert(elem2->variable->sharing_weight > 0);
663 if (elem2->variable->value > 0)
665 if (elem2->consumption_weight > 0)
666 cnst->usage = std::max(cnst->usage, elem2->consumption_weight / elem2->variable->sharing_weight);
668 //If the constraint is saturated, remove it from the set of active constraints (light_tab)
669 if (not double_positive(cnst->usage, sg_maxmin_precision) ||
670 not double_positive(cnst->remaining, cnst->bound * sg_maxmin_precision)) {
671 if(cnst->cnst_light) {
672 int index = (cnst->cnst_light-cnst_light_tab);
673 XBT_DEBUG("index: %d \t cnst_light_num: %d \t || \t cnst: %p \t cnst->cnst_light: %p "
674 "\t cnst_light_tab: %p usage: %f remaining: %f bound: %f ", index,cnst_light_num, cnst,
675 cnst->cnst_light, cnst_light_tab, cnst->usage, cnst->remaining, cnst->bound);
676 cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
677 cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
679 cnst->cnst_light = nullptr;
682 cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
683 xbt_assert(cnst->active_element_set.count>0, "Should not keep a maximum constraint that has no active"
684 " element! You want to check the maxmin precision and possible rounding effects." );
688 xbt_swag_remove(var, var_list);
691 /* Find out which variables reach the maximum */
694 saturated_constraints.clear();
696 for(pos=0; pos<cnst_light_num; pos++){
697 xbt_assert(cnst_light_tab[pos].cnst->active_element_set.count>0, "Cannot saturate more a constraint that has"
698 " no active element! You may want to change the maxmin precision (--cfg=maxmin/precision:<new_value>)"
699 " because of possible rounding effects.\n\tFor the record, the usage of this constraint is %g while "
700 "the maxmin precision to which it is compared is %g.\n\tThe usage of the previous constraint is %g.",
701 cnst_light_tab[pos].cnst->usage, sg_maxmin_precision, cnst_light_tab[pos-1].cnst->usage);
702 saturated_constraints_update(cnst_light_tab[pos].remaining_over_usage, pos, saturated_constraints, &min_usage);
705 saturated_variable_set_update(cnst_light_tab, saturated_constraints, this);
707 } while (cnst_light_num > 0);
710 if (selective_update_active)
711 remove_all_modified_set();
713 if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
719 delete[] cnst_light_tab;
723 void lmm_solve(lmm_system_t sys)
728 /** \brief Attribute the value bound to var->bound.
730 * \param sys the lmm_system_t
731 * \param var the lmm_variable_t
732 * \param bound the new bound to associate with var
734 * Makes var->bound equal to bound. Whenever this function is called a change is signed in the system. To
735 * avoid false system changing detection it is a good idea to test (bound != 0) before calling it.
737 void s_lmm_system_t::update_variable_bound(lmm_variable_t var, double bound)
742 if (not var->cnsts.empty())
743 update_modified_set(var->cnsts[0].constraint);
746 void s_lmm_variable_t::initialize(simgrid::surf::Action* id_value, double sharing_weight_value, double bound_value,
747 int number_of_constraints, unsigned visited_value)
750 id_int = s_lmm_variable_t::Global_debug_id++;
751 cnsts.reserve(number_of_constraints);
752 sharing_weight = sharing_weight_value;
755 concurrency_share = 1;
757 visited = visited_value;
761 func_fp = func_fp_def;
762 func_fpi = func_fpi_def;
764 variable_set_hookup.next = nullptr;
765 variable_set_hookup.prev = nullptr;
766 saturated_variable_set_hookup.next = nullptr;
767 saturated_variable_set_hookup.prev = nullptr;
770 int s_lmm_variable_t::get_min_concurrency_slack() const
772 int minslack = std::numeric_limits<int>::max();
773 for (s_lmm_element_t const& elem : cnsts) {
774 int slack = elem.constraint->get_concurrency_slack();
775 if (slack < minslack) {
776 // This is only an optimization, to avoid looking at more constraints when slack is already zero
785 //Small remark: In this implementation of lmm_enable_var and lmm_disable_var, we will meet multiple times with var when
786 // running sys->update_modified_set.
787 // A priori not a big performance issue, but we might do better by calling sys->update_modified_set within the for loops
788 // (after doing the first for enabling==1, and before doing the last for disabling==1)
789 void s_lmm_system_t::enable_var(lmm_variable_t var)
791 xbt_assert(not XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug) || var->can_enable());
793 var->sharing_weight = var->staged_weight;
794 var->staged_weight = 0;
796 // Enabling the variable, move to var to list head. Subtlety is: here, we need to call update_modified_set AFTER
797 // moving at least one element of var.
799 xbt_swag_remove(var, &variable_set);
800 xbt_swag_insert_at_head(var, &variable_set);
801 for (s_lmm_element_t& elem : var->cnsts) {
802 xbt_swag_remove(&elem, &(elem.constraint->disabled_element_set));
803 xbt_swag_insert_at_head(&elem, &(elem.constraint->enabled_element_set));
804 elem.increase_concurrency();
806 if (not var->cnsts.empty())
807 update_modified_set(var->cnsts[0].constraint);
809 // When used within on_disabled_var, we would get an assertion fail, because transiently there can be variables
810 // that are staged and could be activated.
811 // Anyway, caller functions all call check_concurrency() in the end.
814 void s_lmm_system_t::disable_var(lmm_variable_t var)
816 xbt_assert(not var->staged_weight, "Staged weight should have been cleared");
817 // Disabling the variable, move to var to list tail. Subtlety is: here, we need to call update_modified_set
818 // BEFORE moving the last element of var.
819 xbt_swag_remove(var, &variable_set);
820 xbt_swag_insert_at_tail(var, &variable_set);
821 if (not var->cnsts.empty())
822 update_modified_set(var->cnsts[0].constraint);
823 for (s_lmm_element_t& elem : var->cnsts) {
824 xbt_swag_remove(&elem, &(elem.constraint->enabled_element_set));
825 xbt_swag_insert_at_tail(&elem, &(elem.constraint->disabled_element_set));
827 xbt_swag_remove(&elem, &(elem.constraint->active_element_set));
829 elem.decrease_concurrency();
832 var->sharing_weight = 0.0;
833 var->staged_weight=0.0;
838 /* /brief Find variables that can be enabled and enable them.
840 * Assuming that the variable has already been removed from non-zero weights
841 * Can we find a staged variable to add?
842 * If yes, check that none of the constraints that this variable is involved in is at the limit of its concurrency
843 * And then add it to enabled variables
845 void s_lmm_system_t::on_disabled_var(lmm_constraint_t cnstr)
847 if (cnstr->get_concurrency_limit() < 0)
850 int numelem = xbt_swag_size(&(cnstr->disabled_element_set));
854 lmm_element_t elem = (lmm_element_t)xbt_swag_getFirst(&(cnstr->disabled_element_set));
856 //Cannot use xbt_swag_foreach, because lmm_enable_var will modify disabled_element_set.. within the loop
857 while (numelem-- && elem) {
859 lmm_element_t nextelem = (lmm_element_t)xbt_swag_getNext(elem, cnstr->disabled_element_set.offset);
861 if (elem->variable->staged_weight > 0 && elem->variable->can_enable()) {
862 //Found a staged variable
863 //TODOLATER: Add random timing function to model reservation protocol fuzziness? Then how to make sure that
864 //staged variables will eventually be called?
865 enable_var(elem->variable);
868 xbt_assert(cnstr->concurrency_current <= cnstr->get_concurrency_limit(), "Concurrency overflow!");
869 if (cnstr->concurrency_current == cnstr->get_concurrency_limit())
875 //We could get an assertion fail, because transiently there can be variables that are staged and could be activated.
876 //And we need to go through all constraints of the disabled var before getting back a coherent state.
877 // Anyway, caller functions all call check_concurrency() in the end.
880 /* \brief update the weight of a variable, and enable/disable it.
881 * @return Returns whether a change was made
883 void s_lmm_system_t::update_variable_weight(lmm_variable_t var, double weight)
885 xbt_assert(weight>=0,"Variable weight should not be negative!");
887 if (weight == var->sharing_weight)
890 int enabling_var = (weight > 0 && var->sharing_weight <= 0);
891 int disabling_var = (weight <= 0 && var->sharing_weight > 0);
893 XBT_IN("(sys=%p, var=%p, weight=%f)", this, var, weight);
897 //Are we enabling this variable?
899 var->staged_weight = weight;
900 int minslack = var->get_min_concurrency_slack();
901 if (minslack < var->concurrency_share) {
902 XBT_DEBUG("Staging var (instead of enabling) because min concurrency slack %i, with weight %f and concurrency"
903 " share %i", minslack, weight, var->concurrency_share);
906 XBT_DEBUG("Enabling var with min concurrency slack %i", minslack);
908 } else if (disabling_var){
909 //Are we disabling this variable?
912 var->sharing_weight = weight;
920 void s_lmm_system_t::update_constraint_bound(lmm_constraint_t cnst, double bound)
923 update_modified_set(cnst);
927 /** \brief Update the constraint set propagating recursively to other constraints so the system should not be entirely
930 * \param sys the lmm_system_t
931 * \param cnst the lmm_constraint_t affected by the change
933 * A recursive algorithm to optimize the system recalculation selecting only constraints that have changed. Each
934 * constraint change is propagated to the list of constraints for each variable.
936 void s_lmm_system_t::update_modified_set_rec(lmm_constraint_t cnst)
940 xbt_swag_foreach(_elem, &cnst->enabled_element_set) {
941 lmm_variable_t var = ((lmm_element_t)_elem)->variable;
942 for (s_lmm_element_t const& elem : var->cnsts) {
943 if (var->visited == visited_counter)
945 if (elem.constraint != cnst && not xbt_swag_belongs(elem.constraint, &modified_constraint_set)) {
946 xbt_swag_insert(elem.constraint, &modified_constraint_set);
947 update_modified_set_rec(elem.constraint);
950 //var will be ignored in later visits as long as sys->visited_counter does not move
951 var->visited = visited_counter;
955 void s_lmm_system_t::update_modified_set(lmm_constraint_t cnst)
957 /* nothing to do if selective update isn't active */
958 if (selective_update_active && not xbt_swag_belongs(cnst, &modified_constraint_set)) {
959 xbt_swag_insert(cnst, &modified_constraint_set);
960 update_modified_set_rec(cnst);
964 void s_lmm_system_t::remove_all_modified_set()
966 // We cleverly un-flag all variables just by incrementing visited_counter
967 // In effect, the var->visited value will no more be equal to visited counter
968 // To be clean, when visited counter has wrapped around, we force these var->visited values so that variables that
969 // were in the modified a long long time ago are not wrongly skipped here, which would lead to very nasty bugs
970 // (i.e. not readibily reproducible, and requiring a lot of run time before happening).
971 if (++visited_counter == 1) {
972 /* the counter wrapped around, reset each variable->visited */
974 xbt_swag_foreach(_var, &variable_set)
975 ((lmm_variable_t)_var)->visited = 0;
977 xbt_swag_reset(&modified_constraint_set);
981 * Returns resource load (in flop per second, or byte per second, or similar)
983 * If the resource is shared (the default case), the load is sum of resource usage made by every variables located on
986 * If the resource is not shared (ie in FATPIPE mode), then the load is the max (not the sum) of all resource usages
987 * located on this resource.
989 * \param cnst the lmm_constraint_t associated to the resource
991 double s_lmm_constraint_t::get_usage() const
994 const_xbt_swag_t elem_list = &enabled_element_set;
997 xbt_swag_foreach(_elem, elem_list)
999 lmm_element_t elem = (lmm_element_t)_elem;
1000 if (elem->consumption_weight > 0) {
1002 result += elem->consumption_weight * elem->variable->value;
1003 else if (result < elem->consumption_weight * elem->variable->value)
1004 result = std::max(result, elem->consumption_weight * elem->variable->value);
1010 int s_lmm_constraint_t::get_variable_amount() const
1013 const_xbt_swag_t elem_list = &enabled_element_set;
1016 xbt_swag_foreach(_elem, elem_list) {
1017 lmm_element_t elem = (lmm_element_t)_elem;
1018 if (elem->consumption_weight > 0)