From c602f438078b5a9c7f0e0c84407892e7016d0933 Mon Sep 17 00:00:00 2001 From: Christian Heinrich Date: Thu, 28 Jun 2018 14:08:22 +0200 Subject: [PATCH] [SMPI/LB] Add files load_balancer.hpp and smpi/sampi.h --- .../smpi/loadbalancer/load_balancer.hpp | 75 +++++++++++++++++++ include/smpi/sampi.h | 32 ++++++++ 2 files changed, 107 insertions(+) create mode 100644 include/simgrid/smpi/loadbalancer/load_balancer.hpp create mode 100644 include/smpi/sampi.h diff --git a/include/simgrid/smpi/loadbalancer/load_balancer.hpp b/include/simgrid/smpi/loadbalancer/load_balancer.hpp new file mode 100644 index 0000000000..37606ba54d --- /dev/null +++ b/include/simgrid/smpi/loadbalancer/load_balancer.hpp @@ -0,0 +1,75 @@ + +#ifndef HAVE_SG_PLUGIN_LB +#define HAVE_SG_PLUGIN_LB + +namespace simgrid { +namespace plugin { +namespace loadbalancer { + +class Mapping { +public: + Mapping() = default; + ~Mapping() = default; + /** Each host can have an arbitrary number of actors -> multimap **/ + typedef std::unordered_multimap host_to_actors_map_t; + host_to_actors_map_t host_to_actors; + + /** Each actor gets assigned to exactly one host -> map **/ + std::map actor_to_host; + + void assign(simgrid::s4u::ActorPtr actor, simgrid::s4u::Host* host) + { + /* Remove "actor" from its old host -> get all elements that have the current host as key **/ + auto range = host_to_actors.equal_range(/* current host */actor_to_host[actor]); + for (auto it = range.first; it != range.second; it++) { + if (it->second == actor) { + host_to_actors.erase(it); // unassign this actor + break; + } + } + + actor_to_host[actor] = host; + host_to_actors.insert({host, actor}); + } + + simgrid::s4u::Host* get_host(simgrid::s4u::ActorPtr actor) { return actor_to_host[actor]; } + + unsigned int count_actors(simgrid::s4u::Host* host) + { + return host_to_actors.count(host); // TODO This is linear in the size of the map. Maybe replace by constant lookup through another map? + } + + void for_each_actor(simgrid::s4u::Host* host, std::function callback) + { + auto range = host_to_actors.equal_range(host); + std::for_each( + range.first, + range.second, + [&callback](host_to_actors_map_t::value_type& x) { callback(x.second); } + ); + } +}; + +class LoadBalancer +{ + std::map actor_computation; + std::map new_mapping; + +public: + LoadBalancer(); + ~LoadBalancer(); + void run(); + void assign(simgrid::s4u::ActorPtr actor, simgrid::s4u::Host* host); + + /** + * FIXME These are functions used for testing and should be re-written or removed + */ + simgrid::s4u::Host* get_mapping(); + void record_actor_computation(simgrid::s4u::ActorPtr actor, double load); +private: +}; + +} +} +} +#endif diff --git a/include/smpi/sampi.h b/include/smpi/sampi.h new file mode 100644 index 0000000000..bdbaa98f4c --- /dev/null +++ b/include/smpi/sampi.h @@ -0,0 +1,32 @@ +/* Copyright (c) 2007-2018. 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. */ + +#ifndef SAMPI_H_ +#define SAMPI_H_ + +#include "malloc.h" +#include + +#define AMPI_CALL(type, name, args) \ + type A##name args __attribute__((weak)); \ + type AP##name args; + +#ifndef HAVE_SMPI +#define malloc(nbytes) _sampi_malloc(nbytes) +#define free(ptr) _sampi_free(ptr) +#endif + +SG_BEGIN_DECL() + +XBT_PUBLIC void* _sampi_malloc(size_t size); +XBT_PUBLIC void _sampi_free(void* ptr); + +AMPI_CALL(XBT_PUBLIC int, MPI_Iteration_in, (MPI_Comm comm)) +AMPI_CALL(XBT_PUBLIC int, MPI_Iteration_out, (MPI_Comm comm)) +AMPI_CALL(XBT_PUBLIC void, MPI_Migrate, (MPI_Comm comm)) + +SG_END_DECL() + +#endif -- 2.20.1