Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
de1750b7e821534863bd559ee3b376e6c2d2cddc
[simgrid.git] / src / surf / maxmin.c
1 /* Copyright (c) 2004-2011. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/mallocator.h"
11 #include "maxmin_private.h"
12 #include <stdlib.h>
13 #include <stdio.h>              /* sprintf */
14 #include <math.h>
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_maxmin, surf,
16                                 "Logging specific to SURF (maxmin)");
17
18 double sg_maxmin_precision = 0.00001;
19
20 static void *lmm_variable_mallocator_new_f(void);
21 static void lmm_variable_mallocator_free_f(void *var);
22 #define lmm_variable_mallocator_reset_f ((void_f_pvoid_t)NULL)
23 static void lmm_update_modified_set(lmm_system_t sys,
24                                     lmm_constraint_t cnst);
25 static void lmm_remove_all_modified_set(lmm_system_t sys);
26 static int Global_debug_id = 1;
27 static int Global_const_debug_id = 1;
28
29 lmm_system_t lmm_system_new(int selective_update)
30 {
31   lmm_system_t l = NULL;
32   s_lmm_variable_t var;
33   s_lmm_constraint_t cnst;
34
35   l = xbt_new0(s_lmm_system_t, 1);
36
37   l->modified = 0;
38   l->selective_update_active = selective_update;
39   l->visited_counter = 1;
40
41   XBT_DEBUG("Setting selective_update_active flag to %d\n",
42          l->selective_update_active);
43
44   xbt_swag_init(&(l->variable_set),
45                 xbt_swag_offset(var, variable_set_hookup));
46   xbt_swag_init(&(l->constraint_set),
47                 xbt_swag_offset(cnst, constraint_set_hookup));
48
49   xbt_swag_init(&(l->active_constraint_set),
50                 xbt_swag_offset(cnst, active_constraint_set_hookup));
51
52   xbt_swag_init(&(l->modified_constraint_set),
53                 xbt_swag_offset(cnst, modified_constraint_set_hookup));
54   xbt_swag_init(&(l->saturated_variable_set),
55                 xbt_swag_offset(var, saturated_variable_set_hookup));
56   xbt_swag_init(&(l->saturated_constraint_set),
57                 xbt_swag_offset(cnst, saturated_constraint_set_hookup));
58
59   l->variable_mallocator = xbt_mallocator_new(65536,
60                                               lmm_variable_mallocator_new_f,
61                                               lmm_variable_mallocator_free_f,
62                                               lmm_variable_mallocator_reset_f);
63
64   return l;
65 }
66
67 void lmm_system_free(lmm_system_t sys)
68 {
69   lmm_variable_t var = NULL;
70   lmm_constraint_t cnst = NULL;
71
72   while ((var = extract_variable(sys))) {
73     XBT_WARN
74         ("Variable %p (%d) still in LMM system when freing it: this may be a bug",
75          var, var->id_int);
76     lmm_var_free(sys, var);
77   }
78
79   while ((cnst = extract_constraint(sys)))
80     lmm_cnst_free(sys, cnst);
81
82   xbt_mallocator_free(sys->variable_mallocator);
83   free(sys);
84 }
85
86 XBT_INLINE void lmm_variable_disable(lmm_system_t sys, lmm_variable_t var)
87 {
88   int i;
89   int n;
90
91   lmm_element_t elem = NULL;
92
93   XBT_IN("(sys=%p, var=%p)", sys, var);
94   sys->modified = 1;
95
96   n = 0;
97   for (i = 0; i < var->cnsts_number; i++) {
98     elem = &var->cnsts[i];
99     xbt_swag_remove(elem, &(elem->constraint->element_set));
100     xbt_swag_remove(elem, &(elem->constraint->active_element_set));
101     if (!xbt_swag_size(&(elem->constraint->element_set)))
102       make_constraint_inactive(sys, elem->constraint);
103     else {
104       if (n < i)
105         var->cnsts[n].constraint = elem->constraint;
106       n++;
107     }
108   }
109   if (n) {
110     var->cnsts_number = n;
111     lmm_update_modified_set(sys, var->cnsts[0].constraint);
112   }
113
114   var->cnsts_number = 0;
115   XBT_OUT();
116 }
117
118 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var)
119 {
120
121   lmm_variable_disable(sys, var);
122   xbt_mallocator_release(sys->variable_mallocator, var);
123 }
124
125 static XBT_INLINE void lmm_cnst_free(lmm_system_t sys,
126                                      lmm_constraint_t cnst)
127 {
128 /*   xbt_assert(xbt_swag_size(&(cnst->element_set)), */
129 /*            "This list should be empty!"); */
130   make_constraint_inactive(sys, cnst);
131   free(cnst);
132 }
133
134 lmm_constraint_t lmm_constraint_new(lmm_system_t sys, void *id,
135                                     double bound_value)
136 {
137   lmm_constraint_t cnst = NULL;
138   s_lmm_element_t elem;
139
140   cnst = xbt_new0(s_lmm_constraint_t, 1);
141   cnst->id = id;
142   cnst->id_int = Global_const_debug_id++;
143   xbt_swag_init(&(cnst->element_set),
144                 xbt_swag_offset(elem, element_set_hookup));
145   xbt_swag_init(&(cnst->active_element_set),
146                 xbt_swag_offset(elem, active_element_set_hookup));
147
148   cnst->bound = bound_value;
149   cnst->usage = 0;
150   cnst->shared = 1;
151   insert_constraint(sys, cnst);
152
153   return cnst;
154 }
155
156 XBT_INLINE void lmm_constraint_shared(lmm_constraint_t cnst)
157 {
158   cnst->shared = 0;
159 }
160
161 XBT_INLINE int lmm_constraint_is_shared(lmm_constraint_t cnst)
162 {
163   return (cnst->shared);
164 }
165
166 XBT_INLINE void lmm_constraint_free(lmm_system_t sys,
167                                     lmm_constraint_t cnst)
168 {
169   remove_constraint(sys, cnst);
170   lmm_cnst_free(sys, cnst);
171 }
172
173 static void *lmm_variable_mallocator_new_f(void)
174 {
175   lmm_variable_t var = xbt_new(s_lmm_variable_t, 1);
176   var->cnsts = NULL; /* will be created by realloc */
177   return var;
178 }
179
180 static void lmm_variable_mallocator_free_f(void *var)
181 {
182   xbt_free(((lmm_variable_t) var)->cnsts);
183   xbt_free(var);
184 }
185
186 lmm_variable_t lmm_variable_new(lmm_system_t sys, void *id,
187                                 double weight,
188                                 double bound, int number_of_constraints)
189 {
190   lmm_variable_t var = NULL;
191   int i;
192
193   XBT_IN("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)",
194           sys, id, weight, bound, number_of_constraints);
195
196   var = xbt_mallocator_get(sys->variable_mallocator);
197   var->id = id;
198   var->id_int = Global_debug_id++;
199   var->cnsts = xbt_realloc(var->cnsts, number_of_constraints * sizeof(s_lmm_element_t));
200   for (i = 0; i < number_of_constraints; i++) {
201     var->cnsts[i].element_set_hookup.next = NULL;
202     var->cnsts[i].element_set_hookup.prev = NULL;
203     var->cnsts[i].active_element_set_hookup.next = NULL;
204     var->cnsts[i].active_element_set_hookup.prev = NULL;
205     var->cnsts[i].constraint = NULL;
206     var->cnsts[i].variable = NULL;
207     var->cnsts[i].value = 0.0;
208   }
209   var->cnsts_size = number_of_constraints;
210   var->cnsts_number = 0;
211   var->weight = weight;
212   var->bound = bound;
213   var->value = 0.0;
214   var->visited = sys->visited_counter - 1;
215   var->mu = 0.0;
216   var->new_mu = 0.0;
217   var->func_f = func_f_def;
218   var->func_fp = func_fp_def;
219   var->func_fpi = func_fpi_def;
220
221   var->variable_set_hookup.next = NULL;
222   var->variable_set_hookup.prev = NULL;
223   var->saturated_variable_set_hookup.next = NULL;
224   var->saturated_variable_set_hookup.prev = NULL;
225
226   if (weight)
227     xbt_swag_insert_at_head(var, &(sys->variable_set));
228   else
229     xbt_swag_insert_at_tail(var, &(sys->variable_set));
230
231   XBT_OUT(" returns %p", var);
232   return var;
233 }
234
235 void lmm_variable_free(lmm_system_t sys, lmm_variable_t var)
236 {
237   remove_variable(sys, var);
238   lmm_var_free(sys, var);
239 }
240
241 XBT_INLINE double lmm_variable_getvalue(lmm_variable_t var)
242 {
243   return (var->value);
244 }
245
246 XBT_INLINE double lmm_variable_getbound(lmm_variable_t var)
247 {
248   return (var->bound);
249 }
250
251 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
252                 lmm_variable_t var, double value)
253 {
254   lmm_element_t elem = NULL;
255
256   sys->modified = 1;
257
258   xbt_assert(var->cnsts_number < var->cnsts_size, "Too much constraints");
259
260   elem = &(var->cnsts[var->cnsts_number++]);
261
262   elem->value = value;
263   elem->constraint = cnst;
264   elem->variable = var;
265
266   if (var->weight)
267     xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
268   else
269     xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
270
271   make_constraint_active(sys, cnst);
272   lmm_update_modified_set(sys, cnst);
273   if (var->cnsts_number > 1)
274     lmm_update_modified_set(sys, var->cnsts[0].constraint);
275 }
276
277 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
278                     lmm_variable_t var, double value)
279 {
280   int i;
281   sys->modified = 1;
282
283   for (i = 0; i < var->cnsts_number; i++)
284     if (var->cnsts[i].constraint == cnst)
285       break;
286
287   if (i < var->cnsts_number) {
288     if (cnst->shared)
289       var->cnsts[i].value += value;
290     else
291       var->cnsts[i].value = MAX(var->cnsts[i].value, value);
292     lmm_update_modified_set(sys, cnst);
293   } else
294     lmm_expand(sys, cnst, var, value);
295 }
296
297 void lmm_elem_set_value(lmm_system_t sys, lmm_constraint_t cnst,
298                         lmm_variable_t var, double value)
299 {
300   int i;
301
302   for (i = 0; i < var->cnsts_number; i++)
303     if (var->cnsts[i].constraint == cnst)
304       break;
305
306   if (i < var->cnsts_number) {
307     var->cnsts[i].value = value;
308     sys->modified = 1;
309     lmm_update_modified_set(sys, cnst);
310   } else
311     DIE_IMPOSSIBLE;
312 }
313
314 XBT_INLINE lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t sys,
315                                                   lmm_variable_t var,
316                                                   int num)
317 {
318   if (num < var->cnsts_number)
319     return (var->cnsts[num].constraint);
320   else
321     return NULL;
322 }
323
324 XBT_INLINE double lmm_get_cnst_weight_from_var(lmm_system_t sys,
325                                                          lmm_variable_t var,
326                                                          int num)
327 {
328   if (num < var->cnsts_number)
329     return (var->cnsts[num].value);
330   else
331     return 0.0;
332 }
333
334 XBT_INLINE int lmm_get_number_of_cnst_from_var(lmm_system_t sys,
335                                                lmm_variable_t var)
336 {
337   return (var->cnsts_number);
338 }
339
340 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t sys,
341                                      lmm_constraint_t cnst,
342                                      lmm_element_t * elem)
343 {
344   if (!(*elem))
345     *elem = xbt_swag_getFirst(&(cnst->element_set));
346   else
347     *elem = xbt_swag_getNext(*elem, cnst->element_set.offset);
348   if (*elem)
349     return (*elem)->variable;
350   else
351     return NULL;
352 }
353
354 XBT_INLINE void *lmm_constraint_id(lmm_constraint_t cnst)
355 {
356   return cnst->id;
357 }
358
359 XBT_INLINE void *lmm_variable_id(lmm_variable_t var)
360 {
361   return var->id;
362 }
363
364 static XBT_INLINE int saturated_constraint_set_update(lmm_system_t sys,
365                                                       lmm_constraint_t
366                                                       cnst,
367                                                       double *min_usage)
368 {
369   XBT_IN("sys=%p, cnst=%p, min_usage=%f", sys, cnst, *min_usage);
370   if (cnst->usage <= 0) {
371     XBT_OUT();
372     return 1;
373   }
374   if (cnst->remaining <= 0) {
375     XBT_OUT();
376     return 1;
377   }
378   if ((*min_usage < 0) || (*min_usage > cnst->remaining / cnst->usage)) {
379     *min_usage = cnst->remaining / cnst->usage;
380     XBT_HERE(" min_usage=%f (cnst->remaining=%f, cnst->usage=%f)",
381              *min_usage, cnst->remaining, cnst->usage);
382     xbt_swag_reset(&sys->saturated_constraint_set);
383     xbt_swag_insert(cnst, &sys->saturated_constraint_set);
384   } else if (*min_usage == cnst->remaining / cnst->usage) {
385     xbt_swag_insert(cnst, &sys->saturated_constraint_set);
386   }
387   XBT_OUT();
388   return 0;
389 }
390
391 static XBT_INLINE void saturated_variable_set_update(lmm_system_t sys)
392 {
393   lmm_constraint_t cnst = NULL;
394   xbt_swag_t cnst_list = NULL;
395   lmm_element_t elem = NULL;
396   xbt_swag_t elem_list = NULL;
397
398   cnst_list = &(sys->saturated_constraint_set);
399   while ((cnst = xbt_swag_getFirst(cnst_list))) {
400     elem_list = &(cnst->active_element_set);
401     xbt_swag_foreach(elem, elem_list) {
402       if (elem->variable->weight <= 0)
403         break;
404       if ((elem->value > 0))
405         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
406     }
407     xbt_swag_remove(cnst, cnst_list);
408   }
409 }
410
411 void lmm_print(lmm_system_t sys)
412 {
413   lmm_constraint_t cnst = NULL;
414   lmm_element_t elem = NULL;
415   lmm_variable_t var = NULL;
416   xbt_swag_t cnst_list = NULL;
417   xbt_swag_t var_list = NULL;
418   xbt_swag_t elem_list = NULL;
419   char print_buf[1024];
420   char *trace_buf = xbt_malloc0(sizeof(char));
421   double sum = 0.0;
422
423   /* Printing Objective */
424   var_list = &(sys->variable_set);
425   sprintf(print_buf, "MAX-MIN ( ");
426   trace_buf =
427       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
428   strcat(trace_buf, print_buf);
429   xbt_swag_foreach(var, var_list) {
430     sprintf(print_buf, "'%d'(%f) ", var->id_int, var->weight);
431     trace_buf =
432         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
433     strcat(trace_buf, print_buf);
434   }
435   sprintf(print_buf, ")");
436   trace_buf =
437       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
438   strcat(trace_buf, print_buf);
439   XBT_DEBUG("%20s", trace_buf);
440   trace_buf[0] = '\000';
441
442   XBT_DEBUG("Constraints");
443   /* Printing Constraints */
444   cnst_list = &(sys->active_constraint_set);
445   xbt_swag_foreach(cnst, cnst_list) {
446     sum = 0.0;
447     elem_list = &(cnst->element_set);
448     sprintf(print_buf, "\t");
449     trace_buf =
450         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
451     strcat(trace_buf, print_buf);
452     sprintf(print_buf, "%s(",(cnst->shared)?"":"max");
453     trace_buf =
454       xbt_realloc(trace_buf,
455                   strlen(trace_buf) + strlen(print_buf) + 1);
456     strcat(trace_buf, print_buf);      
457     xbt_swag_foreach(elem, elem_list) {
458       sprintf(print_buf, "%f.'%d'(%f) %s ", elem->value,
459               elem->variable->id_int, elem->variable->value,(cnst->shared)?"+":",");
460       trace_buf =
461           xbt_realloc(trace_buf,
462                       strlen(trace_buf) + strlen(print_buf) + 1);
463       strcat(trace_buf, print_buf);
464       if(cnst->shared) 
465         sum += elem->value * elem->variable->value;
466       else 
467         sum = MAX(sum,elem->value * elem->variable->value);
468     }
469     sprintf(print_buf, "0) <= %f ('%d')", cnst->bound, cnst->id_int);
470     trace_buf =
471         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
472     strcat(trace_buf, print_buf);
473
474     if (!cnst->shared) {
475       sprintf(print_buf, " [MAX-Constraint]");
476       trace_buf =
477           xbt_realloc(trace_buf,
478                       strlen(trace_buf) + strlen(print_buf) + 1);
479       strcat(trace_buf, print_buf);
480     }
481     XBT_DEBUG("%s", trace_buf);
482     trace_buf[0] = '\000';
483     xbt_assert(!double_positive(sum - cnst->bound),
484                 "Incorrect value (%f is not smaller than %f): %g",
485                 sum, cnst->bound, sum - cnst->bound);
486   }
487
488   XBT_DEBUG("Variables");
489   /* Printing Result */
490   xbt_swag_foreach(var, var_list) {
491     if (var->bound > 0) {
492       XBT_DEBUG("'%d'(%f) : %f (<=%f)", var->id_int, var->weight, var->value,
493              var->bound);
494       xbt_assert(!double_positive(var->value - var->bound),
495                   "Incorrect value (%f is not smaller than %f",
496                   var->value, var->bound);
497     } else {
498       XBT_DEBUG("'%d'(%f) : %f", var->id_int, var->weight, var->value);
499     }
500   }
501
502   free(trace_buf);
503 }
504
505 void lmm_solve(lmm_system_t sys)
506 {
507   lmm_variable_t var = NULL;
508   lmm_constraint_t cnst = NULL;
509   lmm_constraint_t cnst_next = NULL;
510   lmm_element_t elem = NULL;
511   xbt_swag_t cnst_list = NULL;
512   xbt_swag_t var_list = NULL;
513   xbt_swag_t elem_list = NULL;
514   double min_usage = -1;
515   double min_bound = -1;
516
517   if (!(sys->modified))
518     return;
519
520   XBT_IN("(sys=%p)", sys);
521
522   /*
523    * Compute Usage and store the variables that reach the maximum.
524    */
525   cnst_list =
526       sys->
527       selective_update_active ? &(sys->modified_constraint_set) :
528       &(sys->active_constraint_set);
529
530   XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
531   /* Init: Only modified code portions */
532   xbt_swag_foreach(cnst, cnst_list) {
533     elem_list = &(cnst->element_set);
534     //XBT_DEBUG("Variable set : %d", xbt_swag_size(elem_list));
535     xbt_swag_foreach(elem, elem_list) {
536       var = elem->variable;
537       if (var->weight <= 0.0)
538         break;
539       var->value = 0.0;
540     }
541   }
542
543   xbt_swag_foreach_safe(cnst, cnst_next, cnst_list) {
544     /* INIT */
545     cnst->remaining = cnst->bound;
546     if (cnst->remaining == 0)
547       continue;
548     cnst->usage = 0;
549     elem_list = &(cnst->element_set);
550     xbt_swag_foreach(elem, elem_list) {
551       /* 0-weighted elements (ie, sleep actions) are at the end of the swag and we don't want to consider them */
552       if (elem->variable->weight <= 0)
553         break;
554       if ((elem->value > 0)) {
555         if (cnst->shared)
556           cnst->usage += elem->value / elem->variable->weight;
557         else if (cnst->usage < elem->value / elem->variable->weight)
558           cnst->usage = elem->value / elem->variable->weight;
559
560         make_elem_active(elem);
561         if (sys->keep_track)
562           xbt_swag_insert(elem->variable->id, sys->keep_track);
563       }
564     }
565     XBT_DEBUG("Constraint Usage '%d' : %f", cnst->id_int, cnst->usage);
566     /* Saturated constraints update */
567     if(cnst->usage>0) {
568       xbt_swag_remove(cnst, cnst_list);
569       xbt_swag_insert_at_head(cnst, cnst_list);
570     }
571     saturated_constraint_set_update(sys, cnst, &min_usage);
572   }
573   saturated_variable_set_update(sys);
574
575   /* Saturated variables update */
576
577   do {
578     /* Fix the variables that have to be */
579     var_list = &(sys->saturated_variable_set);
580
581     xbt_swag_foreach(var, var_list) {
582       if (var->weight <= 0.0)
583         DIE_IMPOSSIBLE;
584       /* First check if some of these variables have reach their upper
585          bound and update min_usage accordingly. */
586       XBT_DEBUG
587           ("var=%d, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f",
588            var->id_int, var->bound, var->weight, min_usage,
589            var->bound * var->weight);
590       if ((var->bound > 0) && (var->bound * var->weight < min_usage)) {
591         if (min_bound < 0)
592           min_bound = var->bound;
593         else
594           min_bound = MIN(min_bound, var->bound);
595         XBT_DEBUG("Updated min_bound=%f", min_bound);
596       }
597     }
598
599
600     while ((var = xbt_swag_getFirst(var_list))) {
601       int i;
602
603       if (min_bound < 0) {
604         var->value = min_usage / var->weight;
605       } else {
606         if (min_bound == var->bound)
607           var->value = var->bound;
608         else {
609           xbt_swag_remove(var, var_list);
610           continue;
611         }
612       }
613       XBT_DEBUG("Min usage: %f, Var(%d)->weight: %f, Var(%d)->value: %f ",
614              min_usage, var->id_int, var->weight, var->id_int, var->value);
615
616
617       /* Update usage */
618
619       for (i = 0; i < var->cnsts_number; i++) {
620         elem = &var->cnsts[i];
621         cnst = elem->constraint;
622         if (cnst->shared) {
623           double_update(&(cnst->remaining), elem->value * var->value);
624           double_update(&(cnst->usage), elem->value / var->weight);
625           if(cnst->usage<=0 || cnst->remaining<=0) {
626             xbt_swag_remove(cnst, cnst_list);
627             xbt_swag_insert_at_tail(cnst, cnst_list);
628           }
629           make_elem_inactive(elem);
630         } else {                /* FIXME one day: We recompute usage.... :( */
631           cnst->usage = 0.0;
632           make_elem_inactive(elem);
633           elem_list = &(cnst->element_set);
634           xbt_swag_foreach(elem, elem_list) {
635             if (elem->variable->weight <= 0)
636               break;
637             if (elem->variable->value > 0)
638               break;
639             if ((elem->value > 0)) {
640               cnst->usage =
641                   MAX(cnst->usage, elem->value / elem->variable->weight);
642               XBT_DEBUG("Constraint Usage %d : %f", cnst->id_int,
643                      cnst->usage);
644               make_elem_active(elem);
645             }
646           }
647         }
648       }
649       xbt_swag_remove(var, var_list);
650     }
651
652     /* Find out which variables reach the maximum */
653     min_usage = -1;
654     min_bound = -1;
655
656     xbt_swag_foreach(cnst, cnst_list) {
657       if(saturated_constraint_set_update(sys, cnst, &min_usage)) break;
658     }
659     saturated_variable_set_update(sys);
660
661   } while (xbt_swag_size(&(sys->saturated_variable_set)));
662
663   sys->modified = 0;
664   if (sys->selective_update_active)
665     lmm_remove_all_modified_set(sys);
666
667   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
668     lmm_print(sys);
669   }
670   XBT_OUT();
671 }
672
673 /* Not a O(1) function */
674
675 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
676                 lmm_variable_t var, double value)
677 {
678   int i;
679
680   for (i = 0; i < var->cnsts_number; i++)
681     if (var->cnsts[i].constraint == cnst) {
682       var->cnsts[i].value = value;
683       sys->modified = 1;
684       lmm_update_modified_set(sys, cnst);
685       return;
686     }
687 }
688
689 /** \brief Attribute the value bound to var->bound.
690  * 
691  *  \param sys the lmm_system_t
692  *  \param var the lmm_variable_t
693  *  \param bound the new bound to associate with var
694  * 
695  *  Makes var->bound equal to bound. Whenever this function is called 
696  *  a change is  signed in the system. To
697  *  avoid false system changing detection it is a good idea to test 
698  *  (bound != 0) before calling it.
699  *
700  */
701 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
702                                double bound)
703 {
704   sys->modified = 1;
705   var->bound = bound;
706
707   if (var->cnsts_number)
708     lmm_update_modified_set(sys, var->cnsts[0].constraint);
709 }
710
711
712 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
713                                 double weight)
714 {
715   int i;
716   lmm_element_t elem;
717
718   if (weight == var->weight)
719     return;
720   XBT_IN("(sys=%p, var=%p, weight=%f)", sys, var, weight);
721   sys->modified = 1;
722   var->weight = weight;
723   xbt_swag_remove(var, &(sys->variable_set));
724   if (weight)
725     xbt_swag_insert_at_head(var, &(sys->variable_set));
726   else
727     xbt_swag_insert_at_tail(var, &(sys->variable_set));
728
729   for (i = 0; i < var->cnsts_number; i++) {
730     elem = &var->cnsts[i];
731     xbt_swag_remove(elem, &(elem->constraint->element_set));
732     if (weight)
733       xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
734     else
735       xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
736
737     if (i == 0)
738       lmm_update_modified_set(sys, elem->constraint);
739   }
740   if (!weight)
741     var->value = 0.0;
742
743   XBT_OUT();
744 }
745
746 XBT_INLINE double lmm_get_variable_weight(lmm_variable_t var)
747 {
748   return var->weight;
749 }
750
751 XBT_INLINE void lmm_update_constraint_bound(lmm_system_t sys,
752                                             lmm_constraint_t cnst,
753                                             double bound)
754 {
755   sys->modified = 1;
756   lmm_update_modified_set(sys, cnst);
757   cnst->bound = bound;
758 }
759
760 XBT_INLINE int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
761 {
762   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
763 }
764
765 XBT_INLINE lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t
766                                                             sys)
767 {
768   return xbt_swag_getFirst(&(sys->active_constraint_set));
769 }
770
771 XBT_INLINE lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t
772                                                            sys,
773                                                            lmm_constraint_t
774                                                            cnst)
775 {
776   return xbt_swag_getNext(cnst, (sys->active_constraint_set).offset);
777 }
778
779 #ifdef HAVE_LATENCY_BOUND_TRACKING
780 XBT_INLINE int lmm_is_variable_limited_by_latency(lmm_variable_t var)
781 {
782   return (double_equals(var->bound, var->value));
783 }
784 #endif
785
786
787 /** \brief Update the constraint set propagating recursively to
788  *  other constraints so the system should not be entirely computed.
789  *
790  *  \param sys the lmm_system_t
791  *  \param cnst the lmm_constraint_t affected by the change
792  *
793  *  A recursive algorithm to optimize the system recalculation selecting only
794  *  constraints that have changed. Each constraint change is propagated
795  *  to the list of constraints for each variable.
796  */
797 static void lmm_update_modified_set_rec(lmm_system_t sys,
798                                         lmm_constraint_t cnst)
799 {
800   lmm_element_t elem;
801
802   xbt_swag_foreach(elem, &cnst->element_set) {
803     lmm_variable_t var = elem->variable;
804     s_lmm_element_t *cnsts = var->cnsts;
805     int i;
806     for (i = 0; var->visited != sys->visited_counter
807              && i < var->cnsts_number ; i++) {
808       if (cnsts[i].constraint != cnst
809           && !xbt_swag_belongs(cnsts[i].constraint,
810                                &sys->modified_constraint_set)) {
811         xbt_swag_insert(cnsts[i].constraint, &sys->modified_constraint_set);
812         lmm_update_modified_set_rec(sys, cnsts[i].constraint);
813       }
814     }
815     var->visited = sys->visited_counter;
816   }
817 }
818
819 static void lmm_update_modified_set(lmm_system_t sys,
820                                     lmm_constraint_t cnst)
821 {
822   /* nothing to do if selective update isn't active */
823   if (sys->selective_update_active
824       && !xbt_swag_belongs(cnst, &sys->modified_constraint_set)) {
825     xbt_swag_insert(cnst, &sys->modified_constraint_set);
826     lmm_update_modified_set_rec(sys, cnst);
827   }
828 }
829
830 /** \brief Remove all constraints of the modified_constraint_set.
831  *
832  *  \param sys the lmm_system_t
833  */
834 static void lmm_remove_all_modified_set(lmm_system_t sys)
835 {
836   if (++sys->visited_counter == 1) {
837     /* the counter wrapped around, reset each variable->visited */
838     lmm_variable_t var;
839     xbt_swag_foreach(var, &sys->variable_set)
840       var->visited = 0;
841   }
842   xbt_swag_reset(&sys->modified_constraint_set);
843 }