From: Martin Quinson Date: Tue, 26 Dec 2017 23:05:42 +0000 (+0100) Subject: move the host list into the Engine X-Git-Tag: v3.19~393 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/1c006a013e83828a40b76b6c652a995b5c2fa552?ds=sidebyside move the host list into the Engine --- diff --git a/include/simgrid/host.h b/include/simgrid/host.h index d839c96423..3c869c33ae 100644 --- a/include/simgrid/host.h +++ b/include/simgrid/host.h @@ -16,8 +16,6 @@ SG_BEGIN_DECL() -XBT_PUBLIC(void) sg_host_exit(); - XBT_PUBLIC(size_t) sg_host_count(); XBT_PUBLIC(sg_host_t *) sg_host_list(); diff --git a/include/simgrid/s4u/Engine.hpp b/include/simgrid/s4u/Engine.hpp index 4b39af125d..795389997a 100644 --- a/include/simgrid/s4u/Engine.hpp +++ b/include/simgrid/s4u/Engine.hpp @@ -56,6 +56,14 @@ public: /** @brief Load a deployment file and launch the actors that it contains */ void loadDeployment(const char* deploy); +protected: + friend s4u::Host; + void addHost(std::string name, simgrid::s4u::Host * host); + void delHost(std::string name); + +public: + simgrid::s4u::Host* hostByName(std::string name); + simgrid::s4u::Host* hostByNameOrNull(std::string name); size_t getHostCount(); void getHostList(std::vector * whereTo); size_t getLinkCount(); diff --git a/src/kernel/EngineImpl.cpp b/src/kernel/EngineImpl.cpp index 779a7b0f91..df3b8939d0 100644 --- a/src/kernel/EngineImpl.cpp +++ b/src/kernel/EngineImpl.cpp @@ -8,13 +8,31 @@ #include "src/kernel/routing/NetPoint.hpp" #include "src/kernel/routing/NetZoneImpl.hpp" +#include + namespace simgrid { namespace kernel { EngineImpl::EngineImpl() = default; EngineImpl::~EngineImpl() { - sg_host_exit(); // Hosts should be part of the engine, at some point + /* copy all names to not modify the map while iterating over it. + * + * Plus, the hosts are destroyed in the lexicographic order to ensure + * that the output is reproducible: we don't want to kill them in the + * pointer order as it could be platform-dependent, which would break + * the tests. + */ + std::vector names; + for (auto const& kv : hosts_) + names.push_back(kv.second->getName()); + + std::sort(names.begin(), names.end()); + + for (auto const& name : names) + hosts_.at(name)->destroy(); + + /* Also delete the other data */ delete netRoot_; for (auto const& kv : netpoints_) delete kv.second; diff --git a/src/kernel/EngineImpl.hpp b/src/kernel/EngineImpl.hpp index 8593bf88da..4e2ef24bd5 100644 --- a/src/kernel/EngineImpl.hpp +++ b/src/kernel/EngineImpl.hpp @@ -3,6 +3,7 @@ /* 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. */ +#include #include #include #include @@ -18,6 +19,7 @@ public: kernel::routing::NetZoneImpl* netRoot_ = nullptr; private: + std::map hosts_; std::unordered_map netpoints_; friend simgrid::s4u::Engine; }; diff --git a/src/s4u/s4u_engine.cpp b/src/s4u/s4u_engine.cpp index 97f2d89bb7..f991b12959 100644 --- a/src/s4u/s4u_engine.cpp +++ b/src/s4u/s4u_engine.cpp @@ -81,19 +81,35 @@ void Engine::loadDeployment(const char *deploy) { SIMIX_launch_application(deploy); } -// FIXME: The following duplicates the content of s4u::Host -extern std::map host_list; /** @brief Returns the amount of hosts in the platform */ size_t Engine::getHostCount() { - return host_list.size(); + return pimpl->hosts_.size(); } /** @brief Fills the passed list with all hosts found in the platform */ void Engine::getHostList(std::vector* list) { - for (auto const& kv : host_list) + for (auto const& kv : pimpl->hosts_) list->push_back(kv.second); } +void Engine::addHost(std::string name, simgrid::s4u::Host* host) +{ + pimpl->hosts_[name] = host; +} +void Engine::delHost(std::string name) +{ + pimpl->hosts_.erase(name); +} +simgrid::s4u::Host* Engine::hostByName(std::string name) +{ + return pimpl->hosts_.at(name); // Will raise a std::out_of_range if the host does not exist +} +simgrid::s4u::Host* Engine::hostByNameOrNull(std::string name) +{ + auto host = pimpl->hosts_.find(name); + return host == pimpl->hosts_.end() ? nullptr : host->second; +} + /** @brief Returns the amount of links in the platform */ size_t Engine::getLinkCount() { diff --git a/src/s4u/s4u_host.cpp b/src/s4u/s4u_host.cpp index 82d6d66ca2..2e25a3d198 100644 --- a/src/s4u/s4u_host.cpp +++ b/src/s4u/s4u_host.cpp @@ -33,8 +33,6 @@ template class Extendable; namespace s4u { -std::map host_list; // FIXME: move it to Engine - simgrid::xbt::signal Host::onCreation; simgrid::xbt::signal Host::onDestruction; simgrid::xbt::signal Host::onStateChange; @@ -44,7 +42,7 @@ Host::Host(const char* name) : name_(name) { xbt_assert(Host::by_name_or_null(name) == nullptr, "Refusing to create a second host named '%s'.", name); - host_list[name_] = this; + Engine::getInstance()->addHost(std::string(name_), this); new simgrid::surf::HostImpl(this); } @@ -72,27 +70,26 @@ void Host::destroy() if (not currentlyDestroying_) { currentlyDestroying_ = true; onDestruction(*this); - host_list.erase(name_); + Engine::getInstance()->delHost(std::string(name_)); delete this; } } Host* Host::by_name(std::string name) { - return host_list.at(name); // Will raise a std::out_of_range if the host does not exist + return Engine::getInstance()->hostByName(name); } Host* Host::by_name(const char* name) { - return host_list.at(std::string(name)); // Will raise a std::out_of_range if the host does not exist + return Engine::getInstance()->hostByName(std::string(name)); } Host* Host::by_name_or_null(const char* name) { - return by_name_or_null(std::string(name)); + return Engine::getInstance()->hostByNameOrNull(std::string(name)); } Host* Host::by_name_or_null(std::string name) { - auto host = host_list.find(name); - return host == host_list.end() ? nullptr : host->second; + return Engine::getInstance()->hostByNameOrNull(name); } Host *Host::current(){ diff --git a/src/simgrid/host.cpp b/src/simgrid/host.cpp index 51da4ba691..c1200be21f 100644 --- a/src/simgrid/host.cpp +++ b/src/simgrid/host.cpp @@ -7,6 +7,7 @@ #include #include "simgrid/host.h" +#include "simgrid/s4u/Engine.hpp" #include "simgrid/s4u/Host.hpp" #include "xbt/Extendable.hpp" #include "xbt/dict.h" @@ -18,39 +19,11 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sg_host, sd, "Logging specific to sg_hosts"); -// FIXME: The following duplicates the content of s4u::Host -namespace simgrid { -namespace s4u { -extern std::map host_list; -} -} - extern "C" { -void sg_host_exit() -{ - /* copy all names to not modify the map while iterating over it. - * - * Plus, the hosts are destroyed in the lexicographic order to ensure - * that the output is reproducible: we don't want to kill them in the - * pointer order as it could be platform-dependent, which would break - * the tests. - */ - std::vector names = std::vector(); - for (auto const& kv : simgrid::s4u::host_list) - names.push_back(kv.second->getName()); - - std::sort(names.begin(), names.end()); - - for (auto const& name : names) - simgrid::s4u::host_list.at(name)->destroy(); - - // host_list.clear(); This would be sufficient if the dict would contain smart_ptr. It's now useless -} - size_t sg_host_count() { - return simgrid::s4u::host_list.size(); + return simgrid::s4u::Engine::getInstance()->getHostCount(); } /** @brief Returns the host list * @@ -64,6 +37,7 @@ size_t sg_host_count() */ sg_host_t *sg_host_list() { xbt_assert(sg_host_count() > 0, "There is no host!"); + return (sg_host_t*)xbt_dynar_to_array(sg_hosts_as_dynar()); } @@ -97,8 +71,10 @@ xbt_dynar_t sg_hosts_as_dynar() { xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t),nullptr); - for (auto const& kv : simgrid::s4u::host_list) { - simgrid::s4u::Host* host = kv.second; + std::vector list; + simgrid::s4u::Engine::getInstance()->getHostList(&list); + + for (auto const& host : list) { if (host && host->pimpl_netpoint && host->pimpl_netpoint->isHost()) xbt_dynar_push(res, &host); } diff --git a/src/smpi/internals/smpi_deployment.cpp b/src/smpi/internals/smpi_deployment.cpp index d5ef6a582b..d470ebdb34 100644 --- a/src/smpi/internals/smpi_deployment.cpp +++ b/src/smpi/internals/smpi_deployment.cpp @@ -7,6 +7,7 @@ #include "SmpiHost.hpp" #include "private.hpp" #include "simgrid/msg.h" /* barrier */ +#include "simgrid/s4u/Engine.hpp" #include "smpi_comm.hpp" #include @@ -35,9 +36,6 @@ public: }; } } -namespace s4u { -extern std::map host_list; -} } using simgrid::smpi::app::Instance; @@ -63,8 +61,9 @@ void SMPI_app_instance_register(const char *name, xbt_main_func_t code, int num_ static int already_called = 0; if (not already_called) { already_called = 1; - for (auto const& item : simgrid::s4u::host_list) { - simgrid::s4u::Host* host = item.second; + std::vector list; + simgrid::s4u::Engine::getInstance()->getHostList(&list); + for (auto const& host : list) { host->extension_set(new simgrid::smpi::SmpiHost(host)); } } diff --git a/src/surf/sg_platf.cpp b/src/surf/sg_platf.cpp index 3b968a4f07..851a89cb13 100644 --- a/src/surf/sg_platf.cpp +++ b/src/surf/sg_platf.cpp @@ -39,13 +39,6 @@ simgrid::xbt::signal on_cluster; } } -// FIXME: The following duplicates the content of s4u::Host -namespace simgrid { -namespace s4u { -extern std::map host_list; -} -} - static int surf_parse_models_setup_already_called = 0; std::map storage_types; @@ -423,8 +416,11 @@ void sg_platf_new_process(sg_platf_process_cbarg_t process) // The requested host does not exist. Do a nice message to the user std::string msg = std::string("Cannot create process '") + process->function + "': host '" + process->host + "' does not exist\nExisting hosts: '"; - for (auto const& kv : simgrid::s4u::host_list) { - simgrid::s4u::Host* host = kv.second; + + std::vector list; + simgrid::s4u::Engine::getInstance()->getHostList(&list); + + for (auto const& host : list) { msg += host->getName(); msg += "', '"; if (msg.length() > 1024) { diff --git a/src/surf/surf_interface.cpp b/src/surf/surf_interface.cpp index b2257a87d9..5960575934 100644 --- a/src/surf/surf_interface.cpp +++ b/src/surf/surf_interface.cpp @@ -321,7 +321,6 @@ void surf_exit() { TRACE_end(); /* Just in case it was not called by the upper layer (or there is no upper layer) */ - sg_host_exit(); sg_link_exit(); for (auto const& e : storage_types) { simgrid::surf::StorageType* stype = e.second;