Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / simdag / sd_daxloader.cpp
index 843e43e..409caee 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009-2016. The SimGrid Team.
+/* Copyright (c) 2009-2017. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -9,17 +9,13 @@
 #include "xbt/file.h" /* xbt_basename() */
 #include "xbt/log.h"
 #include "xbt/misc.h"
-#include "xbt/str.h"
-#include <unordered_map>
+#include <map>
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_daxparse, sd, "Parsing DAX files");
 
 extern "C" {
-  #undef CLEANUP
-  #include "dax_dtd.h"
-  #define register /* g++ don't like register, so don't say it */
-  #include "dax_dtd.c"
-  #undef register
+#include "dax_dtd.h"
+#include "dax_dtd.c"
 }
 
 /* Ensure that transfer tasks have unique names even though a file is used several times */
@@ -35,20 +31,20 @@ void uniq_transfer_task_name(SD_task_t task)
 }
 
 static bool children_are_marked(SD_task_t task){
-  for (SD_task_t it : *task->successors)
+  for (SD_task_t const& it : *task->successors)
     if (it->marked == 0)
       return false;
-  for (SD_task_t it : *task->outputs)
+  for (SD_task_t const& it : *task->outputs)
     if (it->marked == 0)
       return false;
   return true;
 }
 
 static bool parents_are_marked(SD_task_t task){
-  for (SD_task_t it : *task->predecessors)
+  for (SD_task_t const& it : *task->predecessors)
     if (it->marked == 0)
       return false;
-  for (SD_task_t it : *task->inputs)
+  for (SD_task_t const& it : *task->inputs)
     if (it->marked == 0)
       return false;
   return true;
@@ -68,17 +64,17 @@ bool acyclic_graph_detail(xbt_dynar_t dag){
   }
   while (not current.empty()) {
     std::vector<SD_task_t> next;
-    for (auto t: current){
+    for (auto const& t : current) {
       //Mark task
       t->marked = 1;
-      for (SD_task_t input : *t->inputs){
+      for (SD_task_t const& input : *t->inputs) {
         input->marked=1;
         // Inputs are communication, hence they can have only one predecessor
         SD_task_t input_pred = *(input->predecessors->begin());
         if (children_are_marked(input_pred))
           next.push_back(input_pred);
       }
-      for (SD_task_t pred : *t->predecessors) {
+      for (SD_task_t const& pred : *t->predecessors) {
         if (children_are_marked(pred))
           next.push_back(pred);
       }
@@ -109,22 +105,22 @@ bool acyclic_graph_detail(xbt_dynar_t dag){
     while (not current.empty()) {
       std::vector<SD_task_t> next;
       //test if the current iteration is done
-      for (auto t: current){
+      for (auto const& t : current) {
         t->marked = 1;
-        for (SD_task_t output : *t->outputs) {
+        for (SD_task_t const& output : *t->outputs) {
           output->marked = 1;
           // outputs are communication, hence they can have only one successor
           SD_task_t output_succ = *(output->successors->begin());
           if (parents_are_marked(output_succ))
             next.push_back(output_succ);
         }
-        for (SD_task_t succ : *t->successors) {
+        for (SD_task_t const& succ : *t->successors) {
           if (parents_are_marked(succ))
             next.push_back(succ);
         }
-        current.clear();
-        current = next;
       }
+      current.clear();
+      current = next;
     }
 
     all_marked = true;
@@ -141,8 +137,8 @@ bool acyclic_graph_detail(xbt_dynar_t dag){
 static YY_BUFFER_STATE input_buffer;
 
 static xbt_dynar_t result;
-static std::unordered_map<std::string, SD_task_t> jobs;
-static xbt_dict_t files;
+static std::map<std::string, SD_task_t> jobs;
+static std::map<std::string, SD_task_t> files;
 static SD_task_t current_job;
 static SD_task_t root_task;
 static SD_task_t end_task;
@@ -158,9 +154,7 @@ static void dax_task_free(void *task)
  */
 xbt_dynar_t SD_daxload(const char *filename)
 {
-  xbt_dict_cursor_t cursor;
   SD_task_t file;
-  char* name;
   FILE* in_file = fopen(filename, "r");
   xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
   input_buffer = dax__create_buffer(in_file, 10);
@@ -168,7 +162,6 @@ xbt_dynar_t SD_daxload(const char *filename)
   dax_lineno = 1;
 
   result = xbt_dynar_new(sizeof(SD_task_t), dax_task_free);
-  files = xbt_dict_new_homogeneous(&dax_task_free);
   root_task = SD_task_create_comp_seq("root", nullptr, 0);
   /* by design the root task is always SCHEDULABLE */
   SD_task_set_state(root_task, SD_SCHEDULABLE);
@@ -189,25 +182,26 @@ xbt_dynar_t SD_daxload(const char *filename)
    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
    */
 
-  xbt_dict_foreach(files, cursor, name, file) {
+  for (auto const& elm : files) {
+    file = elm.second;
     SD_task_t newfile;
     if (file->predecessors->empty()) {
-      for (SD_task_t it : *file->successors) {
+      for (SD_task_t const& it : *file->successors) {
         newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
         SD_task_dependency_add(nullptr, nullptr, root_task, newfile);
         SD_task_dependency_add(nullptr, nullptr, newfile, it);
         xbt_dynar_push(result, &newfile);
       }
     } else if (file->successors->empty()) {
-      for (SD_task_t it : *file->predecessors){
+      for (SD_task_t const& it : *file->predecessors) {
         newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
         SD_task_dependency_add(nullptr, nullptr, it, newfile);
         SD_task_dependency_add(nullptr, nullptr, newfile, end_task);
         xbt_dynar_push(result, &newfile);
       }
     } else {
-      for (SD_task_t it : *file->predecessors) {
-        for (SD_task_t it2 : *file->successors) {
+      for (SD_task_t const& it : *file->predecessors) {
+        for (SD_task_t const& it2 : *file->successors) {
           if (it == it2) {
             XBT_WARN ("File %s is produced and consumed by task %s."
                       "This loop dependency will prevent the execution of the task.", file->name, it->name);
@@ -225,7 +219,8 @@ xbt_dynar_t SD_daxload(const char *filename)
   xbt_dynar_push(result, &end_task);
 
   /* Free previous copy of the files */
-  xbt_dict_free(&files);
+  for (auto const& elm : files)
+    SD_task_destroy(elm.second);
   unsigned int cpt;
   xbt_dynar_foreach(result, cpt, file) {
     if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
@@ -295,12 +290,14 @@ void STag_dax__uses()
   bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
 
   XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
-  SD_task_t file = static_cast<SD_task_t>(xbt_dict_get_or_null(files, A_dax__uses_file));
-  if (file == nullptr) {
+  auto it = files.find(A_dax__uses_file);
+  SD_task_t file;
+  if (it == files.end()) {
     file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
     sd_global->initial_tasks->erase(file);
-    xbt_dict_set(files, A_dax__uses_file, file, nullptr);
+    files[A_dax__uses_file] = file;
   } else {
+    file = it->second;
     if (file->amount < size || file->amount > size) {
       XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, SD_task_get_amount(file), size);
     }
@@ -318,9 +315,10 @@ void STag_dax__uses()
 static SD_task_t current_child;
 void STag_dax__child()
 {
-  try {
-    current_child = jobs.at(A_dax__child_ref);
-  } catch (std::out_of_range& unfound) {
+  auto job = jobs.find(A_dax__child_ref);
+  if (job != jobs.end()) {
+    current_child = job->second;
+  } else {
     throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
                             ": Asked to add dependencies to the non-existent " + A_dax__child_ref + "task");
   }
@@ -333,11 +331,12 @@ void ETag_dax__child()
 
 void STag_dax__parent()
 {
-  try {
-    SD_task_t parent = jobs.at(A_dax__parent_ref);
+  auto job = jobs.find(A_dax__parent_ref);
+  if (job != jobs.end()) {
+    SD_task_t parent = job->second;
     SD_task_dependency_add(nullptr, nullptr, parent, current_child);
     XBT_DEBUG("Control-flow dependency from %s to %s", current_child->name, parent->name);
-  } catch (std::out_of_range& unfound) {
+  } else {
     throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
                             ": Asked to add a dependency from " + current_child->name + " to " + A_dax__parent_ref +
                             ", but " + A_dax__parent_ref + " does not exist");