Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move the host list into the Engine
authorMartin Quinson <martin.quinson@loria.fr>
Tue, 26 Dec 2017 23:05:42 +0000 (00:05 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Tue, 26 Dec 2017 23:22:10 +0000 (00:22 +0100)
include/simgrid/host.h
include/simgrid/s4u/Engine.hpp
src/kernel/EngineImpl.cpp
src/kernel/EngineImpl.hpp
src/s4u/s4u_engine.cpp
src/s4u/s4u_host.cpp
src/simgrid/host.cpp
src/smpi/internals/smpi_deployment.cpp
src/surf/sg_platf.cpp
src/surf/surf_interface.cpp

index d839c96..3c869c3 100644 (file)
@@ -16,8 +16,6 @@
 
 SG_BEGIN_DECL()
 
 
 SG_BEGIN_DECL()
 
-XBT_PUBLIC(void) sg_host_exit();
-
 XBT_PUBLIC(size_t) sg_host_count();
 XBT_PUBLIC(sg_host_t *) sg_host_list();
 
 XBT_PUBLIC(size_t) sg_host_count();
 XBT_PUBLIC(sg_host_t *) sg_host_list();
 
index 4b39af1..7953899 100644 (file)
@@ -56,6 +56,14 @@ public:
   /** @brief Load a deployment file and launch the actors that it contains */
   void loadDeployment(const char* deploy);
 
   /** @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<Host*> * whereTo);
   size_t getLinkCount();
   size_t getHostCount();
   void getHostList(std::vector<Host*> * whereTo);
   size_t getLinkCount();
index 779a7b0..df3b893 100644 (file)
@@ -8,13 +8,31 @@
 #include "src/kernel/routing/NetPoint.hpp"
 #include "src/kernel/routing/NetZoneImpl.hpp"
 
 #include "src/kernel/routing/NetPoint.hpp"
 #include "src/kernel/routing/NetZoneImpl.hpp"
 
+#include <algorithm>
+
 namespace simgrid {
 namespace kernel {
 
 EngineImpl::EngineImpl() = default;
 EngineImpl::~EngineImpl()
 {
 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<std::string> 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;
   delete netRoot_;
   for (auto const& kv : netpoints_)
     delete kv.second;
index 8593bf8..4e2ef24 100644 (file)
@@ -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. */
 
 /* 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 <map>
 #include <simgrid/s4u/NetZone.hpp>
 #include <simgrid/s4u/forward.hpp>
 #include <string>
 #include <simgrid/s4u/NetZone.hpp>
 #include <simgrid/s4u/forward.hpp>
 #include <string>
@@ -18,6 +19,7 @@ public:
   kernel::routing::NetZoneImpl* netRoot_ = nullptr;
 
 private:
   kernel::routing::NetZoneImpl* netRoot_ = nullptr;
 
 private:
+  std::map<std::string, simgrid::s4u::Host*> hosts_;
   std::unordered_map<std::string, simgrid::kernel::routing::NetPoint*> netpoints_;
   friend simgrid::s4u::Engine;
 };
   std::unordered_map<std::string, simgrid::kernel::routing::NetPoint*> netpoints_;
   friend simgrid::s4u::Engine;
 };
index 97f2d89..f991b12 100644 (file)
@@ -81,19 +81,35 @@ void Engine::loadDeployment(const char *deploy)
 {
   SIMIX_launch_application(deploy);
 }
 {
   SIMIX_launch_application(deploy);
 }
-// FIXME: The following duplicates the content of s4u::Host
-extern std::map<std::string, simgrid::s4u::Host*> host_list;
 /** @brief Returns the amount of hosts in the platform */
 size_t Engine::getHostCount()
 {
 /** @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<Host*>* list)
 {
 }
 /** @brief Fills the passed list with all hosts found in the platform */
 void Engine::getHostList(std::vector<Host*>* list)
 {
-  for (auto const& kv : host_list)
+  for (auto const& kv : pimpl->hosts_)
     list->push_back(kv.second);
 }
     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()
 {
 /** @brief Returns the amount of links in the platform */
 size_t Engine::getLinkCount()
 {
index 82d6d66..2e25a3d 100644 (file)
@@ -33,8 +33,6 @@ template class Extendable<simgrid::s4u::Host>;
 
 namespace s4u {
 
 
 namespace s4u {
 
-std::map<std::string, simgrid::s4u::Host*> host_list; // FIXME: move it to Engine
-
 simgrid::xbt::signal<void(Host&)> Host::onCreation;
 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
 simgrid::xbt::signal<void(Host&)> Host::onStateChange;
 simgrid::xbt::signal<void(Host&)> Host::onCreation;
 simgrid::xbt::signal<void(Host&)> Host::onDestruction;
 simgrid::xbt::signal<void(Host&)> 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);
   : 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);
 }
 
   new simgrid::surf::HostImpl(this);
 }
 
@@ -72,27 +70,26 @@ void Host::destroy()
   if (not currentlyDestroying_) {
     currentlyDestroying_ = true;
     onDestruction(*this);
   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)
 {
     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)
 {
 }
 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)
 {
 }
 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)
 {
 }
 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(){
 }
 
 Host *Host::current(){
index 51da4ba..c1200be 100644 (file)
@@ -7,6 +7,7 @@
 #include <vector>
 
 #include "simgrid/host.h"
 #include <vector>
 
 #include "simgrid/host.h"
+#include "simgrid/s4u/Engine.hpp"
 #include "simgrid/s4u/Host.hpp"
 #include "xbt/Extendable.hpp"
 #include "xbt/dict.h"
 #include "simgrid/s4u/Host.hpp"
 #include "xbt/Extendable.hpp"
 #include "xbt/dict.h"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sg_host, sd, "Logging specific to sg_hosts");
 
 
 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<std::string, simgrid::s4u::Host*> host_list;
-}
-}
-
 extern "C" {
 
 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<std::string> names = std::vector<std::string>();
-  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()
 {
 size_t sg_host_count()
 {
-  return simgrid::s4u::host_list.size();
+  return simgrid::s4u::Engine::getInstance()->getHostCount();
 }
 /** @brief Returns the host list
  *
 }
 /** @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!");
  */
 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());
 }
 
   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);
 
 {
   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<simgrid::s4u::Host*> 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);
   }
     if (host && host->pimpl_netpoint && host->pimpl_netpoint->isHost())
       xbt_dynar_push(res, &host);
   }
index d5ef6a5..d470ebd 100644 (file)
@@ -7,6 +7,7 @@
 #include "SmpiHost.hpp"
 #include "private.hpp"
 #include "simgrid/msg.h" /* barrier */
 #include "SmpiHost.hpp"
 #include "private.hpp"
 #include "simgrid/msg.h" /* barrier */
+#include "simgrid/s4u/Engine.hpp"
 #include "smpi_comm.hpp"
 #include <map>
 
 #include "smpi_comm.hpp"
 #include <map>
 
@@ -35,9 +36,6 @@ public:
 };
 }
 }
 };
 }
 }
-namespace s4u {
-extern std::map<std::string, simgrid::s4u::Host*> host_list;
-}
 }
 
 using simgrid::smpi::app::Instance;
 }
 
 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;
   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<simgrid::s4u::Host*> list;
+    simgrid::s4u::Engine::getInstance()->getHostList(&list);
+    for (auto const& host : list) {
       host->extension_set(new simgrid::smpi::SmpiHost(host));
     }
   }
       host->extension_set(new simgrid::smpi::SmpiHost(host));
     }
   }
index 3b968a4..851a89c 100644 (file)
@@ -39,13 +39,6 @@ simgrid::xbt::signal<void(ClusterCreationArgs*)> on_cluster;
 }
 }
 
 }
 }
 
-// FIXME: The following duplicates the content of s4u::Host
-namespace simgrid {
-namespace s4u {
-extern std::map<std::string, simgrid::s4u::Host*> host_list;
-}
-}
-
 static int surf_parse_models_setup_already_called = 0;
 std::map<std::string, simgrid::surf::StorageType*> storage_types;
 
 static int surf_parse_models_setup_already_called = 0;
 std::map<std::string, simgrid::surf::StorageType*> 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: '";
     // 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<simgrid::s4u::Host*> list;
+    simgrid::s4u::Engine::getInstance()->getHostList(&list);
+
+    for (auto const& host : list) {
       msg += host->getName();
       msg += "', '";
       if (msg.length() > 1024) {
       msg += host->getName();
       msg += "', '";
       if (msg.length() > 1024) {
index b2257a8..5960575 100644 (file)
@@ -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) */
 
 {
   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;
   sg_link_exit();
   for (auto const& e : storage_types) {
     simgrid::surf::StorageType* stype = e.second;