Logo AND Algorithmique Numérique Distribuée

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