Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Added a get Df function in order to implement beta*df during share_resources.
[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 #include <stdio.h> /* sprintf */
15 #include <math.h>
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_maxmin, surf,
17                                 "Logging specific to SURF (maxmin)");
18
19 static void *lmm_variable_mallocator_new_f(void);
20 static void lmm_variable_mallocator_free_f(void *var);
21 static void lmm_variable_mallocator_reset_f(void *var);
22
23 lmm_system_t lmm_system_new(void)
24 {
25   lmm_system_t l = NULL;
26   s_lmm_variable_t var;
27   s_lmm_constraint_t cnst;
28
29   l = xbt_new0(s_lmm_system_t, 1);
30
31   l->modified = 0;
32   xbt_swag_init(&(l->variable_set),
33                 xbt_swag_offset(var, variable_set_hookup));
34   xbt_swag_init(&(l->constraint_set),
35                 xbt_swag_offset(cnst, constraint_set_hookup));
36
37   xbt_swag_init(&(l->active_constraint_set),
38                 xbt_swag_offset(cnst, active_constraint_set_hookup));
39
40   xbt_swag_init(&(l->saturated_variable_set),
41                 xbt_swag_offset(var, saturated_variable_set_hookup));
42   xbt_swag_init(&(l->saturated_constraint_set),
43                 xbt_swag_offset(cnst, saturated_constraint_set_hookup));
44
45   l->variable_mallocator = xbt_mallocator_new(64,
46                                               lmm_variable_mallocator_new_f,
47                                               lmm_variable_mallocator_free_f,
48                                               lmm_variable_mallocator_reset_f);
49
50   return l;
51 }
52
53 void lmm_system_free(lmm_system_t sys)
54 {
55   lmm_variable_t var = NULL;
56   lmm_constraint_t cnst = NULL;
57
58   while ((var = extract_variable(sys)))
59     lmm_var_free(sys, var);
60
61   while ((cnst = extract_constraint(sys)))
62     lmm_cnst_free(sys, cnst);
63
64   xbt_mallocator_free(sys->variable_mallocator);
65   free(sys);
66 }
67
68 void lmm_variable_disable(lmm_system_t sys, lmm_variable_t var)
69 {
70   int i;
71   lmm_element_t elem = NULL;
72
73   XBT_IN2("(sys=%p, var=%p)", sys, var);
74   sys->modified = 1;
75
76   for (i = 0; i < var->cnsts_number; i++) {
77     elem = &var->cnsts[i];
78     xbt_swag_remove(elem, &(elem->constraint->element_set));
79     xbt_swag_remove(elem, &(elem->constraint->active_element_set));
80     if (!xbt_swag_size(&(elem->constraint->element_set)))
81       make_constraint_inactive(sys, elem->constraint);
82   }
83   var->cnsts_number = 0;
84   XBT_OUT;
85 }
86
87 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var)
88 {
89
90   lmm_variable_disable(sys, var);
91   free(var->cnsts);
92   xbt_mallocator_release(sys->variable_mallocator, var);
93 }
94
95 static void lmm_cnst_free(lmm_system_t sys, lmm_constraint_t cnst)
96 {
97 /*   xbt_assert0(xbt_swag_size(&(cnst->element_set)), */
98 /*            "This list should be empty!"); */
99   remove_active_constraint(sys, cnst);
100   free(cnst);
101 }
102
103 lmm_constraint_t lmm_constraint_new(lmm_system_t sys, void *id,
104                                     double bound_value)
105 {
106   lmm_constraint_t cnst = NULL;
107   s_lmm_element_t elem;
108
109   cnst = xbt_new0(s_lmm_constraint_t, 1);
110   cnst->id = id;
111   xbt_swag_init(&(cnst->element_set),
112                 xbt_swag_offset(elem, element_set_hookup));
113   xbt_swag_init(&(cnst->active_element_set),
114                 xbt_swag_offset(elem, active_element_set_hookup));
115
116   cnst->bound = bound_value;
117   cnst->usage = 0;
118   cnst->shared = 1;
119   insert_constraint(sys, cnst);
120
121   return cnst;
122 }
123
124 void lmm_constraint_shared(lmm_constraint_t cnst)
125 {
126   cnst->shared = 0;
127 }
128
129 int lmm_constraint_is_shared(lmm_constraint_t cnst)
130 {
131   return (cnst->shared);
132 }
133
134 void lmm_constraint_free(lmm_system_t sys, lmm_constraint_t cnst)
135 {
136   remove_constraint(sys, cnst);
137   lmm_cnst_free(sys, cnst);
138 }
139
140 static void *lmm_variable_mallocator_new_f(void)
141 {
142   return xbt_new(s_lmm_variable_t, 1);
143 }
144
145 static void lmm_variable_mallocator_free_f(void *var)
146 {
147   xbt_free(var);
148 }
149
150 static void lmm_variable_mallocator_reset_f(void *var)
151 {
152   /* memset to zero like calloc */
153   memset(var, 0, sizeof(s_lmm_variable_t));
154 }
155
156 lmm_variable_t lmm_variable_new(lmm_system_t sys, void *id,
157                                 double weight,
158                                 double bound, int number_of_constraints)
159 {
160   lmm_variable_t var = NULL;
161   int i;
162
163   XBT_IN5("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)",
164           sys, id, weight, bound, number_of_constraints);
165
166   var = xbt_mallocator_get(sys->variable_mallocator);
167   var->id = id;
168   var->cnsts = xbt_new0(s_lmm_element_t, number_of_constraints);
169   for (i = 0; i < number_of_constraints; i++) {
170     /* Should be useless because of the 
171        calloc but it seems to help valgrind... */
172     var->cnsts[i].element_set_hookup.next = NULL;
173     var->cnsts[i].element_set_hookup.prev = NULL;
174     var->cnsts[i].active_element_set_hookup.next = NULL;
175     var->cnsts[i].active_element_set_hookup.prev = NULL;
176     var->cnsts[i].constraint = NULL;
177     var->cnsts[i].variable = NULL;
178     var->cnsts[i].value = 0.0;
179   }
180   var->cnsts_size = number_of_constraints;
181   var->cnsts_number = 0;        /* Should be useless because of the 
182                                    calloc but it seems to help valgrind... */
183   var->weight = weight;
184   var->bound = bound;
185   var->value = 0.0;
186   var->df = 0.0;
187
188   var->func_f = func_f_def;
189   var->func_fp = func_fp_def;
190   var->func_fpi = func_fpi_def;
191
192   if (weight)
193     xbt_swag_insert_at_head(var, &(sys->variable_set));
194   else
195     xbt_swag_insert_at_tail(var, &(sys->variable_set));
196   XBT_OUT;
197   return var;
198 }
199
200 void lmm_variable_free(lmm_system_t sys, lmm_variable_t var)
201 {
202   remove_variable(sys, var);
203   lmm_var_free(sys, var);
204 }
205
206 double lmm_variable_getvalue(lmm_variable_t var)
207 {
208   return (var->value);
209 }
210
211 double lmm_variable_getbound(lmm_variable_t var)
212 {
213   return (var->bound);
214 }
215
216 double lmm_variable_getdf(lmm_variable_t var)
217 {
218   return (var->df);
219 }
220
221 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
222                 lmm_variable_t var, double value)
223 {
224   lmm_element_t elem = NULL;
225
226   sys->modified = 1;
227
228   xbt_assert0(var->cnsts_number < var->cnsts_size, "Too much constraints");
229
230   elem = &(var->cnsts[var->cnsts_number++]);
231
232   elem->value = value;
233   elem->constraint = cnst;
234   elem->variable = var;
235
236   if (var->weight)
237     xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
238   else
239     xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
240
241   make_constraint_active(sys, cnst);
242 }
243
244 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
245                     lmm_variable_t var, double value)
246 {
247   int i;
248   sys->modified = 1;
249
250   for (i = 0; i < var->cnsts_number; i++)
251     if (var->cnsts[i].constraint == cnst)
252       break;
253
254   if (i < var->cnsts_number) {
255     if(cnst->shared) var->cnsts[i].value += value;
256     else var->cnsts[i].value = MAX(var->cnsts[i].value,value);
257   }  else
258     lmm_expand(sys, cnst, var, value);
259 }
260
261 void lmm_elem_set_value(lmm_system_t sys, lmm_constraint_t cnst,
262                         lmm_variable_t var, double value)
263 {
264   int i;
265
266   for (i = 0; i < var->cnsts_number; i++)
267     if (var->cnsts[i].constraint == cnst)
268       break;
269
270   if (i < var->cnsts_number) {
271     var->cnsts[i].value = value;
272     sys->modified = 1;
273   } else
274     DIE_IMPOSSIBLE;
275 }
276
277 lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t sys,
278                                        lmm_variable_t var, int num)
279 {
280   if (num < var->cnsts_number)
281     return (var->cnsts[num].constraint);
282   else
283     return NULL;
284 }
285
286 int lmm_get_number_of_cnst_from_var(lmm_system_t sys, lmm_variable_t var)
287 {
288   return (var->cnsts_number);
289 }
290
291 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t sys,
292                                      lmm_constraint_t cnst,
293                                      lmm_element_t * elem)
294 {
295   if (!(*elem))
296     *elem = xbt_swag_getFirst(&(cnst->element_set));
297   else
298     *elem = xbt_swag_getNext(*elem, cnst->element_set.offset);
299   if(*elem)
300     return (*elem)->variable;
301   else 
302     return NULL;
303 }
304
305 void *lmm_constraint_id(lmm_constraint_t cnst)
306 {
307   return cnst->id;
308 }
309
310 void *lmm_variable_id(lmm_variable_t var)
311 {
312   return var->id;
313 }
314
315 static void saturated_constraint_set_update(lmm_system_t sys,
316                                             lmm_constraint_t cnst,
317                                             double *min_usage)
318 {
319   lmm_constraint_t useless_cnst = NULL;
320
321   XBT_IN3("sys=%p, cnst=%p, min_usage=%f", sys, cnst, *min_usage);
322   if (cnst->usage <= 0) {
323     XBT_OUT;
324     return;
325   }
326   if (cnst->remaining <= 0) {
327     XBT_OUT;
328     return;
329   }
330   if ((*min_usage < 0) || (*min_usage > cnst->remaining / cnst->usage)) {
331     *min_usage = cnst->remaining / cnst->usage;
332     LOG3(xbt_log_priority_trace,
333          "min_usage=%f (cnst->remaining=%f, cnst->usage=%f)", *min_usage,
334          cnst->remaining, cnst->usage);
335     while ((useless_cnst =
336             xbt_swag_getFirst(&(sys->saturated_constraint_set))))
337       xbt_swag_remove(useless_cnst, &(sys->saturated_constraint_set));
338
339     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
340   } else if (*min_usage == cnst->remaining / cnst->usage) {
341     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
342   }
343   XBT_OUT;
344 }
345
346 static void saturated_variable_set_update(lmm_system_t sys)
347 {
348   lmm_constraint_t cnst = NULL;
349   xbt_swag_t cnst_list = NULL;
350   lmm_element_t elem = NULL;
351   xbt_swag_t elem_list = NULL;
352
353   cnst_list = &(sys->saturated_constraint_set);
354   while ((cnst = xbt_swag_getFirst(cnst_list))) {
355     elem_list = &(cnst->active_element_set);
356     xbt_swag_foreach(elem, elem_list) {
357       if (elem->variable->weight <= 0)
358         break;
359       if ((elem->value > 0))
360         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
361     }
362     xbt_swag_remove(cnst, cnst_list);
363   }
364 }
365
366 void lmm_print(lmm_system_t sys)
367 {
368   lmm_constraint_t cnst = NULL;
369   lmm_element_t elem = NULL;
370   lmm_variable_t var = NULL;
371   xbt_swag_t cnst_list = NULL;
372   xbt_swag_t var_list = NULL;
373   xbt_swag_t elem_list = NULL;
374   char print_buf[1024];
375   char *trace_buf = xbt_malloc0(sizeof(char));
376   double sum = 0.0;
377
378   /* Printing Objective */
379   var_list = &(sys->variable_set);
380   sprintf(print_buf, "MAX-MIN ( ");
381   trace_buf =
382       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
383   strcat(trace_buf, print_buf);
384   xbt_swag_foreach(var, var_list) {
385     sprintf(print_buf, "'%p'(%f) ", var, var->weight);
386     trace_buf =
387         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
388     strcat(trace_buf, print_buf);
389   }
390   sprintf(print_buf, ")");
391   trace_buf =
392       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
393   strcat(trace_buf, print_buf);
394   DEBUG1("%s", trace_buf);
395   trace_buf[0] = '\000';
396
397   DEBUG0("Constraints");
398   /* Printing Constraints */
399   cnst_list = &(sys->active_constraint_set);
400   xbt_swag_foreach(cnst, cnst_list) {
401     sum = 0.0;
402     elem_list = &(cnst->element_set);
403     sprintf(print_buf, "\t");
404     trace_buf =
405         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
406     strcat(trace_buf, print_buf);
407     xbt_swag_foreach(elem, elem_list) {
408       sprintf(print_buf, "%f.'%p'(%f) + ", elem->value,
409               elem->variable, elem->variable->value);
410       trace_buf =
411           xbt_realloc(trace_buf,
412                       strlen(trace_buf) + strlen(print_buf) + 1);
413       strcat(trace_buf, print_buf);
414       sum += elem->value * elem->variable->value;
415     }
416     sprintf(print_buf, "0 <= %f ('%p')", cnst->bound, cnst);
417     trace_buf =
418         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
419     strcat(trace_buf, print_buf);
420
421     if (!cnst->shared) {
422       sprintf(print_buf, " [MAX-Constraint]");
423       trace_buf =
424           xbt_realloc(trace_buf,
425                       strlen(trace_buf) + strlen(print_buf) + 1);
426       strcat(trace_buf, print_buf);
427     }
428     DEBUG1("%s", trace_buf);
429     trace_buf[0] = '\000';
430     xbt_assert3(!double_positive(sum - cnst->bound),
431                 "Incorrect value (%f is not smaller than %f): %g",
432                 sum, cnst->bound, sum - cnst->bound);    
433   }
434
435   DEBUG0("Variables");
436   /* Printing Result */
437   xbt_swag_foreach(var, var_list) {
438     if (var->bound > 0) {
439       DEBUG4("'%p'(%f) : %f (<=%f)", var, var->weight, var->value,
440              var->bound);
441       xbt_assert2(!double_positive(var->value - var->bound),
442                   "Incorrect value (%f is not smaller than %f",
443                   var->value, var->bound);
444     } else
445       DEBUG3("'%p'(%f) : %f", var, var->weight, var->value);
446   }
447
448   free(trace_buf);
449 }
450
451 void lmm_solve(lmm_system_t sys)
452 {
453   lmm_variable_t var = NULL;
454   lmm_constraint_t cnst = NULL;
455   lmm_element_t elem = NULL;
456   xbt_swag_t cnst_list = NULL;
457   xbt_swag_t var_list = NULL;
458   xbt_swag_t elem_list = NULL;
459   double min_usage = -1;
460
461   if (!(sys->modified))
462     return;
463
464   /* Init */
465   var_list = &(sys->variable_set);
466   DEBUG1("Variable set : %d", xbt_swag_size(var_list));
467   xbt_swag_foreach(var, var_list) {
468     int nb=0;
469     int i;
470     if(var->weight<=0.0) break;
471     var->value = 0.0;
472     for (i = 0; i < var->cnsts_number; i++) {
473       if(var->cnsts[i].value==0.0) nb++;
474     }
475     if((nb==var->cnsts_number) && (var->weight>0.0))
476       var->value = 1.0;
477   }
478
479   /* 
480    * Compute Usage and store the variables that reach the maximum.
481    */
482   cnst_list = &(sys->active_constraint_set);
483   DEBUG1("Active constraints : %d", xbt_swag_size(cnst_list));
484   xbt_swag_foreach(cnst, cnst_list) {
485     /* INIT */
486     cnst->remaining = cnst->bound;
487     cnst->usage = 0;
488     elem_list = &(cnst->element_set);
489     cnst->usage = 0.0;
490     xbt_swag_foreach(elem, elem_list) {
491       if (elem->variable->weight <= 0)
492         break;
493       if ((elem->value > 0)) {
494         if (cnst->shared)
495           cnst->usage += elem->value / elem->variable->weight;
496         else if (cnst->usage < elem->value / elem->variable->weight)
497           cnst->usage = elem->value / elem->variable->weight;
498         DEBUG2("Constraint Usage %p : %f", cnst, cnst->usage);
499         make_elem_active(elem);
500       }
501     }
502     /* Saturated constraints update */
503     saturated_constraint_set_update(sys, cnst, &min_usage);
504   }
505   saturated_variable_set_update(sys);
506
507   /* Saturated variables update */
508
509   do {
510     /* Fix the variables that have to be */
511     var_list = &(sys->saturated_variable_set);
512
513     xbt_swag_foreach(var, var_list) {
514       if(var->weight<=0.0) DIE_IMPOSSIBLE;
515       /* First check if some of these variables have reach their upper
516          bound and update min_usage accordingly. */
517       DEBUG5
518           ("var=%p, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f",
519            var, var->bound, var->weight, min_usage,
520            var->bound * var->weight);
521       if ((var->bound > 0) && (var->bound * var->weight < min_usage)) {
522         min_usage = var->bound * var->weight;
523         DEBUG1("Updated min_usage=%f", min_usage);
524       }
525     }
526
527
528     while ((var = xbt_swag_getFirst(var_list))) {
529       int i;
530
531       var->value = min_usage / var->weight;
532       DEBUG5("Min usage: %f, Var(%p)->weight: %f, Var(%p)->value: %f ",
533              min_usage, var, var->weight, var, var->value);
534
535
536       /* Update usage */
537
538       for (i = 0; i < var->cnsts_number; i++) {
539         elem = &var->cnsts[i];
540         cnst = elem->constraint;
541         if (cnst->shared) {
542           double_update(&(cnst->remaining), elem->value * var->value);
543           double_update(&(cnst->usage), elem->value / var->weight);
544           make_elem_inactive(elem);
545         } else {                /* FIXME one day: We recompute usage.... :( */
546           cnst->usage = 0.0;
547           make_elem_inactive(elem);
548           xbt_swag_foreach(elem, elem_list) {
549             if (elem->variable->weight <= 0)
550               break;
551             if (elem->variable->value > 0)
552               break;
553             if ((elem->value > 0)) {
554               if (cnst->usage < elem->value / elem->variable->weight)
555                 cnst->usage = elem->value / elem->variable->weight;
556               DEBUG2("Constraint Usage %p : %f", cnst, cnst->usage);
557               make_elem_active(elem);
558             }
559           }
560         }
561       }
562       xbt_swag_remove(var, var_list);
563     }
564
565     /* Find out which variables reach the maximum */
566     cnst_list = &(sys->active_constraint_set);
567     min_usage = -1;
568     xbt_swag_foreach(cnst, cnst_list) {
569       saturated_constraint_set_update(sys, cnst, &min_usage);
570     }
571     saturated_variable_set_update(sys);
572
573   } while (xbt_swag_size(&(sys->saturated_variable_set)));
574
575   sys->modified = 0;
576   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
577     lmm_print(sys);
578   }
579 }
580
581 /* Not a O(1) function */
582
583 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
584                 lmm_variable_t var, double value)
585 {
586   int i;
587
588   sys->modified = 1;
589   for (i = 0; i < var->cnsts_number; i++)
590     if (var->cnsts[i].constraint == cnst) {
591       var->cnsts[i].value = value;
592       return;
593     }
594 }
595
596 /** \brief Attribute the value bound to var->bound.
597  * 
598  *  \param sys the lmm_system_t
599  *  \param var the lmm_variable_t
600  *  \param bound the new bound to associate with var
601  * 
602  *  Makes var->bound equal to bound. Whenever this function is called 
603  *  a change is  signed in the system. To
604  *  avoid false system changing detection it is a good idea to test 
605  *  (bound != 0) before calling it.
606  *
607  */
608 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
609                                double bound)
610 {
611   sys->modified = 1;
612   var->bound = bound;
613 }
614
615 /** \brief Add the value delta to var->df (the sum of latencies).
616  * 
617  *  \param sys the lmm_system_t associated
618  *  \param var the lmm_variable_t which need to updated
619  *  \param delta the variation of the latency
620  * 
621  *  Add the value delta to var->df (the sum of latencys associated to the
622  *  flow). Whenever this function is called a change is  signed in the system. To
623  *  avoid false system changing detection it is a good idea to test 
624  *  (delta != 0) before calling it.
625  *
626  */
627 void lmm_update_variable_latency(lmm_system_t sys, lmm_variable_t var,
628                                  double delta)
629 {
630   sys->modified = 1;
631   var->df += delta;
632 }
633
634 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
635                                 double weight)
636 {
637   int i;
638   lmm_element_t elem;
639
640   if(weight==var->weight) return;
641   XBT_IN3("(sys=%p, var=%p, weight=%f)", sys, var, weight);
642   sys->modified = 1;
643   var->weight = weight;
644   xbt_swag_remove(var, &(sys->variable_set));
645   if (weight)
646     xbt_swag_insert_at_head(var, &(sys->variable_set));
647   else
648     xbt_swag_insert_at_tail(var, &(sys->variable_set));
649
650   for (i = 0; i < var->cnsts_number; i++) {
651     elem = &var->cnsts[i];
652     xbt_swag_remove(elem, &(elem->constraint->element_set));
653     if (weight)
654       xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
655     else
656       xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
657   }
658   if(!weight)
659     var->value = 0.0;
660
661   XBT_OUT;
662 }
663
664 double lmm_get_variable_weight(lmm_variable_t var)
665 {
666   return var->weight;
667 }
668
669 void lmm_update_constraint_bound(lmm_system_t sys, lmm_constraint_t cnst,
670                                  double bound)
671 {
672   sys->modified = 1;
673   cnst->bound = bound;
674 }
675
676 int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
677 {
678   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
679 }
680
681 lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t sys)
682 {
683   return xbt_swag_getFirst(&(sys->active_constraint_set));
684 }
685
686 lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t sys,
687                                                 lmm_constraint_t cnst)
688 {
689   return xbt_swag_getNext(cnst, (sys->active_constraint_set).offset);
690 }