Logo AND Algorithmique Numérique Distribuée

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