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

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