Logo AND Algorithmique Numérique Distribuée

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