Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Pointer to const.
[simgrid.git] / src / s4u / s4u_Host.cpp
index 83b0a00..3345eca 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. */
 
+#include "simgrid/Exception.hpp"
 #include "simgrid/host.h"
 #include "simgrid/kernel/routing/NetPoint.hpp"
 #include "simgrid/s4u/Actor.hpp"
@@ -12,6 +13,7 @@
 #include "simgrid/s4u/VirtualMachine.hpp"
 #include "src/plugins/vm/VirtualMachineImpl.hpp"
 #include "src/surf/HostImpl.hpp"
+#include "xbt/parse_units.hpp"
 
 #include <algorithm>
 #include <string>
@@ -30,13 +32,6 @@ xbt::signal<void(Host const&)> Host::on_destruction;
 xbt::signal<void(Host const&)> Host::on_state_change;
 xbt::signal<void(Host const&)> Host::on_speed_change;
 
-Host::Host(const std::string& name) : name_(name)
-{
-  xbt_assert(Host::by_name_or_null(name_) == nullptr, "Refusing to create a second host named '%s'.", get_cname());
-  Engine::get_instance()->host_register(name_, this);
-  new surf::HostImpl(this);
-}
-
 Host* Host::set_netpoint(kernel::routing::NetPoint* netpoint)
 {
   pimpl_netpoint_ = netpoint;
@@ -45,7 +40,6 @@ Host* Host::set_netpoint(kernel::routing::NetPoint* netpoint)
 
 Host::~Host()
 {
-  delete pimpl_;
   if (pimpl_netpoint_ != nullptr) // not removed yet by a children class
     Engine::get_instance()->netpoint_unregister(pimpl_netpoint_);
   delete pimpl_cpu;
@@ -60,9 +54,7 @@ Host::~Host()
  */
 void Host::destroy()
 {
-  on_destruction(*this);
-  Engine::get_instance()->host_unregister(std::string(name_));
-  delete this;
+  kernel::actor::simcall([this] { this->pimpl_->destroy(); });
 }
 
 Host* Host::by_name(const std::string& name)
@@ -82,6 +74,16 @@ Host* Host::current()
   return self->get_host();
 }
 
+xbt::string const& Host::get_name() const
+{
+  return this->pimpl_->get_name();
+}
+
+const char* Host::get_cname() const
+{
+  return this->pimpl_->get_cname();
+}
+
 void Host::turn_on()
 {
   if (not is_on()) {
@@ -176,16 +178,21 @@ NetZone* Host::get_englobing_zone()
   return pimpl_netpoint_->get_englobing_zone()->get_iface();
 }
 
-void Host::sendto(Host* dest, double byte_amount)
+void Host::sendto(Host* dest, double byte_amount) // deprecated 331
 {
-  sendto_async(dest, byte_amount)->wait();
+  Comm::sendto_async(this, dest, byte_amount)->wait();
 }
 
-CommPtr Host::sendto_async(Host* dest, double byte_amount)
+CommPtr Host::sendto_async(Host* dest, double byte_amount) // deprecated 331
 {
   return Comm::sendto_async(this, dest, byte_amount);
 }
 
+void Host::send_to(Host* dest, double byte_amount) // deprecated 330
+{
+  Comm::sendto(this, dest, byte_amount);
+}
+
 /** Get the properties assigned to a host */
 const std::unordered_map<std::string, std::string>* Host::get_properties() const
 {
@@ -212,9 +219,10 @@ Host* Host::set_properties(const std::unordered_map<std::string, std::string>& p
 
 /** Specify a profile turning the host on and off according to an exhaustive list or a stochastic law.
  * The profile must contain boolean values. */
-void Host::set_state_profile(kernel::profile::Profile* p)
+Host* Host::set_state_profile(kernel::profile::Profile* p)
 {
-  return kernel::actor::simcall([this, p] { pimpl_cpu->set_state_profile(p); });
+  kernel::actor::simcall([this, p] { pimpl_cpu->set_state_profile(p); });
+  return this;
 }
 /** Specify a profile modeling the external load according to an exhaustive list or a stochastic law.
  *
@@ -222,9 +230,10 @@ void Host::set_state_profile(kernel::profile::Profile* p)
  * of the initial value. This means that the actual value is obtained by multiplying the initial value (the peek speed
  * at this pstate level) by the rate coming from the profile.
  */
-void Host::set_speed_profile(kernel::profile::Profile* p)
+Host* Host::set_speed_profile(kernel::profile::Profile* p)
 {
-  return kernel::actor::simcall([this, p] { pimpl_cpu->set_speed_profile(p); });
+  kernel::actor::simcall([this, p] { pimpl_cpu->set_speed_profile(p); });
+  return this;
 }
 
 /** @brief Get the peak processor speed (in flops/s), at the specified pstate  */
@@ -251,11 +260,40 @@ int Host::get_core_count() const
   return this->pimpl_cpu->get_core_count();
 }
 
+Host* Host::set_core_count(int core_count)
+{
+  kernel::actor::simcall([this, core_count] { this->pimpl_cpu->set_core_count(core_count); });
+  return this;
+}
+
+Host* Host::set_pstate_speed(const std::vector<double>& speed_per_state)
+{
+  kernel::actor::simcall([this, &speed_per_state] { pimpl_cpu->set_pstate_speed(speed_per_state); });
+  return this;
+}
+
+Host* Host::set_pstate_speed(const std::vector<std::string>& speed_per_state)
+{
+  std::vector<double> speed_list(speed_per_state.size());
+  for (const auto& speed_str : speed_per_state) {
+    try {
+      double speed = xbt_parse_get_speed("", 0, speed_str.c_str(), nullptr, "");
+      speed_list.push_back(speed);
+    } catch (const simgrid::ParseError&) {
+      xbt_die("Host(%s): Impossible to set_pstate_speed, invalid speed %s", get_cname(), speed_str.c_str());
+    }
+  }
+  set_pstate_speed(speed_list);
+  return this;
+}
+
 /** @brief Set the pstate at which the host should run */
-void Host::set_pstate(int pstate_index)
+Host* Host::set_pstate(int pstate_index)
 {
   kernel::actor::simcall([this, pstate_index] { this->pimpl_cpu->set_pstate(pstate_index); });
+  return this;
 }
+
 /** @brief Retrieve the pstate at which the host is currently running */
 int Host::get_pstate() const
 {
@@ -264,14 +302,14 @@ int Host::get_pstate() const
 
 std::vector<Disk*> Host::get_disks() const
 {
-  return kernel::actor::simcall([this] { return this->pimpl_->get_disks(); });
+  return this->pimpl_->get_disks();
 }
 
-Disk* Host::create_disk()
+Disk* Host::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
 {
-  auto pimpl = surf_disk_model->create_disk();
-  pimpl->set_host(this);
-  return pimpl->get_iface();
+  auto disk =
+      this->get_netpoint()->get_englobing_zone()->get_disk_model()->create_disk(name, read_bandwidth, write_bandwidth);
+  return disk->set_host(this)->get_iface();
 }
 
 void Host::add_disk(const Disk* disk)
@@ -315,12 +353,13 @@ size_t sg_host_count()
 sg_host_t* sg_host_list()
 {
   const simgrid::s4u::Engine* e = simgrid::s4u::Engine::get_instance();
-  size_t host_count       = e->get_host_count();
+  size_t host_count             = e->get_host_count();
+
   xbt_assert(host_count > 0, "There is no host!");
   std::vector<simgrid::s4u::Host*> hosts = e->get_all_hosts();
 
   sg_host_t* res = xbt_new(sg_host_t, hosts.size());
-  memcpy(res, hosts.data(), sizeof(sg_host_t) * hosts.size());
+  std::copy(begin(hosts), end(hosts), res);
 
   return res;
 }
@@ -379,27 +418,14 @@ void sg_host_data_set(sg_host_t host, void* userdata) // XBT_ATTRIB_DEPRECATED_v
 {
   sg_host_set_data(host, userdata);
 }
-void* sg_host_user(sg_host_t host) // XBT_ATTRIB_DEPRECATED_v328
-{
-  return host->get_data();
-}
-void sg_host_user_set(sg_host_t host, void* userdata) // XBT_ATTRIB_DEPRECATED_v328
-{
-  host->set_data(userdata);
-}
-void sg_host_user_destroy(sg_host_t host) // XBT_ATTRIB_DEPRECATED_v328
-{
-  host->set_data(nullptr);
-}
 
 // ========= Disk related functions ============
 void sg_host_get_disks(const_sg_host_t host, unsigned int* disk_count, sg_disk_t** disks)
 {
   std::vector<sg_disk_t> list = host->get_disks();
   *disk_count                 = list.size();
-  *disks                      = static_cast<sg_disk_t*>(xbt_malloc(sizeof(sg_disk_t) * (*disk_count)));
-  for (size_t i = 0; i < *disk_count; i++)
-    (*disks)[i] = list[i];
+  *disks                      = xbt_new(sg_disk_t, list.size());
+  std::copy(begin(list), end(list), *disks);
 }
 
 // =========== user-level functions ===============
@@ -509,8 +535,9 @@ int sg_host_is_on(const_sg_host_t host)
 /** @brief Get the properties of a host */
 xbt_dict_t sg_host_get_properties(const_sg_host_t host)
 {
-  xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
   const std::unordered_map<std::string, std::string>* props = host->get_properties();
+  xbt_dict_t as_dict                                        = xbt_dict_new_homogeneous(xbt_free_f);
+
   if (props == nullptr)
     return nullptr;
   for (auto const& elm : *props) {
@@ -600,7 +627,7 @@ double sg_host_route_bandwidth(const_sg_host_t from, const_sg_host_t to) // XBT_
 
 void sg_host_sendto(sg_host_t from, sg_host_t to, double byte_amount)
 {
-  from->sendto(to, byte_amount);
+  simgrid::s4u::Comm::sendto(from, to, byte_amount);
 }
 
 /** @brief Displays debugging information about a host */