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

Private GIT Repository
modification to fairstrategy
[loba.git] / loba_fairstrategy.cpp
1 #include "loba_fairstrategy.h"
2 #include <algorithm>
3 #include <xbt/log.h>
4
5 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(loba);
6
7 /* simple version:
8  *   load balance with a least-loaded neighbor,
9  *   without breaking the ping-pong condition
10  */
11
12 class compare {
13 public : 
14         bool operator()(const neighbor*a, const neighbor*b)     {
15                 return a->get_load()>b->get_load();
16         }
17 };
18
19 double loba_fairstrategy::load_balance(double my_load)
20 {
21     int imin = -1;
22     int imax = -1;
23     double min = my_load;
24     double max = -1.0;
25
26                 std::sort(pneigh.begin(),pneigh.end(),compare());
27
28                 //print_loads();
29
30                 INFO0("list voisins");
31                 for(unsigned i=0;i< pneigh.size() ; ++i) {
32                         INFO2("%s %f ", pneigh[i]->get_name(), pneigh[i]->get_load());
33                 }
34
35                 double sum_sent=0;
36                 bool found=true;
37
38                 while(found) {
39                         found=false;
40                         for (unsigned i = 0 ; i < pneigh.size() ; ++i) {
41         double l = pneigh[i]->get_load();
42         if (l >= my_load)
43                                         continue;
44                                 if(l<my_load+2) {
45                                         found=true;
46                                         pneigh[i]->set_load(l+1);
47                                         pneigh[i]->add_to_send(1);
48                                         INFO1("sent to %s",pneigh[i]->get_name());
49                                         my_load--;
50                                         sum_sent++;
51                                 }
52                         }
53                 }
54
55
56                 
57                 return sum_sent;
58     
59 }
60
61 // Local variables:
62 // mode: c++
63 // End: