Logo AND Algorithmique Numérique Distribuée

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