Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simdag/dot,simdag/dax] move the test for acyclic graph to the daxloader
[simgrid.git] / src / simdag / sd_dotloader.c
1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "private.h"
8 #include "simdag/simdag.h"
9 #include "xbt/misc.h"
10 #include "xbt/log.h"
11 #include <stdbool.h>
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_dotparse, sd, "Parsing DOT files");
14
15 #undef CLEANUP
16
17 #ifdef HAVE_CGRAPH_H
18 #include <graphviz/cgraph.h>
19 #elif HAVE_AGRAPH_H
20 #include <graphviz/agraph.h>
21 #endif
22
23 void dot_add_task(Agnode_t * dag_node);
24 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge);
25 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge);
26 xbt_dynar_t SD_dotload_FILE(FILE * in_file);
27
28 static double dot_parse_double(const char *string)
29 {
30   if (string == NULL)
31     return -1;
32   int ret = 0;
33   double value = -1;
34
35   ret = sscanf(string, "%lg", &value);
36   if (ret != 1)
37     WARN1("%s is not a double", string);
38   return value;
39 }
40
41 static int dot_parse_int(const char *string)
42 {
43   if (string == NULL)
44     return -10;
45   int ret = 0;
46   int value = -1;
47
48   ret = sscanf(string, "%d", &value);
49   if (ret != 1)
50     WARN1("%s is not an integer", string);
51   return value;
52 }
53
54 static xbt_dynar_t result;
55 static xbt_dict_t jobs;
56 static xbt_dict_t files;
57 static xbt_dict_t computers;
58 static SD_task_t root_task, end_task;
59 static Agraph_t *dag_dot;
60 static bool schedule = true;
61
62 static void dump_res()
63 {
64   unsigned int cursor;
65   SD_task_t task;
66   xbt_dynar_foreach(result, cursor, task) {
67     INFO1("Task %d", cursor);
68     SD_task_dump(task);
69   }
70 }
71
72
73 static void dot_task_free(void *task)
74 {
75   SD_task_t t = task;
76   SD_task_destroy(t);
77 }
78
79 static void TRACE_sd_dotloader (SD_task_t task, const char *category)
80 {
81   if (category){
82     if (strlen (category) != 0){
83       TRACE_category (category);
84       TRACE_sd_set_task_category (task, category);
85     }
86   }
87 }
88
89 /** @brief loads a DOT file describing a DAG
90  * 
91  * See http://www.graphviz.org/doc/info/lang.html
92  * for more details.
93  * To obtain information about transfers and tasks, two attributes are
94  * required : size on task (execution time in Flop) and size on edge
95  * (the amount of data transfer in bit).
96  * if they aren't here, there choose to be equal to zero.
97  */
98 xbt_dynar_t SD_dotload(const char *filename)
99 {
100   FILE *in_file = fopen(filename, "r");
101   xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
102   SD_dotload_FILE(in_file);
103   fclose(in_file);
104   xbt_dynar_t computer = NULL;
105   xbt_dict_cursor_t dict_cursor;
106   char *computer_name;
107   xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
108     xbt_dynar_free(&computer);
109   }
110   xbt_dict_free(&computers);
111   return result;
112 }
113
114 xbt_dynar_t SD_dotload_with_sched(const char *filename){
115   FILE *in_file = fopen(filename, "r");
116   xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
117   SD_dotload_FILE(in_file);
118   fclose(in_file);
119
120   if(schedule == true){
121     xbt_dynar_t computer = NULL;
122     xbt_dict_cursor_t dict_cursor;
123     char *computer_name;
124     const SD_workstation_t *workstations = SD_workstation_get_list ();
125     xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
126       int count_computer = dot_parse_int(computer_name);
127       unsigned int count=0;
128       SD_task_t task;
129       SD_task_t task_previous = NULL;
130       xbt_dynar_foreach(computer,count,task){
131         // add dependency between the previous and the task to avoid
132         // parallel execution
133         if(task != NULL ){
134           if(task_previous != NULL &&
135              !SD_task_dependency_exists(task_previous, task))
136             SD_task_dependency_add(NULL, NULL, task_previous, task);
137           SD_task_schedulel(task, 1, workstations[count_computer]);
138           task_previous = task;
139         }
140       }
141       xbt_dynar_free(&computer);
142     }
143     xbt_dict_free(&computers);
144     if(acyclic_graph_detection(result))
145       return result;
146     else
147       WARN0("There is at least one cycle in the provided task graph");
148   }else{
149     WARN0("The scheduling is ignored");
150   }
151   return NULL;
152 }
153
154 xbt_dynar_t SD_dotload_FILE(FILE * in_file)
155 {
156   xbt_assert0(in_file, "Unable to use a null file descriptor\n");
157   dag_dot = agread(in_file, NIL(Agdisc_t *));
158
159   result = xbt_dynar_new(sizeof(SD_task_t), dot_task_free);
160   files = xbt_dict_new();
161   jobs = xbt_dict_new();
162   computers = xbt_dict_new();
163   root_task = SD_task_create_comp_seq("root", NULL, 0);
164   /* by design the root task is always SCHEDULABLE */
165   __SD_task_set_state(root_task, SD_SCHEDULABLE);
166
167   xbt_dict_set(jobs, "root", root_task, NULL);
168   xbt_dynar_push(result, &root_task);
169   end_task = SD_task_create_comp_seq("end", NULL, 0);
170   xbt_dict_set(jobs, "end", end_task, NULL);
171
172   Agnode_t *dag_node = NULL;
173   for (dag_node = agfstnode(dag_dot); dag_node;
174 #ifdef HAVE_CGRAPH_H
175        dag_node = agnxtnode(dag_dot, dag_node)
176 #elif HAVE_AGRAPH_H
177        dag_node = agnxtnode(dag_node)
178 #endif
179        ) {
180
181   dot_add_task(dag_node);
182   }
183   agclose(dag_dot);
184   xbt_dict_free(&jobs);
185
186   /* And now, post-process the files.
187    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
188    * Files not produced in the system are said to be produced by root task (top of DAG).
189    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
190    */
191   xbt_dict_cursor_t cursor;
192   SD_task_t file;
193   char *name;
194   xbt_dict_foreach(files, cursor, name, file) {
195     unsigned int cpt1, cpt2;
196     SD_task_t newfile = NULL;
197     SD_dependency_t depbefore, depafter;
198     if (xbt_dynar_length(file->tasks_before) == 0) {
199       xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
200         SD_task_t newfile =
201             SD_task_create_comm_e2e(file->name, NULL, file->amount);
202         SD_task_dependency_add(NULL, NULL, root_task, newfile);
203         SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
204         xbt_dynar_push(result, &newfile);
205       }
206     } else if (xbt_dynar_length(file->tasks_after) == 0) {
207       xbt_dynar_foreach(file->tasks_before, cpt2, depbefore) {
208         SD_task_t newfile =
209             SD_task_create_comm_e2e(file->name, NULL, file->amount);
210         SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
211         SD_task_dependency_add(NULL, NULL, newfile, end_task);
212         xbt_dynar_push(result, &newfile);
213       }
214     } else {
215       xbt_dynar_foreach(file->tasks_before, cpt1, depbefore) {
216         xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
217           if (depbefore->src == depafter->dst) {
218             WARN2
219                 ("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
220                  file->name, depbefore->src->name);
221           }
222           newfile =
223               SD_task_create_comm_e2e(file->name, NULL, file->amount);
224           SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
225           SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
226           xbt_dynar_push(result, &newfile);
227         }
228       }
229     }
230   }
231
232   /* Push end task last */
233   xbt_dynar_push(result, &end_task);
234
235   /* Free previous copy of the files */
236   xbt_dict_free(&files);
237   if(acyclic_graph_detection(result))
238     return result;
239   acyclic_graph_detail(result);
240   return NULL;
241 }
242
243 /* dot_add_task create a sd_task and all transfers required for this
244  * task. The execution time of the task is given by the attribute size.
245  * The unit of size is the Flop.*/
246 void dot_add_task(Agnode_t * dag_node)
247 {
248   char *name = agnameof(dag_node);
249   SD_task_t current_job;
250   double runtime = dot_parse_double(agget(dag_node, (char *) "size"));
251
252   DEBUG3("See <job id=%s runtime=%s %.0f>", name,
253         agget(dag_node, (char *) "size"), runtime);
254   current_job = xbt_dict_get_or_null(jobs, name);
255   if (current_job == NULL) {
256     current_job =
257         SD_task_create_comp_seq(name, NULL , runtime);
258 #ifdef HAVE_TRACING
259    TRACE_sd_dotloader (current_job, agget (dag_node, (char*)"category"));
260 #endif
261     xbt_dict_set(jobs, name, current_job, NULL);
262     xbt_dynar_push(result, &current_job);
263   }
264   Agedge_t *e;
265   int count = 0;
266
267 #ifdef HAVE_CGRAPH_H
268   for (e = agfstin(dag_dot, dag_node); e; e = agnxtin(dag_dot, e))
269 #elif HAVE_AGRAPH_H
270   for (e = agfstin(dag_node); e; e = agnxtin(e))
271 #endif
272   {
273   dot_add_input_dependencies(current_job, e);
274   count++;
275   }
276   if (count == 0 && current_job != root_task) {
277     SD_task_dependency_add(NULL, NULL, root_task, current_job);
278   }
279   count = 0;
280 #ifdef HAVE_CGRAPH_H
281   for (e = agfstout(dag_dot, dag_node); e; e = agnxtout(dag_dot, e))
282 #elif HAVE_AGRAPH_H
283   for (e = agfstout(dag_node); e; e = agnxtout(e))
284 #endif
285   {
286
287     dot_add_output_dependencies(current_job, e);
288     count++;
289   }
290   if (count == 0 && current_job != end_task) {
291     SD_task_dependency_add(NULL, NULL, current_job, end_task);
292   }
293
294   if(schedule || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose)){
295     /* try to take the information to schedule the task only if all is
296      * right*/
297     // performer is the computer which execute the task
298     unsigned long performer = -1;
299     char * char_performer = agget(dag_node, (char *) "performer");
300     if (char_performer != NULL)
301       performer = (long) dot_parse_int(char_performer);
302
303     // order is giving the task order on one computer
304     unsigned long order = -1;
305     char * char_order = agget(dag_node, (char *) "order");
306     if (char_order != NULL)
307       order = (long) dot_parse_int(char_order);
308     xbt_dynar_t computer = NULL;
309     //INFO2("performer = %d, order=%d",performer,order);
310     if(performer != -1 && order != -1){
311       //necessary parameters are given
312       computer = xbt_dict_get_or_null(computers, char_performer);
313       if(computer == NULL){
314         computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
315         xbt_dict_set(computers, char_performer, computer, NULL);
316       }
317       if(performer < sd_global->workstation_count){
318         // the  wanted computer is available
319         SD_task_t *task_test = NULL;
320         if(order < computer->used)
321           task_test = xbt_dynar_get_ptr(computer,order);
322         if(task_test != NULL && *task_test != NULL && *task_test != current_job){
323           /*the user gives the same order to several tasks*/
324           schedule = false;
325           VERB4("The task %s starts on the computer %s at the position : %s like the task %s",
326                  (*task_test)->name, char_performer, char_order, current_job->name);
327         }else{
328           //the parameter seems to be ok
329           xbt_dynar_set_as(computer, order, SD_task_t, current_job);
330         }
331       }else{
332         /*the platform has not enough processors to schedule the DAG like
333         *the user wants*/
334         schedule = false;
335         VERB0("The schedule is ignored, there are not enough computers");
336       }
337     }
338     else {
339       //one of necessary parameters are not given
340       schedule = false;
341       VERB1("The schedule is ignored, the task %s is not correctly schedule", current_job->name);
342     }
343   }
344 }
345
346 /* dot_add_output_dependencies create the dependencies between a task
347  * and a transfers. This is given by the edges in the dot file. 
348  * The amount of data transfers is given by the attribute size on the
349  * edge. */
350 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge)
351 {
352   SD_task_t file;
353
354   char name[80];
355   sprintf(name, "%s->%s", agnameof(agtail(edge)), agnameof(aghead(edge)));
356   double size = dot_parse_double(agget(edge, (char *) "size"));
357   DEBUG2("size : %e, get size : %s", size, agget(edge, (char *) "size"));
358
359   if (size > 0) {
360     file = xbt_dict_get_or_null(files, name);
361     if (file == NULL) {
362       file = SD_task_create_comm_e2e(name, NULL, size);
363 #ifdef HAVE_TRACING
364       TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
365 #endif
366       xbt_dict_set(files, name, file, &dot_task_free);
367     } else {
368       if (SD_task_get_amount(file) != size) {
369         WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
370               name, SD_task_get_amount(file), size);
371       }
372     }
373     SD_task_dependency_add(NULL, NULL, file, current_job);
374   } else {
375     file = xbt_dict_get_or_null(jobs, agnameof(agtail(edge)));
376     if (file != NULL) {
377       SD_task_dependency_add(NULL, NULL, file, current_job);
378     }
379   }
380 }
381
382 /* dot_add_output_dependencies create the dependencies between a
383  * transfers and a task. This is given by the edges in the dot file.
384  * The amount of data transfers is given by the attribute size on the
385  * edge. */
386 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge)
387 {
388   SD_task_t file;
389   char name[80];
390   sprintf(name, "%s->%s", agnameof(agtail(edge)), agnameof(aghead(edge)));
391   double size = dot_parse_double(agget(edge, (char *) "size"));
392   DEBUG2("size : %e, get size : %s", size, agget(edge, (char *) "size"));
393
394   if (size > 0) {
395     file = xbt_dict_get_or_null(files, name);
396     if (file == NULL) {
397       file = SD_task_create_comm_e2e(name, NULL, size);
398 #ifdef HAVE_TRACING
399       TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
400 #endif
401       xbt_dict_set(files, name, file, &dot_task_free);
402     } else {
403       if (SD_task_get_amount(file) != size) {
404         WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
405               name, SD_task_get_amount(file), size);
406       }
407     }
408     SD_task_dependency_add(NULL, NULL, current_job, file);
409     if (xbt_dynar_length(file->tasks_before) > 1) {
410       WARN1("File %s created at more than one location...", file->name);
411     }
412   } else {
413     file = xbt_dict_get_or_null(jobs, agnameof(aghead(edge)));
414     if (file != NULL) {
415       SD_task_dependency_add(NULL, NULL, current_job, file);
416     }
417   }
418 }