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

Private GIT Repository
timestamp: add possibility to choose clock type.
[loba.git] / loba_bulk.cpp
1 #include <xbt/log.h>
2 #include <math.h>
3
4 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(loba);
5
6 #include "loba_bulk.h"
7
8 void loba_bulk::load_balance()
9 {
10         float myLoad = get_load();
11
12         unsigned NbNwMinLoad = 0;
13         unsigned NbNwLowerLoad = 0;
14         unsigned NbNeighbours = pneigh.size();
15
16         double *S = new double[NbNeighbours];
17         for (unsigned i = 0; i < NbNeighbours; i++) 
18                 S[i] = 0.0;
19         //What is the minimum and maximum load under myLoad?
20         float minLoad = myLoad;
21         for (unsigned i = 0; i < NbNeighbours; i++) {
22                 if (pneigh[i]->get_load() < minLoad) {
23                         minLoad = pneigh[i]->get_load();
24                 }
25                 if (pneigh[i]->get_load() < myLoad) {
26                         NbNwLowerLoad++;
27                 }
28         }
29         for (unsigned i = 0; i < NbNeighbours; i++)
30                 if (pneigh[i]->get_load() == minLoad)
31                         NbNwMinLoad++;
32
33         float maxLoad = minLoad;
34         for (unsigned i = 0; i < NbNeighbours; i++) {
35                 if (pneigh[i]->get_load() > minLoad && pneigh[i]->get_load() < myLoad)
36                         if (maxLoad < pneigh[i]->get_load())
37                                 maxLoad = pneigh[i]->get_load();
38         }
39
40         double alpha = 0.0;
41
42         if (NbNwLowerLoad && NbNwMinLoad < NbNwLowerLoad) //There is one or many neighbors with minimum load but not all neighbors have minimum load
43                 alpha = (1. / ((double) NbNwMinLoad + 2));
44         if (NbNwMinLoad == NbNwLowerLoad) //All neighbors have minimum load
45                 alpha = (1. / ((double) NbNwMinLoad + 1));
46         float myS = 0.;
47         //There exist underloaded neighbors
48         if (NbNwMinLoad && myLoad != 0.0) {
49                 for (unsigned i = 0; i < NbNeighbours; i++) {
50                         if (pneigh[i]->get_load() == minLoad) {
51                                 S[i] = floor(alpha * (myLoad - minLoad));
52                                 myS +=  S[i];
53                         } else {
54                                 if (pneigh[i]->get_load() < myLoad) {
55                                         S[i] = floor(alpha * (myLoad - pneigh[i]->get_load()));
56                                         myS +=  S[i];
57                                 }
58                         }
59                 }
60                 //Check assumption 4.2 (b) page 520
61                 bool HaveToCorrectS = false;
62                 for (unsigned i = 0; i < NbNeighbours; i++) {
63                         if (pneigh[i]->get_load() < myLoad) { //
64                                 //Condition 4.6
65                                 if ((myLoad - myS) < (pneigh[i]->get_load() + S[i])) {
66                                         HaveToCorrectS = true;
67                                 }
68                         }
69                 }
70                 if (HaveToCorrectS) {
71                                 for (unsigned i = 0; i < NbNeighbours; i++) {
72                                         while (((myLoad - myS) < pneigh[i]->get_load() + S[i]) && (S[i] > 0)) {
73                                                 myS -= 1.0;
74                                                 S[i] -= 1.0;
75                                         }
76                                 }
77                 }
78         }//End there are underloaded neighbors;
79         for (unsigned i = 0 ; i < NbNeighbours ; i++) {
80                 send(pneigh[i], S[i]);
81                 XBT_DEBUG("sent to %s", pneigh[i]->get_name());
82         }
83
84         delete[] S;
85 }
86
87 // Local variables:
88 // mode: c++
89 // End: