Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Bug fix.
[simgrid.git] / src / surf / fair_bottleneck.c
1 /*      $Id$ */
2
3 /* Copyright (c) 2007 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 "maxmin_private.h"
12 #include <stdlib.h>
13 #include <math.h>
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_maxmin);
16 #define SHOW_EXPR_G(expr) DEBUG1(#expr " = %g",expr);
17 #define SHOW_EXPR_D(expr) DEBUG1(#expr " = %d",expr);
18 #define SHOW_EXPR_P(expr) DEBUG1(#expr " = %p",expr);
19
20 void bottleneck_solve(lmm_system_t sys)
21 {
22   lmm_variable_t var = NULL;
23   lmm_constraint_t cnst = NULL;
24   lmm_constraint_t useless_cnst = NULL;
25   s_lmm_constraint_t s_cnst;
26   lmm_constraint_t cnst_next = NULL;
27   lmm_element_t elem = NULL;
28   xbt_swag_t cnst_list = NULL;
29   xbt_swag_t var_list = NULL;
30   xbt_swag_t elem_list = NULL;
31   double min_usage = -1;
32   int i;
33
34   static s_xbt_swag_t cnst_to_update;
35
36   if (!(sys->modified))
37     return;
38
39   /* Init */
40   xbt_swag_init(&(cnst_to_update),
41                 xbt_swag_offset(s_cnst, saturated_constraint_set_hookup));
42
43   var_list = &(sys->variable_set);
44   DEBUG1("Variable set : %d", xbt_swag_size(var_list));
45   xbt_swag_foreach(var, var_list) {
46     int nb=0;
47     var->value = 0.0;
48     for (i = 0; i < var->cnsts_number; i++) {
49       if(var->cnsts[i].value==0.0) nb++;
50     }
51     if((nb==var->cnsts_number) && (var->weight>0.0))
52       var->value = 1.0;
53   }
54
55   cnst_list = &(sys->active_constraint_set);
56   DEBUG1("Active constraints : %d", xbt_swag_size(cnst_list));
57   xbt_swag_foreach(cnst, cnst_list) {
58     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
59   }
60   cnst_list = &(sys->saturated_constraint_set);
61   xbt_swag_foreach(cnst, cnst_list) {
62     cnst->remaining = cnst->bound;
63     cnst->usage = 0.0;
64   }
65
66   DEBUG0("Fair bottleneck Initialized");
67
68   /* 
69    * Compute Usage and store the variables that reach the maximum.
70    */
71   while (1) {
72 /*     if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) { */
73 /*       DEBUG0("Fair bottleneck done"); */
74 /*       lmm_print(sys); */
75 /*     } */
76     DEBUG1("******* Constraints to process: %d *******", xbt_swag_size(cnst_list));
77     xbt_swag_foreach_safe(cnst, cnst_next, cnst_list) {
78       int nb = 0;
79       double max_elem = 0.0;
80       DEBUG1("Processing cnst %p ", cnst);
81       elem_list = &(cnst->element_set);
82       cnst->usage = 0.0;
83       xbt_swag_foreach(elem, elem_list) {
84         if (elem->variable->weight <= 0)
85           break;
86         if ((elem->value > 0)) {
87           nb++;
88           if (max_elem > 0)
89             max_elem =
90                 MAX(max_elem, elem->value / elem->variable->weight);
91           else
92             max_elem = elem->value / elem->variable->weight;
93         }
94         //      make_elem_active(elem);
95       }
96       DEBUG2("\tmax_elem : %g with %d variables",  max_elem,nb);
97       if(nb>0 && !cnst->shared)
98         nb = 1;
99       cnst->usage = max_elem * nb;
100       DEBUG3("\tConstraint Usage %p : %f with %d variables", cnst, cnst->usage,nb);
101       if(!nb) {
102         xbt_swag_remove(cnst, cnst_list);
103         continue;
104       }
105       /* Saturated constraints update */
106       if (min_usage < 0 || min_usage > cnst->remaining / cnst->usage) {
107         DEBUG3("Update min_usage (%g) with cnst %p -> %g",min_usage, cnst,
108                cnst->remaining / cnst->usage);
109
110         min_usage = cnst->remaining / cnst->usage;
111         while ((useless_cnst = xbt_swag_extract(&(cnst_to_update)))) {
112           xbt_swag_insert_at_head(useless_cnst, cnst_list);
113         }
114         xbt_swag_remove(cnst, cnst_list);
115         xbt_swag_insert(cnst, &(cnst_to_update));
116       } else if (min_usage == cnst->remaining / cnst->usage) {
117         DEBUG2("Keep   min_usage (%g) with cnst %p",min_usage, cnst);
118         xbt_swag_remove(cnst, cnst_list);
119         xbt_swag_insert(cnst, &(cnst_to_update));
120       }
121     }
122
123     if (!xbt_swag_size(&cnst_to_update))
124       break;
125
126     while ((cnst_next = xbt_swag_extract(&cnst_to_update))) {
127       int nb = 0;
128       double remaining = cnst_next->remaining;
129       elem_list = &(cnst_next->element_set);
130       xbt_swag_foreach(elem, elem_list) {
131         if (elem->variable->weight <= 0)
132           break;
133         if ((elem->value > 0))
134           nb++;
135       }
136       if(nb>0 && !(cnst_next->shared))
137         nb = 1;
138
139       DEBUG1("Updating for cnst %p",cnst_next);
140
141       xbt_swag_foreach(elem, elem_list) {
142         if (elem->value <= 0) continue;
143
144         var = elem->variable;
145
146         if (var->weight <= 0)
147           break;
148         if (var->value == 0.0) {
149           DEBUG2("\tUpdating var %p (%g)",var,var->value);
150           var->value = var->weight * remaining / (nb * elem->value);
151
152           /* Update usage */
153
154           for (i = 0; i < var->cnsts_number; i++) {
155             lmm_element_t elm = &var->cnsts[i];
156             cnst = elm->constraint;
157             DEBUG1("\t\tUpdating cnst %p",cnst);
158             double_update(&(cnst->remaining), elm->value * var->value);
159             double_update(&(cnst->usage), elm->value / var->weight);
160             //              make_elem_inactive(elm);
161           }
162         }
163       }
164     }
165   }
166
167   sys->modified = 0;
168   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
169     DEBUG0("Fair bottleneck done");
170     lmm_print(sys);
171   }
172 }