Logo AND Algorithmique Numérique Distribuée

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