Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reindent everything (possibly breaking all branches, but for the last time)
[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_variable_t var_next = NULL;
24   lmm_constraint_t 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     DEBUG1("Handling variable %p", var);
49     xbt_swag_insert(var, &(sys->saturated_variable_set));
50     for (i = 0; i < var->cnsts_number; i++) {
51       if (var->cnsts[i].value == 0.0)
52         nb++;
53     }
54     if ((nb == var->cnsts_number) && (var->weight > 0.0)) {
55       DEBUG1("Err, finally, there is no need to take care of variable %p",
56              var);
57       xbt_swag_remove(var, &(sys->saturated_variable_set));
58       var->value = 1.0;
59     }
60     if (var->weight <= 0.0) {
61       DEBUG1("Err, finally, there is no need to take care of variable %p",
62              var);
63       xbt_swag_remove(var, &(sys->saturated_variable_set));
64     }
65   }
66   var_list = &(sys->saturated_variable_set);
67
68   cnst_list = &(sys->active_constraint_set);
69   DEBUG1("Active constraints : %d", xbt_swag_size(cnst_list));
70   xbt_swag_foreach(cnst, cnst_list) {
71     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
72   }
73   cnst_list = &(sys->saturated_constraint_set);
74   xbt_swag_foreach(cnst, cnst_list) {
75     cnst->remaining = cnst->bound;
76     cnst->usage = 0.0;
77   }
78
79   DEBUG0("Fair bottleneck Initialized");
80
81   /* 
82    * Compute Usage and store the variables that reach the maximum.
83    */
84   do {
85     if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
86       DEBUG0("Fair bottleneck done");
87       lmm_print(sys);
88     }
89     DEBUG1("******* Constraints to process: %d *******",
90            xbt_swag_size(cnst_list));
91     min_usage = -1;
92     xbt_swag_foreach_safe(cnst, cnst_next, cnst_list) {
93       int nb = 0;
94       DEBUG1("Processing cnst %p ", cnst);
95       elem_list = &(cnst->element_set);
96       cnst->usage = 0.0;
97       xbt_swag_foreach(elem, elem_list) {
98         if (elem->variable->weight <= 0)
99           break;
100         if ((elem->value > 0) && xbt_swag_belongs(elem->variable, var_list))
101           nb++;
102       }
103       DEBUG1("\tThere are %d variables", nb);
104       if (nb > 0 && !cnst->shared)
105         nb = 1;
106       if (!nb) {
107         cnst->remaining = 0.0;
108         cnst->usage = cnst->remaining;
109         xbt_swag_remove(cnst, cnst_list);
110         continue;
111       }
112       cnst->usage = cnst->remaining / nb;
113       DEBUG3("\tConstraint Usage %p : %f with %d variables", cnst,
114              cnst->usage, nb);
115     }
116
117     xbt_swag_foreach_safe(var, var_next, var_list) {
118       double min_inc = var->cnsts[0].constraint->usage / var->cnsts[0].value;
119       for (i = 1; i < var->cnsts_number; i++) {
120         lmm_element_t elm = &var->cnsts[i];
121         min_inc = MIN(min_inc, elm->constraint->usage / elm->value);
122       }
123       if (var->bound > 0)
124         min_inc = MIN(min_inc, var->bound - var->value);
125       var->mu = min_inc;
126       DEBUG2("Updating variable %p maximum increment: %g", var, var->mu);
127       var->value += var->mu;
128       if (var->value == var->bound) {
129         xbt_swag_remove(var, var_list);
130       }
131     }
132
133     xbt_swag_foreach_safe(cnst, cnst_next, cnst_list) {
134       DEBUG1("Updating cnst %p ", cnst);
135       elem_list = &(cnst->element_set);
136       xbt_swag_foreach(elem, elem_list) {
137         if (elem->variable->weight <= 0)
138           break;
139         if (cnst->shared) {
140           DEBUG4("\tUpdate constraint %p (%g) with variable %p by %g",
141                  cnst, cnst->remaining, elem->variable, elem->variable->mu);
142           double_update(&(cnst->remaining), elem->value * elem->variable->mu);
143         } else {
144           DEBUG4
145             ("\tNon-Shared variable. Update constraint usage of %p (%g) with variable %p by %g",
146              cnst, cnst->usage, elem->variable, elem->variable->mu);
147           cnst->usage = MIN(cnst->usage, elem->value * elem->variable->mu);
148         }
149       }
150       if (!cnst->shared) {
151         DEBUG3("\tUpdate constraint %p (%g) by %g",
152                cnst, cnst->remaining, cnst->usage);
153
154         double_update(&(cnst->remaining), cnst->usage);
155       }
156
157       DEBUG2("\tRemaining for %p : %g", cnst, cnst->remaining);
158       if (cnst->remaining == 0.0) {
159         DEBUG1("\tGet rid of constraint %p", cnst);
160
161         xbt_swag_remove(cnst, cnst_list);
162         xbt_swag_foreach(elem, elem_list) {
163           if (elem->variable->weight <= 0)
164             break;
165           if (elem->value > 0) {
166             DEBUG1("\t\tGet rid of variable %p", elem->variable);
167             xbt_swag_remove(elem->variable, var_list);
168           }
169         }
170       }
171     }
172   } while (xbt_swag_size(var_list));
173
174   xbt_swag_foreach_safe(cnst, cnst_next, cnst_list) {
175     xbt_swag_remove(cnst, cnst_list);
176   }
177   sys->modified = 0;
178   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
179     DEBUG0("Fair bottleneck done");
180     lmm_print(sys);
181   }
182 }