+#include <cmath> // std::isfinite
+#include <functional>
+#include <numeric>
+#include <xbt/log.h>
+
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(loba);
+
+#include "loba_2besteffort.h"
+
+void loba_2besteffort::load_balance()
+{
+ using std::placeholders::_1;
+ using std::placeholders::_2;
+
+ pneigh_sort_by_load(std::less<double>());
+ print_loads_p(false, xbt_log_priority_debug);
+
+ double sum = get_load() +
+ std::accumulate(pneigh.begin(), pneigh.end(), 0.0,
+ std::bind(std::plus<double>(), _1,
+ std::bind(&neighbor::get_load, _2)));
+ double mean = sum / (pneigh.size() + 1);
+ XBT_DEBUG("sum = %g ; mean = %g", sum, mean);
+
+ if (!std::isfinite(mean))
+ return;
+
+ for (unsigned i = 0 ; i < pneigh.size() ; ++i) {
+ double transfer = std::min(mean - pneigh[i]->get_load(),
+ get_load() - mean);
+ // don't continue if get_load() <= mean, or pneigh[i]->get_load >= mean
+ if (transfer <= 0.0)
+ break;
+ send(pneigh[i], transfer);
+ XBT_DEBUG("sent %g to %s", transfer, pneigh[i]->get_name());
+ }
+}
+
+// Local variables:
+// mode: c++
+// End: