Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
88c0625603dc1a047f1e0173855a9f16f2b051b1
[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 "src/kernel/lmm/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::kernel::lmm::bottleneck_solve(lmm_system_t sys)
21 {
22   void* _var;
23   void* _var_next;
24   void* _elem;
25   lmm_variable_t var    = nullptr;
26   lmm_element_t elem   = nullptr;
27   xbt_swag_t var_list  = nullptr;
28   xbt_swag_t elem_list = nullptr;
29
30   if (not sys->modified)
31     return;
32
33   var_list = &(sys->variable_set);
34   XBT_DEBUG("Variable set : %d", xbt_swag_size(var_list));
35   xbt_swag_foreach(_var, var_list)
36   {
37     var        = static_cast<lmm_variable_t>(_var);
38     var->value = 0.0;
39     XBT_DEBUG("Handling variable %p", var);
40     if (var->sharing_weight > 0.0 && std::find_if(begin(var->cnsts), end(var->cnsts), [](s_lmm_element_t const& x) {
41                                        return x.consumption_weight != 0.0;
42                                      }) != end(var->cnsts)) {
43       xbt_swag_insert(var, &(sys->saturated_variable_set));
44     } else {
45       XBT_DEBUG("Err, finally, there is no need to take care of variable %p", var);
46       if (var->sharing_weight > 0.0)
47         var->value = 1.0;
48     }
49   }
50   var_list = &(sys->saturated_variable_set);
51
52   XBT_DEBUG("Active constraints : %zu", sys->active_constraint_set.size());
53   for (s_lmm_constraint_t& cnst : sys->active_constraint_set) {
54     sys->saturated_constraint_set.push_back(cnst);
55   }
56   for (s_lmm_constraint_t& cnst : sys->saturated_constraint_set) {
57     cnst.remaining = cnst.bound;
58     cnst.usage     = 0.0;
59   }
60
61   XBT_DEBUG("Fair bottleneck Initialized");
62
63   /*
64    * Compute Usage and store the variables that reach the maximum.
65    */
66   auto& cnst_list = sys->saturated_constraint_set;
67   do {
68     if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
69       XBT_DEBUG("Fair bottleneck done");
70       sys->print();
71     }
72     XBT_DEBUG("******* Constraints to process: %zu *******", cnst_list.size());
73     for (auto iter = std::begin(cnst_list); iter != std::end(cnst_list);) {
74       s_lmm_constraint_t& cnst = *iter;
75       int nb = 0;
76       XBT_DEBUG("Processing cnst %p ", &cnst);
77       elem_list  = &cnst.enabled_element_set;
78       cnst.usage = 0.0;
79       xbt_swag_foreach(_elem, elem_list)
80       {
81         elem = static_cast<lmm_element_t>(_elem);
82         xbt_assert(elem->variable->sharing_weight > 0);
83         if ((elem->consumption_weight > 0) && xbt_swag_belongs(elem->variable, var_list))
84           nb++;
85       }
86       XBT_DEBUG("\tThere are %d variables", nb);
87       if (nb > 0 && not cnst.sharing_policy)
88         nb = 1;
89       if (nb == 0) {
90         cnst.remaining = 0.0;
91         cnst.usage     = 0.0;
92         iter           = cnst_list.erase(iter);
93       } else {
94         cnst.usage = cnst.remaining / nb;
95         XBT_DEBUG("\tConstraint Usage %p : %f with %d variables", &cnst, cnst.usage, nb);
96         iter++;
97       }
98     }
99
100     xbt_swag_foreach_safe(_var, _var_next, var_list)
101     {
102       var            = static_cast<lmm_variable_t>(_var);
103       double min_inc = DBL_MAX;
104       for (s_lmm_element_t const& elm : var->cnsts) {
105         if (elm.consumption_weight > 0)
106           min_inc = std::min(min_inc, elm.constraint->usage / elm.consumption_weight);
107       }
108       if (var->bound > 0)
109         min_inc = std::min(min_inc, var->bound - var->value);
110       var->mu   = min_inc;
111       XBT_DEBUG("Updating variable %p maximum increment: %g", var, var->mu);
112       var->value += var->mu;
113       if (var->value == var->bound) {
114         xbt_swag_remove(var, var_list);
115       }
116     }
117
118     for (auto iter = std::begin(cnst_list); iter != std::end(cnst_list);) {
119       s_lmm_constraint_t& cnst = *iter;
120       XBT_DEBUG("Updating cnst %p ", &cnst);
121       elem_list = &cnst.enabled_element_set;
122       xbt_swag_foreach(_elem, elem_list)
123       {
124         elem = static_cast<lmm_element_t>(_elem);
125         xbt_assert(elem->variable->sharing_weight > 0);
126         if (cnst.sharing_policy) {
127           XBT_DEBUG("\tUpdate constraint %p (%g) with variable %p by %g", &cnst, cnst.remaining, elem->variable,
128                     elem->variable->mu);
129           double_update(&cnst.remaining, elem->consumption_weight * elem->variable->mu, sg_maxmin_precision);
130         } else {
131           XBT_DEBUG("\tNon-Shared variable. Update constraint usage of %p (%g) with variable %p by %g", &cnst,
132                     cnst.usage, elem->variable, elem->variable->mu);
133           cnst.usage = std::min(cnst.usage, elem->consumption_weight * elem->variable->mu);
134         }
135       }
136       if (not cnst.sharing_policy) {
137         XBT_DEBUG("\tUpdate constraint %p (%g) by %g", &cnst, cnst.remaining, cnst.usage);
138
139         double_update(&cnst.remaining, cnst.usage, sg_maxmin_precision);
140       }
141
142       XBT_DEBUG("\tRemaining for %p : %g", &cnst, cnst.remaining);
143       if (cnst.remaining <= 0.0) {
144         XBT_DEBUG("\tGet rid of constraint %p", &cnst);
145
146         iter = cnst_list.erase(iter);
147         xbt_swag_foreach(_elem, elem_list)
148         {
149           elem = static_cast<lmm_element_t>(_elem);
150           if (elem->variable->sharing_weight <= 0)
151             break;
152           if (elem->consumption_weight > 0) {
153             XBT_DEBUG("\t\tGet rid of variable %p", elem->variable);
154             xbt_swag_remove(elem->variable, var_list);
155           }
156         }
157       } else {
158         iter++;
159       }
160     }
161   } while (xbt_swag_size(var_list));
162
163   cnst_list.clear();
164   sys->modified = true;
165   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
166     XBT_DEBUG("Fair bottleneck done");
167     sys->print();
168   }
169 }