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

Private GIT Repository
c372f74da2a508d8ac3c7d677cd93242075e339e
[loba.git] / loba_fairstrategy.cpp
1 #include <algorithm>
2 #include <xbt/log.h>
3
4 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(loba);
5
6 #include "loba_fairstrategy.h"
7
8 /* simple version:
9  *   load balance with a least-loaded neighbor,
10  *   without breaking the ping-pong condition
11  */
12
13 class compare {
14 public:
15     bool operator()(const neighbor*a, const neighbor*b) {
16         return a->get_load() > b->get_load();
17     }
18 };
19
20 double loba_fairstrategy::load_balance(double my_load)
21 {
22     std::sort(pneigh.begin(), pneigh.end(), compare());
23
24     print_loads_p(false, xbt_log_priority_debug);
25
26     double sum_sent = 0;
27     bool found = true;
28
29     while (found) {
30         found = false;
31         for (unsigned i = 0 ; i < pneigh.size() ; ++i) {
32             if (pneigh[i]->get_load() <= my_load - 2) {
33                 found = true;
34                 pneigh[i]->add_load(1);
35                 pneigh[i]->add_to_send(1);
36                 DEBUG1("sent to %s", pneigh[i]->get_name());
37                 my_load--;
38                 sum_sent++;
39             }
40         }
41     }
42
43     return sum_sent;
44 }
45
46 // Local variables:
47 // mode: c++
48 // End: