From fe14db8f94df5cc46ba2f2a5d688a1b0b763ec29 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Fri, 9 Mar 2018 15:37:11 +0100 Subject: [PATCH] stop using sg_host_list() from C++, and improve its implementation --- .../s4u-platform-properties.cpp | 6 +++--- src/simdag/sd_dotloader.cpp | 8 +++++--- src/simgrid/host.cpp | 7 ++++++- src/surf/plugins/host_energy.cpp | 14 +++++++------- teshsuite/s4u/comm-pt2pt/comm-pt2pt.cpp | 5 +++-- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/examples/s4u/platform-properties/s4u-platform-properties.cpp b/examples/s4u/platform-properties/s4u-platform-properties.cpp index 307a941853..dabffc1553 100644 --- a/examples/s4u/platform-properties/s4u-platform-properties.cpp +++ b/examples/s4u/platform-properties/s4u-platform-properties.cpp @@ -106,10 +106,10 @@ int main(int argc, char* argv[]) size_t totalHosts = sg_host_count(); XBT_INFO("There are %zu hosts in the environment", totalHosts); - simgrid::s4u::Host** hosts = sg_host_list(); - for (unsigned int i = 0; i < totalHosts; i++) + std::vector hosts; + e.getHostList(&hosts); + for (unsigned int i = 0; i < hosts.size(); i++) XBT_INFO("Host '%s' runs at %.0f flops/s", hosts[i]->getCname(), hosts[i]->getSpeed()); - xbt_free(hosts); e.loadDeployment(argv[2]); e.run(); diff --git a/src/simdag/sd_dotloader.cpp b/src/simdag/sd_dotloader.cpp index ee34cab0de..4979a2e2dd 100644 --- a/src/simdag/sd_dotloader.cpp +++ b/src/simdag/sd_dotloader.cpp @@ -5,6 +5,7 @@ * under the terms of the license (GNU LGPL) which comes with this package. */ #include "simdag_private.hpp" +#include "simgrid/s4u/Engine.hpp" #include "simgrid/simdag.h" #include "src/internal_config.h" #include "xbt/file.hpp" @@ -212,7 +213,9 @@ xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool sched if(schedule){ if (schedule_success) { - sg_host_t* workstations = sg_host_list(); + std::vector hosts; + simgrid::s4u::Engine::getInstance()->getHostList(&hosts); + for (auto const& elm : computers) { SD_task_t previous_task = nullptr; for (auto const& task : *elm.second) { @@ -221,13 +224,12 @@ xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool sched if (previous_task && not SD_task_dependency_exists(previous_task, task)) SD_task_dependency_add(previous_task, task); - SD_task_schedulel(task, 1, workstations[atoi(elm.first.c_str())]); + SD_task_schedulel(task, 1, hosts[atoi(elm.first.c_str())]); previous_task = task; } } delete elm.second; } - xbt_free(workstations); } else { XBT_WARN("The scheduling is ignored"); for (auto const& elm : computers) diff --git a/src/simgrid/host.cpp b/src/simgrid/host.cpp index 3c9fe63827..e49a988134 100644 --- a/src/simgrid/host.cpp +++ b/src/simgrid/host.cpp @@ -37,8 +37,13 @@ size_t sg_host_count() */ sg_host_t *sg_host_list() { xbt_assert(sg_host_count() > 0, "There is no host!"); + std::vector hosts; + simgrid::s4u::Engine::getInstance()->getHostList(&hosts); - return (sg_host_t*)xbt_dynar_to_array(sg_hosts_as_dynar()); + sg_host_t* res = (sg_host_t*)malloc(sizeof(sg_host_t) * hosts.size()); + memcpy(res, hosts.data(), sizeof(sg_host_t) * hosts.size()); + + return res; } const char *sg_host_get_name(sg_host_t host) diff --git a/src/surf/plugins/host_energy.cpp b/src/surf/plugins/host_energy.cpp index 171f6b7dcc..8826efc78a 100644 --- a/src/surf/plugins/host_energy.cpp +++ b/src/surf/plugins/host_energy.cpp @@ -432,15 +432,16 @@ static void onHostDestruction(simgrid::s4u::Host& host) static void onSimulationEnd() { - sg_host_t* host_list = sg_host_list(); - int host_count = sg_host_count(); + std::vector hosts; + simgrid::s4u::Engine::getInstance()->getHostList(&hosts); + double total_energy = 0.0; // Total energy consumption (whole platform) double used_hosts_energy = 0.0; // Energy consumed by hosts that computed something - for (int i = 0; i < host_count; i++) { - if (dynamic_cast(host_list[i]) == nullptr) { // Ignore virtual machines + for (size_t i = 0; i < hosts.size(); i++) { + if (dynamic_cast(hosts[i]) == nullptr) { // Ignore virtual machines - bool host_was_used = (sg_host_get_computed_flops(host_list[i]) != 0); - double energy = host_list[i]->extension()->getConsumedEnergy(); + bool host_was_used = (sg_host_get_computed_flops(hosts[i]) != 0); + double energy = hosts[i]->extension()->getConsumedEnergy(); total_energy += energy; if (host_was_used) used_hosts_energy += energy; @@ -448,7 +449,6 @@ static void onSimulationEnd() } XBT_INFO("Total energy consumption: %f Joules (used hosts: %f Joules; unused/idle hosts: %f)", total_energy, used_hosts_energy, total_energy - used_hosts_energy); - xbt_free(host_list); } /* **************************** Public interface *************************** */ diff --git a/teshsuite/s4u/comm-pt2pt/comm-pt2pt.cpp b/teshsuite/s4u/comm-pt2pt/comm-pt2pt.cpp index 41be1f3aff..c82af7b2b3 100644 --- a/teshsuite/s4u/comm-pt2pt/comm-pt2pt.cpp +++ b/teshsuite/s4u/comm-pt2pt/comm-pt2pt.cpp @@ -182,10 +182,11 @@ int main(int argc, char* argv[]) } xbt_assert(argSend.front().size() == argRecv.front().size(), "Sender and receiver spec must be of the same size"); - simgrid::s4u::Host** hosts = sg_host_list(); + std::vector hosts; + e.getHostList(&hosts); + simgrid::s4u::Actor::createActor("sender", hosts[0], sender, argSend); simgrid::s4u::Actor::createActor("recver", hosts[1], receiver, argRecv); - xbt_free(hosts); e.run(); XBT_INFO("Simulation time %g", e.getClock()); -- 2.20.1