Logo AND Algorithmique Numérique Distribuée

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