Logo AND Algorithmique Numérique Distribuée

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