Logo AND Algorithmique Numérique Distribuée

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