]> AND Private Git Repository - loba.git/blob - loba_2besteffort.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Version 0.4.
[loba.git] / loba_2besteffort.cpp
1 #include <cmath>                // std::isfinite
2 #include <numeric>
3 #include <xbt/log.h>
4
5 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(loba);
6
7 #include "loba_2besteffort.h"
8
9 void loba_2besteffort::load_balance()
10 {
11     pneigh_sort_by_load(std::less<double>());
12     print_loads_p(false, xbt_log_priority_debug);
13
14     double sum = get_load() +
15         std::accumulate(pneigh.begin(), pneigh.end(), 0.0,
16                         [](double x, const neighbor* n) {
17                             return x + n->get_load();
18                         });
19     double mean = sum / (pneigh.size() + 1);
20     XBT_DEBUG("sum = %g ; mean = %g", sum, mean);
21
22     if (!std::isfinite(mean))
23         return;
24
25     for (unsigned i = 0 ; i < pneigh.size() ; ++i) {
26         double transfer = std::min(mean - pneigh[i]->get_load(),
27                                    get_load() - mean);
28         // don't continue if get_load() <= mean, or pneigh[i]->get_load >= mean
29         if (transfer <= 0.0)
30             break;
31         if (opt::loba_best_divisor)
32             transfer /= opt::loba_best_divisor;
33         send(pneigh[i], transfer);
34         XBT_DEBUG("sent %g to %s", transfer, pneigh[i]->get_name());
35     }
36 }
37
38 // Local variables:
39 // mode: c++
40 // End: