Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix gcc 6.2 builds
[simgrid.git] / src / simdag / sd_dotloader.cpp
index 03d1eda..b112f36 100644 (file)
@@ -4,43 +4,26 @@
 /* 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 "src/simdag/simdag_private.h"
+#include "src/internal_config.h"
 #include "simgrid/simdag.h"
 #include "simgrid/simdag.h"
-#include "xbt/misc.h"
-#include "xbt/log.h"
-#include <stdbool.h>
+#include "xbt/file.h"
 #include <string.h>
 #include <string.h>
-#include <libgen.h>
+#include "simdag_private.hpp"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_dotparse, sd, "Parsing DOT files");
 
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_dotparse, sd, "Parsing DOT files");
 
-#undef CLEANUP
-
-#ifdef HAVE_CGRAPH_H
+#if HAVE_GRAPHVIZ
 #include <graphviz/cgraph.h>
 #include <graphviz/cgraph.h>
-#elif HAVE_AGRAPH_H
-#include <graphviz/agraph.h>
-#define agnxtnode(dot, node)    agnxtnode(node)
-#define agfstin(dot, node)      agfstin(node)
-#define agnxtin(dot, edge)      agnxtin(edge)
-#define agfstout(dot, node)     agfstout(node)
-#define agnxtout(dot, edge)     agnxtout(edge)
-#endif
 
 typedef enum {
   sequential =0,
   parallel
 } seq_par_t;
 
 
 typedef enum {
   sequential =0,
   parallel
 } seq_par_t;
 
-xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par);
-
-static xbt_dict_t jobs;
-static xbt_dict_t computers;
-static bool schedule = false;
+xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par, bool schedule);
 
 static void dot_task_p_free(void *task) {
 
 static void dot_task_p_free(void *task) {
-  SD_task_t *t = (SD_task_t *)task;
-  SD_task_destroy(*t);
+  SD_task_destroy(*(SD_task_t *)task);
 }
 
 /** @brief loads a DOT file describing a DAG
 }
 
 /** @brief loads a DOT file describing a DAG
@@ -52,159 +35,101 @@ static void dot_task_p_free(void *task) {
  * If this attribute is ommited, the default value is zero.
  */
 xbt_dynar_t SD_dotload(const char *filename) {
  * If this attribute is ommited, the default value is zero.
  */
 xbt_dynar_t SD_dotload(const char *filename) {
-  return SD_dotload_generic(filename, sequential);
+  return SD_dotload_generic(filename, sequential, false);
 }
 
 xbt_dynar_t SD_PTG_dotload(const char * filename) {
 }
 
 xbt_dynar_t SD_PTG_dotload(const char * filename) {
-  return SD_dotload_generic(filename, parallel);
+  return SD_dotload_generic(filename, parallel, false);
 }
 
 xbt_dynar_t SD_dotload_with_sched(const char *filename) {
 }
 
 xbt_dynar_t SD_dotload_with_sched(const char *filename) {
-  schedule = true;
-  xbt_dynar_t result = SD_dotload_generic(filename, sequential);
-
-  if(schedule){
-    xbt_dynar_t computer = NULL;
-    xbt_dict_cursor_t dict_cursor;
-    char *computer_name;
-    const sg_host_t *workstations = sg_host_list ();
-    xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
-      int count_computer = atoi(computer_name);
-      unsigned int count=0;
-      SD_task_t task;
-      SD_task_t task_previous = NULL;
-      xbt_dynar_foreach(computer,count,task){
-        /* add dependency between the previous and the task to avoid parallel execution */
-        if(task != NULL ){
-          if(task_previous != NULL && !SD_task_dependency_exists(task_previous, task))
-            SD_task_dependency_add(NULL, NULL, task_previous, task);
-
-          SD_task_schedulel(task, 1, workstations[count_computer]);
-          task_previous = task;
-        }
-      }
-      xbt_dynar_free(&computer);
-    }
-    xbt_dict_free(&computers);
-    if(acyclic_graph_detail(result))
-      return result;
-    else
-      XBT_WARN("There is at least one cycle in the provided task graph");
-  }else{
-    XBT_WARN("The scheduling is ignored");
-  }
-  xbt_dynar_t computer = NULL;
-  xbt_dict_cursor_t dict_cursor;
-  char *computer_name;
-  xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
-    xbt_dynar_free(&computer);
-  }
-  xbt_dict_free(&computers);
-  xbt_dynar_free(&result);
-  return NULL;
+  return SD_dotload_generic(filename, sequential, true);
 }
 
 }
 
-#ifdef HAVE_CGRAPH_H
 static int edge_compare(const void *a, const void *b)
 {
   unsigned va = AGSEQ(*(Agedge_t **)a);
   unsigned vb = AGSEQ(*(Agedge_t **)b);
   return va == vb ? 0 : (va < vb ? -1 : 1);
 }
 static int edge_compare(const void *a, const void *b)
 {
   unsigned va = AGSEQ(*(Agedge_t **)a);
   unsigned vb = AGSEQ(*(Agedge_t **)b);
   return va == vb ? 0 : (va < vb ? -1 : 1);
 }
-#endif
 
 
-xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par){
+xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par, bool schedule){
   xbt_assert(filename, "Unable to use a null file descriptor\n");
   xbt_assert(filename, "Unable to use a null file descriptor\n");
+  FILE *in_file = fopen(filename, "r");
+  xbt_assert(in_file != nullptr, "Failed to open file: %s", filename);
+
   unsigned int i;
   unsigned int i;
+  SD_task_t root;
+  SD_task_t end;
+  SD_task_t task;
+  xbt_dict_t computers;
+  xbt_dynar_t computer = nullptr;
+  xbt_dict_cursor_t dict_cursor;
+  bool schedule_success = true;
+
+  xbt_dict_t jobs = xbt_dict_new_homogeneous(nullptr);
   xbt_dynar_t result = xbt_dynar_new(sizeof(SD_task_t), dot_task_p_free);
   xbt_dynar_t result = xbt_dynar_new(sizeof(SD_task_t), dot_task_p_free);
-  jobs = xbt_dict_new_homogeneous(NULL);
-  FILE *in_file = fopen(filename, "r");
-  if (in_file == NULL)
-    xbt_die("Failed to open file: %s", filename);
+
   Agraph_t * dag_dot = agread(in_file, NIL(Agdisc_t *));
   Agraph_t * dag_dot = agread(in_file, NIL(Agdisc_t *));
-  SD_task_t root, end, task;
 
   if (schedule)
 
   if (schedule)
-    computers = xbt_dict_new_homogeneous(NULL);
+    computers = xbt_dict_new_homogeneous(nullptr);
 
   /* Create all the nodes */
 
   /* Create all the nodes */
-  Agnode_t *node = NULL;
+  Agnode_t *node = nullptr;
   for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
     char *name = agnameof(node);
   for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
     char *name = agnameof(node);
-    double amount = atof(agget(node, (char *) "size"));
-    double alpha = 0.0;
-
-    if (!(task = (SD_task_t)xbt_dict_get_or_null(jobs, name))) {
+    double amount = atof(agget(node, (char*)"size"));
+    task = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, name));
+    if (task == nullptr) {
       if (seq_or_par == sequential){
         XBT_DEBUG("See <job id=%s amount =%.0f>", name, amount);
       if (seq_or_par == sequential){
         XBT_DEBUG("See <job id=%s amount =%.0f>", name, amount);
-        task = SD_task_create_comp_seq(name, NULL , amount);
+        task = SD_task_create_comp_seq(name, nullptr , amount);
       } else {
       } else {
-        alpha = atof(agget(node, (char *) "alpha"));
+        double alpha = atof(agget(node, (char *) "alpha"));
         XBT_DEBUG("See <job id=%s amount =%.0f alpha = %.3f>", name, amount, alpha);
         XBT_DEBUG("See <job id=%s amount =%.0f alpha = %.3f>", name, amount, alpha);
-        task = SD_task_create_comp_par_amdahl(name, NULL , amount, alpha);
+        task = SD_task_create_comp_par_amdahl(name, nullptr , amount, alpha);
       }
 
       }
 
-      xbt_dict_set(jobs, name, task, NULL);
+      xbt_dict_set(jobs, name, task, nullptr);
 
 
-      if (!strcmp(name, "root")){
-        /* by design the root task is always SCHEDULABLE */
-        SD_task_set_state(task, SD_SCHEDULABLE);
-        /* Put it at the beginning of the dynar */
-        xbt_dynar_insert_at(result, 0, &task);
-      } else {
-        if (!strcmp(name, "end")){
-          XBT_DEBUG("Declaration of the 'end' node, don't store it yet.");
-          end = task;
-          /* Should be inserted later in the dynar */
-        } else {
-          xbt_dynar_push(result, &task);
-        }
-      }
+      if (strcmp(name,"root") && strcmp(name,"end"))
+        xbt_dynar_push(result, &task);
 
 
-      if((seq_or_par == sequential) && (schedule || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose))){
+      if((seq_or_par == sequential) &&
+        ((schedule && schedule_success) || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose))){
         /* try to take the information to schedule the task only if all is right*/
         /* try to take the information to schedule the task only if all is right*/
-        int performer, order;
         char *char_performer = agget(node, (char *) "performer");
         char *char_order = agget(node, (char *) "order");
         char *char_performer = agget(node, (char *) "performer");
         char *char_order = agget(node, (char *) "order");
-        /* performer is the computer which execute the task */
-        performer = ((!char_performer || !strcmp(char_performer,"")) ? -1:atoi(char_performer));
-        /* order is giving the task order on one computer */
-        order = ((!char_order || !strcmp(char_order, ""))? -1:atoi(char_order));
-
-        XBT_DEBUG ("Task '%s' is scheduled on workstation '%d' in position '%d'", task->name, performer, order);
-        xbt_dynar_t computer = NULL;
-        if(performer != -1 && order != -1){
-          /* required parameters are given */
-          computer = (xbt_dynar_t)xbt_dict_get_or_null(computers, char_performer);
-          if(computer == NULL){
-            computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
-            xbt_dict_set(computers, char_performer, computer, NULL);
+        /* Tasks will execute on in a given "order" on a given set of "performer" hosts */
+        int performer = ((!char_performer || !strcmp(char_performer,"")) ? -1:atoi(char_performer));
+        int order = ((!char_order || !strcmp(char_order, ""))? -1:atoi(char_order));
+
+        if((performer != -1 && order != -1) && performer < (int) sg_host_count()){
+          /* required parameters are given and less performers than hosts are required */
+          XBT_DEBUG ("Task '%s' is scheduled on workstation '%d' in position '%d'", task->name, performer, order);
+          computer = static_cast<xbt_dynar_t> (xbt_dict_get_or_null(computers, char_performer));
+          if(computer == nullptr){
+            computer = xbt_dynar_new(sizeof(SD_task_t), nullptr);
+            xbt_dict_set(computers, char_performer, computer, nullptr);
           }
           }
-          if(performer < xbt_dict_length(host_list)){
-            /* the wanted computer is available */
-            SD_task_t *task_test = NULL;
-            if((unsigned int)order < computer->used)
-              task_test = (SD_task_t *)xbt_dynar_get_ptr(computer,order);
-            if(task_test != NULL && *task_test != NULL && *task_test != task){
-              /* the user gives the same order to several tasks */
-              schedule = false;
-              XBT_VERB("The task %s starts on the computer %s at the position : %s like the task %s",
-                     (*task_test)->name, char_performer, char_order, task->name);
-            }else{
-              /* the parameter seems to be ok */
-              xbt_dynar_set_as(computer, order, SD_task_t, task);
+
+          if(static_cast<unsigned int>(order) < xbt_dynar_length(computer)){
+            SD_task_t *task_test = (SD_task_t *)xbt_dynar_get_ptr(computer,order);
+            if(*task_test && *task_test != task){
+              /* the user gave the same order to several tasks */
+              schedule_success = false;
+              XBT_VERB("Task '%s' wants to start on performer '%s' at the same position '%s' as task '%s'",
+                       (*task_test)->name, char_performer, char_order, task->name);
+              continue;
             }
             }
-          }else{
-            /* the platform has not enough processors to schedule the DAG like
-             * the user wants*/
-            schedule = false;
-            XBT_VERB("The schedule is ignored, there are not enough computers");
           }
           }
-        }
-        else {
+          /* the parameter seems to be ok */
+          xbt_dynar_set_as(computer, order, SD_task_t, task);
+        } else {
           /* one of required parameters is not given */
           /* one of required parameters is not given */
-          schedule = false;
-          XBT_VERB("The schedule is ignored, the task %s is not correctly scheduled", task->name);
+          schedule_success = false;
+          XBT_VERB("The schedule is ignored, task '%s' can not be scheduled on %d hosts", task->name, performer);
         }
       }
     } else {
         }
       }
     } else {
@@ -213,93 +138,75 @@ xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par){
   }
 
   /*Check if 'root' and 'end' nodes have been explicitly declared.  If not, create them. */
   }
 
   /*Check if 'root' and 'end' nodes have been explicitly declared.  If not, create them. */
-  if (!(root = (SD_task_t)xbt_dict_get_or_null(jobs, "root"))){
-    if (seq_or_par == sequential)
-      root = SD_task_create_comp_seq("root", NULL, 0);
-    else
-      root = SD_task_create_comp_par_amdahl("root", NULL, 0, 0);
-    /* by design the root task is always SCHEDULABLE */
-    SD_task_set_state(root, SD_SCHEDULABLE);
-    /* Put it at the beginning of the dynar */
-    xbt_dynar_insert_at(result, 0, &root);
-  }
+  root = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, "root"));
+  if (root == nullptr)
+    root = (seq_or_par == sequential?SD_task_create_comp_seq("root", nullptr, 0):
+                                     SD_task_create_comp_par_amdahl("root", nullptr, 0, 0));
 
 
-  if (!(end = (SD_task_t)xbt_dict_get_or_null(jobs, "end"))){
-    if (seq_or_par == sequential)
-      end = SD_task_create_comp_seq("end", NULL, 0);
-    else
-      end = SD_task_create_comp_par_amdahl("end", NULL, 0, 0);
-  }
+  SD_task_set_state(root, SD_SCHEDULABLE);   /* by design the root task is always SCHEDULABLE */
+  xbt_dynar_insert_at(result, 0, &root);     /* Put it at the beginning of the dynar */
+
+  end = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, "end"));
+  if (end == nullptr)
+    end = (seq_or_par == sequential?SD_task_create_comp_seq("end", nullptr, 0):
+                                    SD_task_create_comp_par_amdahl("end", nullptr, 0, 0));
 
   /* Create edges */
 
   /* Create edges */
-  xbt_dynar_t edges = xbt_dynar_new(sizeof(Agedge_t*), NULL);
+  xbt_dynar_t edges = xbt_dynar_new(sizeof(Agedge_t*), nullptr);
   for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
   for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
-    unsigned cursor;
     Agedge_t * edge;
     xbt_dynar_reset(edges);
     for (edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge))
       xbt_dynar_push_as(edges, Agedge_t *, edge);
     Agedge_t * edge;
     xbt_dynar_reset(edges);
     for (edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge))
       xbt_dynar_push_as(edges, Agedge_t *, edge);
-#ifdef HAVE_CGRAPH_H
-    /* Hack: circumvent a bug in libcgraph, where the edges are not always given back in creation order.  We sort them
-     * again, according to their sequence id.  The problem appears to be solved (i.e.: I did not test it) in graphviz'
-     * mercurial repository by the following changeset:
-     *    changeset:   8431:d5f1fb7e8103
-     *    user:        Emden Gansner <erg@research.att.com>
-     *    date:        Tue Oct 11 12:38:58 2011 -0400
-     *    summary:     Make sure edges are stored in node creation order
-     * It should be fixed in graphviz 2.30 and above.
-     */
+
+    /* Be sure edges are sorted */
     xbt_dynar_sort(edges, edge_compare);
     xbt_dynar_sort(edges, edge_compare);
-#endif
-    xbt_dynar_foreach(edges, cursor, edge) {
-      SD_task_t src, dst;
+
+    xbt_dynar_foreach(edges, i, edge) {
       char *src_name=agnameof(agtail(edge));
       char *dst_name=agnameof(aghead(edge));
       double size = atof(agget(edge, (char *) "size"));
 
       char *src_name=agnameof(agtail(edge));
       char *dst_name=agnameof(aghead(edge));
       double size = atof(agget(edge, (char *) "size"));
 
-      src = (SD_task_t)xbt_dict_get_or_null(jobs, src_name);
-      dst = (SD_task_t)xbt_dict_get_or_null(jobs, dst_name);
+      SD_task_t src = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, src_name));
+      SD_task_t dst = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, dst_name));
 
       if (size > 0) {
 
       if (size > 0) {
-        char *name = (char*)xbt_malloc((strlen(src_name)+strlen(dst_name)+6)*sizeof(char));
-        sprintf(name, "%s->%s", src_name, dst_name);
+        char *name = bprintf("%s->%s", src_name, dst_name);
         XBT_DEBUG("See <transfer id=%s amount = %.0f>", name, size);
         XBT_DEBUG("See <transfer id=%s amount = %.0f>", name, size);
-        if (!(task = (SD_task_t)xbt_dict_get_or_null(jobs, name))) {
+        task = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, name));
+        if (task == nullptr) {
           if (seq_or_par == sequential)
           if (seq_or_par == sequential)
-            task = SD_task_create_comm_e2e(name, NULL , size);
+            task = SD_task_create_comm_e2e(name, nullptr , size);
           else
           else
-            task = SD_task_create_comm_par_mxn_1d_block(name, NULL , size);
-          SD_task_dependency_add(NULL, NULL, src, task);
-          SD_task_dependency_add(NULL, NULL, task, dst);
-          xbt_dict_set(jobs, name, task, NULL);
+            task = SD_task_create_comm_par_mxn_1d_block(name, nullptr , size);
+          SD_task_dependency_add(nullptr, nullptr, src, task);
+          SD_task_dependency_add(nullptr, nullptr, task, dst);
+          xbt_dict_set(jobs, name, task, nullptr);
           xbt_dynar_push(result, &task);
         } else {
           XBT_WARN("Task '%s' is defined more than once", name);
         }
         xbt_free(name);
       } else {
           xbt_dynar_push(result, &task);
         } else {
           XBT_WARN("Task '%s' is defined more than once", name);
         }
         xbt_free(name);
       } else {
-        SD_task_dependency_add(NULL, NULL, src, dst);
+        SD_task_dependency_add(nullptr, nullptr, src, dst);
       }
     }
   }
   xbt_dynar_free(&edges);
 
       }
     }
   }
   xbt_dynar_free(&edges);
 
-  /* all compute and transfer tasks have been created, put the "end" node at the end of dynar */
   XBT_DEBUG("All tasks have been created, put %s at the end of the dynar", end->name);
   xbt_dynar_push(result, &end);
 
   /* Connect entry tasks to 'root', and exit tasks to 'end'*/
   xbt_dynar_foreach (result, i, task){
   XBT_DEBUG("All tasks have been created, put %s at the end of the dynar", end->name);
   xbt_dynar_push(result, &end);
 
   /* Connect entry tasks to 'root', and exit tasks to 'end'*/
   xbt_dynar_foreach (result, i, task){
-    if (task == root || task == end)
-      continue;
-    if (xbt_dynar_is_empty(task->tasks_before)) {
-      XBT_DEBUG("file '%s' has no source. Add dependency from 'root'", task->name);
-      SD_task_dependency_add(NULL, NULL, root, task);
+    if (task->predecessors->empty() && task->inputs->empty() && task != root) {
+      XBT_DEBUG("Task '%s' has no source. Add dependency from 'root'", task->name);
+      SD_task_dependency_add(nullptr, nullptr, root, task);
     }
 
     }
 
-    if (xbt_dynar_is_empty(task->tasks_after)) {
-      XBT_DEBUG("file '%s' has no destination. Add dependency to 'end'", task->name);
-      SD_task_dependency_add(NULL, NULL, task, end);
+    if (task->successors->empty() && task->outputs->empty() && task != end) {
+      XBT_DEBUG("Task '%s' has no destination. Add dependency to 'end'", task->name);
+      SD_task_dependency_add(nullptr, nullptr, task, end);
     }
   }
 
     }
   }
 
@@ -307,10 +214,53 @@ xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par){
   xbt_dict_free(&jobs);
   fclose(in_file);
 
   xbt_dict_free(&jobs);
   fclose(in_file);
 
-  if (!acyclic_graph_detail(result)) {
-    XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.", basename((char*)filename));
+  if(schedule){
+    char *computer_name;
+    if (schedule_success) {
+      const sg_host_t *workstations = sg_host_list ();
+      xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
+        SD_task_t previous_task = nullptr;
+        xbt_dynar_foreach(computer, i, task){
+          /* add dependency between the previous and the task to avoid parallel execution */
+          if(task){
+            if(previous_task && !SD_task_dependency_exists(previous_task, task))
+              SD_task_dependency_add(nullptr, nullptr, previous_task, task);
+
+            SD_task_schedulel(task, 1, workstations[atoi(computer_name)]);
+            previous_task = task;
+          }
+        }
+        xbt_dynar_free(&computer);
+      }
+      xbt_dict_free(&computers);
+    } else {
+      XBT_WARN("The scheduling is ignored");
+      xbt_dict_foreach(computers,dict_cursor,computer_name,computer)
+        xbt_dynar_free(&computer);
+      xbt_dict_free(&computers);
+      xbt_dynar_free(&result);
+      result = nullptr;
+    }
+  }
+
+  if (result && !acyclic_graph_detail(result)) {
+    char* base = xbt_basename(filename);
+    XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.", base);
+    free(base);
     xbt_dynar_free(&result);
     xbt_dynar_free(&result);
-    result = NULL;
+    result = nullptr;
   }
   return result;
 }
   }
   return result;
 }
+#else
+xbt_dynar_t SD_dotload(const char *filename) {
+  xbt_die("SD_dotload_generic() is not usable because graphviz was not found.\n"
+      "Please install graphviz, graphviz-dev, and libgraphviz-dev (and erase CMakeCache.txt) before recompiling.");
+}
+xbt_dynar_t SD_dotload_with_sched(const char *filename) {
+  return SD_dotload(filename);
+}
+xbt_dynar_t SD_PTG_dotload(const char * filename) {
+  return SD_dotload(filename);
+}
+#endif