Logo AND Algorithmique Numérique Distribuée

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