Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use std::min/std::max instead of MIN/MAX in C++ files.
[simgrid.git] / src / surf / maxmin.cpp
1 /* Copyright (c) 2004-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 /* \file callbacks.h */
7
8 #include "maxmin_private.hpp"
9 #include "xbt/log.h"
10 #include "xbt/mallocator.h"
11 #include "xbt/sysdep.h"
12 #include <algorithm>
13 #include <cmath>
14 #include <cstdlib>
15 #include <cxxabi.h>
16 #include <limits>
17 #include <vector>
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_maxmin, surf, "Logging specific to SURF (maxmin)");
20
21 typedef std::vector<int> dyn_light_t;
22
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 */
26
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;
34
35 static inline void lmm_cnst_free(lmm_system_t sys, lmm_constraint_t cnst);
36
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);
43
44 inline int lmm_element_concurrency(lmm_element_t elem) {
45   //Ignore element with weight less than one (e.g. cross-traffic)
46   return (elem->consumption_weight >= 1) ? 1 : 0;
47   //There are other alternatives, but they will change the behaviour of the model..
48   //So do not use it unless you want to make a new model.
49   //If you do, remember to change the variables concurrency share to reflect it.
50   //Potential examples are:
51   //return (elem->weight>0)?1:0;//Include element as soon  as weight is non-zero
52   //return (int)ceil(elem->weight);//Include element as the rounded-up integer value of the element weight
53 }
54
55 inline void lmm_decrease_concurrency(lmm_element_t elem) {
56   xbt_assert(elem->constraint->concurrency_current>=lmm_element_concurrency(elem));
57   elem->constraint->concurrency_current-=lmm_element_concurrency(elem);
58 }
59
60 inline void lmm_increase_concurrency(lmm_element_t elem) {
61   elem->constraint->concurrency_current+= lmm_element_concurrency(elem);
62
63   lmm_constraint_t cnstr=elem->constraint;
64
65   if(cnstr->concurrency_current > cnstr->concurrency_maximum)
66     cnstr->concurrency_maximum= cnstr->concurrency_current;
67
68   xbt_assert(cnstr->concurrency_limit<0 || cnstr->concurrency_current<=cnstr->concurrency_limit,
69              "Concurrency limit overflow!");
70 }
71
72 static void lmm_check_concurrency(lmm_system_t sys)
73 {
74   // These checks are very expensive, so do them only if we want to debug SURF LMM
75   if (not XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug))
76     return;
77
78   void* cnstIt;
79   xbt_swag_foreach(cnstIt, &(sys->constraint_set))
80   {
81     lmm_constraint_t cnst = (lmm_constraint_t)cnstIt;
82     int concurrency       = 0;
83     void* elemIt;
84     xbt_swag_foreach(elemIt, &(cnst->enabled_element_set))
85     {
86       lmm_element_t elem = (lmm_element_t)elemIt;
87       xbt_assert(elem->variable->sharing_weight > 0);
88       concurrency += lmm_element_concurrency(elem);
89     }
90
91     xbt_swag_foreach(elemIt, &(cnst->disabled_element_set))
92     {
93       lmm_element_t elem = (lmm_element_t)elemIt;
94       // We should have staged variables only if concurrency is reached in some constraint
95       xbt_assert(cnst->concurrency_limit < 0 || elem->variable->staged_weight == 0 ||
96                      lmm_cnstrs_min_concurrency_slack(elem->variable) < elem->variable->concurrency_share,
97                  "should not have staged variable!");
98     }
99
100     xbt_assert(cnst->concurrency_limit < 0 || cnst->concurrency_limit >= concurrency, "concurrency check failed!");
101     xbt_assert(cnst->concurrency_current == concurrency, "concurrency_current is out-of-date!");
102   }
103
104   // Check that for each variable, all corresponding elements are in the same state (i.e. same element sets)
105   void* varIt;
106   xbt_swag_foreach(varIt, &(sys->variable_set))
107   {
108     lmm_variable_t var = (lmm_variable_t)varIt;
109
110     if (var->cnsts.empty())
111       continue;
112
113     lmm_element_t elem     = &var->cnsts[0];
114     int belong_to_enabled  = xbt_swag_belongs(elem, &(elem->constraint->enabled_element_set));
115     int belong_to_disabled = xbt_swag_belongs(elem, &(elem->constraint->disabled_element_set));
116     int belong_to_active   = xbt_swag_belongs(elem, &(elem->constraint->active_element_set));
117
118     for (s_lmm_element_t const& elem : var->cnsts) {
119       xbt_assert(belong_to_enabled == xbt_swag_belongs(&elem, &(elem.constraint->enabled_element_set)),
120                  "Variable inconsistency (1): enabled_element_set");
121       xbt_assert(belong_to_disabled == xbt_swag_belongs(&elem, &(elem.constraint->disabled_element_set)),
122                  "Variable inconsistency (2): disabled_element_set");
123       xbt_assert(belong_to_active == xbt_swag_belongs(&elem, &(elem.constraint->active_element_set)),
124                  "Variable inconsistency (3): active_element_set");
125     }
126   }
127 }
128
129 static inline void lmm_variable_remove(lmm_system_t sys, lmm_variable_t var)
130 {
131   XBT_IN("(sys=%p, var=%p)", sys, var);
132   sys->modified = 1;
133
134   // TODOLATER Can do better than that by leaving only the variable in only one enabled_element_set, call
135   // lmm_update_modified_set, and then remove it..
136   if (not var->cnsts.empty())
137     lmm_update_modified_set(sys, var->cnsts[0].constraint);
138
139   for (s_lmm_element_t& elem : var->cnsts) {
140     if (var->sharing_weight > 0)
141       lmm_decrease_concurrency(&elem);
142     xbt_swag_remove(&elem, &(elem.constraint->enabled_element_set));
143     xbt_swag_remove(&elem, &(elem.constraint->disabled_element_set));
144     xbt_swag_remove(&elem, &(elem.constraint->active_element_set));
145     int nelements = xbt_swag_size(&(elem.constraint->enabled_element_set)) +
146                     xbt_swag_size(&(elem.constraint->disabled_element_set));
147     if (nelements == 0)
148       make_constraint_inactive(sys, elem.constraint);
149     else
150       lmm_on_disabled_var(sys, elem.constraint);
151   }
152
153   // Check if we can enable new variables going through the constraints where var was.
154   // Do it after removing all elements, so the first disabled variables get priority over those with smaller requirement
155   for (s_lmm_element_t& elem : var->cnsts) {
156     if (xbt_swag_size(&(elem.constraint->disabled_element_set)))
157       lmm_on_disabled_var(sys, elem.constraint);
158   }
159
160   var->cnsts.clear();
161
162   lmm_check_concurrency(sys);
163
164   XBT_OUT();
165 }
166
167 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var)
168 {
169   lmm_variable_remove(sys, var);
170   xbt_mallocator_release(sys->variable_mallocator, var);
171 }
172
173 lmm_system_t lmm_system_new(bool selective_update)
174 {
175   s_lmm_variable_t var;
176   s_lmm_constraint_t cnst;
177
178   lmm_system_t l = new s_lmm_system_t();
179
180   l->modified = 0;
181   l->selective_update_active = selective_update;
182   l->visited_counter = 1;
183
184   XBT_DEBUG("Setting selective_update_active flag to %d", l->selective_update_active);
185
186   xbt_swag_init(&(l->variable_set), xbt_swag_offset(var, variable_set_hookup));
187   xbt_swag_init(&(l->constraint_set), xbt_swag_offset(cnst, constraint_set_hookup));
188
189   xbt_swag_init(&(l->active_constraint_set), xbt_swag_offset(cnst, active_constraint_set_hookup));
190
191   xbt_swag_init(&(l->modified_constraint_set), xbt_swag_offset(cnst, modified_constraint_set_hookup));
192   xbt_swag_init(&(l->saturated_variable_set), xbt_swag_offset(var, saturated_variable_set_hookup));
193   xbt_swag_init(&(l->saturated_constraint_set), xbt_swag_offset(cnst, saturated_constraint_set_hookup));
194
195   l->variable_mallocator = xbt_mallocator_new(65536,
196                                               lmm_variable_mallocator_new_f,
197                                               lmm_variable_mallocator_free_f,
198                                               lmm_variable_mallocator_reset_f);
199
200   l->solve_fun = &lmm_solve;
201
202   return l;
203 }
204
205 void lmm_system_free(lmm_system_t sys)
206 {
207   lmm_variable_t var = nullptr;
208   lmm_constraint_t cnst = nullptr;
209
210   if (sys == nullptr)
211     return;
212
213   while ((var = (lmm_variable_t) extract_variable(sys))) {
214     int status;
215     char* demangled = abi::__cxa_demangle(typeid(*var->id).name(), 0, 0, &status);
216
217     XBT_WARN("Probable bug: a %s variable (#%d) not removed before the LMM system destruction.", demangled,
218              var->id_int);
219     xbt_free(demangled);
220     lmm_var_free(sys, var);
221   }
222   while ((cnst = (lmm_constraint_t) extract_constraint(sys)))
223     lmm_cnst_free(sys, cnst);
224
225   xbt_mallocator_free(sys->variable_mallocator);
226   delete sys;
227 }
228
229 static inline void lmm_cnst_free(lmm_system_t sys, lmm_constraint_t cnst)
230 {
231   make_constraint_inactive(sys, cnst);
232   delete cnst;
233 }
234
235 lmm_constraint_t lmm_constraint_new(lmm_system_t sys, void *id, double bound_value)
236 {
237   lmm_constraint_t cnst = nullptr;
238   s_lmm_element_t elem;
239
240   cnst         = new s_lmm_constraint_t();
241   cnst->id     = id;
242   cnst->id_int = Global_const_debug_id++;
243   xbt_swag_init(&(cnst->enabled_element_set), xbt_swag_offset(elem, enabled_element_set_hookup));
244   xbt_swag_init(&(cnst->disabled_element_set), xbt_swag_offset(elem, disabled_element_set_hookup));
245   xbt_swag_init(&(cnst->active_element_set), xbt_swag_offset(elem, active_element_set_hookup));
246
247   cnst->bound = bound_value;
248   cnst->concurrency_maximum=0;
249   cnst->concurrency_current=0;
250   cnst->concurrency_limit  = sg_concurrency_limit;
251   cnst->usage = 0;
252   cnst->sharing_policy = 1; /* FIXME: don't hardcode the value */
253   insert_constraint(sys, cnst);
254
255   return cnst;
256 }
257
258 int lmm_constraint_concurrency_limit_get(lmm_constraint_t cnst)
259 {
260  return cnst->concurrency_limit;
261 }
262
263 void lmm_constraint_concurrency_limit_set(lmm_constraint_t cnst, int concurrency_limit)
264 {
265   xbt_assert(concurrency_limit<0 || cnst->concurrency_maximum<=concurrency_limit,
266              "New concurrency limit should be larger than observed concurrency maximum. Maybe you want to call"
267              " lmm_constraint_concurrency_maximum_reset() to reset the maximum?");
268   cnst->concurrency_limit = concurrency_limit;
269 }
270
271 void lmm_constraint_concurrency_maximum_reset(lmm_constraint_t cnst)
272 {
273   cnst->concurrency_maximum = 0;
274 }
275
276 int lmm_constraint_concurrency_maximum_get(lmm_constraint_t cnst)
277 {
278  xbt_assert(cnst->concurrency_limit<0 || cnst->concurrency_maximum<=cnst->concurrency_limit,
279             "Very bad: maximum observed concurrency is higher than limit. This is a bug of SURF, please report it.");
280   return cnst->concurrency_maximum;
281 }
282
283 void lmm_constraint_shared(lmm_constraint_t cnst)
284 {
285   cnst->sharing_policy = 0;
286 }
287
288 /** Return true if the constraint is shared, and false if it's FATPIPE */
289 int lmm_constraint_sharing_policy(lmm_constraint_t cnst)
290 {
291   return (cnst->sharing_policy);
292 }
293
294 /* @brief Remove a constraint
295  * Currently this is dead code, but it is exposed in maxmin.hpp
296  * Apparently, this call was designed assuming that constraint would no more have elements in it.
297  * If not the case, assertion will fail, and you need to add calls e.g. to lmm_shrink before effectively removing it.
298  */
299 inline void lmm_constraint_free(lmm_system_t sys,lmm_constraint_t cnst)
300 {
301   xbt_assert(not xbt_swag_size(&(cnst->active_element_set)), "Removing constraint but it still has active elements");
302   xbt_assert(not xbt_swag_size(&(cnst->enabled_element_set)), "Removing constraint but it still has enabled elements");
303   xbt_assert(not xbt_swag_size(&(cnst->disabled_element_set)),
304              "Removing constraint but it still has disabled elements");
305   remove_constraint(sys, cnst);
306   lmm_cnst_free(sys, cnst);
307 }
308
309 static void *lmm_variable_mallocator_new_f()
310 {
311   return new s_lmm_variable_t;
312 }
313
314 static void lmm_variable_mallocator_free_f(void *var)
315 {
316   delete static_cast<lmm_variable_t>(var);
317 }
318
319 lmm_variable_t lmm_variable_new(lmm_system_t sys, simgrid::surf::Action* id, double sharing_weight, double bound,
320                                 int number_of_constraints)
321 {
322   XBT_IN("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)", sys, id, sharing_weight, bound, number_of_constraints);
323
324   lmm_variable_t var = (lmm_variable_t)xbt_mallocator_get(sys->variable_mallocator);
325   var->id = id;
326   var->id_int = Global_debug_id++;
327   var->cnsts.reserve(number_of_constraints);
328   var->sharing_weight    = sharing_weight;
329   var->staged_weight = 0.0;
330   var->bound = bound;
331   var->concurrency_share = 1;
332   var->value = 0.0;
333   var->visited = sys->visited_counter - 1;
334   var->mu = 0.0;
335   var->new_mu = 0.0;
336   var->func_f = func_f_def;
337   var->func_fp = func_fp_def;
338   var->func_fpi = func_fpi_def;
339
340   var->variable_set_hookup.next = nullptr;
341   var->variable_set_hookup.prev = nullptr;
342   var->saturated_variable_set_hookup.next = nullptr;
343   var->saturated_variable_set_hookup.prev = nullptr;
344
345   if (sharing_weight)
346     xbt_swag_insert_at_head(var, &(sys->variable_set));
347   else
348     xbt_swag_insert_at_tail(var, &(sys->variable_set));
349
350   XBT_OUT(" returns %p", var);
351   return var;
352 }
353
354 void lmm_variable_free(lmm_system_t sys, lmm_variable_t var)
355 {
356   remove_variable(sys, var);
357   lmm_var_free(sys, var);
358 }
359
360 double lmm_variable_getvalue(lmm_variable_t var)
361 {
362   return (var->value);
363 }
364
365 void lmm_variable_concurrency_share_set(lmm_variable_t var, short int concurrency_share)
366 {
367   var->concurrency_share=concurrency_share;
368 }
369
370 double lmm_variable_getbound(lmm_variable_t var)
371 {
372   return (var->bound);
373 }
374
375 void lmm_shrink(lmm_system_t sys, lmm_constraint_t cnst, lmm_variable_t var)
376 {
377   auto elem_it = std::find_if(begin(var->cnsts), end(var->cnsts),
378                               [&cnst](s_lmm_element_t const& x) { return x.constraint == cnst; });
379   if (elem_it == end(var->cnsts)) {
380     XBT_DEBUG("cnst %p is not found in var %p", cnst, var);
381     return;
382   }
383   s_lmm_element_t& elem = *elem_it;
384
385   sys->modified = 1;
386
387   XBT_DEBUG("remove elem(value %f, cnst %p, var %p) in var %p", elem.consumption_weight, elem.constraint, elem.variable,
388             var);
389
390   /* We are going to change the constraint object and the variable object.
391    * Propagate this change to other objects. Calling here before removing variable from not active elements
392    * (inactive elements are not visited)
393    */
394   lmm_update_modified_set(sys, cnst);
395   //Useful in case var was already removed from the constraint
396   lmm_update_modified_set(sys, var->cnsts[0].constraint); // will look up enabled_element_set of this constraint, and
397                                                      //then each var in the enabled_element_set, and each var->cnsts[i].
398
399   if (xbt_swag_remove(&elem, &(elem.constraint->enabled_element_set)))
400     lmm_decrease_concurrency(&elem);
401
402   xbt_swag_remove(&elem, &(elem.constraint->active_element_set));
403   elem.constraint         = nullptr;
404   elem.variable           = nullptr;
405   elem.consumption_weight = 0;
406
407   var->cnsts.pop_back();
408
409   //No variable in this constraint -> make it inactive
410   if (xbt_swag_size(&(cnst->enabled_element_set))+xbt_swag_size(&(cnst->disabled_element_set)) == 0)
411     make_constraint_inactive(sys, cnst);
412   else {
413     //Check maxconcurrency to see if we can enable new variables
414     lmm_on_disabled_var(sys, elem.constraint);
415   }
416
417   lmm_check_concurrency(sys);
418 }
419
420 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst, lmm_variable_t var, double consumption_weight)
421 {
422   sys->modified = 1;
423
424   //Check if this variable already has an active element in this constraint
425   //If it does, substract it from the required slack
426   int current_share = 0;
427   if(var->concurrency_share>1){
428     for (s_lmm_element_t& elem : var->cnsts) {
429       if (elem.constraint == cnst && xbt_swag_belongs(&elem, &(elem.constraint->enabled_element_set)))
430         current_share += lmm_element_concurrency(&elem);
431     }
432   }
433
434   //Check if we need to disable the variable
435   if (var->sharing_weight > 0 && var->concurrency_share - current_share > lmm_concurrency_slack(cnst)) {
436     double weight = var->sharing_weight;
437     lmm_disable_var(sys,var);
438     for (s_lmm_element_t const& elem : var->cnsts)
439       lmm_on_disabled_var(sys, elem.constraint);
440     consumption_weight = 0;
441     var->staged_weight=weight;
442     xbt_assert(not var->sharing_weight);
443   }
444
445   xbt_assert(var->cnsts.size() < var->cnsts.capacity(), "Too much constraints");
446
447   var->cnsts.resize(var->cnsts.size() + 1);
448   s_lmm_element_t& elem = var->cnsts.back();
449
450   elem.consumption_weight = consumption_weight;
451   elem.constraint         = cnst;
452   elem.variable           = var;
453
454   if (var->sharing_weight) {
455     xbt_swag_insert_at_head(&elem, &(elem.constraint->enabled_element_set));
456     lmm_increase_concurrency(&elem);
457   } else
458     xbt_swag_insert_at_tail(&elem, &(elem.constraint->disabled_element_set));
459
460   if (not sys->selective_update_active) {
461     make_constraint_active(sys, cnst);
462   } else if (elem.consumption_weight > 0 || var->sharing_weight > 0) {
463     make_constraint_active(sys, cnst);
464     lmm_update_modified_set(sys, cnst);
465     //TODOLATER: Why do we need this second call?
466     if (var->cnsts.size() > 1)
467       lmm_update_modified_set(sys, var->cnsts[0].constraint);
468   }
469
470   lmm_check_concurrency(sys);
471 }
472
473 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst, lmm_variable_t var, double value)
474 {
475   sys->modified = 1;
476
477   lmm_check_concurrency(sys);
478
479   //BEWARE: In case you have multiple elements in one constraint, this will always add value to the first element.
480   auto elem_it = std::find_if(begin(var->cnsts), end(var->cnsts),
481                               [&cnst](s_lmm_element_t const& x) { return x.constraint == cnst; });
482   if (elem_it != end(var->cnsts)) {
483     s_lmm_element_t& elem = *elem_it;
484     if (var->sharing_weight)
485       lmm_decrease_concurrency(&elem);
486
487     if (cnst->sharing_policy)
488       elem.consumption_weight += value;
489     else
490       elem.consumption_weight = std::max(elem.consumption_weight, value);
491
492     //We need to check that increasing value of the element does not cross the concurrency limit
493     if (var->sharing_weight) {
494       if (lmm_concurrency_slack(cnst) < lmm_element_concurrency(&elem)) {
495         double weight = var->sharing_weight;
496         lmm_disable_var(sys,var);
497         for (s_lmm_element_t const& elem2 : var->cnsts)
498           lmm_on_disabled_var(sys, elem2.constraint);
499         var->staged_weight=weight;
500         xbt_assert(not var->sharing_weight);
501       }
502       lmm_increase_concurrency(&elem);
503     }
504     lmm_update_modified_set(sys, cnst);
505   } else
506     lmm_expand(sys, cnst, var, value);
507
508   lmm_check_concurrency(sys);
509 }
510
511 lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t /*sys*/, lmm_variable_t var, unsigned num)
512 {
513   if (num < var->cnsts.size())
514     return (var->cnsts[num].constraint);
515   else
516     return nullptr;
517 }
518
519 double lmm_get_cnst_weight_from_var(lmm_system_t /*sys*/, lmm_variable_t var, unsigned num)
520 {
521   if (num < var->cnsts.size())
522     return (var->cnsts[num].consumption_weight);
523   else
524     return 0.0;
525 }
526
527 int lmm_get_number_of_cnst_from_var(lmm_system_t /*sys*/, lmm_variable_t var)
528 {
529   return (var->cnsts.size());
530 }
531
532 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t /*sys*/, lmm_constraint_t cnst, lmm_element_t * elem)
533 {
534   if (*elem == nullptr) {
535     // That is the first call, pick the first element among enabled_element_set (or disabled_element_set if
536     // enabled_element_set is empty)
537     *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->enabled_element_set));
538     if (*elem == nullptr)
539       *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->disabled_element_set));
540   } else {
541     //elem is not null, so we carry on
542     if(xbt_swag_belongs(*elem,&(cnst->enabled_element_set))){
543       //Look at enabled_element_set, and jump to disabled_element_set when finished
544       *elem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->enabled_element_set.offset);
545       if (*elem == nullptr)
546         *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->disabled_element_set));
547     } else {
548       *elem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->disabled_element_set.offset);
549     }
550   }
551   if (*elem)
552     return (*elem)->variable;
553   else
554     return nullptr;
555 }
556
557 //if we modify the swag between calls, normal version may loop forever
558 //this safe version ensures that we browse the swag elements only once
559 lmm_variable_t lmm_get_var_from_cnst_safe(lmm_system_t /*sys*/, lmm_constraint_t cnst, lmm_element_t * elem,
560                                           lmm_element_t * nextelem, int * numelem)
561 {
562   if (*elem == nullptr) {
563     *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->enabled_element_set));
564     *numelem = xbt_swag_size(&(cnst->enabled_element_set))+xbt_swag_size(&(cnst->disabled_element_set))-1;
565     if (*elem == nullptr)
566       *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->disabled_element_set));
567   }else{
568     *elem = *nextelem;
569     if(*numelem>0){
570      (*numelem) --;
571     }else
572       return nullptr;
573   }
574   if (*elem){
575     //elem is not null, so we carry on
576     if(xbt_swag_belongs(*elem,&(cnst->enabled_element_set))){
577       //Look at enabled_element_set, and jump to disabled_element_set when finished
578       *nextelem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->enabled_element_set.offset);
579       if (*nextelem == nullptr)
580         *nextelem = (lmm_element_t) xbt_swag_getFirst(&(cnst->disabled_element_set));
581     } else {
582       *nextelem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->disabled_element_set.offset);
583     }
584     return (*elem)->variable;
585   }else
586     return nullptr;
587 }
588
589 void *lmm_constraint_id(lmm_constraint_t cnst)
590 {
591   return cnst->id;
592 }
593
594 void *lmm_variable_id(lmm_variable_t var)
595 {
596   return var->id;
597 }
598
599 static inline void saturated_constraint_set_update(double usage, int cnst_light_num,
600                                                    dyn_light_t& saturated_constraint_set, double* min_usage)
601 {
602   xbt_assert(usage > 0,"Impossible");
603
604   if (*min_usage < 0 || *min_usage > usage) {
605     *min_usage = usage;
606     XBT_HERE(" min_usage=%f (cnst->remaining / cnst->usage =%f)", *min_usage, usage);
607     saturated_constraint_set.assign(1, cnst_light_num);
608   } else if (*min_usage == usage) {
609     saturated_constraint_set.emplace_back(cnst_light_num);
610   }
611 }
612
613 static inline void saturated_variable_set_update(s_lmm_constraint_light_t* cnst_light_tab,
614                                                  const dyn_light_t& saturated_constraint_set, lmm_system_t sys)
615 {
616   /* Add active variables (i.e. variables that need to be set) from the set of constraints to saturate (cnst_light_tab)*/
617   for (int const& saturated_cnst : saturated_constraint_set) {
618     lmm_constraint_light_t cnst = &cnst_light_tab[saturated_cnst];
619     void* _elem;
620     xbt_swag_t elem_list = &(cnst->cnst->active_element_set);
621     xbt_swag_foreach(_elem, elem_list) {
622       lmm_element_t elem = (lmm_element_t)_elem;
623       //Visiting active_element_set, so, by construction, should never get a zero weight, correct?
624       xbt_assert(elem->variable->sharing_weight > 0);
625       if (elem->consumption_weight > 0)
626         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
627     }
628   }
629 }
630
631 void lmm_print(lmm_system_t sys)
632 {
633   std::string buf       = std::string("MAX-MIN ( ");
634   void* _var;
635
636   /* Printing Objective */
637   xbt_swag_t var_list = &(sys->variable_set);
638   xbt_swag_foreach(_var, var_list) {
639     lmm_variable_t var = (lmm_variable_t)_var;
640     buf = buf + "'" + std::to_string(var->id_int) + "'(" + std::to_string(var->sharing_weight) + ") ";
641   }
642   buf += ")";
643   XBT_DEBUG("%20s", buf.c_str());
644   buf.clear();
645
646   XBT_DEBUG("Constraints");
647   /* Printing Constraints */
648   void* _cnst;
649   xbt_swag_t cnst_list = &(sys->active_constraint_set);
650   xbt_swag_foreach(_cnst, cnst_list) {
651     lmm_constraint_t cnst = (lmm_constraint_t)_cnst;
652     double sum = 0.0;
653     //Show  the enabled variables
654     void* _elem;
655     xbt_swag_t elem_list = &(cnst->enabled_element_set);
656     buf += "\t";
657     buf += ((cnst->sharing_policy) ? "(" : "max(");
658     xbt_swag_foreach(_elem, elem_list) {
659       lmm_element_t elem = (lmm_element_t)_elem;
660       buf  = buf + std::to_string(elem->consumption_weight) + ".'" + std::to_string(elem->variable->id_int) + "'(" +
661             std::to_string(elem->variable->value) + ")" + ((cnst->sharing_policy) ? " + " : " , ");
662       if(cnst->sharing_policy)
663         sum += elem->consumption_weight * elem->variable->value;
664       else
665         sum = std::max(sum, elem->consumption_weight * elem->variable->value);
666     }
667     //TODO: Adding disabled elements only for test compatibility, but do we really want them to be printed?
668     elem_list = &(cnst->disabled_element_set);
669     xbt_swag_foreach(_elem, elem_list) {
670       lmm_element_t elem = (lmm_element_t)_elem;
671       buf  = buf + std::to_string(elem->consumption_weight) + ".'" + std::to_string(elem->variable->id_int) + "'(" +
672             std::to_string(elem->variable->value) + ")" + ((cnst->sharing_policy) ? " + " : " , ");
673       if(cnst->sharing_policy)
674         sum += elem->consumption_weight * elem->variable->value;
675       else
676         sum = std::max(sum, elem->consumption_weight * elem->variable->value);
677     }
678
679     buf = buf + "0) <= " + std::to_string(cnst->bound) + " ('" + std::to_string(cnst->id_int) + "')";
680
681     if (not cnst->sharing_policy) {
682       buf += " [MAX-Constraint]";
683     }
684     XBT_DEBUG("%s", buf.c_str());
685     buf.clear();
686     xbt_assert(not double_positive(sum - cnst->bound, cnst->bound * sg_maxmin_precision),
687                "Incorrect value (%f is not smaller than %f): %g", sum, cnst->bound, sum - cnst->bound);
688   }
689
690   XBT_DEBUG("Variables");
691   /* Printing Result */
692   xbt_swag_foreach(_var, var_list) {
693     lmm_variable_t var = (lmm_variable_t)_var;
694     if (var->bound > 0) {
695       XBT_DEBUG("'%d'(%f) : %f (<=%f)", var->id_int, var->sharing_weight, var->value, var->bound);
696       xbt_assert(not double_positive(var->value - var->bound, var->bound * sg_maxmin_precision),
697                  "Incorrect value (%f is not smaller than %f", var->value, var->bound);
698     } else {
699       XBT_DEBUG("'%d'(%f) : %f", var->id_int, var->sharing_weight, var->value);
700     }
701   }
702 }
703
704 void lmm_solve(lmm_system_t sys)
705 {
706   void* _cnst;
707   void* _cnst_next;
708   void* _elem;
709   double min_usage = -1;
710   double min_bound = -1;
711
712   if (not sys->modified)
713     return;
714
715   XBT_IN("(sys=%p)", sys);
716
717   /* Compute Usage and store the variables that reach the maximum. If selective_update_active is true, only constraints
718    * that changed are considered. Otherwise all constraints with active actions are considered.
719    */
720   xbt_swag_t cnst_list = sys->selective_update_active ? &(sys->modified_constraint_set) : &(sys->active_constraint_set);
721
722   XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
723   /* Init: Only modified code portions: reset the value of active variables */
724   xbt_swag_foreach(_cnst, cnst_list) {
725     lmm_constraint_t cnst = (lmm_constraint_t)_cnst;
726     xbt_swag_t elem_list  = &(cnst->enabled_element_set);
727     xbt_swag_foreach(_elem, elem_list) {
728       lmm_variable_t var = ((lmm_element_t)_elem)->variable;
729       xbt_assert(var->sharing_weight > 0.0);
730       var->value = 0.0;
731     }
732   }
733
734   s_lmm_constraint_light_t* cnst_light_tab = new s_lmm_constraint_light_t[xbt_swag_size(cnst_list)]();
735   int cnst_light_num = 0;
736   dyn_light_t saturated_constraint_set;
737
738   xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list) {
739     lmm_constraint_t cnst = (lmm_constraint_t)_cnst;
740     /* INIT: Collect constraints that actually need to be saturated (i.e remaining  and usage are strictly positive)
741      * into cnst_light_tab. */
742     cnst->remaining = cnst->bound;
743     if (not double_positive(cnst->remaining, cnst->bound * sg_maxmin_precision))
744       continue;
745     cnst->usage = 0;
746     xbt_swag_t elem_list = &(cnst->enabled_element_set);
747     xbt_swag_foreach(_elem, elem_list) {
748       lmm_element_t elem = (lmm_element_t)_elem;
749       xbt_assert(elem->variable->sharing_weight > 0);
750       if (elem->consumption_weight > 0) {
751         if (cnst->sharing_policy)
752           cnst->usage += elem->consumption_weight / elem->variable->sharing_weight;
753         else if (cnst->usage < elem->consumption_weight / elem->variable->sharing_weight)
754           cnst->usage = elem->consumption_weight / elem->variable->sharing_weight;
755
756         make_elem_active(elem);
757         simgrid::surf::Action *action = static_cast<simgrid::surf::Action*>(elem->variable->id);
758         if (sys->keep_track && not action->is_linked())
759           sys->keep_track->push_back(*action);
760       }
761     }
762     XBT_DEBUG("Constraint '%d' usage: %f remaining: %f concurrency: %i<=%i<=%i", cnst->id_int, cnst->usage,
763               cnst->remaining,cnst->concurrency_current,cnst->concurrency_maximum,cnst->concurrency_limit);
764     /* Saturated constraints update */
765
766     if(cnst->usage > 0) {
767       cnst_light_tab[cnst_light_num].cnst = cnst;
768       cnst->cnst_light = &(cnst_light_tab[cnst_light_num]);
769       cnst_light_tab[cnst_light_num].remaining_over_usage = cnst->remaining / cnst->usage;
770       saturated_constraint_set_update(cnst_light_tab[cnst_light_num].remaining_over_usage,
771         cnst_light_num, saturated_constraint_set, &min_usage);
772       xbt_assert(cnst->active_element_set.count>0, "There is no sense adding a constraint that has no active element!");
773       cnst_light_num++;
774     }
775   }
776
777   saturated_variable_set_update(  cnst_light_tab, saturated_constraint_set, sys);
778
779   /* Saturated variables update */
780   do {
781     /* Fix the variables that have to be */
782     xbt_swag_t var_list = &(sys->saturated_variable_set);
783     void* _var;
784     lmm_variable_t var = nullptr;
785     xbt_swag_foreach(_var, var_list) {
786       var = (lmm_variable_t)_var;
787       if (var->sharing_weight <= 0.0)
788         DIE_IMPOSSIBLE;
789       /* First check if some of these variables could reach their upper bound and update min_bound accordingly. */
790       XBT_DEBUG("var=%d, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f", var->id_int,
791                 var->bound, var->sharing_weight, min_usage, var->bound * var->sharing_weight);
792       if ((var->bound > 0) && (var->bound * var->sharing_weight < min_usage)) {
793         if (min_bound < 0)
794           min_bound = var->bound * var->sharing_weight;
795         else
796           min_bound = std::min(min_bound, (var->bound * var->sharing_weight));
797         XBT_DEBUG("Updated min_bound=%f", min_bound);
798       }
799     }
800
801     while ((var = (lmm_variable_t)xbt_swag_getFirst(var_list))) {
802       if (min_bound < 0) {
803         //If no variable could reach its bound, deal iteratively the constraints usage ( at worst one constraint is
804         // saturated at each cycle)
805         var->value = min_usage / var->sharing_weight;
806         XBT_DEBUG("Setting var (%d) value to %f\n", var->id_int, var->value);
807       } else {
808          //If there exist a variable that can reach its bound, only update it (and other with the same bound) for now.
809          if (double_equals(min_bound, var->bound * var->sharing_weight, sg_maxmin_precision)) {
810            var->value = var->bound;
811            XBT_DEBUG("Setting %p (%d) value to %f\n", var, var->id_int, var->value);
812          } else {
813            // Variables which bound is different are not considered for this cycle, but they will be afterwards.
814            XBT_DEBUG("Do not consider %p (%d) \n", var, var->id_int);
815            xbt_swag_remove(var, var_list);
816            continue;
817          }
818       }
819       XBT_DEBUG("Min usage: %f, Var(%d)->weight: %f, Var(%d)->value: %f ", min_usage, var->id_int, var->sharing_weight,
820                 var->id_int, var->value);
821
822       /* Update the usage of contraints where this variable is involved */
823       for (s_lmm_element_t& elem : var->cnsts) {
824         lmm_constraint_t cnst = elem.constraint;
825         if (cnst->sharing_policy) {
826           // Remember: shared constraints require that sum(elem.value * var->value) < cnst->bound
827           double_update(&(cnst->remaining), elem.consumption_weight * var->value, cnst->bound * sg_maxmin_precision);
828           double_update(&(cnst->usage), elem.consumption_weight / var->sharing_weight, sg_maxmin_precision);
829           // If the constraint is saturated, remove it from the set of active constraints (light_tab)
830           if (not double_positive(cnst->usage, sg_maxmin_precision) ||
831               not double_positive(cnst->remaining, cnst->bound * sg_maxmin_precision)) {
832             if (cnst->cnst_light) {
833               int index = (cnst->cnst_light-cnst_light_tab);
834               XBT_DEBUG("index: %d \t cnst_light_num: %d \t || usage: %f remaining: %f bound: %f  ",
835                          index,cnst_light_num, cnst->usage, cnst->remaining, cnst->bound);
836               cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
837               cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
838               cnst_light_num--;
839               cnst->cnst_light = nullptr;
840             }
841           } else {
842             cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
843           }
844           make_elem_inactive(&elem);
845         } else {
846           // Remember: non-shared constraints only require that max(elem.value * var->value) < cnst->bound
847           cnst->usage = 0.0;
848           make_elem_inactive(&elem);
849           xbt_swag_t elem_list = &(cnst->enabled_element_set);
850           xbt_swag_foreach(_elem, elem_list) {
851             lmm_element_t elem2 = static_cast<lmm_element_t>(_elem);
852             xbt_assert(elem2->variable->sharing_weight > 0);
853             if (elem2->variable->value > 0)
854               continue;
855             if (elem2->consumption_weight > 0)
856               cnst->usage = std::max(cnst->usage, elem2->consumption_weight / elem2->variable->sharing_weight);
857           }
858           //If the constraint is saturated, remove it from the set of active constraints (light_tab)
859           if (not double_positive(cnst->usage, sg_maxmin_precision) ||
860               not double_positive(cnst->remaining, cnst->bound * sg_maxmin_precision)) {
861             if(cnst->cnst_light) {
862               int index = (cnst->cnst_light-cnst_light_tab);
863               XBT_DEBUG("index: %d \t cnst_light_num: %d \t || \t cnst: %p \t cnst->cnst_light: %p "
864                         "\t cnst_light_tab: %p usage: %f remaining: %f bound: %f  ", index,cnst_light_num, cnst,
865                         cnst->cnst_light, cnst_light_tab, cnst->usage, cnst->remaining, cnst->bound);
866               cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
867               cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
868               cnst_light_num--;
869               cnst->cnst_light = nullptr;
870             }
871           } else {
872             cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
873             xbt_assert(cnst->active_element_set.count>0, "Should not keep a maximum constraint that has no active"
874                        " element! You want to check the maxmin precision and possible rounding effects." );
875           }
876         }
877       }
878       xbt_swag_remove(var, var_list);
879     }
880
881     /* Find out which variables reach the maximum */
882     min_usage = -1;
883     min_bound = -1;
884     saturated_constraint_set.clear();
885     int pos;
886     for(pos=0; pos<cnst_light_num; pos++){
887       xbt_assert(cnst_light_tab[pos].cnst->active_element_set.count>0, "Cannot saturate more a constraint that has"
888                  " no active element! You may want to change the maxmin precision (--cfg=maxmin/precision:<new_value>)"
889                  " because of possible rounding effects.\n\tFor the record, the usage of this constraint is %g while "
890                  "the maxmin precision to which it is compared is %g.\n\tThe usage of the previous constraint is %g.",
891                  cnst_light_tab[pos].cnst->usage, sg_maxmin_precision, cnst_light_tab[pos-1].cnst->usage);
892       saturated_constraint_set_update(cnst_light_tab[pos].remaining_over_usage, pos, saturated_constraint_set,
893                                       &min_usage);
894     }
895
896     saturated_variable_set_update(cnst_light_tab, saturated_constraint_set, sys);
897
898   } while (cnst_light_num > 0);
899
900   sys->modified = 0;
901   if (sys->selective_update_active)
902     lmm_remove_all_modified_set(sys);
903
904   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
905     lmm_print(sys);
906   }
907
908   lmm_check_concurrency(sys);
909
910   delete[] cnst_light_tab;
911   XBT_OUT();
912 }
913
914 /** \brief Attribute the value bound to var->bound.
915  *
916  *  \param sys the lmm_system_t
917  *  \param var the lmm_variable_t
918  *  \param bound the new bound to associate with var
919  *
920  *  Makes var->bound equal to bound. Whenever this function is called a change is  signed in the system. To
921  *  avoid false system changing detection it is a good idea to test (bound != 0) before calling it.
922  */
923 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var, double bound)
924 {
925   sys->modified = 1;
926   var->bound = bound;
927
928   if (not var->cnsts.empty())
929     lmm_update_modified_set(sys, var->cnsts[0].constraint);
930 }
931
932 int lmm_concurrency_slack(lmm_constraint_t cnstr){
933   //FIXME MARTIN: Replace by infinite value std::numeric_limits<int>::(max)(), or something better within Simgrid?
934   if(cnstr->concurrency_limit<0)
935     return 666;
936
937   return  cnstr->concurrency_limit - cnstr->concurrency_current;
938 }
939
940 /** \brief Measure the minimum concurrency slack across all constraints where the given var is involved */
941 int lmm_cnstrs_min_concurrency_slack(lmm_variable_t var){
942   int minslack = std::numeric_limits<int>::max();
943   for (s_lmm_element_t const& elem : var->cnsts) {
944     int slack = lmm_concurrency_slack(elem.constraint);
945
946     //This is only an optimization, to avoid looking at more constraints when slack is already zero
947     //Disable it when debugging to let lmm_concurrency_slack catch nasty things
948     if (not slack && not XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug))
949       return 0;
950
951     if(minslack>slack)
952       minslack=slack;
953   }
954
955   return minslack;
956 }
957
958 /* /Check if a variable can be enabled
959  *
960  * Make sure to set staged_weight before, if your intent is only to check concurrency
961  */
962 int lmm_can_enable_var(lmm_variable_t var){
963   return var->staged_weight>0 && lmm_cnstrs_min_concurrency_slack(var)>=var->concurrency_share;
964 }
965
966 //Small remark: In this implementation of lmm_enable_var and lmm_disable_var, we will meet multiple times with var when
967 // running lmm_update_modified_set.
968 //A priori not a big performance issue, but we might do better by calling lmm_update_modified_set within the for loops
969 // (after doing the first for enabling==1, and before doing the last for disabling==1)
970 void lmm_enable_var(lmm_system_t sys, lmm_variable_t var){
971   xbt_assert(lmm_can_enable_var(var));
972
973   var->sharing_weight = var->staged_weight;
974   var->staged_weight = 0;
975
976   // Enabling the variable, move to var to list head. Subtlety is: here, we need to call lmm_update_modified_set AFTER
977   // moving at least one element of var.
978
979   xbt_swag_remove(var, &(sys->variable_set));
980   xbt_swag_insert_at_head(var, &(sys->variable_set));
981   for (s_lmm_element_t& elem : var->cnsts) {
982     xbt_swag_remove(&elem, &(elem.constraint->disabled_element_set));
983     xbt_swag_insert_at_head(&elem, &(elem.constraint->enabled_element_set));
984     lmm_increase_concurrency(&elem);
985   }
986   if (not var->cnsts.empty())
987     lmm_update_modified_set(sys, var->cnsts[0].constraint);
988
989   //When used within lmm_on_disabled_var, we would get an assertion fail, because transiently there can be variables
990   // that are staged and could be activated.
991   //Anyway, caller functions all call lmm_check_concurrency() in the end.
992 }
993
994 void lmm_disable_var(lmm_system_t sys, lmm_variable_t var){
995   xbt_assert(not var->staged_weight, "Staged weight should have been cleared");
996   // Disabling the variable, move to var to list tail. Subtlety is: here, we need to call lmm_update_modified_set BEFORE
997   // moving the last element of var.
998   xbt_swag_remove(var, &(sys->variable_set));
999   xbt_swag_insert_at_tail(var, &(sys->variable_set));
1000   if (not var->cnsts.empty())
1001     lmm_update_modified_set(sys, var->cnsts[0].constraint);
1002   for (s_lmm_element_t& elem : var->cnsts) {
1003     xbt_swag_remove(&elem, &(elem.constraint->enabled_element_set));
1004     xbt_swag_insert_at_tail(&elem, &(elem.constraint->disabled_element_set));
1005
1006     xbt_swag_remove(&elem, &(elem.constraint->active_element_set));
1007
1008     lmm_decrease_concurrency(&elem);
1009   }
1010
1011   var->sharing_weight = 0.0;
1012   var->staged_weight=0.0;
1013   var->value = 0.0;
1014   lmm_check_concurrency(sys);
1015 }
1016
1017 /* /brief Find variables that can be enabled and enable them.
1018  *
1019  * Assuming that the variable has already been removed from non-zero weights
1020  * Can we find a staged variable to add?
1021  * If yes, check that none of the constraints that this variable is involved in is at the limit of its concurrency
1022  * And then add it to enabled variables
1023  */
1024 void lmm_on_disabled_var(lmm_system_t sys, lmm_constraint_t cnstr){
1025
1026   if(cnstr->concurrency_limit<0)
1027     return;
1028
1029   int numelem = xbt_swag_size(&(cnstr->disabled_element_set));
1030   if (not numelem)
1031     return;
1032
1033   lmm_element_t elem = (lmm_element_t)xbt_swag_getFirst(&(cnstr->disabled_element_set));
1034
1035   //Cannot use xbt_swag_foreach, because lmm_enable_var will modify disabled_element_set.. within the loop
1036   while (numelem-- && elem) {
1037
1038     lmm_element_t nextelem = (lmm_element_t)xbt_swag_getNext(elem, cnstr->disabled_element_set.offset);
1039
1040     if (elem->variable->staged_weight>0 ){
1041       //Found a staged variable
1042       //TODOLATER: Add random timing function to model reservation protocol fuzziness? Then how to make sure that
1043       //staged variables will eventually be called?
1044       if(lmm_can_enable_var(elem->variable)){
1045         lmm_enable_var(sys,elem->variable);
1046       }
1047     }
1048
1049     xbt_assert(cnstr->concurrency_current<=cnstr->concurrency_limit,"Concurrency overflow!");
1050     if(cnstr->concurrency_current==cnstr->concurrency_limit)
1051       break;
1052
1053     elem = nextelem;
1054   }
1055
1056   //We could get an assertion fail, because transiently there can be variables that are staged and could be activated.
1057   //And we need to go through all constraints of the disabled var before getting back a coherent state.
1058   //Anyway, caller functions all call lmm_check_concurrency() in the end.
1059 }
1060
1061 /* \brief update the weight of a variable, and enable/disable it.
1062  * @return Returns whether a change was made
1063  */
1064 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var, double weight)
1065 {
1066   xbt_assert(weight>=0,"Variable weight should not be negative!");
1067
1068   if (weight == var->sharing_weight)
1069     return;
1070
1071   int enabling_var  = (weight > 0 && var->sharing_weight <= 0);
1072   int disabling_var = (weight <= 0 && var->sharing_weight > 0);
1073
1074   XBT_IN("(sys=%p, var=%p, weight=%f)", sys, var, weight);
1075
1076   sys->modified = 1;
1077
1078   //Are we enabling this variable?
1079   if (enabling_var){
1080     var->staged_weight = weight;
1081     int minslack       = lmm_cnstrs_min_concurrency_slack(var);
1082     if (minslack < var->concurrency_share) {
1083       XBT_DEBUG("Staging var (instead of enabling) because min concurrency slack %i, with weight %f and concurrency"
1084                 " share %i", minslack, weight, var->concurrency_share);
1085       return;
1086     }
1087     XBT_DEBUG("Enabling var with min concurrency slack %i", minslack);
1088     lmm_enable_var(sys,var);
1089   } else if (disabling_var){
1090     //Are we disabling this variable?
1091     lmm_disable_var(sys,var);
1092   } else {
1093     var->sharing_weight = weight;
1094   }
1095
1096   lmm_check_concurrency(sys);
1097
1098   XBT_OUT();
1099 }
1100
1101 double lmm_get_variable_weight(lmm_variable_t var)
1102 {
1103   return var->sharing_weight;
1104 }
1105
1106 void lmm_update_constraint_bound(lmm_system_t sys, lmm_constraint_t cnst, double bound)
1107 {
1108   sys->modified = 1;
1109   lmm_update_modified_set(sys, cnst);
1110   cnst->bound = bound;
1111 }
1112
1113 int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
1114 {
1115   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
1116 }
1117
1118 inline lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t sys)
1119 {
1120   return (lmm_constraint_t)xbt_swag_getFirst(&(sys->active_constraint_set));
1121 }
1122
1123 inline lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t sys, lmm_constraint_t cnst)
1124 {
1125   return (lmm_constraint_t)xbt_swag_getNext(cnst, (sys->active_constraint_set).offset);
1126 }
1127
1128 /** \brief Update the constraint set propagating recursively to other constraints so the system should not be entirely
1129  *  computed.
1130  *
1131  *  \param sys the lmm_system_t
1132  *  \param cnst the lmm_constraint_t affected by the change
1133  *
1134  *  A recursive algorithm to optimize the system recalculation selecting only constraints that have changed. Each
1135  *  constraint change is propagated to the list of constraints for each variable.
1136  */
1137 static void lmm_update_modified_set_rec(lmm_system_t sys, lmm_constraint_t cnst)
1138 {
1139   void* _elem;
1140
1141   //TODOLATER: Why lmm_modified_set has been changed in git version 2392B5157...? Looks equivalent logically and less obvious..
1142   xbt_swag_foreach(_elem, &cnst->enabled_element_set) {
1143     lmm_variable_t var = ((lmm_element_t)_elem)->variable;
1144     for (s_lmm_element_t const& elem : var->cnsts) {
1145       if (var->visited == sys->visited_counter)
1146         break;
1147       if (elem.constraint != cnst && not xbt_swag_belongs(elem.constraint, &sys->modified_constraint_set)) {
1148         xbt_swag_insert(elem.constraint, &sys->modified_constraint_set);
1149         lmm_update_modified_set_rec(sys, elem.constraint);
1150       }
1151     }
1152     //var will be ignored in later visits as long as sys->visited_counter does not move
1153     var->visited = sys->visited_counter;
1154   }
1155 }
1156
1157 static void lmm_update_modified_set(lmm_system_t sys, lmm_constraint_t cnst)
1158 {
1159   /* nothing to do if selective update isn't active */
1160   if (sys->selective_update_active && not xbt_swag_belongs(cnst, &sys->modified_constraint_set)) {
1161     xbt_swag_insert(cnst, &sys->modified_constraint_set);
1162     lmm_update_modified_set_rec(sys, cnst);
1163   }
1164 }
1165
1166 /** \brief Remove all constraints of the modified_constraint_set.
1167  *
1168  *  \param sys the lmm_system_t
1169  */
1170 static void lmm_remove_all_modified_set(lmm_system_t sys)
1171 {
1172   // We cleverly un-flag all variables just by incrementing sys->visited_counter
1173   // In effect, the var->visited value will no more be equal to sys->visited counter
1174   // To be clean, when visited counter has wrapped around, we force these var->visited values so that variables that
1175   // were in the modified a long long time ago are not wrongly skipped here, which would lead to very nasty bugs
1176   // (i.e. not readibily reproducible, and requiring a lot of run time before happening).
1177   if (++sys->visited_counter == 1) {
1178     /* the counter wrapped around, reset each variable->visited */
1179   void *_var;
1180     xbt_swag_foreach(_var, &sys->variable_set)
1181       ((lmm_variable_t)_var)->visited = 0;
1182   }
1183   xbt_swag_reset(&sys->modified_constraint_set);
1184 }
1185
1186 /**
1187  * Returns resource load (in flop per second, or byte per second, or similar)
1188  *
1189  * If the resource is shared (the default case), the load is sum of resource usage made by every variables located on
1190  * this resource.
1191  *
1192  * If the resource is not shared (ie in FATPIPE mode), then the load is the max (not the sum) of all resource usages
1193  * located on this resource.
1194  *
1195  * \param cnst the lmm_constraint_t associated to the resource
1196  */
1197 double lmm_constraint_get_usage(lmm_constraint_t cnst) {
1198   double usage         = 0.0;
1199   xbt_swag_t elem_list = &(cnst->enabled_element_set);
1200   void* _elem;
1201
1202   xbt_swag_foreach(_elem, elem_list)
1203   {
1204     lmm_element_t elem = (lmm_element_t)_elem;
1205     if (elem->consumption_weight > 0) {
1206       if (cnst->sharing_policy)
1207         usage += elem->consumption_weight * elem->variable->value;
1208       else if (usage < elem->consumption_weight * elem->variable->value)
1209         usage = std::max(usage, elem->consumption_weight * elem->variable->value);
1210     }
1211   }
1212   return usage;
1213 }
1214
1215 int lmm_constraint_get_variable_amount(lmm_constraint_t cnst) {
1216   int usage = 0;
1217   xbt_swag_t elem_list = &(cnst->enabled_element_set);
1218   void *_elem;
1219
1220   xbt_swag_foreach(_elem, elem_list) {
1221     lmm_element_t elem = (lmm_element_t)_elem;
1222     if (elem->consumption_weight > 0)
1223       usage++;
1224   }
1225  return usage;
1226 }