Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simdag/dotloader,example/simdag/dot] correction of the warning at the
[simgrid.git] / src / simdag / sd_dotloader.c
index 31b9e8b..f62679a 100644 (file)
@@ -8,15 +8,22 @@
 #include "simdag/simdag.h"
 #include "xbt/misc.h"
 #include "xbt/log.h"
+#include <stdbool.h>
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_dotparse, sd, "Parsing DOT files");
 
 #undef CLEANUP
-#include <cgraph.h>
+
+#ifdef HAVE_CGRAPH_H
+#include <graphviz/cgraph.h>
+#elif HAVE_AGRAPH_H
+#include <graphviz/agraph.h>
+#endif
 
 void dot_add_task(Agnode_t * dag_node);
 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge);
 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge);
+bool child_are_marked(SD_task_t task);
 xbt_dynar_t SD_dotload_FILE(FILE * in_file);
 
 static double dot_parse_double(const char *string)
@@ -37,7 +44,7 @@ static int dot_parse_int(const char *string)
   if (string == NULL)
     return -10;
   int ret = 0;
-  int value;
+  int value = -1;
 
   ret = sscanf(string, "%d", &value);
   if (ret != 1)
@@ -48,8 +55,10 @@ static int dot_parse_int(const char *string)
 static xbt_dynar_t result;
 static xbt_dict_t jobs;
 static xbt_dict_t files;
+static xbt_dict_t computers;
 static SD_task_t root_task, end_task;
 static Agraph_t *dag_dot;
+static bool schedule = true;
 
 static void dump_res()
 {
@@ -61,12 +70,108 @@ static void dump_res()
   }
 }
 
+bool child_are_marked(SD_task_t task){
+  SD_task_t child_task = NULL;
+  bool all_marked = true;
+  SD_dependency_t depafter = NULL;
+  unsigned int count;
+  xbt_dynar_foreach(task->tasks_after,count,depafter){
+    child_task = depafter->dst;
+    //test marked
+    if(child_task->marked == 0) {
+      all_marked = false;
+      break;
+    }
+    child_task = NULL;
+  }
+  return all_marked;
+}
+
+bool acyclic_graph_detection(xbt_dynar_t dag){
+  unsigned int count=0, count_current=0;
+  bool all_marked = true;
+  SD_task_t task = NULL, parent_task = NULL;
+  SD_dependency_t depbefore = NULL;
+  xbt_dynar_t next = NULL, current = xbt_dynar_new(sizeof(SD_task_t),NULL);
+
+  xbt_dynar_foreach(dag,count,task){
+    if(task->kind == SD_TASK_COMM_E2E) continue;
+    task->marked = 0;
+    if(xbt_dynar_length(task->tasks_after) == 0){
+      xbt_dynar_push(current, &task);
+    }
+  }
+  task = NULL;
+  count = 0;
+  //test if something has to be done for the next iteration
+  while(xbt_dynar_length(current) != 0){
+    next = xbt_dynar_new(sizeof(SD_task_t),NULL);
+    //test if the current iteration is done
+    count_current=0;
+    xbt_dynar_foreach(current,count_current,task){
+      if (task == NULL) continue;
+      count = 0;
+      //push task in next
+      task->marked = 1;
+      count = 0;
+      xbt_dynar_foreach(task->tasks_before,count,depbefore){
+        parent_task = depbefore->src;
+        if(parent_task->kind == SD_TASK_COMM_E2E){
+          unsigned int j=0;
+          parent_task->marked = 1;
+          xbt_dynar_foreach(parent_task->tasks_before,j,depbefore){
+            parent_task = depbefore->src;
+            if(child_are_marked(parent_task))
+              xbt_dynar_push(next, &parent_task);
+          }
+        } else{
+          if(child_are_marked(parent_task))
+            xbt_dynar_push(next, &parent_task);
+        }
+        parent_task = NULL;
+      }
+      task = NULL;
+      count = 0;
+    }
+    xbt_dynar_free(&current);
+    current = next;
+    next = NULL;
+  }
+  xbt_dynar_free(&current);
+  current = NULL;
+  all_marked = true;
+  xbt_dynar_foreach(dag,count,task){
+    if(task->kind == SD_TASK_COMM_E2E) continue;
+    //test if all tasks are marked
+    if(task->marked == 0){
+      WARN1("test %s",task->name);
+      all_marked = false;
+      break;
+    }
+  }
+  task = NULL;
+  if(!all_marked){
+    DEBUG0("there are a cycle in your DAG");
+  }
+  return all_marked;
+}
+
 static void dot_task_free(void *task)
 {
   SD_task_t t = task;
   SD_task_destroy(t);
 }
 
+static void TRACE_sd_dotloader (SD_task_t task, const char *category)
+{
+  if (category){
+    if (strlen (category) != 0){
+      TRACE_category (category);
+      TRACE_sd_set_task_category (task, category);
+    }
+  }
+}
+
 /** @brief loads a DOT file describing a DAG
  * 
  * See http://www.graphviz.org/doc/info/lang.html
@@ -82,9 +187,56 @@ xbt_dynar_t SD_dotload(const char *filename)
   xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
   SD_dotload_FILE(in_file);
   fclose(in_file);
+  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);
   return result;
 }
 
+xbt_dynar_t SD_dotload_with_sched(const char *filename){
+  FILE *in_file = fopen(filename, "r");
+  xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
+  SD_dotload_FILE(in_file);
+  fclose(in_file);
+
+  if(schedule == true){
+    xbt_dynar_t computer = NULL;
+    xbt_dict_cursor_t dict_cursor;
+    char *computer_name;
+    const SD_workstation_t *workstations = SD_workstation_get_list ();
+    xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
+      int count_computer = dot_parse_int(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_detection(result))
+      return result;
+    else
+      WARN0("There are a cycle in your task graph");
+  }else{
+    WARN0("No scheduling provided");
+  }
+  return NULL;
+}
+
 xbt_dynar_t SD_dotload_FILE(FILE * in_file)
 {
   xbt_assert0(in_file, "Unable to use a null file descriptor\n");
@@ -93,6 +245,7 @@ xbt_dynar_t SD_dotload_FILE(FILE * in_file)
   result = xbt_dynar_new(sizeof(SD_task_t), dot_task_free);
   files = xbt_dict_new();
   jobs = xbt_dict_new();
+  computers = xbt_dict_new();
   root_task = SD_task_create_comp_seq("root", NULL, 0);
   /* by design the root task is always SCHEDULABLE */
   __SD_task_set_state(root_task, SD_SCHEDULABLE);
@@ -104,8 +257,14 @@ xbt_dynar_t SD_dotload_FILE(FILE * in_file)
 
   Agnode_t *dag_node = NULL;
   for (dag_node = agfstnode(dag_dot); dag_node;
-       dag_node = agnxtnode(dag_dot, dag_node)) {
-    dot_add_task(dag_node);
+#ifdef HAVE_CGRAPH_H
+       dag_node = agnxtnode(dag_dot, dag_node)
+#elif HAVE_AGRAPH_H
+       dag_node = agnxtnode(dag_node)
+#endif
+       ) {
+
+  dot_add_task(dag_node);
   }
   agclose(dag_dot);
   xbt_dict_free(&jobs);
@@ -161,8 +320,9 @@ xbt_dynar_t SD_dotload_FILE(FILE * in_file)
 
   /* Free previous copy of the files */
   xbt_dict_free(&files);
-
-  return result;
+  if(acyclic_graph_detection(result))
+    return result;
+  return NULL;
 }
 
 /* dot_add_task create a sd_task and all transfers required for this
@@ -173,34 +333,103 @@ void dot_add_task(Agnode_t * dag_node)
   char *name = agnameof(dag_node);
   SD_task_t current_job;
   double runtime = dot_parse_double(agget(dag_node, (char *) "size"));
-  long performer =
-      (long) dot_parse_int((char *) agget(dag_node, (char *) "performer"));
-  INFO3("See <job id=%s runtime=%s %.0f>", name,
+
+  DEBUG3("See <job id=%s runtime=%s %.0f>", name,
         agget(dag_node, (char *) "size"), runtime);
   current_job = xbt_dict_get_or_null(jobs, name);
   if (current_job == NULL) {
     current_job =
-        SD_task_create_comp_seq(name, (void *) performer, runtime);
+        SD_task_create_comp_seq(name, NULL , runtime);
+#ifdef HAVE_TRACING
+   TRACE_sd_dotloader (current_job, agget (dag_node, (char*)"category"));
+#endif
     xbt_dict_set(jobs, name, current_job, NULL);
     xbt_dynar_push(result, &current_job);
   }
   Agedge_t *e;
   int count = 0;
-  for (e = agfstin(dag_dot, dag_node); e; e = agnxtin(dag_dot, e)) {
-    dot_add_input_dependencies(current_job, e);
-    count++;
+
+#ifdef HAVE_CGRAPH_H
+  for (e = agfstin(dag_dot, dag_node); e; e = agnxtin(dag_dot, e))
+#elif HAVE_AGRAPH_H
+  for (e = agfstin(dag_node); e; e = agnxtin(e))
+#endif
+  {
+  dot_add_input_dependencies(current_job, e);
+  count++;
   }
   if (count == 0 && current_job != root_task) {
     SD_task_dependency_add(NULL, NULL, root_task, current_job);
   }
   count = 0;
-  for (e = agfstout(dag_dot, dag_node); e; e = agnxtout(dag_dot, e)) {
+#ifdef HAVE_CGRAPH_H
+  for (e = agfstout(dag_dot, dag_node); e; e = agnxtout(dag_dot, e))
+#elif HAVE_AGRAPH_H
+  for (e = agfstout(dag_node); e; e = agnxtout(e))
+#endif
+  {
+
     dot_add_output_dependencies(current_job, e);
     count++;
   }
   if (count == 0 && current_job != end_task) {
     SD_task_dependency_add(NULL, NULL, current_job, end_task);
   }
+
+  if(schedule){
+    /* try to take the information to schedule the task only if all is
+     * right*/
+    // performer is the computer which execute the task
+    unsigned long performer = -1;
+    char * char_performer = agget(dag_node, (char *) "performer");
+    if (char_performer != NULL)
+      performer = (long) dot_parse_int(char_performer);
+
+    // order is giving the task order on one computer
+    unsigned long order = -1;
+    char * char_order = agget(dag_node, (char *) "order");
+    if (char_order != NULL)
+      order = (long) dot_parse_int(char_order);
+    xbt_dynar_t computer = NULL;
+    //INFO2("performer = %d, order=%d",performer,order);
+    if(performer != -1 && order != -1){
+      //necessary parameters are given
+      computer = 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);
+      }
+      if(performer < sd_global->workstation_count){
+        // the  wanted computer is available
+        SD_task_t *task_test = NULL;
+        if(order < computer->used)
+          task_test = xbt_dynar_get_ptr(computer,order);
+        if(task_test != NULL && *task_test != NULL && *task_test != current_job){
+          /*the user gives the same order to several tasks*/
+          schedule = false;
+          DEBUG0("scheduling does not take into account, several task has\
+                the same order");
+        }else{
+          //the parameter seems to be ok
+          xbt_dynar_set_as(computer, order, SD_task_t, current_job);
+        }
+      }else{
+        /*the platform has not enough processors to schedule the DAG like
+        *the user wants*/
+        schedule = false;
+        DEBUG0("scheduling does not take into account, not enough computers");
+      }
+    }
+    else if((performer == -1 && order != -1) ||
+            (performer != -1 && order == -1)){
+      //one of necessary parameters are not given
+      schedule = false;
+      DEBUG0("scheduling does not take into account");
+    } else {
+      //No schedule available
+      schedule = false;
+    }
+  }
 }
 
 /* dot_add_output_dependencies create the dependencies between a task
@@ -214,13 +443,15 @@ void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge)
   char name[80];
   sprintf(name, "%s->%s", agnameof(agtail(edge)), agnameof(aghead(edge)));
   double size = dot_parse_double(agget(edge, (char *) "size"));
-  INFO2("size : %e, get size : %s", size, agget(edge, (char *) "size"));
-  //int sender = dot_parse_int(agget(edge,(char*)"sender"));
-  //int reciever = dot_parse_int(agget(edge,(char*)"reciever"));
+  DEBUG2("size : %e, get size : %s", size, agget(edge, (char *) "size"));
+
   if (size > 0) {
     file = xbt_dict_get_or_null(files, name);
     if (file == NULL) {
       file = SD_task_create_comm_e2e(name, NULL, size);
+#ifdef HAVE_TRACING
+      TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
+#endif
       xbt_dict_set(files, name, file, &dot_task_free);
     } else {
       if (SD_task_get_amount(file) != size) {
@@ -247,15 +478,15 @@ void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge)
   char name[80];
   sprintf(name, "%s->%s", agnameof(agtail(edge)), agnameof(aghead(edge)));
   double size = dot_parse_double(agget(edge, (char *) "size"));
-  INFO2("size : %e, get size : %s", size, agget(edge, (char *) "size"));
-  //int sender = dot_parse_int(agget(edge,(char*)"sender"));
-  //int reciever = dot_parse_int(agget(edge,(char*)"reciever"));
+  DEBUG2("size : %e, get size : %s", size, agget(edge, (char *) "size"));
 
-  //INFO2("See <uses file=%s %s>",A_dot__uses_file,(is_input?"in":"out"));
   if (size > 0) {
     file = xbt_dict_get_or_null(files, name);
     if (file == NULL) {
       file = SD_task_create_comm_e2e(name, NULL, size);
+#ifdef HAVE_TRACING
+      TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
+#endif
       xbt_dict_set(files, name, file, &dot_task_free);
     } else {
       if (SD_task_get_amount(file) != size) {