Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
split a function in two and kill it
[simgrid.git] / src / smpi / plugins / load_balancer / load_balancer.hpp
1
2 #ifndef HAVE_SG_PLUGIN_LB
3 #define HAVE_SG_PLUGIN_LB
4
5 #include <simgrid/s4u.hpp>
6
7 namespace simgrid {
8 namespace plugin {
9 namespace loadbalancer {
10
11 class XBT_PRIVATE Mapping {
12 public:
13   Mapping() = default;
14   ~Mapping() = default;
15   /** Each host can have an arbitrary number of actors -> multimap **/
16   typedef std::unordered_multimap<simgrid::s4u::Host*, simgrid::s4u::ActorPtr> host_to_actors_map_t;
17   host_to_actors_map_t host_to_actors;
18
19   /** Each actor gets assigned to exactly one host -> map **/
20   std::map<simgrid::s4u::ActorPtr, simgrid::s4u::Host*> actor_to_host;
21
22   void assign(simgrid::s4u::ActorPtr actor, simgrid::s4u::Host* host)
23   {
24     /* Remove "actor" from its old host -> get all elements that have the current host as key **/
25     auto range = host_to_actors.equal_range(/* current host */actor_to_host[actor]);
26     for (auto it = range.first; it != range.second; it++) {
27       if (it->second == actor) {
28         host_to_actors.erase(it); // unassign this actor
29         break;
30       }
31     }
32
33     actor_to_host[actor] = host;
34     host_to_actors.insert({host, actor});
35   }
36
37   simgrid::s4u::Host* get_host(simgrid::s4u::ActorPtr actor) { return actor_to_host[actor]; }
38
39   unsigned int count_actors(simgrid::s4u::Host* host)
40   {
41     return host_to_actors.count(host); // TODO This is linear in the size of the map. Maybe replace by constant lookup through another map?
42   }
43
44   void for_each_actor(simgrid::s4u::Host* host, std::function<void(simgrid::s4u::ActorPtr)> callback)
45   {
46     auto range = host_to_actors.equal_range(host);
47     std::for_each(
48         range.first,
49         range.second,
50         [&callback](host_to_actors_map_t::value_type& x) { callback(x.second); }
51     );
52   }
53 };
54
55 class XBT_PRIVATE LoadBalancer
56 {
57   Mapping new_mapping;
58   std::map</*proc id*/int, double> actor_computation;
59
60 public:
61   LoadBalancer();
62   ~LoadBalancer();
63   void run();
64   void assign(simgrid::s4u::ActorPtr actor, simgrid::s4u::Host* host);
65   
66   /**
67    * FIXME These are functions used for testing and should be re-written or removed
68    */
69   simgrid::s4u::Host* get_mapping();
70   void record_actor_computation(simgrid::s4u::ActorPtr actor, double load);
71 private:
72 };
73
74 }
75 }
76 }
77 #endif