Logo AND Algorithmique Numérique Distribuée

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