Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move LMM-related stuff to its directory
[simgrid.git] / src / kernel / lmm / fair_bottleneck.cpp
1 /* Copyright (c) 2007-2011, 2013-2017. 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 "surf/maxmin.hpp"
8 #include "xbt/log.h"
9 #include "xbt/sysdep.h"
10 #include <algorithm>
11 #include <cfloat>
12 #include <cmath>
13 #include <cstdlib>
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_maxmin);
16 #define SHOW_EXPR_G(expr) XBT_DEBUG(#expr " = %g", expr);
17 #define SHOW_EXPR_D(expr) XBT_DEBUG(#expr " = %d", expr);
18 #define SHOW_EXPR_P(expr) XBT_DEBUG(#expr " = %p", expr);
19
20 void simgrid::surf::bottleneck_solve(lmm_system_t sys)
21 {
22   void* _var;
23   void* _var_next;
24   void* _cnst;
25   void* _cnst_next;
26   void* _elem;
27   lmm_variable_t var    = nullptr;
28   lmm_constraint_t cnst = nullptr;
29   s_lmm_constraint_t s_cnst;
30   lmm_element_t elem   = nullptr;
31   xbt_swag_t cnst_list = nullptr;
32   xbt_swag_t var_list  = nullptr;
33   xbt_swag_t elem_list = nullptr;
34
35   static s_xbt_swag_t cnst_to_update;
36
37   if (not sys->modified)
38     return;
39
40   /* Init */
41   xbt_swag_init(&(cnst_to_update), xbt_swag_offset(s_cnst, saturated_constraint_set_hookup));
42
43   var_list = &(sys->variable_set);
44   XBT_DEBUG("Variable set : %d", xbt_swag_size(var_list));
45   xbt_swag_foreach(_var, var_list)
46   {
47     var        = static_cast<lmm_variable_t>(_var);
48     var->value = 0.0;
49     XBT_DEBUG("Handling variable %p", var);
50     xbt_swag_insert(var, &(sys->saturated_variable_set));
51     auto weighted = std::find_if(begin(var->cnsts), end(var->cnsts),
52                                  [](s_lmm_element_t const& x) { return x.consumption_weight != 0.0; });
53     if (weighted == end(var->cnsts) && var->sharing_weight > 0.0) {
54       XBT_DEBUG("Err, finally, there is no need to take care of variable %p", var);
55       xbt_swag_remove(var, &(sys->saturated_variable_set));
56       var->value = 1.0;
57     }
58     if (var->sharing_weight <= 0.0) {
59       XBT_DEBUG("Err, finally, there is no need to take care of variable %p", 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   {
69     cnst = static_cast<lmm_constraint_t>(_cnst);
70     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
71   }
72   cnst_list = &(sys->saturated_constraint_set);
73   xbt_swag_foreach(_cnst, cnst_list)
74   {
75     cnst            = static_cast<lmm_constraint_t>(_cnst);
76     cnst->remaining = cnst->bound;
77     cnst->usage     = 0.0;
78   }
79
80   XBT_DEBUG("Fair bottleneck Initialized");
81
82   /*
83    * Compute Usage and store the variables that reach the maximum.
84    */
85   do {
86     if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
87       XBT_DEBUG("Fair bottleneck done");
88       sys->print();
89     }
90     XBT_DEBUG("******* Constraints to process: %d *******", xbt_swag_size(cnst_list));
91     xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list)
92     {
93       cnst   = static_cast<lmm_constraint_t>(_cnst);
94       int nb = 0;
95       XBT_DEBUG("Processing cnst %p ", cnst);
96       elem_list   = &(cnst->enabled_element_set);
97       cnst->usage = 0.0;
98       xbt_swag_foreach(_elem, elem_list)
99       {
100         elem = static_cast<lmm_element_t>(_elem);
101         xbt_assert(elem->variable->sharing_weight > 0);
102         if ((elem->consumption_weight > 0) && xbt_swag_belongs(elem->variable, var_list))
103           nb++;
104       }
105       XBT_DEBUG("\tThere are %d variables", nb);
106       if (nb > 0 && not cnst->sharing_policy)
107         nb = 1;
108       if (not nb) {
109         cnst->remaining = 0.0;
110         cnst->usage     = cnst->remaining;
111         xbt_swag_remove(cnst, cnst_list);
112         continue;
113       }
114       cnst->usage = cnst->remaining / nb;
115       XBT_DEBUG("\tConstraint Usage %p : %f with %d variables", cnst, cnst->usage, nb);
116     }
117
118     xbt_swag_foreach_safe(_var, _var_next, var_list)
119     {
120       var            = static_cast<lmm_variable_t>(_var);
121       double min_inc = DBL_MAX;
122       for (s_lmm_element_t const& elm : var->cnsts) {
123         if (elm.consumption_weight > 0)
124           min_inc = std::min(min_inc, elm.constraint->usage / elm.consumption_weight);
125       }
126       if (var->bound > 0)
127         min_inc = std::min(min_inc, var->bound - var->value);
128       var->mu   = min_inc;
129       XBT_DEBUG("Updating variable %p maximum increment: %g", var, var->mu);
130       var->value += var->mu;
131       if (var->value == var->bound) {
132         xbt_swag_remove(var, var_list);
133       }
134     }
135
136     xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list)
137     {
138       cnst = static_cast<lmm_constraint_t>(_cnst);
139       XBT_DEBUG("Updating cnst %p ", cnst);
140       elem_list = &(cnst->enabled_element_set);
141       xbt_swag_foreach(_elem, elem_list)
142       {
143         elem = static_cast<lmm_element_t>(_elem);
144         xbt_assert(elem->variable->sharing_weight > 0);
145         if (cnst->sharing_policy) {
146           XBT_DEBUG("\tUpdate constraint %p (%g) with variable %p by %g", cnst, cnst->remaining, elem->variable,
147                     elem->variable->mu);
148           double_update(&(cnst->remaining), elem->consumption_weight * elem->variable->mu, sg_maxmin_precision);
149         } else {
150           XBT_DEBUG("\tNon-Shared variable. Update constraint usage of %p (%g) with variable %p by %g", cnst,
151                     cnst->usage, elem->variable, elem->variable->mu);
152           cnst->usage = std::min(cnst->usage, elem->consumption_weight * elem->variable->mu);
153         }
154       }
155       if (not cnst->sharing_policy) {
156         XBT_DEBUG("\tUpdate constraint %p (%g) by %g", cnst, cnst->remaining, cnst->usage);
157
158         double_update(&(cnst->remaining), cnst->usage, sg_maxmin_precision);
159       }
160
161       XBT_DEBUG("\tRemaining for %p : %g", cnst, cnst->remaining);
162       if (cnst->remaining <= 0.0) {
163         XBT_DEBUG("\tGet rid of constraint %p", cnst);
164
165         xbt_swag_remove(cnst, cnst_list);
166         xbt_swag_foreach(_elem, elem_list)
167         {
168           elem = static_cast<lmm_element_t>(_elem);
169           if (elem->variable->sharing_weight <= 0)
170             break;
171           if (elem->consumption_weight > 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 = true;
182   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
183     XBT_DEBUG("Fair bottleneck done");
184     sys->print();
185   }
186 }