Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bb1bd489cabcc42d57f156bc89ca80378567f66d
[simgrid.git] / src / kernel / lmm / fair_bottleneck.cpp
1 /* Copyright (c) 2007-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/kernel/lmm/maxmin.hpp"
7 #include "xbt/log.h"
8 #include "xbt/sysdep.h"
9 #include <algorithm>
10 #include <cfloat>
11 #include <cmath>
12 #include <cstdlib>
13 #include <xbt/utility.hpp>
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_maxmin);
16
17 simgrid::kernel::lmm::System* simgrid::kernel::lmm::make_new_fair_bottleneck_system(bool selective_update)
18 {
19   return new simgrid::kernel::lmm::FairBottleneck(selective_update);
20 }
21
22 void simgrid::kernel::lmm::FairBottleneck::bottleneck_solve()
23 {
24   if (not modified_)
25     return;
26
27   XBT_DEBUG("Variable set : %zu", variable_set.size());
28   for (Variable& var : variable_set) {
29     var.value = 0.0;
30     XBT_DEBUG("Handling variable %p", &var);
31     if (var.sharing_weight > 0.0 && std::find_if(begin(var.cnsts), end(var.cnsts), [](Element const& x) {
32                                       return x.consumption_weight != 0.0;
33                                     }) != end(var.cnsts)) {
34       saturated_variable_set.push_back(var);
35     } else {
36       XBT_DEBUG("Err, finally, there is no need to take care of variable %p", &var);
37       if (var.sharing_weight > 0.0)
38         var.value = 1.0;
39     }
40   }
41
42   XBT_DEBUG("Active constraints : %zu", active_constraint_set.size());
43   for (Constraint& cnst : active_constraint_set) {
44     saturated_constraint_set.push_back(cnst);
45   }
46   for (Constraint& cnst : saturated_constraint_set) {
47     cnst.remaining = cnst.bound;
48     cnst.usage     = 0.0;
49   }
50
51   XBT_DEBUG("Fair bottleneck Initialized");
52
53   /*
54    * Compute Usage and store the variables that reach the maximum.
55    */
56   auto& var_list  = saturated_variable_set;
57   auto& cnst_list = saturated_constraint_set;
58   do {
59     if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
60       XBT_DEBUG("Fair bottleneck done");
61       print();
62     }
63     XBT_DEBUG("******* Constraints to process: %zu *******", cnst_list.size());
64     for (auto iter = std::begin(cnst_list); iter != std::end(cnst_list);) {
65       Constraint& cnst = *iter;
66       int nb = 0;
67       XBT_DEBUG("Processing cnst %p ", &cnst);
68       cnst.usage = 0.0;
69       for (Element& elem : cnst.enabled_element_set) {
70         xbt_assert(elem.variable->sharing_weight > 0);
71         if (elem.consumption_weight > 0 && elem.variable->saturated_variable_set_hook.is_linked())
72           nb++;
73       }
74       XBT_DEBUG("\tThere are %d variables", nb);
75       if (nb > 0 && not cnst.sharing_policy)
76         nb = 1;
77       if (nb == 0) {
78         cnst.remaining = 0.0;
79         cnst.usage     = 0.0;
80         iter           = cnst_list.erase(iter);
81       } else {
82         cnst.usage = cnst.remaining / nb;
83         XBT_DEBUG("\tConstraint Usage %p : %f with %d variables", &cnst, cnst.usage, nb);
84         iter++;
85       }
86     }
87
88     for (auto iter = std::begin(var_list); iter != std::end(var_list);) {
89       Variable& var  = *iter;
90       double min_inc = DBL_MAX;
91       for (Element const& elm : var.cnsts) {
92         if (elm.consumption_weight > 0)
93           min_inc = std::min(min_inc, elm.constraint->usage / elm.consumption_weight);
94       }
95       if (var.bound > 0)
96         min_inc = std::min(min_inc, var.bound - var.value);
97       var.mu    = min_inc;
98       XBT_DEBUG("Updating variable %p maximum increment: %g", &var, var.mu);
99       var.value += var.mu;
100       if (var.value == var.bound)
101         iter = var_list.erase(iter);
102       else
103         iter++;
104     }
105
106     for (auto iter = std::begin(cnst_list); iter != std::end(cnst_list);) {
107       Constraint& cnst = *iter;
108       XBT_DEBUG("Updating cnst %p ", &cnst);
109       if (cnst.sharing_policy) {
110         for (Element& elem : cnst.enabled_element_set) {
111           xbt_assert(elem.variable->sharing_weight > 0);
112           XBT_DEBUG("\tUpdate constraint %p (%g) with variable %p by %g", &cnst, cnst.remaining, elem.variable,
113                     elem.variable->mu);
114           double_update(&cnst.remaining, elem.consumption_weight * elem.variable->mu, sg_maxmin_precision);
115         }
116       } else {
117         for (Element& elem : cnst.enabled_element_set) {
118           xbt_assert(elem.variable->sharing_weight > 0);
119           XBT_DEBUG("\tNon-Shared variable. Update constraint usage of %p (%g) with variable %p by %g", &cnst,
120                     cnst.usage, elem.variable, elem.variable->mu);
121           cnst.usage = std::min(cnst.usage, elem.consumption_weight * elem.variable->mu);
122         }
123         XBT_DEBUG("\tUpdate constraint %p (%g) by %g", &cnst, cnst.remaining, cnst.usage);
124         double_update(&cnst.remaining, cnst.usage, sg_maxmin_precision);
125       }
126
127       XBT_DEBUG("\tRemaining for %p : %g", &cnst, cnst.remaining);
128       if (cnst.remaining <= 0.0) {
129         XBT_DEBUG("\tGet rid of constraint %p", &cnst);
130
131         iter = cnst_list.erase(iter);
132         for (Element& elem : cnst.enabled_element_set) {
133           if (elem.variable->sharing_weight <= 0)
134             break;
135           if (elem.consumption_weight > 0 && elem.variable->saturated_variable_set_hook.is_linked()) {
136             XBT_DEBUG("\t\tGet rid of variable %p", elem.variable);
137             simgrid::xbt::intrusive_erase(var_list, *elem.variable);
138           }
139         }
140       } else {
141         iter++;
142       }
143     }
144   } while (not var_list.empty());
145
146   cnst_list.clear();
147   modified_ = true;
148   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
149     XBT_DEBUG("Fair bottleneck done");
150     print();
151   }
152 }