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

Private GIT Repository
Update TODO.
[loba.git] / loba_makhoul.cpp
index 67a2359c569cf18f669ac17193125e0dfe2cdb36..2fca60b5ba5f18d1f8dc9782e49acdd0ebde6114 100644 (file)
@@ -1,30 +1,39 @@
-#include <algorithm>
 #include <xbt/log.h>
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(loba);
 
+#include <algorithm>
+#include <tr1/functional>
 #include "loba_makhoul.h"
 
-class compare {
-public:
-    bool operator()(const neighbor*a, const neighbor*b) {
-        return a->get_load() < b->get_load();
-    }
-};
-
 void loba_makhoul::load_balance()
 {
-    std::sort(pneigh.begin(), pneigh.end(), compare());
+    using std::tr1::bind;
+    using std::tr1::placeholders::_1;
+
+    pneigh_sort_by_load(std::less<double>());
 
     print_loads_p(false, xbt_log_priority_debug);
 
+    // find limit between least loaded neighbors, and more loaded ones
+    pneigh_type::iterator bound =
+        std::find_if(pneigh.begin(), pneigh.end(),
+                     bind(std::less_equal<double>(),
+                          get_load(), bind(&neighbor::get_load, _1)));
+
+    // compute load differences
+    std::vector<double> delta(bound - pneigh.begin());
+    std::transform(pneigh.begin(), bound, delta.begin(),
+                   bind(std::minus<double>(),
+                        get_load(), bind(&neighbor::get_load, _1)));
+
+    // send load...
     double alpha = 1.0 / (pneigh.size() + 1.0);
-    double delta;
     for (unsigned i = 0 ;
-         i < pneigh.size() &&
-             (delta = get_load() - pneigh[i]->get_load()) > 0.0 ;
-         ++i) {
-        send(pneigh[i], delta * alpha);
+         i < delta.size() && pneigh[i]->get_load() < get_load() ; ++i) {
+        double balance = delta[i] * alpha;
+        send(pneigh[i], balance);
+        XBT_DEBUG("sent %g to %s", balance, pneigh[i]->get_name());
     }
 }