Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use std::vector for lists of model descriptions.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Tue, 2 Apr 2019 13:38:40 +0000 (15:38 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Tue, 2 Apr 2019 14:48:59 +0000 (16:48 +0200)
src/simgrid/sg_config.cpp
src/surf/surf_interface.cpp
src/surf/surf_interface.hpp

index d479532..29ed310 100644 (file)
@@ -93,10 +93,8 @@ static void sg_config_cmd_line(int *argc, char **argv)
       printf("\n");
       model_help("network", surf_network_model_description);
       printf("\nLong description of all optimization levels accepted by the models of this simulator:\n");
       printf("\n");
       model_help("network", surf_network_model_description);
       printf("\nLong description of all optimization levels accepted by the models of this simulator:\n");
-      for (int k = 0; surf_optimization_mode_description[k].name; k++)
-        printf("  %s: %s\n",
-               surf_optimization_mode_description[k].name,
-               surf_optimization_mode_description[k].description);
+      for (auto const& item : surf_optimization_mode_description)
+        printf("  %s: %s\n", item.name, item.description);
       printf("Both network and CPU models have 'Lazy' as default optimization level\n\n");
       shall_exit = true;
     } else if (parse_args && not strcmp(argv[i], "--help-tracing")) {
       printf("Both network and CPU models have 'Lazy' as default optimization level\n\n");
       shall_exit = true;
     } else if (parse_args && not strcmp(argv[i], "--help-tracing")) {
@@ -123,12 +121,12 @@ static void _sg_cfg_cb__plugin(const std::string& value)
     return;
 
   if (value == "help") {
     return;
 
   if (value == "help") {
-    model_help("plugin", surf_plugin_description);
+    model_help("plugin", *surf_plugin_description);
     exit(0);
   }
 
     exit(0);
   }
 
-  int plugin_id = find_model_description(surf_plugin_description, value);
-  surf_plugin_description[plugin_id].model_init_preparse();
+  int plugin_id = find_model_description(*surf_plugin_description, value);
+  (*surf_plugin_description)[plugin_id].model_init_preparse();
 }
 
 /* callback of the host/model variable */
 }
 
 /* callback of the host/model variable */
@@ -215,28 +213,24 @@ static void _sg_cfg_cb_contexts_parallel_mode(const std::string& mode_name)
 }
 
 /* build description line with possible values */
 }
 
 /* build description line with possible values */
-static void describe_model(char *result,int resultsize,
-                           const s_surf_model_description_t model_description[],
-                           const char *name,
-                           const char *description)
+static void declare_model_flag(const std::string& name, const std::string& value,
+                               const std::function<void(std::string const&)>& callback,
+                               const std::vector<surf_model_description_t>& model_description, const std::string& type,
+                               const std::string& descr)
 {
 {
-  result[0] = '\0';
-  char *p = result;
-  p += snprintf(result,resultsize-1, "%s. Possible values: %s", description,
-            model_description[0].name ? model_description[0].name : "n/a");
-  for (int i = 1; model_description[i].name; i++)
-    p += snprintf(p,resultsize-(p-result)-1, ", %s", model_description[i].name);
-  p += snprintf(p,resultsize-(p-result)-1, ".\n       (use 'help' as a value to see the long description of each %s)", name);
-
-  xbt_assert(p<result+resultsize-1,"Buffer too small to display the model description of %s",name);
+  std::string description = descr + ". Possible values: ";
+  std::string sep         = "";
+  for (auto const& item : model_description) {
+    description += sep + item.name;
+    sep = ", ";
+  }
+  description += ".\n       (use 'help' as a value to see the long description of each " + type + ")";
+  simgrid::config::declare_flag<std::string>(name, description, value, callback);
 }
 
 /* create the config set, register what should be and parse the command line*/
 void sg_config_init(int *argc, char **argv)
 {
 }
 
 /* create the config set, register what should be and parse the command line*/
 void sg_config_init(int *argc, char **argv)
 {
-  const int descsize = 1024;
-  char description[descsize];
-
   /* Create the configuration support */
   if (_sg_cfg_init_status != 0) { /* Only create stuff if not already inited */
     XBT_WARN("Call to sg_config_init() after initialization ignored");
   /* Create the configuration support */
   if (_sg_cfg_init_status != 0) { /* Only create stuff if not already inited */
     XBT_WARN("Call to sg_config_init() after initialization ignored");
@@ -244,24 +238,22 @@ void sg_config_init(int *argc, char **argv)
   }
 
   /* Plugins configuration */
   }
 
   /* Plugins configuration */
-  describe_model(description, descsize, surf_plugin_description, "plugin", "The plugins");
-  simgrid::config::declare_flag<std::string>("plugin", description, "", &_sg_cfg_cb__plugin);
+  declare_model_flag("plugin", "", &_sg_cfg_cb__plugin, *surf_plugin_description, "plugin", "The plugins");
 
 
-  describe_model(description, descsize, surf_cpu_model_description, "model", "The model to use for the CPU");
-  simgrid::config::declare_flag<std::string>("cpu/model", description, "Cas01", &_sg_cfg_cb__cpu_model);
+  declare_model_flag("cpu/model", "Cas01", &_sg_cfg_cb__cpu_model, surf_cpu_model_description, "model",
+                     "The model to use for the CPU");
 
 
-  describe_model(description, descsize, surf_storage_model_description, "model", "The model to use for the storage");
-  simgrid::config::declare_flag<std::string>("storage/model", description, "default", &_sg_cfg_cb__storage_mode);
+  declare_model_flag("storage/model", "default", &_sg_cfg_cb__storage_mode, surf_storage_model_description, "model",
+                     "The model to use for the storage");
 
 
-  describe_model(description, descsize, surf_network_model_description, "model", "The model to use for the network");
-  simgrid::config::declare_flag<std::string>("network/model", description, "LV08", &_sg_cfg_cb__network_model);
+  declare_model_flag("network/model", "LV08", &_sg_cfg_cb__network_model, surf_network_model_description, "model",
+                     "The model to use for the network");
 
 
-  describe_model(description, descsize, surf_optimization_mode_description, "optimization mode",
-                 "The optimization modes to use for the network");
-  simgrid::config::declare_flag<std::string>("network/optim", description, "Lazy", &_sg_cfg_cb__optimization_mode);
+  declare_model_flag("network/optim", "Lazy", &_sg_cfg_cb__optimization_mode, surf_optimization_mode_description,
+                     "optimization mode", "The optimization modes to use for the network");
 
 
-  describe_model(description, descsize, surf_host_model_description, "model", "The model to use for the host");
-  simgrid::config::declare_flag<std::string>("host/model", description, "default", &_sg_cfg_cb__host_model);
+  declare_model_flag("host/model", "default", &_sg_cfg_cb__host_model, surf_host_model_description, "model",
+                     "The model to use for the host");
 
   simgrid::config::bind_flag(sg_surf_precision, "surf/precision",
                              "Numerical precision used when updating simulation times (in seconds)");
 
   simgrid::config::bind_flag(sg_surf_precision, "surf/precision",
                              "Numerical precision used when updating simulation times (in seconds)");
index d2f6966..21b718b 100644 (file)
@@ -36,29 +36,23 @@ std::vector<std::string> surf_path;
 std::set<std::string> watched_hosts;
 extern std::map<std::string, simgrid::kernel::resource::StorageType*> storage_types;
 
 std::set<std::string> watched_hosts;
 extern std::map<std::string, simgrid::kernel::resource::StorageType*> storage_types;
 
-s_surf_model_description_t* surf_plugin_description = nullptr;
+std::vector<surf_model_description_t>* surf_plugin_description = nullptr;
+
 static void XBT_ATTRIB_DESTRUCTOR(800) simgrid_free_plugin_description()
 {
 static void XBT_ATTRIB_DESTRUCTOR(800) simgrid_free_plugin_description()
 {
-  xbt_free(surf_plugin_description);
+  delete surf_plugin_description;
+  surf_plugin_description = nullptr;
 }
 
 XBT_PUBLIC void simgrid_add_plugin_description(const char* name, const char* description, void_f_void_t init_fun)
 {
 }
 
 XBT_PUBLIC void simgrid_add_plugin_description(const char* name, const char* description, void_f_void_t init_fun)
 {
-  static int plugin_amount = 0;
-
-  /* no need to check for plugin name conflict: the compiler already ensures that the generated
-   * simgrid_##id##_plugin_register() is unique */
-
-  plugin_amount++;
-  surf_plugin_description = static_cast<s_surf_model_description_t*>(
-      xbt_realloc(surf_plugin_description, sizeof(s_surf_model_description_t) * (plugin_amount + 2)));
-
-  surf_plugin_description[plugin_amount - 1] = {name, description, init_fun};
-  surf_plugin_description[plugin_amount]     = {nullptr, nullptr, nullptr}; // this array must be null terminated
+  if (not surf_plugin_description)
+    surf_plugin_description = new std::vector<surf_model_description_t>;
+  surf_plugin_description->emplace_back(surf_model_description_t{name, description, init_fun});
 }
 
 /* Don't forget to update the option description in smx_config when you change this */
 }
 
 /* Don't forget to update the option description in smx_config when you change this */
-s_surf_model_description_t surf_network_model_description[] = {
+const std::vector<surf_model_description_t> surf_network_model_description = {
     {"LV08", "Realistic network analytic model (slow-start modeled by multiplying latency by 13.01, bandwidth by .97; "
              "bottleneck sharing uses a payload of S=20537 for evaluating RTT). ",
      &surf_network_model_init_LegrandVelho},
     {"LV08", "Realistic network analytic model (slow-start modeled by multiplying latency by 13.01, bandwidth by .97; "
              "bottleneck sharing uses a payload of S=20537 for evaluating RTT). ",
      &surf_network_model_init_LegrandVelho},
@@ -83,7 +77,6 @@ s_surf_model_description_t surf_network_model_description[] = {
     {"Vegas",
      "Model from Steven H. Low using lagrange_solve instead of lmm_solve (experts only; check the code for more info).",
      &surf_network_model_init_Vegas},
     {"Vegas",
      "Model from Steven H. Low using lagrange_solve instead of lmm_solve (experts only; check the code for more info).",
      &surf_network_model_init_Vegas},
-    {nullptr, nullptr, nullptr} /* this array must be nullptr terminated */
 };
 
 #if ! HAVE_SMPI
 };
 
 #if ! HAVE_SMPI
@@ -100,28 +93,29 @@ void surf_network_model_init_NS3() {
 }
 #endif
 
 }
 #endif
 
-s_surf_model_description_t surf_cpu_model_description[] = {
-  {"Cas01", "Simplistic CPU model (time=size/power).", &surf_cpu_model_init_Cas01},
-  {nullptr, nullptr,  nullptr}      /* this array must be nullptr terminated */
+const std::vector<surf_model_description_t> surf_cpu_model_description = {
+    {"Cas01", "Simplistic CPU model (time=size/power).", &surf_cpu_model_init_Cas01},
 };
 
 };
 
-s_surf_model_description_t surf_host_model_description[] = {
-  {"default",   "Default host model. Currently, CPU:Cas01 and network:LV08 (with cross traffic enabled)", &surf_host_model_init_current_default},
-  {"compound",  "Host model that is automatically chosen if you change the network and CPU models", &surf_host_model_init_compound},
-  {"ptask_L07", "Host model somehow similar to Cas01+CM02 but allowing parallel tasks", &surf_host_model_init_ptask_L07},
-  {nullptr, nullptr, nullptr}      /* this array must be nullptr terminated */
+const std::vector<surf_model_description_t> surf_host_model_description = {
+    {"default", "Default host model. Currently, CPU:Cas01 and network:LV08 (with cross traffic enabled)",
+     &surf_host_model_init_current_default},
+    {"compound", "Host model that is automatically chosen if you change the network and CPU models",
+     &surf_host_model_init_compound},
+    {"ptask_L07", "Host model somehow similar to Cas01+CM02 but allowing parallel tasks",
+     &surf_host_model_init_ptask_L07},
 };
 
 };
 
-s_surf_model_description_t surf_optimization_mode_description[] = {
-  {"Lazy", "Lazy action management (partial invalidation in lmm + heap in action remaining).", nullptr},
-  {"TI",   "Trace integration. Highly optimized mode when using availability traces (only available for the Cas01 CPU model for now).", nullptr},
-  {"Full", "Full update of remaining and variables. Slow but may be useful when debugging.", nullptr},
-  {nullptr, nullptr, nullptr}      /* this array must be nullptr terminated */
+const std::vector<surf_model_description_t> surf_optimization_mode_description = {
+    {"Lazy", "Lazy action management (partial invalidation in lmm + heap in action remaining).", nullptr},
+    {"TI", "Trace integration. Highly optimized mode when using availability traces (only available for the Cas01 CPU "
+           "model for now).",
+     nullptr},
+    {"Full", "Full update of remaining and variables. Slow but may be useful when debugging.", nullptr},
 };
 
 };
 
-s_surf_model_description_t surf_storage_model_description[] = {
-  {"default", "Simplistic storage model.", &surf_storage_model_init_default},
-  {nullptr, nullptr,  nullptr}      /* this array must be nullptr terminated */
+const std::vector<surf_model_description_t> surf_storage_model_description = {
+    {"default", "Simplistic storage model.", &surf_storage_model_init_default},
 };
 
 double NOW = 0;
 };
 
 double NOW = 0;
@@ -190,25 +184,29 @@ FILE* surf_fopen(const std::string& name, const char* mode)
 }
 
 /** Displays the long description of all registered models, and quit */
 }
 
 /** Displays the long description of all registered models, and quit */
-void model_help(const char *category, s_surf_model_description_t * table)
+void model_help(const char* category, const std::vector<surf_model_description_t>& table)
 {
   printf("Long description of the %s models accepted by this simulator:\n", category);
 {
   printf("Long description of the %s models accepted by this simulator:\n", category);
-  for (int i = 0; table[i].name; i++)
-    printf("  %s: %s\n", table[i].name, table[i].description);
+  for (auto const& item : table)
+    printf("  %s: %s\n", item.name, item.description);
 }
 
 }
 
-int find_model_description(s_surf_model_description_t* table, const std::string& name)
+int find_model_description(const std::vector<surf_model_description_t>& table, const std::string& name)
 {
 {
-  for (int i = 0; table[i].name; i++)
-    if (name == table[i].name)
-      return i;
+  auto pos = std::find_if(table.begin(), table.end(),
+                          [&name](const surf_model_description_t& item) { return item.name == name; });
+  if (pos != table.end())
+    return std::distance(table.begin(), pos);
 
 
-  if (not table[0].name)
+  if (table.empty())
     xbt_die("No model is valid! This is a bug.");
 
     xbt_die("No model is valid! This is a bug.");
 
-  std::string name_list = std::string(table[0].name);
-  for (int i = 1; table[i].name; i++)
-    name_list = name_list + ", " + table[i].name;
+  std::string sep;
+  std::string name_list;
+  for (auto const& item : table) {
+    name_list += sep + item.name;
+    sep = ", ";
+  }
 
   xbt_die("Model '%s' is invalid! Valid models are: %s.", name.c_str(), name_list.c_str());
   return -1;
 
   xbt_die("Model '%s' is invalid! Valid models are: %s.", name.c_str(), name_list.c_str());
   return -1;
index 38fad62..82ca23e 100644 (file)
@@ -206,15 +206,14 @@ XBT_PUBLIC void surf_storage_model_init_default();
  *  Model Descriptions
  * -------------------- */
 /** @brief Resource model description */
  *  Model Descriptions
  * -------------------- */
 /** @brief Resource model description */
-struct surf_model_description {
+struct surf_model_description_t {
   const char* name;
   const char* description;
   void_f_void_t model_init_preparse;
 };
   const char* name;
   const char* description;
   void_f_void_t model_init_preparse;
 };
-typedef struct surf_model_description s_surf_model_description_t;
 
 
-XBT_PUBLIC int find_model_description(s_surf_model_description_t* table, const std::string& name);
-XBT_PUBLIC void model_help(const char* category, s_surf_model_description_t* table);
+XBT_PUBLIC int find_model_description(const std::vector<surf_model_description_t>& table, const std::string& name);
+XBT_PUBLIC void model_help(const char* category, const std::vector<surf_model_description_t>& table);
 
 #define SIMGRID_REGISTER_PLUGIN(id, desc, init)                                                                        \
   static void XBT_ATTRIB_CONSTRUCTOR(800) simgrid_##id##_plugin_register()                                             \
 
 #define SIMGRID_REGISTER_PLUGIN(id, desc, init)                                                                        \
   static void XBT_ATTRIB_CONSTRUCTOR(800) simgrid_##id##_plugin_register()                                             \
@@ -225,18 +224,18 @@ XBT_PUBLIC void model_help(const char* category, s_surf_model_description_t* tab
 XBT_PUBLIC void simgrid_add_plugin_description(const char* name, const char* description, void_f_void_t init_fun);
 
 /** @brief The list of all available plugins */
 XBT_PUBLIC void simgrid_add_plugin_description(const char* name, const char* description, void_f_void_t init_fun);
 
 /** @brief The list of all available plugins */
-XBT_PUBLIC_DATA s_surf_model_description_t* surf_plugin_description;
+XBT_PUBLIC_DATA std::vector<surf_model_description_t>* surf_plugin_description;
 /** @brief The list of all available optimization modes (both for cpu and networks).
  *  These optimization modes can be set using --cfg=cpu/optim:... and --cfg=network/optim:... */
 /** @brief The list of all available optimization modes (both for cpu and networks).
  *  These optimization modes can be set using --cfg=cpu/optim:... and --cfg=network/optim:... */
-XBT_PUBLIC_DATA s_surf_model_description_t surf_optimization_mode_description[];
+XBT_PUBLIC_DATA const std::vector<surf_model_description_t> surf_optimization_mode_description;
 /** @brief The list of all cpu models (pick one with --cfg=cpu/model) */
 /** @brief The list of all cpu models (pick one with --cfg=cpu/model) */
-XBT_PUBLIC_DATA s_surf_model_description_t surf_cpu_model_description[];
+XBT_PUBLIC_DATA const std::vector<surf_model_description_t> surf_cpu_model_description;
 /** @brief The list of all network models (pick one with --cfg=network/model) */
 /** @brief The list of all network models (pick one with --cfg=network/model) */
-XBT_PUBLIC_DATA s_surf_model_description_t surf_network_model_description[];
+XBT_PUBLIC_DATA const std::vector<surf_model_description_t> surf_network_model_description;
 /** @brief The list of all storage models (pick one with --cfg=storage/model) */
 /** @brief The list of all storage models (pick one with --cfg=storage/model) */
-XBT_PUBLIC_DATA s_surf_model_description_t surf_storage_model_description[];
+XBT_PUBLIC_DATA const std::vector<surf_model_description_t> surf_storage_model_description;
 /** @brief The list of all host models (pick one with --cfg=host/model:) */
 /** @brief The list of all host models (pick one with --cfg=host/model:) */
-XBT_PUBLIC_DATA s_surf_model_description_t surf_host_model_description[];
+XBT_PUBLIC_DATA const std::vector<surf_model_description_t> surf_host_model_description;
 
 /**********
  * Action *
 
 /**********
  * Action *