Logo AND Algorithmique Numérique Distribuée

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