Logo AND Algorithmique Numérique Distribuée

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