Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
introduce simgrid_get_all_hosts
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Fri, 7 Feb 2020 10:04:44 +0000 (11:04 +0100)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Fri, 7 Feb 2020 10:04:44 +0000 (11:04 +0100)
This is a replacement to sg_hosts_as_dynar.
It allocates and builds the list of all the hosts seen by the Engine,
sorted by host names. Deallocating the memory is up to the user.

include/simgrid/engine.h
src/s4u/s4u_Engine.cpp

index 95b9ae4..87d337a 100644 (file)
@@ -7,6 +7,7 @@
 #define INCLUDE_SIMGRID_ENGINE_H_
 
 #include <simgrid/forward.h>
+#include <stddef.h>
 
 SG_BEGIN_DECL /* C interface */
 /** Initialize the SimGrid engine, taking the command line parameters of your main function. */
@@ -50,6 +51,8 @@ XBT_PUBLIC int simgrid_get_actor_count();
  */
 XBT_PUBLIC void sg_config_continue_after_help();
 
+XBT_PUBLIC void simgrid_get_all_hosts(size_t* host_count, sg_host_t** hosts);
+
 SG_END_DECL
 
 #endif /* INCLUDE_SIMGRID_ENGINE_H_ */
index c30e3ef..65ef3a1 100644 (file)
@@ -23,6 +23,7 @@
 #include "surf/surf.hpp" // routing_platf. FIXME:KILLME. SOON
 #include <simgrid/Exception.hpp>
 
+#include <algorithm>
 #include <string>
 
 XBT_LOG_NEW_CATEGORY(s4u, "Log channels of the S4U (Simgrid for you) interface");
@@ -438,3 +439,20 @@ int simgrid_get_actor_count()
 {
   return simgrid::s4u::Engine::get_instance()->get_actor_count();
 }
+
+void simgrid_get_all_hosts(size_t* host_count, sg_host_t** hosts)
+{
+  simgrid::s4u::Engine* e               = simgrid::s4u::Engine::get_instance();
+  *host_count                           = e->get_host_count();
+  std::vector<simgrid::s4u::Host*> list = e->get_all_hosts();
+
+  auto last = std::remove_if(begin(list), end(list), [](const simgrid::s4u::Host* host) {
+    return not host || not host->get_netpoint() || not host->get_netpoint()->is_host();
+  });
+  std::sort(begin(list), last,
+            [](const simgrid::s4u::Host* a, const simgrid::s4u::Host* b) { return a->get_name() < b->get_name(); });
+
+  *hosts = static_cast<sg_host_t*>(xbt_malloc(sizeof(sg_host_t) * (*host_count)));
+  for (size_t i = 0; i < *host_count; i++)
+    (*hosts)[i] = list[i];
+}