Logo AND Algorithmique Numérique Distribuée

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