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

Private GIT Repository
Distinguish lb_iter and comp_iter.
[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();
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             double l = pneigh[i]->get_load();
33             if (l >= my_load)
34                 continue;
35             if (l < my_load+2) {
36                 found=true;
37                 pneigh[i]->add_load(1);
38                 pneigh[i]->add_to_send(1);
39                 INFO1("sent to %s",pneigh[i]->get_name());
40                 my_load--;
41                 sum_sent++;
42             }
43         }
44     }
45
46     return sum_sent;
47 }
48
49 // Local variables:
50 // mode: c++
51 // End: