Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / src / smpi / plugins / load_balancer / LoadBalancer.cpp
index e0fc633..2c93726 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2006-2018. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2006-2020. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -18,15 +18,16 @@ namespace simgrid {
 namespace plugin {
 namespace loadbalancer {
 
-struct XBT_PRIVATE compare_hosts {
-  bool operator()(const simgrid::s4u::Host* a, const simgrid::s4u::Host* b) const;
+class XBT_PRIVATE compare_hosts {
+public:
+  bool operator()(simgrid::s4u::Host* const a, simgrid::s4u::Host* const b) const;
 };
 
 typedef boost::heap::fibonacci_heap<simgrid::s4u::Host*, boost::heap::compare<compare_hosts>>::handle_type heap_handle;
 
 /**
- * Structure that imitates a std::pair, but it allows us 
- * to use meaningful names instead of .first and .second 
+ * Structure that imitates a std::pair, but it allows us
+ * to use meaningful names instead of .first and .second
  */
 struct XBT_PRIVATE pair_handle_load
 {
@@ -34,27 +35,18 @@ struct XBT_PRIVATE pair_handle_load
   double load;
 };
 
-static std::map<const simgrid::s4u::Host*, pair_handle_load> additional_load;
+static std::map<simgrid::s4u::Host* const, pair_handle_load> additional_load;
 
-bool compare_hosts::operator()(const simgrid::s4u::Host* a, const simgrid::s4u::Host* b) const {
-  return /*sg_host_get_avg_load(a) +*/ additional_load[a].load > /*sg_host_get_avg_load(b) +*/ additional_load[b].load;
+bool compare_hosts::operator()(simgrid::s4u::Host* const a, simgrid::s4u::Host* const b) const {
+  return additional_load[a].load > additional_load[b].load;
 }
 
 
-LoadBalancer::LoadBalancer()
-{
-}
-
-LoadBalancer::~LoadBalancer()
-{
-}
-
 void LoadBalancer::run()
 {
   simgrid::s4u::Engine* engine                     = simgrid::s4u::Engine::get_instance();
-  std::vector<simgrid::s4u::Host*> available_hosts = engine->get_filtered_hosts([](simgrid::s4u::Host* host) {
-    return not host->is_off();
-  });
+  std::vector<simgrid::s4u::Host*> available_hosts =
+      engine->get_filtered_hosts([](const simgrid::s4u::Host* host) { return host->is_on(); });
   xbt_assert(available_hosts.size() > 0, "No hosts available; are they all switched off?");
 
   // TODO: Account for daemon background load (-> use especially the availability file)
@@ -62,7 +54,7 @@ void LoadBalancer::run()
   std::vector<simgrid::s4u::ActorPtr> all_actors =
       engine->get_filtered_actors([](simgrid::s4u::ActorPtr actor) { return not actor->is_daemon(); });
 
-  for (auto& actor : all_actors) {
+  for (auto const& actor : all_actors) {
     new_mapping.assign(actor, actor->get_host());
   }
   // Sort the actors, from highest to lowest load; we then just iterate over these actors
@@ -82,16 +74,19 @@ void LoadBalancer::run()
   for (auto& host : available_hosts) {
     std::vector<simgrid::s4u::ActorPtr> actors = host->get_all_actors();
     heap_handle update_handle                  = usable_hosts.push(host); // Required to update elements in the heap
-    additional_load[host]                      = {update_handle, 0}; // Save the handle for later
-    for (auto& actor : actors) {
-      additional_load[host].load += actor_computation[actor->get_pid()];
+    additional_load[host]                      = {update_handle, 0};      // Save the handle for later
+    const double total_flops_computed          = sg_host_get_computed_flops(host);
+    for (auto const& actor : actors) {
+      additional_load[host].load += actor_computation[actor->get_pid()] / total_flops_computed; // Normalize load - this allows comparison
+                                                                                                // even between hosts with different frequencies
       XBT_DEBUG("Actor %li -> %f", actor->get_pid(), actor_computation[actor->get_pid()]);
     }
+    usable_hosts.increase(update_handle);
     XBT_DEBUG("Host %s initialized to %f", host->get_cname(), additional_load[host].load);
   }
 
   // Implementation of the Greedy algorithm
-  for (auto& actor : all_actors) {
+  for (auto const& actor : all_actors) {
     simgrid::s4u::Host* target_host = usable_hosts.top(); // This is the host with the lowest load
 
     simgrid::s4u::Host* cur_mapped_host = new_mapping.get_host(actor);
@@ -136,14 +131,14 @@ void LoadBalancer::run()
   }
 }
 
-simgrid::s4u::Host* LoadBalancer::get_mapping()
+simgrid::s4u::Host* LoadBalancer::get_mapping(simgrid::s4u::ActorPtr actor)
 {
-  return new_mapping.get_host(simgrid::s4u::Actor::self());
+  return new_mapping.get_host(actor);
 }
 
-void LoadBalancer::record_actor_computation(simgrid::s4u::ActorPtr actor, double load)
+void LoadBalancer::record_actor_computation(simgrid::s4u::Actor const& actor, double load)
 {
-  actor_computation[actor->get_pid()] += load;
+  actor_computation[actor.get_pid()] += load;
 }
 }
 }