Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move ugly dict closer to the C APIs
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Mon, 31 Jul 2017 16:15:34 +0000 (18:15 +0200)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Mon, 31 Jul 2017 16:17:50 +0000 (18:17 +0200)
+ revise a bit an ugly simdag example

14 files changed:
examples/simdag/properties/sd_properties.c
include/simgrid/host.h
include/simgrid/s4u/Host.hpp
include/simgrid/s4u/Storage.hpp
src/msg/msg_host.cpp
src/msg/msg_io.cpp
src/s4u/s4u_host.cpp
src/s4u/s4u_storage.cpp
src/simgrid/host.cpp
src/surf/PropertyHolder.cpp
src/surf/PropertyHolder.hpp
src/surf/sg_platf.cpp
teshsuite/s4u/storage_client_server/storage_client_server.cpp
teshsuite/simdag/flatifier/flatifier.cpp

index 697c10c..2e437de 100644 (file)
@@ -29,12 +29,13 @@ int main(int argc, char **argv)
   const char *name1 = sg_host_get_name(h1);
   const char *name2 = sg_host_get_name(h2);
 
   const char *name1 = sg_host_get_name(h1);
   const char *name2 = sg_host_get_name(h2);
 
-  /* Get the property list of 'host1' */
+  /* Trying to set a new property */
+  sg_host_set_property_value(h1, "NewProp", "newValue");
+
+  /* Get the property list of 'host1'. This is only a copy of the internal data structure.*/
   XBT_INFO("Property list for host %s", name1);
   xbt_dict_t props = sg_host_get_properties(h1);
 
   XBT_INFO("Property list for host %s", name1);
   xbt_dict_t props = sg_host_get_properties(h1);
 
-  /* Trying to set a new property */
-  xbt_dict_set(props, "NewProp", strdup("newValue"), NULL);
 
   /* Print the properties of 'host1' */
   xbt_dict_foreach(props, cursor, key, data) {
 
   /* Print the properties of 'host1' */
   xbt_dict_foreach(props, cursor, key, data) {
@@ -62,7 +63,7 @@ int main(int argc, char **argv)
     XBT_INFO("\tProperty: %s is undefined", exist);
   else {
     XBT_INFO("\tProperty: %s old value: %s", exist, value);
     XBT_INFO("\tProperty: %s is undefined", exist);
   else {
     XBT_INFO("\tProperty: %s old value: %s", exist, value);
-    xbt_dict_set(props, exist, strdup("250"), NULL);
+    sg_host_set_property_value(h2, exist, "250");
   }
 
   /* Test if we have changed the value */
   }
 
   /* Test if we have changed the value */
index 1c418b2..d839c96 100644 (file)
@@ -46,6 +46,7 @@ XBT_PUBLIC(int) sg_host_get_pstate(sg_host_t host);
 XBT_PUBLIC(void) sg_host_set_pstate(sg_host_t host,int pstate);
 XBT_PUBLIC(xbt_dict_t) sg_host_get_properties(sg_host_t host);
 XBT_PUBLIC(const char*) sg_host_get_property_value(sg_host_t host, const char* name);
 XBT_PUBLIC(void) sg_host_set_pstate(sg_host_t host,int pstate);
 XBT_PUBLIC(xbt_dict_t) sg_host_get_properties(sg_host_t host);
 XBT_PUBLIC(const char*) sg_host_get_property_value(sg_host_t host, const char* name);
+XBT_PUBLIC(void) sg_host_set_property_value(sg_host_t host, const char* name, const char* value);
 XBT_PUBLIC(void) sg_host_route(sg_host_t from, sg_host_t to, xbt_dynar_t links);
 XBT_PUBLIC(double) sg_host_route_latency(sg_host_t from, sg_host_t to);
 XBT_PUBLIC(double) sg_host_route_bandwidth(sg_host_t from, sg_host_t to);
 XBT_PUBLIC(void) sg_host_route(sg_host_t from, sg_host_t to, xbt_dynar_t links);
 XBT_PUBLIC(double) sg_host_route_latency(sg_host_t from, sg_host_t to);
 XBT_PUBLIC(double) sg_host_route_bandwidth(sg_host_t from, sg_host_t to);
index d43c985..33f9710 100644 (file)
@@ -86,7 +86,7 @@ public:
 
   double getSpeed();
   int getCoreCount();
 
   double getSpeed();
   int getCoreCount();
-  xbt_dict_t getProperties();
+  std::unordered_map<std::string, std::string>* getProperties();
   const char* getProperty(const char* key);
   void setProperty(const char* key, const char* value);
   void getProcesses(std::vector<ActorPtr> * list);
   const char* getProperty(const char* key);
   void setProperty(const char* key, const char* value);
   void getProcesses(std::vector<ActorPtr> * list);
index 2ece591..e3b1d90 100644 (file)
@@ -36,7 +36,7 @@ public:
   sg_size_t getSizeFree();
   sg_size_t getSizeUsed();
 
   sg_size_t getSizeFree();
   sg_size_t getSizeUsed();
 
-  xbt_dict_t getProperties();
+  std::unordered_map<std::string, std::string>* getProperties();
   const char* getProperty(const char* key);
   void setProperty(const char* key, const char* value);
   std::map<std::string, sg_size_t>* getContent();
   const char* getProperty(const char* key);
   void setProperty(const char* key, const char* value);
   std::map<std::string, sg_size_t>* getContent();
index fa83f62..82a9ad7 100644 (file)
@@ -165,7 +165,14 @@ const char *MSG_host_get_property_value(msg_host_t host, const char *name)
 xbt_dict_t MSG_host_get_properties(msg_host_t host)
 {
   xbt_assert((host != nullptr), "Invalid parameters (host is nullptr)");
 xbt_dict_t MSG_host_get_properties(msg_host_t host)
 {
   xbt_assert((host != nullptr), "Invalid parameters (host is nullptr)");
-  return host->getProperties();
+  xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
+  std::unordered_map<std::string, std::string>* props = host->getProperties();
+  if (props == nullptr)
+    return nullptr;
+  for (auto elm : *props) {
+    xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
+  }
+  return as_dict;
 }
 
 /** \ingroup m_host_management
 }
 
 /** \ingroup m_host_management
index 1913d3b..648db4d 100644 (file)
@@ -388,7 +388,14 @@ sg_size_t MSG_storage_get_used_size(msg_storage_t storage)
 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
 {
   xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)");
 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
 {
   xbt_assert((storage != nullptr), "Invalid parameters (storage is nullptr)");
-  return storage->getProperties();
+  xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
+  std::unordered_map<std::string, std::string>* props = storage->getProperties();
+  if (props == nullptr)
+    return nullptr;
+  for (auto elm : *props) {
+    xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
+  }
+  return as_dict;
 }
 
 /** \ingroup msg_storage_management
 }
 
 /** \ingroup msg_storage_management
@@ -470,14 +477,14 @@ void *MSG_storage_get_data(msg_storage_t storage)
 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
 {
   std::map<std::string, sg_size_t>* content = storage->getContent();
 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
 {
   std::map<std::string, sg_size_t>* content = storage->getContent();
-  xbt_dict_t content_dict = xbt_dict_new_homogeneous(&free);
+  xbt_dict_t content_as_dict = xbt_dict_new_homogeneous(xbt_free_f);
 
   for (auto entry : *content) {
     sg_size_t* psize = static_cast<sg_size_t*>(malloc(sizeof(sg_size_t)));
     *psize           = entry.second;
 
   for (auto entry : *content) {
     sg_size_t* psize = static_cast<sg_size_t*>(malloc(sizeof(sg_size_t)));
     *psize           = entry.second;
-    xbt_dict_set(content_dict, entry.first.c_str(), psize, nullptr);
+    xbt_dict_set(content_as_dict, entry.first.c_str(), psize, nullptr);
   }
   }
-  return content_dict;
+  return content_as_dict;
 }
 
 /** \ingroup msg_storage_management
 }
 
 /** \ingroup msg_storage_management
index f39b2a2..9dafa33 100644 (file)
@@ -177,7 +177,7 @@ void Host::routeTo(Host* dest, std::vector<surf::LinkImpl*>* links, double* late
 }
 
 /** Get the properties assigned to a host */
 }
 
 /** Get the properties assigned to a host */
-xbt_dict_t Host::getProperties()
+std::unordered_map<std::string, std::string>* Host::getProperties()
 {
   return simgrid::simix::kernelImmediate([this] {
     return this->pimpl_->getProperties();
 {
   return simgrid::simix::kernelImmediate([this] {
     return this->pimpl_->getProperties();
index 74770d9..58ad8f4 100644 (file)
@@ -60,7 +60,7 @@ sg_size_t Storage::getSize()
   return pimpl_->getSize();
 }
 
   return pimpl_->getSize();
 }
 
-xbt_dict_t Storage::getProperties()
+std::unordered_map<std::string, std::string>* Storage::getProperties()
 {
   return simgrid::simix::kernelImmediate([this] { return pimpl_->getProperties(); });
 }
 {
   return simgrid::simix::kernelImmediate([this] { return pimpl_->getProperties(); });
 }
index 1deab79..07f8017 100644 (file)
@@ -180,7 +180,14 @@ void sg_host_set_pstate(sg_host_t host,int pstate) {
 
 /** @brief Get the properties of an host */
 xbt_dict_t sg_host_get_properties(sg_host_t host) {
 
 /** @brief Get the properties of an host */
 xbt_dict_t sg_host_get_properties(sg_host_t host) {
-  return host->getProperties();
+  xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
+  std::unordered_map<std::string, std::string>* props = host->getProperties();
+  if (props == nullptr)
+    return nullptr;
+  for (auto elm : *props) {
+    xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
+  }
+  return as_dict;
 }
 
 /** \ingroup m_host_management
 }
 
 /** \ingroup m_host_management
@@ -192,8 +199,14 @@ xbt_dict_t sg_host_get_properties(sg_host_t host) {
 */
 const char *sg_host_get_property_value(sg_host_t host, const char *name)
 {
 */
 const char *sg_host_get_property_value(sg_host_t host, const char *name)
 {
-  return (const char*) xbt_dict_get_or_null(sg_host_get_properties(host), name);
+  return host->getProperty(name);
+}
+
+void sg_host_set_property_value(sg_host_t host, const char* name, const char* value)
+{
+  host->setProperty(name, value);
 }
 }
+
 /**
  * \brief Find a route between two hosts
  *
 /**
  * \brief Find a route between two hosts
  *
@@ -244,21 +257,15 @@ double sg_host_route_bandwidth(sg_host_t from, sg_host_t to)
 /** @brief Displays debugging information about a host */
 void sg_host_dump(sg_host_t host)
 {
 /** @brief Displays debugging information about a host */
 void sg_host_dump(sg_host_t host)
 {
-  xbt_dict_t props;
-
   XBT_INFO("Displaying host %s", host->getCname());
   XBT_INFO("  - speed: %.0f", host->getSpeed());
   XBT_INFO("  - available speed: %.2f", sg_host_get_available_speed(host));
   XBT_INFO("Displaying host %s", host->getCname());
   XBT_INFO("  - speed: %.0f", host->getSpeed());
   XBT_INFO("  - available speed: %.2f", sg_host_get_available_speed(host));
-  props = host->getProperties();
+  std::unordered_map<std::string, std::string>* props = host->getProperties();
 
 
-  if (not xbt_dict_is_empty(props)) {
+  if (not props->empty()) {
     XBT_INFO("  - properties:");
     XBT_INFO("  - properties:");
-    xbt_dict_cursor_t cursor = nullptr;
-    char* key;
-    char* data;
-
-    xbt_dict_foreach(props,cursor,key,data) {
-      XBT_INFO("    %s->%s",key,data);
+    for (auto elm : *props) {
+      XBT_INFO("    %s->%s", elm.first.c_str(), elm.second.c_str());
     }
   }
 }
     }
   }
 }
index 2eda708..03210a7 100644 (file)
@@ -3,7 +3,6 @@
 /* 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 "xbt/sysdep.h"
 #include "PropertyHolder.hpp"
 
 namespace simgrid {
 #include "PropertyHolder.hpp"
 
 namespace simgrid {
@@ -12,27 +11,32 @@ namespace surf {
 PropertyHolder::PropertyHolder() = default;
 
 PropertyHolder::~PropertyHolder() {
 PropertyHolder::PropertyHolder() = default;
 
 PropertyHolder::~PropertyHolder() {
-  xbt_dict_free(&properties_);
+  delete properties_;
 }
 
 /** @brief Return the property associated to the provided key (or nullptr if not existing) */
 const char *PropertyHolder::getProperty(const char*key) {
   if (properties_ == nullptr)
     return nullptr;
 }
 
 /** @brief Return the property associated to the provided key (or nullptr if not existing) */
 const char *PropertyHolder::getProperty(const char*key) {
   if (properties_ == nullptr)
     return nullptr;
-  return static_cast<const char*>(xbt_dict_get_or_null(properties_,key));
+  try {
+    return properties_->at(key).c_str();
+  } catch (std::out_of_range& unfound) {
+    return nullptr;
+  }
 }
 
 /** @brief Change the value of a given key in the property set */
 void PropertyHolder::setProperty(const char*key, const char*value) {
   if (not properties_)
 }
 
 /** @brief Change the value of a given key in the property set */
 void PropertyHolder::setProperty(const char*key, const char*value) {
   if (not properties_)
-    properties_ = xbt_dict_new_homogeneous(xbt_free_f);
-  xbt_dict_set(properties_, key, xbt_strdup(value), nullptr);
+    properties_       = new std::unordered_map<std::string, std::string>;
+  (*properties_)[key] = value;
 }
 
 /** @brief Return the whole set of properties. Don't mess with it, dude! */
 }
 
 /** @brief Return the whole set of properties. Don't mess with it, dude! */
-xbt_dict_t PropertyHolder::getProperties() {
+std::unordered_map<std::string, std::string>* PropertyHolder::getProperties()
+{
   if (not properties_)
   if (not properties_)
-    properties_ = xbt_dict_new_homogeneous(xbt_free_f);
+    properties_ = new std::unordered_map<std::string, std::string>;
   return properties_;
 }
 
   return properties_;
 }
 
index ba06640..afbdbc2 100644 (file)
@@ -5,7 +5,7 @@
 
 #ifndef SRC_SURF_PROPERTYHOLDER_HPP_
 #define SRC_SURF_PROPERTYHOLDER_HPP_
 
 #ifndef SRC_SURF_PROPERTYHOLDER_HPP_
 #define SRC_SURF_PROPERTYHOLDER_HPP_
-#include <xbt/dict.h>
+#include <unordered_map>
 
 namespace simgrid {
 namespace surf {
 
 namespace simgrid {
 namespace surf {
@@ -26,9 +26,10 @@ public:
   /* FIXME: This should not be exposed, as users may do bad things with the dict they got (it's not a copy).
    * But some user API expose this call so removing it is not so easy.
    */
   /* FIXME: This should not be exposed, as users may do bad things with the dict they got (it's not a copy).
    * But some user API expose this call so removing it is not so easy.
    */
-  xbt_dict_t getProperties();
+  std::unordered_map<std::string, std::string>* getProperties();
+
 private:
 private:
-  xbt_dict_t properties_ = nullptr;
+  std::unordered_map<std::string, std::string>* properties_ = nullptr;
 };
 
 } /* namespace surf */
 };
 
 } /* namespace surf */
index cd31839..ec19598 100644 (file)
@@ -100,7 +100,6 @@ void sg_platf_new_host(sg_platf_host_cbarg_t args)
     host->pimpl_cpu->setPState(args->pstate);
   if (args->coord && strcmp(args->coord, ""))
     new simgrid::kernel::routing::vivaldi::Coords(host->pimpl_netpoint, args->coord);
     host->pimpl_cpu->setPState(args->pstate);
   if (args->coord && strcmp(args->coord, ""))
     new simgrid::kernel::routing::vivaldi::Coords(host->pimpl_netpoint, args->coord);
-
 }
 
 /** @brief Add a "router" to the network element list */
 }
 
 /** @brief Add a "router" to the network element list */
index db4614c..7d71f93 100644 (file)
@@ -10,15 +10,13 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(storage, "Messages specific for this simulation");
 
 static void display_storage_properties(simgrid::s4u::Storage* storage)
 {
 
 static void display_storage_properties(simgrid::s4u::Storage* storage)
 {
-  xbt_dict_t props = storage->getProperties();
-  if (xbt_dict_length(props) > 0) {
+  std::unordered_map<std::string, std::string>* props = storage->getProperties();
+  if (not props->empty()) {
     XBT_INFO("\tProperties of mounted storage: %s", storage->getName());
 
     XBT_INFO("\tProperties of mounted storage: %s", storage->getName());
 
-    xbt_dict_cursor_t cursor = NULL;
-    char* key;
-    char* data;
-    xbt_dict_foreach (props, cursor, key, data)
-      XBT_INFO("\t\t'%s' -> '%s'", key, data);
+    for (auto elm : *props) {
+      XBT_INFO("    %s->%s", elm.first.c_str(), elm.second.c_str());
+    }
   } else {
     XBT_INFO("\tNo property attached.");
   }
   } else {
     XBT_INFO("\tNo property attached.");
   }
index bd04422..bb2aa74 100644 (file)
@@ -81,6 +81,7 @@ static void dump_platform()
     } else {
       std::printf("/>\n");
     }
     } else {
       std::printf("/>\n");
     }
+    xbt_dict_free(&props);
   }
 
   // Routers
   }
 
   // Routers