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 "maxmin_private.hpp"
10 #include "xbt/mallocator.h"
11 #include "xbt/sysdep.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_maxmin, surf, "Logging specific to SURF (maxmin)");
21 typedef std::vector<int> dyn_light_t;
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 */
27 static void *lmm_variable_mallocator_new_f();
28 static void lmm_variable_mallocator_free_f(void *var);
29 #define lmm_variable_mallocator_reset_f ((void_f_pvoid_t)nullptr)
30 static void lmm_update_modified_set(lmm_system_t sys, lmm_constraint_t cnst);
31 static void lmm_remove_all_modified_set(lmm_system_t sys);
32 static int Global_debug_id = 1;
33 static int Global_const_debug_id = 1;
35 static inline void lmm_cnst_free(lmm_system_t sys, lmm_constraint_t cnst);
37 static void lmm_on_disabled_var(lmm_system_t sys, lmm_constraint_t cnstr);
38 static void lmm_enable_var(lmm_system_t sys, lmm_variable_t var);
39 static int lmm_can_enable_var(lmm_variable_t var);
40 static void lmm_disable_var(lmm_system_t sys, lmm_variable_t var);
41 static int lmm_concurrency_slack(lmm_constraint_t cnstr);
42 static int lmm_cnstrs_min_concurrency_slack(lmm_variable_t var);
44 static inline int lmm_element_concurrency(lmm_element_t elem)
46 //Ignore element with weight less than one (e.g. cross-traffic)
47 return (elem->consumption_weight >= 1) ? 1 : 0;
48 //There are other alternatives, but they will change the behaviour of the model..
49 //So do not use it unless you want to make a new model.
50 //If you do, remember to change the variables concurrency share to reflect it.
51 //Potential examples are:
52 //return (elem->weight>0)?1:0;//Include element as soon as weight is non-zero
53 //return (int)ceil(elem->weight);//Include element as the rounded-up integer value of the element weight
56 static inline void lmm_decrease_concurrency(lmm_element_t elem)
58 xbt_assert(elem->constraint->concurrency_current>=lmm_element_concurrency(elem));
59 elem->constraint->concurrency_current-=lmm_element_concurrency(elem);
62 static inline void lmm_increase_concurrency(lmm_element_t elem)
64 elem->constraint->concurrency_current+= lmm_element_concurrency(elem);
66 lmm_constraint_t cnstr=elem->constraint;
68 if(cnstr->concurrency_current > cnstr->concurrency_maximum)
69 cnstr->concurrency_maximum= cnstr->concurrency_current;
71 xbt_assert(cnstr->concurrency_limit<0 || cnstr->concurrency_current<=cnstr->concurrency_limit,
72 "Concurrency limit overflow!");
75 static void lmm_check_concurrency(lmm_system_t sys)
77 // These checks are very expensive, so do them only if we want to debug SURF LMM
78 if (not XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug))
82 xbt_swag_foreach(cnstIt, &(sys->constraint_set))
84 lmm_constraint_t cnst = (lmm_constraint_t)cnstIt;
87 xbt_swag_foreach(elemIt, &(cnst->enabled_element_set))
89 lmm_element_t elem = (lmm_element_t)elemIt;
90 xbt_assert(elem->variable->sharing_weight > 0);
91 concurrency += lmm_element_concurrency(elem);
94 xbt_swag_foreach(elemIt, &(cnst->disabled_element_set))
96 lmm_element_t elem = (lmm_element_t)elemIt;
97 // We should have staged variables only if concurrency is reached in some constraint
98 xbt_assert(cnst->concurrency_limit < 0 || elem->variable->staged_weight == 0 ||
99 lmm_cnstrs_min_concurrency_slack(elem->variable) < elem->variable->concurrency_share,
100 "should not have staged variable!");
103 xbt_assert(cnst->concurrency_limit < 0 || cnst->concurrency_limit >= concurrency, "concurrency check failed!");
104 xbt_assert(cnst->concurrency_current == concurrency, "concurrency_current is out-of-date!");
107 // Check that for each variable, all corresponding elements are in the same state (i.e. same element sets)
109 xbt_swag_foreach(varIt, &(sys->variable_set))
111 lmm_variable_t var = (lmm_variable_t)varIt;
113 if (var->cnsts.empty())
116 lmm_element_t elem = &var->cnsts[0];
117 int belong_to_enabled = xbt_swag_belongs(elem, &(elem->constraint->enabled_element_set));
118 int belong_to_disabled = xbt_swag_belongs(elem, &(elem->constraint->disabled_element_set));
119 int belong_to_active = xbt_swag_belongs(elem, &(elem->constraint->active_element_set));
121 for (s_lmm_element_t const& elem : var->cnsts) {
122 xbt_assert(belong_to_enabled == xbt_swag_belongs(&elem, &(elem.constraint->enabled_element_set)),
123 "Variable inconsistency (1): enabled_element_set");
124 xbt_assert(belong_to_disabled == xbt_swag_belongs(&elem, &(elem.constraint->disabled_element_set)),
125 "Variable inconsistency (2): disabled_element_set");
126 xbt_assert(belong_to_active == xbt_swag_belongs(&elem, &(elem.constraint->active_element_set)),
127 "Variable inconsistency (3): active_element_set");
132 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var)
134 XBT_IN("(sys=%p, var=%p)", sys, var);
137 // TODOLATER Can do better than that by leaving only the variable in only one enabled_element_set, call
138 // lmm_update_modified_set, and then remove it..
139 if (not var->cnsts.empty())
140 lmm_update_modified_set(sys, var->cnsts[0].constraint);
142 for (s_lmm_element_t& elem : var->cnsts) {
143 if (var->sharing_weight > 0)
144 lmm_decrease_concurrency(&elem);
145 xbt_swag_remove(&elem, &(elem.constraint->enabled_element_set));
146 xbt_swag_remove(&elem, &(elem.constraint->disabled_element_set));
147 xbt_swag_remove(&elem, &(elem.constraint->active_element_set));
148 int nelements = xbt_swag_size(&(elem.constraint->enabled_element_set)) +
149 xbt_swag_size(&(elem.constraint->disabled_element_set));
151 make_constraint_inactive(sys, elem.constraint);
153 lmm_on_disabled_var(sys, elem.constraint);
156 // Check if we can enable new variables going through the constraints where var was.
157 // Do it after removing all elements, so the first disabled variables get priority over those with smaller requirement
158 for (s_lmm_element_t& elem : var->cnsts) {
159 if (xbt_swag_size(&(elem.constraint->disabled_element_set)))
160 lmm_on_disabled_var(sys, elem.constraint);
165 lmm_check_concurrency(sys);
167 xbt_mallocator_release(sys->variable_mallocator, var);
171 lmm_system_t lmm_system_new(bool selective_update)
173 s_lmm_variable_t var;
174 s_lmm_constraint_t cnst;
176 lmm_system_t l = new s_lmm_system_t();
179 l->selective_update_active = selective_update;
180 l->visited_counter = 1;
182 XBT_DEBUG("Setting selective_update_active flag to %d", l->selective_update_active);
184 xbt_swag_init(&(l->variable_set), xbt_swag_offset(var, variable_set_hookup));
185 xbt_swag_init(&(l->constraint_set), xbt_swag_offset(cnst, constraint_set_hookup));
187 xbt_swag_init(&(l->active_constraint_set), xbt_swag_offset(cnst, active_constraint_set_hookup));
189 xbt_swag_init(&(l->modified_constraint_set), xbt_swag_offset(cnst, modified_constraint_set_hookup));
190 xbt_swag_init(&(l->saturated_variable_set), xbt_swag_offset(var, saturated_variable_set_hookup));
191 xbt_swag_init(&(l->saturated_constraint_set), xbt_swag_offset(cnst, saturated_constraint_set_hookup));
193 l->variable_mallocator = xbt_mallocator_new(65536,
194 lmm_variable_mallocator_new_f,
195 lmm_variable_mallocator_free_f,
196 lmm_variable_mallocator_reset_f);
198 l->solve_fun = &lmm_solve;
203 void lmm_system_free(lmm_system_t sys)
205 lmm_variable_t var = nullptr;
206 lmm_constraint_t cnst = nullptr;
211 while ((var = (lmm_variable_t) extract_variable(sys))) {
213 char* demangled = abi::__cxa_demangle(typeid(*var->id).name(), 0, 0, &status);
215 XBT_WARN("Probable bug: a %s variable (#%d) not removed before the LMM system destruction.", demangled,
218 lmm_var_free(sys, var);
220 while ((cnst = (lmm_constraint_t) extract_constraint(sys)))
221 lmm_cnst_free(sys, cnst);
223 xbt_mallocator_free(sys->variable_mallocator);
227 static inline void lmm_cnst_free(lmm_system_t sys, lmm_constraint_t cnst)
229 make_constraint_inactive(sys, cnst);
233 lmm_constraint_t lmm_constraint_new(lmm_system_t sys, void *id, double bound_value)
235 lmm_constraint_t cnst = nullptr;
236 s_lmm_element_t elem;
238 cnst = new s_lmm_constraint_t();
240 cnst->id_int = Global_const_debug_id++;
241 xbt_swag_init(&(cnst->enabled_element_set), xbt_swag_offset(elem, enabled_element_set_hookup));
242 xbt_swag_init(&(cnst->disabled_element_set), xbt_swag_offset(elem, disabled_element_set_hookup));
243 xbt_swag_init(&(cnst->active_element_set), xbt_swag_offset(elem, active_element_set_hookup));
245 cnst->bound = bound_value;
246 cnst->concurrency_maximum=0;
247 cnst->concurrency_current=0;
248 cnst->concurrency_limit = sg_concurrency_limit;
250 cnst->sharing_policy = 1; /* FIXME: don't hardcode the value */
251 insert_constraint(sys, cnst);
256 int lmm_constraint_concurrency_limit_get(lmm_constraint_t cnst)
258 return cnst->concurrency_limit;
261 void lmm_constraint_concurrency_limit_set(lmm_constraint_t cnst, int concurrency_limit)
263 xbt_assert(concurrency_limit<0 || cnst->concurrency_maximum<=concurrency_limit,
264 "New concurrency limit should be larger than observed concurrency maximum. Maybe you want to call"
265 " lmm_constraint_concurrency_maximum_reset() to reset the maximum?");
266 cnst->concurrency_limit = concurrency_limit;
269 void lmm_constraint_concurrency_maximum_reset(lmm_constraint_t cnst)
271 cnst->concurrency_maximum = 0;
274 int lmm_constraint_concurrency_maximum_get(lmm_constraint_t cnst)
276 xbt_assert(cnst->concurrency_limit<0 || cnst->concurrency_maximum<=cnst->concurrency_limit,
277 "Very bad: maximum observed concurrency is higher than limit. This is a bug of SURF, please report it.");
278 return cnst->concurrency_maximum;
281 void lmm_constraint_shared(lmm_constraint_t cnst)
283 cnst->sharing_policy = 0;
286 /** Return true if the constraint is shared, and false if it's FATPIPE */
287 int lmm_constraint_sharing_policy(lmm_constraint_t cnst)
289 return (cnst->sharing_policy);
292 static void *lmm_variable_mallocator_new_f()
294 return new s_lmm_variable_t;
297 static void lmm_variable_mallocator_free_f(void *var)
299 delete static_cast<lmm_variable_t>(var);
302 lmm_variable_t lmm_variable_new(lmm_system_t sys, simgrid::surf::Action* id, double sharing_weight, double bound,
303 int number_of_constraints)
305 XBT_IN("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)", sys, id, sharing_weight, bound, number_of_constraints);
307 lmm_variable_t var = (lmm_variable_t)xbt_mallocator_get(sys->variable_mallocator);
309 var->id_int = Global_debug_id++;
310 var->cnsts.reserve(number_of_constraints);
311 var->sharing_weight = sharing_weight;
312 var->staged_weight = 0.0;
314 var->concurrency_share = 1;
316 var->visited = sys->visited_counter - 1;
319 var->func_f = func_f_def;
320 var->func_fp = func_fp_def;
321 var->func_fpi = func_fpi_def;
323 var->variable_set_hookup.next = nullptr;
324 var->variable_set_hookup.prev = nullptr;
325 var->saturated_variable_set_hookup.next = nullptr;
326 var->saturated_variable_set_hookup.prev = nullptr;
329 xbt_swag_insert_at_head(var, &(sys->variable_set));
331 xbt_swag_insert_at_tail(var, &(sys->variable_set));
333 XBT_OUT(" returns %p", var);
337 void lmm_variable_free(lmm_system_t sys, lmm_variable_t var)
339 remove_variable(sys, var);
340 lmm_var_free(sys, var);
343 double lmm_variable_getvalue(lmm_variable_t var)
348 void lmm_variable_concurrency_share_set(lmm_variable_t var, short int concurrency_share)
350 var->concurrency_share=concurrency_share;
353 double lmm_variable_getbound(lmm_variable_t var)
358 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst, lmm_variable_t var, double consumption_weight)
362 //Check if this variable already has an active element in this constraint
363 //If it does, substract it from the required slack
364 int current_share = 0;
365 if(var->concurrency_share>1){
366 for (s_lmm_element_t& elem : var->cnsts) {
367 if (elem.constraint == cnst && xbt_swag_belongs(&elem, &(elem.constraint->enabled_element_set)))
368 current_share += lmm_element_concurrency(&elem);
372 //Check if we need to disable the variable
373 if (var->sharing_weight > 0 && var->concurrency_share - current_share > lmm_concurrency_slack(cnst)) {
374 double weight = var->sharing_weight;
375 lmm_disable_var(sys,var);
376 for (s_lmm_element_t const& elem : var->cnsts)
377 lmm_on_disabled_var(sys, elem.constraint);
378 consumption_weight = 0;
379 var->staged_weight=weight;
380 xbt_assert(not var->sharing_weight);
383 xbt_assert(var->cnsts.size() < var->cnsts.capacity(), "Too much constraints");
385 var->cnsts.resize(var->cnsts.size() + 1);
386 s_lmm_element_t& elem = var->cnsts.back();
388 elem.consumption_weight = consumption_weight;
389 elem.constraint = cnst;
392 if (var->sharing_weight) {
393 xbt_swag_insert_at_head(&elem, &(elem.constraint->enabled_element_set));
394 lmm_increase_concurrency(&elem);
396 xbt_swag_insert_at_tail(&elem, &(elem.constraint->disabled_element_set));
398 if (not sys->selective_update_active) {
399 make_constraint_active(sys, cnst);
400 } else if (elem.consumption_weight > 0 || var->sharing_weight > 0) {
401 make_constraint_active(sys, cnst);
402 lmm_update_modified_set(sys, cnst);
403 //TODOLATER: Why do we need this second call?
404 if (var->cnsts.size() > 1)
405 lmm_update_modified_set(sys, var->cnsts[0].constraint);
408 lmm_check_concurrency(sys);
411 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst, lmm_variable_t var, double value)
415 lmm_check_concurrency(sys);
417 //BEWARE: In case you have multiple elements in one constraint, this will always add value to the first element.
418 auto elem_it = std::find_if(begin(var->cnsts), end(var->cnsts),
419 [&cnst](s_lmm_element_t const& x) { return x.constraint == cnst; });
420 if (elem_it != end(var->cnsts)) {
421 s_lmm_element_t& elem = *elem_it;
422 if (var->sharing_weight)
423 lmm_decrease_concurrency(&elem);
425 if (cnst->sharing_policy)
426 elem.consumption_weight += value;
428 elem.consumption_weight = std::max(elem.consumption_weight, value);
430 //We need to check that increasing value of the element does not cross the concurrency limit
431 if (var->sharing_weight) {
432 if (lmm_concurrency_slack(cnst) < lmm_element_concurrency(&elem)) {
433 double weight = var->sharing_weight;
434 lmm_disable_var(sys,var);
435 for (s_lmm_element_t const& elem2 : var->cnsts)
436 lmm_on_disabled_var(sys, elem2.constraint);
437 var->staged_weight=weight;
438 xbt_assert(not var->sharing_weight);
440 lmm_increase_concurrency(&elem);
442 lmm_update_modified_set(sys, cnst);
444 lmm_expand(sys, cnst, var, value);
446 lmm_check_concurrency(sys);
449 lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t /*sys*/, lmm_variable_t var, unsigned num)
451 if (num < var->cnsts.size())
452 return (var->cnsts[num].constraint);
457 double lmm_get_cnst_weight_from_var(lmm_system_t /*sys*/, lmm_variable_t var, unsigned num)
459 if (num < var->cnsts.size())
460 return (var->cnsts[num].consumption_weight);
465 int lmm_get_number_of_cnst_from_var(lmm_system_t /*sys*/, lmm_variable_t var)
467 return (var->cnsts.size());
470 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t /*sys*/, lmm_constraint_t cnst, lmm_element_t * elem)
472 if (*elem == nullptr) {
473 // That is the first call, pick the first element among enabled_element_set (or disabled_element_set if
474 // enabled_element_set is empty)
475 *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->enabled_element_set));
476 if (*elem == nullptr)
477 *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->disabled_element_set));
479 //elem is not null, so we carry on
480 if(xbt_swag_belongs(*elem,&(cnst->enabled_element_set))){
481 //Look at enabled_element_set, and jump to disabled_element_set when finished
482 *elem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->enabled_element_set.offset);
483 if (*elem == nullptr)
484 *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->disabled_element_set));
486 *elem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->disabled_element_set.offset);
490 return (*elem)->variable;
495 //if we modify the swag between calls, normal version may loop forever
496 //this safe version ensures that we browse the swag elements only once
497 lmm_variable_t lmm_get_var_from_cnst_safe(lmm_system_t /*sys*/, lmm_constraint_t cnst, lmm_element_t * elem,
498 lmm_element_t * nextelem, int * numelem)
500 if (*elem == nullptr) {
501 *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->enabled_element_set));
502 *numelem = xbt_swag_size(&(cnst->enabled_element_set))+xbt_swag_size(&(cnst->disabled_element_set))-1;
503 if (*elem == nullptr)
504 *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->disabled_element_set));
513 //elem is not null, so we carry on
514 if(xbt_swag_belongs(*elem,&(cnst->enabled_element_set))){
515 //Look at enabled_element_set, and jump to disabled_element_set when finished
516 *nextelem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->enabled_element_set.offset);
517 if (*nextelem == nullptr)
518 *nextelem = (lmm_element_t) xbt_swag_getFirst(&(cnst->disabled_element_set));
520 *nextelem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->disabled_element_set.offset);
522 return (*elem)->variable;
527 void *lmm_constraint_id(lmm_constraint_t cnst)
532 void *lmm_variable_id(lmm_variable_t var)
537 static inline void saturated_constraint_set_update(double usage, int cnst_light_num,
538 dyn_light_t& saturated_constraint_set, double* min_usage)
540 xbt_assert(usage > 0,"Impossible");
542 if (*min_usage < 0 || *min_usage > usage) {
544 XBT_HERE(" min_usage=%f (cnst->remaining / cnst->usage =%f)", *min_usage, usage);
545 saturated_constraint_set.assign(1, cnst_light_num);
546 } else if (*min_usage == usage) {
547 saturated_constraint_set.emplace_back(cnst_light_num);
551 static inline void saturated_variable_set_update(s_lmm_constraint_light_t* cnst_light_tab,
552 const dyn_light_t& saturated_constraint_set, lmm_system_t sys)
554 /* Add active variables (i.e. variables that need to be set) from the set of constraints to saturate (cnst_light_tab)*/
555 for (int const& saturated_cnst : saturated_constraint_set) {
556 lmm_constraint_light_t cnst = &cnst_light_tab[saturated_cnst];
558 xbt_swag_t elem_list = &(cnst->cnst->active_element_set);
559 xbt_swag_foreach(_elem, elem_list) {
560 lmm_element_t elem = (lmm_element_t)_elem;
561 //Visiting active_element_set, so, by construction, should never get a zero weight, correct?
562 xbt_assert(elem->variable->sharing_weight > 0);
563 if (elem->consumption_weight > 0)
564 xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
569 void lmm_print(lmm_system_t sys)
571 std::string buf = std::string("MAX-MIN ( ");
574 /* Printing Objective */
575 xbt_swag_t var_list = &(sys->variable_set);
576 xbt_swag_foreach(_var, var_list) {
577 lmm_variable_t var = (lmm_variable_t)_var;
578 buf = buf + "'" + std::to_string(var->id_int) + "'(" + std::to_string(var->sharing_weight) + ") ";
581 XBT_DEBUG("%20s", buf.c_str());
584 XBT_DEBUG("Constraints");
585 /* Printing Constraints */
587 xbt_swag_t cnst_list = &(sys->active_constraint_set);
588 xbt_swag_foreach(_cnst, cnst_list) {
589 lmm_constraint_t cnst = (lmm_constraint_t)_cnst;
591 //Show the enabled variables
593 xbt_swag_t elem_list = &(cnst->enabled_element_set);
595 buf += ((cnst->sharing_policy) ? "(" : "max(");
596 xbt_swag_foreach(_elem, elem_list) {
597 lmm_element_t elem = (lmm_element_t)_elem;
598 buf = buf + std::to_string(elem->consumption_weight) + ".'" + std::to_string(elem->variable->id_int) + "'(" +
599 std::to_string(elem->variable->value) + ")" + ((cnst->sharing_policy) ? " + " : " , ");
600 if(cnst->sharing_policy)
601 sum += elem->consumption_weight * elem->variable->value;
603 sum = std::max(sum, elem->consumption_weight * elem->variable->value);
605 //TODO: Adding disabled elements only for test compatibility, but do we really want them to be printed?
606 elem_list = &(cnst->disabled_element_set);
607 xbt_swag_foreach(_elem, elem_list) {
608 lmm_element_t elem = (lmm_element_t)_elem;
609 buf = buf + std::to_string(elem->consumption_weight) + ".'" + std::to_string(elem->variable->id_int) + "'(" +
610 std::to_string(elem->variable->value) + ")" + ((cnst->sharing_policy) ? " + " : " , ");
611 if(cnst->sharing_policy)
612 sum += elem->consumption_weight * elem->variable->value;
614 sum = std::max(sum, elem->consumption_weight * elem->variable->value);
617 buf = buf + "0) <= " + std::to_string(cnst->bound) + " ('" + std::to_string(cnst->id_int) + "')";
619 if (not cnst->sharing_policy) {
620 buf += " [MAX-Constraint]";
622 XBT_DEBUG("%s", buf.c_str());
624 xbt_assert(not double_positive(sum - cnst->bound, cnst->bound * sg_maxmin_precision),
625 "Incorrect value (%f is not smaller than %f): %g", sum, cnst->bound, sum - cnst->bound);
628 XBT_DEBUG("Variables");
629 /* Printing Result */
630 xbt_swag_foreach(_var, var_list) {
631 lmm_variable_t var = (lmm_variable_t)_var;
632 if (var->bound > 0) {
633 XBT_DEBUG("'%d'(%f) : %f (<=%f)", var->id_int, var->sharing_weight, var->value, var->bound);
634 xbt_assert(not double_positive(var->value - var->bound, var->bound * sg_maxmin_precision),
635 "Incorrect value (%f is not smaller than %f", var->value, var->bound);
637 XBT_DEBUG("'%d'(%f) : %f", var->id_int, var->sharing_weight, var->value);
642 void lmm_solve(lmm_system_t sys)
647 double min_usage = -1;
648 double min_bound = -1;
650 if (not sys->modified)
653 XBT_IN("(sys=%p)", sys);
655 /* Compute Usage and store the variables that reach the maximum. If selective_update_active is true, only constraints
656 * that changed are considered. Otherwise all constraints with active actions are considered.
658 xbt_swag_t cnst_list = sys->selective_update_active ? &(sys->modified_constraint_set) : &(sys->active_constraint_set);
660 XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
661 /* Init: Only modified code portions: reset the value of active variables */
662 xbt_swag_foreach(_cnst, cnst_list) {
663 lmm_constraint_t cnst = (lmm_constraint_t)_cnst;
664 xbt_swag_t elem_list = &(cnst->enabled_element_set);
665 xbt_swag_foreach(_elem, elem_list) {
666 lmm_variable_t var = ((lmm_element_t)_elem)->variable;
667 xbt_assert(var->sharing_weight > 0.0);
672 s_lmm_constraint_light_t* cnst_light_tab = new s_lmm_constraint_light_t[xbt_swag_size(cnst_list)]();
673 int cnst_light_num = 0;
674 dyn_light_t saturated_constraint_set;
676 xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list) {
677 lmm_constraint_t cnst = (lmm_constraint_t)_cnst;
678 /* INIT: Collect constraints that actually need to be saturated (i.e remaining and usage are strictly positive)
679 * into cnst_light_tab. */
680 cnst->remaining = cnst->bound;
681 if (not double_positive(cnst->remaining, cnst->bound * sg_maxmin_precision))
684 xbt_swag_t elem_list = &(cnst->enabled_element_set);
685 xbt_swag_foreach(_elem, elem_list) {
686 lmm_element_t elem = (lmm_element_t)_elem;
687 xbt_assert(elem->variable->sharing_weight > 0);
688 if (elem->consumption_weight > 0) {
689 if (cnst->sharing_policy)
690 cnst->usage += elem->consumption_weight / elem->variable->sharing_weight;
691 else if (cnst->usage < elem->consumption_weight / elem->variable->sharing_weight)
692 cnst->usage = elem->consumption_weight / elem->variable->sharing_weight;
694 make_elem_active(elem);
695 simgrid::surf::Action *action = static_cast<simgrid::surf::Action*>(elem->variable->id);
696 if (sys->keep_track && not action->is_linked())
697 sys->keep_track->push_back(*action);
700 XBT_DEBUG("Constraint '%d' usage: %f remaining: %f concurrency: %i<=%i<=%i", cnst->id_int, cnst->usage,
701 cnst->remaining,cnst->concurrency_current,cnst->concurrency_maximum,cnst->concurrency_limit);
702 /* Saturated constraints update */
704 if(cnst->usage > 0) {
705 cnst_light_tab[cnst_light_num].cnst = cnst;
706 cnst->cnst_light = &(cnst_light_tab[cnst_light_num]);
707 cnst_light_tab[cnst_light_num].remaining_over_usage = cnst->remaining / cnst->usage;
708 saturated_constraint_set_update(cnst_light_tab[cnst_light_num].remaining_over_usage,
709 cnst_light_num, saturated_constraint_set, &min_usage);
710 xbt_assert(cnst->active_element_set.count>0, "There is no sense adding a constraint that has no active element!");
715 saturated_variable_set_update( cnst_light_tab, saturated_constraint_set, sys);
717 /* Saturated variables update */
719 /* Fix the variables that have to be */
720 xbt_swag_t var_list = &(sys->saturated_variable_set);
722 lmm_variable_t var = nullptr;
723 xbt_swag_foreach(_var, var_list) {
724 var = (lmm_variable_t)_var;
725 if (var->sharing_weight <= 0.0)
727 /* First check if some of these variables could reach their upper bound and update min_bound accordingly. */
728 XBT_DEBUG("var=%d, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f", var->id_int,
729 var->bound, var->sharing_weight, min_usage, var->bound * var->sharing_weight);
730 if ((var->bound > 0) && (var->bound * var->sharing_weight < min_usage)) {
732 min_bound = var->bound * var->sharing_weight;
734 min_bound = std::min(min_bound, (var->bound * var->sharing_weight));
735 XBT_DEBUG("Updated min_bound=%f", min_bound);
739 while ((var = (lmm_variable_t)xbt_swag_getFirst(var_list))) {
741 //If no variable could reach its bound, deal iteratively the constraints usage ( at worst one constraint is
742 // saturated at each cycle)
743 var->value = min_usage / var->sharing_weight;
744 XBT_DEBUG("Setting var (%d) value to %f\n", var->id_int, var->value);
746 //If there exist a variable that can reach its bound, only update it (and other with the same bound) for now.
747 if (double_equals(min_bound, var->bound * var->sharing_weight, sg_maxmin_precision)) {
748 var->value = var->bound;
749 XBT_DEBUG("Setting %p (%d) value to %f\n", var, var->id_int, var->value);
751 // Variables which bound is different are not considered for this cycle, but they will be afterwards.
752 XBT_DEBUG("Do not consider %p (%d) \n", var, var->id_int);
753 xbt_swag_remove(var, var_list);
757 XBT_DEBUG("Min usage: %f, Var(%d)->weight: %f, Var(%d)->value: %f ", min_usage, var->id_int, var->sharing_weight,
758 var->id_int, var->value);
760 /* Update the usage of contraints where this variable is involved */
761 for (s_lmm_element_t& elem : var->cnsts) {
762 lmm_constraint_t cnst = elem.constraint;
763 if (cnst->sharing_policy) {
764 // Remember: shared constraints require that sum(elem.value * var->value) < cnst->bound
765 double_update(&(cnst->remaining), elem.consumption_weight * var->value, cnst->bound * sg_maxmin_precision);
766 double_update(&(cnst->usage), elem.consumption_weight / var->sharing_weight, sg_maxmin_precision);
767 // If the constraint is saturated, remove it from the set of active constraints (light_tab)
768 if (not double_positive(cnst->usage, sg_maxmin_precision) ||
769 not double_positive(cnst->remaining, cnst->bound * sg_maxmin_precision)) {
770 if (cnst->cnst_light) {
771 int index = (cnst->cnst_light-cnst_light_tab);
772 XBT_DEBUG("index: %d \t cnst_light_num: %d \t || usage: %f remaining: %f bound: %f ",
773 index,cnst_light_num, cnst->usage, cnst->remaining, cnst->bound);
774 cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
775 cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
777 cnst->cnst_light = nullptr;
780 cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
782 make_elem_inactive(&elem);
784 // Remember: non-shared constraints only require that max(elem.value * var->value) < cnst->bound
786 make_elem_inactive(&elem);
787 xbt_swag_t elem_list = &(cnst->enabled_element_set);
788 xbt_swag_foreach(_elem, elem_list) {
789 lmm_element_t elem2 = static_cast<lmm_element_t>(_elem);
790 xbt_assert(elem2->variable->sharing_weight > 0);
791 if (elem2->variable->value > 0)
793 if (elem2->consumption_weight > 0)
794 cnst->usage = std::max(cnst->usage, elem2->consumption_weight / elem2->variable->sharing_weight);
796 //If the constraint is saturated, remove it from the set of active constraints (light_tab)
797 if (not double_positive(cnst->usage, sg_maxmin_precision) ||
798 not double_positive(cnst->remaining, cnst->bound * sg_maxmin_precision)) {
799 if(cnst->cnst_light) {
800 int index = (cnst->cnst_light-cnst_light_tab);
801 XBT_DEBUG("index: %d \t cnst_light_num: %d \t || \t cnst: %p \t cnst->cnst_light: %p "
802 "\t cnst_light_tab: %p usage: %f remaining: %f bound: %f ", index,cnst_light_num, cnst,
803 cnst->cnst_light, cnst_light_tab, cnst->usage, cnst->remaining, cnst->bound);
804 cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
805 cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
807 cnst->cnst_light = nullptr;
810 cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
811 xbt_assert(cnst->active_element_set.count>0, "Should not keep a maximum constraint that has no active"
812 " element! You want to check the maxmin precision and possible rounding effects." );
816 xbt_swag_remove(var, var_list);
819 /* Find out which variables reach the maximum */
822 saturated_constraint_set.clear();
824 for(pos=0; pos<cnst_light_num; pos++){
825 xbt_assert(cnst_light_tab[pos].cnst->active_element_set.count>0, "Cannot saturate more a constraint that has"
826 " no active element! You may want to change the maxmin precision (--cfg=maxmin/precision:<new_value>)"
827 " because of possible rounding effects.\n\tFor the record, the usage of this constraint is %g while "
828 "the maxmin precision to which it is compared is %g.\n\tThe usage of the previous constraint is %g.",
829 cnst_light_tab[pos].cnst->usage, sg_maxmin_precision, cnst_light_tab[pos-1].cnst->usage);
830 saturated_constraint_set_update(cnst_light_tab[pos].remaining_over_usage, pos, saturated_constraint_set,
834 saturated_variable_set_update(cnst_light_tab, saturated_constraint_set, sys);
836 } while (cnst_light_num > 0);
839 if (sys->selective_update_active)
840 lmm_remove_all_modified_set(sys);
842 if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
846 lmm_check_concurrency(sys);
848 delete[] cnst_light_tab;
852 /** \brief Attribute the value bound to var->bound.
854 * \param sys the lmm_system_t
855 * \param var the lmm_variable_t
856 * \param bound the new bound to associate with var
858 * Makes var->bound equal to bound. Whenever this function is called a change is signed in the system. To
859 * avoid false system changing detection it is a good idea to test (bound != 0) before calling it.
861 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var, double bound)
866 if (not var->cnsts.empty())
867 lmm_update_modified_set(sys, var->cnsts[0].constraint);
870 int lmm_concurrency_slack(lmm_constraint_t cnstr){
871 //FIXME MARTIN: Replace by infinite value std::numeric_limits<int>::(max)(), or something better within Simgrid?
872 if(cnstr->concurrency_limit<0)
875 return cnstr->concurrency_limit - cnstr->concurrency_current;
878 /** \brief Measure the minimum concurrency slack across all constraints where the given var is involved */
879 int lmm_cnstrs_min_concurrency_slack(lmm_variable_t var){
880 int minslack = std::numeric_limits<int>::max();
881 for (s_lmm_element_t const& elem : var->cnsts) {
882 int slack = lmm_concurrency_slack(elem.constraint);
884 //This is only an optimization, to avoid looking at more constraints when slack is already zero
885 //Disable it when debugging to let lmm_concurrency_slack catch nasty things
886 if (not slack && not XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug))
896 /* /Check if a variable can be enabled
898 * Make sure to set staged_weight before, if your intent is only to check concurrency
900 int lmm_can_enable_var(lmm_variable_t var){
901 return var->staged_weight>0 && lmm_cnstrs_min_concurrency_slack(var)>=var->concurrency_share;
904 //Small remark: In this implementation of lmm_enable_var and lmm_disable_var, we will meet multiple times with var when
905 // running lmm_update_modified_set.
906 //A priori not a big performance issue, but we might do better by calling lmm_update_modified_set within the for loops
907 // (after doing the first for enabling==1, and before doing the last for disabling==1)
908 void lmm_enable_var(lmm_system_t sys, lmm_variable_t var){
909 xbt_assert(lmm_can_enable_var(var));
911 var->sharing_weight = var->staged_weight;
912 var->staged_weight = 0;
914 // Enabling the variable, move to var to list head. Subtlety is: here, we need to call lmm_update_modified_set AFTER
915 // moving at least one element of var.
917 xbt_swag_remove(var, &(sys->variable_set));
918 xbt_swag_insert_at_head(var, &(sys->variable_set));
919 for (s_lmm_element_t& elem : var->cnsts) {
920 xbt_swag_remove(&elem, &(elem.constraint->disabled_element_set));
921 xbt_swag_insert_at_head(&elem, &(elem.constraint->enabled_element_set));
922 lmm_increase_concurrency(&elem);
924 if (not var->cnsts.empty())
925 lmm_update_modified_set(sys, var->cnsts[0].constraint);
927 //When used within lmm_on_disabled_var, we would get an assertion fail, because transiently there can be variables
928 // that are staged and could be activated.
929 //Anyway, caller functions all call lmm_check_concurrency() in the end.
932 void lmm_disable_var(lmm_system_t sys, lmm_variable_t var){
933 xbt_assert(not var->staged_weight, "Staged weight should have been cleared");
934 // Disabling the variable, move to var to list tail. Subtlety is: here, we need to call lmm_update_modified_set BEFORE
935 // moving the last element of var.
936 xbt_swag_remove(var, &(sys->variable_set));
937 xbt_swag_insert_at_tail(var, &(sys->variable_set));
938 if (not var->cnsts.empty())
939 lmm_update_modified_set(sys, var->cnsts[0].constraint);
940 for (s_lmm_element_t& elem : var->cnsts) {
941 xbt_swag_remove(&elem, &(elem.constraint->enabled_element_set));
942 xbt_swag_insert_at_tail(&elem, &(elem.constraint->disabled_element_set));
944 xbt_swag_remove(&elem, &(elem.constraint->active_element_set));
946 lmm_decrease_concurrency(&elem);
949 var->sharing_weight = 0.0;
950 var->staged_weight=0.0;
952 lmm_check_concurrency(sys);
955 /* /brief Find variables that can be enabled and enable them.
957 * Assuming that the variable has already been removed from non-zero weights
958 * Can we find a staged variable to add?
959 * If yes, check that none of the constraints that this variable is involved in is at the limit of its concurrency
960 * And then add it to enabled variables
962 void lmm_on_disabled_var(lmm_system_t sys, lmm_constraint_t cnstr){
964 if(cnstr->concurrency_limit<0)
967 int numelem = xbt_swag_size(&(cnstr->disabled_element_set));
971 lmm_element_t elem = (lmm_element_t)xbt_swag_getFirst(&(cnstr->disabled_element_set));
973 //Cannot use xbt_swag_foreach, because lmm_enable_var will modify disabled_element_set.. within the loop
974 while (numelem-- && elem) {
976 lmm_element_t nextelem = (lmm_element_t)xbt_swag_getNext(elem, cnstr->disabled_element_set.offset);
978 if (elem->variable->staged_weight > 0 && lmm_can_enable_var(elem->variable)) {
979 //Found a staged variable
980 //TODOLATER: Add random timing function to model reservation protocol fuzziness? Then how to make sure that
981 //staged variables will eventually be called?
982 lmm_enable_var(sys, elem->variable);
985 xbt_assert(cnstr->concurrency_current<=cnstr->concurrency_limit,"Concurrency overflow!");
986 if(cnstr->concurrency_current==cnstr->concurrency_limit)
992 //We could get an assertion fail, because transiently there can be variables that are staged and could be activated.
993 //And we need to go through all constraints of the disabled var before getting back a coherent state.
994 //Anyway, caller functions all call lmm_check_concurrency() in the end.
997 /* \brief update the weight of a variable, and enable/disable it.
998 * @return Returns whether a change was made
1000 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var, double weight)
1002 xbt_assert(weight>=0,"Variable weight should not be negative!");
1004 if (weight == var->sharing_weight)
1007 int enabling_var = (weight > 0 && var->sharing_weight <= 0);
1008 int disabling_var = (weight <= 0 && var->sharing_weight > 0);
1010 XBT_IN("(sys=%p, var=%p, weight=%f)", sys, var, weight);
1014 //Are we enabling this variable?
1016 var->staged_weight = weight;
1017 int minslack = lmm_cnstrs_min_concurrency_slack(var);
1018 if (minslack < var->concurrency_share) {
1019 XBT_DEBUG("Staging var (instead of enabling) because min concurrency slack %i, with weight %f and concurrency"
1020 " share %i", minslack, weight, var->concurrency_share);
1023 XBT_DEBUG("Enabling var with min concurrency slack %i", minslack);
1024 lmm_enable_var(sys,var);
1025 } else if (disabling_var){
1026 //Are we disabling this variable?
1027 lmm_disable_var(sys,var);
1029 var->sharing_weight = weight;
1032 lmm_check_concurrency(sys);
1037 double lmm_get_variable_weight(lmm_variable_t var)
1039 return var->sharing_weight;
1042 void lmm_update_constraint_bound(lmm_system_t sys, lmm_constraint_t cnst, double bound)
1045 lmm_update_modified_set(sys, cnst);
1046 cnst->bound = bound;
1049 int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
1051 return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
1054 inline lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t sys)
1056 return (lmm_constraint_t)xbt_swag_getFirst(&(sys->active_constraint_set));
1059 inline lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t sys, lmm_constraint_t cnst)
1061 return (lmm_constraint_t)xbt_swag_getNext(cnst, (sys->active_constraint_set).offset);
1064 /** \brief Update the constraint set propagating recursively to other constraints so the system should not be entirely
1067 * \param sys the lmm_system_t
1068 * \param cnst the lmm_constraint_t affected by the change
1070 * A recursive algorithm to optimize the system recalculation selecting only constraints that have changed. Each
1071 * constraint change is propagated to the list of constraints for each variable.
1073 static void lmm_update_modified_set_rec(lmm_system_t sys, lmm_constraint_t cnst)
1077 //TODOLATER: Why lmm_modified_set has been changed in git version 2392B5157...? Looks equivalent logically and less obvious..
1078 xbt_swag_foreach(_elem, &cnst->enabled_element_set) {
1079 lmm_variable_t var = ((lmm_element_t)_elem)->variable;
1080 for (s_lmm_element_t const& elem : var->cnsts) {
1081 if (var->visited == sys->visited_counter)
1083 if (elem.constraint != cnst && not xbt_swag_belongs(elem.constraint, &sys->modified_constraint_set)) {
1084 xbt_swag_insert(elem.constraint, &sys->modified_constraint_set);
1085 lmm_update_modified_set_rec(sys, elem.constraint);
1088 //var will be ignored in later visits as long as sys->visited_counter does not move
1089 var->visited = sys->visited_counter;
1093 static void lmm_update_modified_set(lmm_system_t sys, lmm_constraint_t cnst)
1095 /* nothing to do if selective update isn't active */
1096 if (sys->selective_update_active && not xbt_swag_belongs(cnst, &sys->modified_constraint_set)) {
1097 xbt_swag_insert(cnst, &sys->modified_constraint_set);
1098 lmm_update_modified_set_rec(sys, cnst);
1102 /** \brief Remove all constraints of the modified_constraint_set.
1104 * \param sys the lmm_system_t
1106 static void lmm_remove_all_modified_set(lmm_system_t sys)
1108 // We cleverly un-flag all variables just by incrementing sys->visited_counter
1109 // In effect, the var->visited value will no more be equal to sys->visited counter
1110 // To be clean, when visited counter has wrapped around, we force these var->visited values so that variables that
1111 // were in the modified a long long time ago are not wrongly skipped here, which would lead to very nasty bugs
1112 // (i.e. not readibily reproducible, and requiring a lot of run time before happening).
1113 if (++sys->visited_counter == 1) {
1114 /* the counter wrapped around, reset each variable->visited */
1116 xbt_swag_foreach(_var, &sys->variable_set)
1117 ((lmm_variable_t)_var)->visited = 0;
1119 xbt_swag_reset(&sys->modified_constraint_set);
1123 * Returns resource load (in flop per second, or byte per second, or similar)
1125 * If the resource is shared (the default case), the load is sum of resource usage made by every variables located on
1128 * If the resource is not shared (ie in FATPIPE mode), then the load is the max (not the sum) of all resource usages
1129 * located on this resource.
1131 * \param cnst the lmm_constraint_t associated to the resource
1133 double lmm_constraint_get_usage(lmm_constraint_t cnst) {
1135 xbt_swag_t elem_list = &(cnst->enabled_element_set);
1138 xbt_swag_foreach(_elem, elem_list)
1140 lmm_element_t elem = (lmm_element_t)_elem;
1141 if (elem->consumption_weight > 0) {
1142 if (cnst->sharing_policy)
1143 usage += elem->consumption_weight * elem->variable->value;
1144 else if (usage < elem->consumption_weight * elem->variable->value)
1145 usage = std::max(usage, elem->consumption_weight * elem->variable->value);
1151 int lmm_constraint_get_variable_amount(lmm_constraint_t cnst) {
1153 xbt_swag_t elem_list = &(cnst->enabled_element_set);
1156 xbt_swag_foreach(_elem, elem_list) {
1157 lmm_element_t elem = (lmm_element_t)_elem;
1158 if (elem->consumption_weight > 0)