Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Some values were close to 0 instead of equal to 0, which caused some bad behaviors...
[simgrid.git] / src / surf / maxmin.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/mallocator.h"
12 #include "maxmin_private.h"
13 #include <stdlib.h>
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_maxmin, surf,
15                                 "Logging specific to SURF (maxmin)");
16
17 static void* lmm_variable_mallocator_new_f(void);
18 static void lmm_variable_mallocator_free_f(void *var);
19 static void lmm_variable_mallocator_reset_f(void *var);
20
21 lmm_system_t lmm_system_new(void)
22 {
23   lmm_system_t l = NULL;
24   s_lmm_variable_t var;
25   s_lmm_constraint_t cnst;
26
27   l = xbt_new0(s_lmm_system_t, 1);
28
29   l->modified = 0;
30   xbt_swag_init(&(l->variable_set),
31                 xbt_swag_offset(var, variable_set_hookup));
32   xbt_swag_init(&(l->constraint_set),
33                 xbt_swag_offset(cnst, constraint_set_hookup));
34
35   xbt_swag_init(&(l->active_constraint_set),
36                 xbt_swag_offset(cnst, active_constraint_set_hookup));
37
38   xbt_swag_init(&(l->saturated_variable_set),
39                 xbt_swag_offset(var, saturated_variable_set_hookup));
40   xbt_swag_init(&(l->saturated_constraint_set),
41                 xbt_swag_offset(cnst, saturated_constraint_set_hookup));
42
43   l->variable_mallocator = xbt_mallocator_new(64,
44                                               lmm_variable_mallocator_new_f,
45                                               lmm_variable_mallocator_free_f,
46                                               lmm_variable_mallocator_reset_f);
47
48   return l;
49 }
50
51 void lmm_system_free(lmm_system_t sys)
52 {
53   lmm_variable_t var = NULL;
54   lmm_constraint_t cnst = NULL;
55
56   while ((var = extract_variable(sys)))
57     lmm_var_free(sys, var);
58
59   while ((cnst = extract_constraint(sys)))
60     lmm_cnst_free(sys, cnst);
61
62   xbt_mallocator_free(sys->variable_mallocator);
63   free(sys);
64 }
65
66 void lmm_variable_disable(lmm_system_t sys, lmm_variable_t var)
67 {
68   int i;
69   lmm_element_t elem = NULL;
70
71   XBT_IN2("(sys=%p, var=%p)",sys,var);
72   sys->modified = 1;
73
74   for (i = 0; i < var->cnsts_number; i++) {
75     elem = &var->cnsts[i];
76     xbt_swag_remove(elem, &(elem->constraint->element_set));
77     xbt_swag_remove(elem, &(elem->constraint->active_element_set));
78     if (!xbt_swag_size(&(elem->constraint->element_set)))
79       make_constraint_inactive(sys, elem->constraint);
80   }
81   var->cnsts_number = 0;
82   XBT_OUT;
83 }
84
85 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var)
86 {
87
88   lmm_variable_disable(sys, var);
89   free(var->cnsts);
90   xbt_mallocator_release(sys->variable_mallocator, var);
91 }
92
93 static void lmm_cnst_free(lmm_system_t sys, lmm_constraint_t cnst)
94 {
95 /*   xbt_assert0(xbt_swag_size(&(cnst->element_set)), */
96 /*            "This list should be empty!"); */
97   remove_active_constraint(sys, cnst);
98   free(cnst);
99 }
100
101 lmm_constraint_t lmm_constraint_new(lmm_system_t sys, void *id,
102                                     double bound_value)
103 {
104   lmm_constraint_t cnst = NULL;
105   s_lmm_element_t elem;
106
107   cnst = xbt_new0(s_lmm_constraint_t, 1);
108   cnst->id = id;
109   xbt_swag_init(&(cnst->element_set),
110                 xbt_swag_offset(elem, element_set_hookup));
111   xbt_swag_init(&(cnst->active_element_set),
112                 xbt_swag_offset(elem, active_element_set_hookup));
113
114   cnst->bound = bound_value;
115   cnst->usage = 0;
116   cnst->shared = 1;
117   insert_constraint(sys, cnst);
118
119   return cnst;
120 }
121
122 void lmm_constraint_shared(lmm_constraint_t cnst)
123 {
124   cnst->shared = 0;
125 }
126
127 void lmm_constraint_free(lmm_system_t sys, lmm_constraint_t cnst)
128 {
129   remove_constraint(sys, cnst);
130   lmm_cnst_free(sys, cnst);
131 }
132
133 static void* lmm_variable_mallocator_new_f(void) {
134   return xbt_new(s_lmm_variable_t, 1);
135 }
136
137 static void lmm_variable_mallocator_free_f(void *var) {
138   xbt_free(var);
139 }
140
141 static void lmm_variable_mallocator_reset_f(void *var) {
142   /* memset to zero like calloc */
143   memset(var, 0, sizeof(s_lmm_variable_t));
144 }
145
146 lmm_variable_t lmm_variable_new(lmm_system_t sys, void *id,
147                                 double weight,
148                                 double bound, int number_of_constraints)
149 {
150   lmm_variable_t var = NULL;
151   int i;
152
153   XBT_IN5("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)",
154           sys,id,weight,bound,number_of_constraints);
155
156   var = xbt_mallocator_get(sys->variable_mallocator);
157   var->id = id;
158   var->cnsts = xbt_new0(s_lmm_element_t, number_of_constraints);
159   for(i=0; i<number_of_constraints; i++) {
160     /* Should be useless because of the 
161        calloc but it seems to help valgrind... */
162     var->cnsts[i].element_set_hookup.next = NULL;
163     var->cnsts[i].element_set_hookup.prev = NULL;
164     var->cnsts[i].active_element_set_hookup.next = NULL;
165     var->cnsts[i].active_element_set_hookup.prev = NULL;
166     var->cnsts[i].constraint = NULL;
167     var->cnsts[i].variable = NULL;
168     var->cnsts[i].value = 0.0;
169   }
170   var->cnsts_size = number_of_constraints;
171   var->cnsts_number = 0; /* Should be useless because of the 
172                             calloc but it seems to help valgrind... */
173   var->weight = weight;
174   var->bound = bound;
175   var->value = 0.0;
176   if(weight) xbt_swag_insert_at_head(var,&(sys->variable_set));
177   else xbt_swag_insert_at_tail(var,&(sys->variable_set));
178   XBT_OUT;
179   return var;
180 }
181
182 void lmm_variable_free(lmm_system_t sys, lmm_variable_t var)
183 {
184   remove_variable(sys, var);
185   lmm_var_free(sys, var);
186 }
187
188 double lmm_variable_getvalue(lmm_variable_t var)
189 {
190   return (var->value);
191 }
192
193 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
194                 lmm_variable_t var, double value)
195 {
196   lmm_element_t elem = NULL;
197
198   sys->modified = 1;
199
200   xbt_assert0(var->cnsts_number < var->cnsts_size,
201               "Too much constraints");
202
203   elem = &(var->cnsts[var->cnsts_number++]);
204
205   elem->value = value;
206   elem->constraint = cnst;
207   elem->variable = var;
208
209   if(var->weight) xbt_swag_insert_at_head(elem,&(elem->constraint->element_set));
210   else xbt_swag_insert_at_tail(elem,&(elem->constraint->element_set));
211
212   make_constraint_active(sys, cnst);
213 }
214
215 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
216                     lmm_variable_t var, double value)
217 {
218   int i ; 
219   sys->modified = 1;
220
221   for(i=0; i< var->cnsts_number ; i++)
222     if(var->cnsts[i].constraint == cnst) break;
223   
224   if(i<var->cnsts_number) var->cnsts[i].value +=value;
225   else lmm_expand(sys,cnst,var,value);
226 }
227
228 void lmm_elem_set_value(lmm_system_t sys, lmm_constraint_t cnst,
229                         lmm_variable_t var, double value)
230 {
231   int i ; 
232
233   for(i=0; i< var->cnsts_number ; i++)
234     if(var->cnsts[i].constraint == cnst) break;
235
236   if(i<var->cnsts_number) {
237     var->cnsts[i].value =value;
238     sys->modified = 1;
239   } else DIE_IMPOSSIBLE;
240 }
241
242 lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t sys,
243                                        lmm_variable_t var, int num)
244 {
245   if (num < var->cnsts_number)
246     return (var->cnsts[num].constraint);
247   else
248     return NULL;
249 }
250
251 int lmm_get_number_of_cnst_from_var(lmm_system_t sys, lmm_variable_t var)
252 {
253   return (var->cnsts_number);
254 }
255
256 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t sys,
257                                      lmm_constraint_t cnst,
258                                      lmm_variable_t * var)
259 {
260   if (!(*var))
261     xbt_swag_getFirst(&(cnst->element_set));
262   else
263     *var = xbt_swag_getNext(*var, cnst->element_set.offset);
264   return *var;
265 }
266
267 void *lmm_constraint_id(lmm_constraint_t cnst)
268 {
269   return cnst->id;
270 }
271
272 void *lmm_variable_id(lmm_variable_t var)
273 {
274   return var->id;
275 }
276
277 static void saturated_constraint_set_update(lmm_system_t sys,
278                                             lmm_constraint_t cnst,
279                                             double *min_usage)
280 {
281   lmm_constraint_t useless_cnst = NULL;
282
283   XBT_IN3("sys=%p, cnst=%p, min_usage=%f",sys,cnst,*min_usage);
284   if (cnst->usage <= 0) {
285     XBT_OUT;
286     return;
287   }
288   if (cnst->remaining <= 0) {
289     XBT_OUT;
290     return;
291   }
292   if ((*min_usage < 0) || (*min_usage > cnst->remaining / cnst->usage)) {
293     *min_usage = cnst->remaining / cnst->usage;
294     LOG3(xbt_log_priority_trace, 
295          "min_usage=%f (cnst->remaining=%f, cnst->usage=%f)",*min_usage, 
296          cnst->remaining, cnst->usage);
297     while ((useless_cnst =
298             xbt_swag_getFirst(&(sys->saturated_constraint_set))))
299       xbt_swag_remove(useless_cnst, &(sys->saturated_constraint_set));
300
301     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
302   } else if (*min_usage == cnst->remaining / cnst->usage) {
303     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
304   }
305   XBT_OUT;
306 }
307
308 static void saturated_variable_set_update(lmm_system_t sys)
309 {
310   lmm_constraint_t cnst = NULL;
311   xbt_swag_t cnst_list = NULL;
312   lmm_element_t elem = NULL;
313   xbt_swag_t elem_list = NULL;
314
315   cnst_list = &(sys->saturated_constraint_set);
316   while ((cnst = xbt_swag_getFirst(cnst_list))) {
317     elem_list = &(cnst->active_element_set);
318     xbt_swag_foreach(elem, elem_list) {
319       if(elem->variable->weight<=0) break;
320       if ((elem->value > 0))
321         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
322     }
323     xbt_swag_remove(cnst, cnst_list);
324   }
325 }
326
327 static void lmm_print(lmm_system_t sys)
328 {
329   lmm_constraint_t cnst = NULL;
330   lmm_element_t elem = NULL;
331   lmm_variable_t var = NULL;
332   xbt_swag_t cnst_list = NULL;
333   xbt_swag_t var_list = NULL;
334   xbt_swag_t elem_list = NULL;
335   char print_buf[1024];
336   char *trace_buf=xbt_malloc0(sizeof(char));
337   double sum=0.0;
338
339   /* Printing Objective */
340   var_list = &(sys->variable_set);
341   sprintf(print_buf,"MAX-MIN ( ");
342   trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
343   strcat(trace_buf, print_buf);
344   xbt_swag_foreach(var, var_list) {
345     sprintf(print_buf,"'%p'(%f) ",var,var->weight);
346     trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
347     strcat(trace_buf, print_buf);
348   }
349   sprintf(print_buf,")");
350   trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
351   strcat(trace_buf, print_buf);
352   DEBUG1("%s",trace_buf);
353   trace_buf[0]='\000';
354
355   /* Printing Constraints */
356   cnst_list = &(sys->active_constraint_set);
357   xbt_swag_foreach(cnst, cnst_list) {
358     sum=0.0;
359     elem_list = &(cnst->element_set);
360     sprintf(print_buf,"\t");
361     trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
362     strcat(trace_buf, print_buf);
363     xbt_swag_foreach(elem, elem_list) {
364       sprintf(print_buf,"%f.'%p'(%f) + ",elem->value, 
365               elem->variable,elem->variable->weight);
366       trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
367       strcat(trace_buf, print_buf);
368       sum += elem->value * elem->variable->value;
369     }
370     sprintf(print_buf,"0 <= %f ('%p')",cnst->bound,cnst);
371     trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
372     strcat(trace_buf, print_buf);
373     DEBUG1("%s",trace_buf);
374     trace_buf[0]='\000';
375     xbt_assert2((sum<=cnst->bound), "Incorrect value (%f is not smaller than %f)",
376                 sum,cnst->bound);
377   }
378
379   /* Printing Result */
380   xbt_swag_foreach(var, var_list) {
381     if(var->bound>0) {
382       DEBUG4("'%p'(%f) : %f (<=%f)",var,var->weight,var->value, var->bound);
383       xbt_assert0((var->value<=var->bound), "Incorrect value");
384     }
385     else 
386       DEBUG3("'%p'(%f) : %f",var,var->weight,var->value);
387   }
388
389   free(trace_buf);
390 }
391
392 void lmm_solve(lmm_system_t sys)
393 {
394   lmm_variable_t var = NULL;
395   lmm_constraint_t cnst = NULL;
396   lmm_element_t elem = NULL;
397   xbt_swag_t cnst_list = NULL;
398   xbt_swag_t var_list = NULL;
399   xbt_swag_t elem_list = NULL;
400   double min_usage = -1;
401
402   if (!(sys->modified))
403     return;
404
405   /* Init */
406   var_list = &(sys->variable_set);
407   DEBUG1("Variable set : %d", xbt_swag_size(var_list));
408   xbt_swag_foreach(var, var_list) {
409     var->value = 0.0;
410   }
411
412
413   /* Compute Usage and store the variables that reach the maximum */
414
415   cnst_list = &(sys->active_constraint_set);
416   DEBUG1("Active constraints : %d", xbt_swag_size(cnst_list));
417   xbt_swag_foreach(cnst, cnst_list) {
418     /* INIT */
419     cnst->remaining = cnst->bound;
420     cnst->usage = 0;
421     elem_list = &(cnst->element_set);
422     xbt_swag_foreach(elem, elem_list) {
423       if(elem->variable->weight <=0) break;
424       if ((elem->value > 0)) {
425         if(cnst->shared)
426           cnst->usage += elem->value / elem->variable->weight;
427         else 
428           cnst->usage = 1;
429         make_elem_active(elem);
430       }
431     }
432
433     /* Saturated constraints update */
434     saturated_constraint_set_update(sys, cnst, &min_usage);
435   }
436
437   saturated_variable_set_update(sys);
438
439   /* Saturated variables update */
440
441   do {
442     /* Fix the variables that have to be */
443     var_list = &(sys->saturated_variable_set);
444
445     xbt_swag_foreach(var, var_list) {
446       /* First check if some of these variables have reach their upper
447          bound and update min_usage accordingly. */
448       DEBUG5("var=%p, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f",
449              var, var->bound, var->weight, min_usage,var->bound * var->weight);
450       if ((var->bound > 0) && (var->bound * var->weight < min_usage)) {
451         min_usage = var->bound * var->weight;
452         DEBUG1("Updated min_usage=%f",min_usage);
453       }
454     }
455
456
457     while ((var = xbt_swag_getFirst(var_list))) {
458       int i;
459
460       var->value = min_usage / var->weight;
461
462       /* Update usage */
463
464       for (i = 0; i < var->cnsts_number; i++) {
465         elem = &var->cnsts[i];
466         cnst = elem->constraint;
467         if(cnst->shared) {
468           double_update(&(cnst->remaining), elem->value * var->value);
469           double_update(&(cnst->usage), elem->value / var->weight);
470         }
471         make_elem_inactive(elem);
472       }
473       xbt_swag_remove(var, var_list);
474     }
475
476     /* Find out which variables reach the maximum */
477     cnst_list = &(sys->active_constraint_set);
478     min_usage = -1;
479     xbt_swag_foreach(cnst, cnst_list) {
480       saturated_constraint_set_update(sys, cnst, &min_usage);
481     }
482     saturated_variable_set_update(sys);
483
484   } while (xbt_swag_size(&(sys->saturated_variable_set)));
485
486   sys->modified = 0;
487   if(XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
488     lmm_print(sys);
489   }
490 }
491
492 /* Not a O(1) function */
493
494 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
495                 lmm_variable_t var, double value)
496 {
497   int i;
498
499   sys->modified = 1;
500   for (i = 0; i < var->cnsts_number; i++)
501     if (var->cnsts[i].constraint == cnst) {
502       var->cnsts[i].value = value;
503       return;
504     }
505 }
506
507 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
508                                double bound)
509 {
510   sys->modified = 1;
511   var->bound = bound;
512 }
513
514 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
515                                 double weight)
516 {
517   int i ;
518   lmm_element_t elem;
519
520   XBT_IN3("(sys=%p, var=%p, weight=%f)",sys,var,weight);
521   sys->modified = 1;
522   var->weight = weight;
523   xbt_swag_remove(var,&(sys->variable_set));
524   if(weight) xbt_swag_insert_at_head(var,&(sys->variable_set));
525   else xbt_swag_insert_at_tail(var,&(sys->variable_set));
526
527   for (i = 0; i < var->cnsts_number; i++) {
528     elem = &var->cnsts[i];
529     xbt_swag_remove(elem, &(elem->constraint->element_set));
530     if(weight) xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
531     else xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
532   }
533   XBT_OUT;
534 }
535
536 double lmm_get_variable_weight(lmm_variable_t var)
537                                   
538 {
539   return var->weight;
540 }
541
542 void lmm_update_constraint_bound(lmm_system_t sys, lmm_constraint_t cnst,
543                                  double bound)
544 {
545   sys->modified = 1;
546   cnst->bound = bound;
547 }
548
549 int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
550 {
551   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
552 }
553
554 lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t sys)
555 {
556   return xbt_swag_getFirst(&(sys->active_constraint_set));
557 }
558
559 lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t sys, lmm_constraint_t cnst)
560 {
561   return xbt_swag_getNext(cnst,(sys->active_constraint_set).offset);
562 }
563