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
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 bool child_are_marked(SD_task_t task);
27 xbt_dynar_t SD_dotload_FILE(FILE * in_file);
28
29 static double dot_parse_double(const char *string)
30 {
31   if (string == NULL)
32     return -1;
33   int ret = 0;
34   double value = -1;
35
36   ret = sscanf(string, "%lg", &value);
37   if (ret != 1)
38     WARN1("%s is not a double", string);
39   return value;
40 }
41
42 static int dot_parse_int(const char *string)
43 {
44   if (string == NULL)
45     return -10;
46   int ret = 0;
47   int value = -1;
48
49   ret = sscanf(string, "%d", &value);
50   if (ret != 1)
51     WARN1("%s is not an integer", string);
52   return value;
53 }
54
55 static xbt_dynar_t result;
56 static xbt_dict_t jobs;
57 static xbt_dict_t files;
58 static xbt_dict_t computers;
59 static SD_task_t root_task, end_task;
60 static Agraph_t *dag_dot;
61 static bool schedule = true;
62
63 static void dump_res()
64 {
65   unsigned int cursor;
66   SD_task_t task;
67   xbt_dynar_foreach(result, cursor, task) {
68     INFO1("Task %d", cursor);
69     SD_task_dump(task);
70   }
71 }
72
73 bool child_are_marked(SD_task_t task){
74   SD_task_t child_task = NULL;
75   bool all_marked = true;
76   SD_dependency_t depafter = NULL;
77   unsigned int count;
78   xbt_dynar_foreach(task->tasks_after,count,depafter){
79     child_task = depafter->dst;
80     //test marked
81     if(child_task->marked == 0) {
82       all_marked = false;
83       break;
84     }
85     child_task = NULL;
86   }
87   return all_marked;
88 }
89
90 bool acyclic_graph_detection(xbt_dynar_t dag){
91   unsigned int count=0, count_current=0;
92   bool all_marked = true;
93   SD_task_t task = NULL, parent_task = NULL;
94   SD_dependency_t depbefore = NULL;
95   xbt_dynar_t next = NULL, current = xbt_dynar_new(sizeof(SD_task_t),NULL);
96
97   xbt_dynar_foreach(dag,count,task){
98     if(task->kind == SD_TASK_COMM_E2E) continue;
99     task->marked = 0;
100     if(xbt_dynar_length(task->tasks_after) == 0){
101       xbt_dynar_push(current, &task);
102     }
103   }
104   task = NULL;
105   count = 0;
106   //test if something has to be done for the next iteration
107   while(xbt_dynar_length(current) != 0){
108     next = xbt_dynar_new(sizeof(SD_task_t),NULL);
109     //test if the current iteration is done
110     count_current=0;
111     xbt_dynar_foreach(current,count_current,task){
112       if (task == NULL) continue;
113       count = 0;
114       //push task in next
115       task->marked = 1;
116       count = 0;
117       xbt_dynar_foreach(task->tasks_before,count,depbefore){
118         parent_task = depbefore->src;
119         if(parent_task->kind == SD_TASK_COMM_E2E){
120           unsigned int j=0;
121           parent_task->marked = 1;
122           xbt_dynar_foreach(parent_task->tasks_before,j,depbefore){
123             parent_task = depbefore->src;
124             if(child_are_marked(parent_task))
125               xbt_dynar_push(next, &parent_task);
126           }
127         } else{
128           if(child_are_marked(parent_task))
129             xbt_dynar_push(next, &parent_task);
130         }
131         parent_task = NULL;
132       }
133       task = NULL;
134       count = 0;
135     }
136     xbt_dynar_free(&current);
137     current = next;
138     next = NULL;
139   }
140   xbt_dynar_free(&current);
141   current = NULL;
142   all_marked = true;
143   xbt_dynar_foreach(dag,count,task){
144     if(task->kind == SD_TASK_COMM_E2E) continue;
145     //test if all tasks are marked
146     if(task->marked == 0){
147       WARN1("test %s",task->name);
148       all_marked = false;
149       break;
150     }
151   }
152   task = NULL;
153   if(!all_marked){
154     DEBUG0("there are a cycle in your DAG");
155   }
156   return all_marked;
157 }
158
159 static void dot_task_free(void *task)
160 {
161   SD_task_t t = task;
162   SD_task_destroy(t);
163 }
164
165 static void TRACE_sd_dotloader (SD_task_t task, const char *category)
166 {
167   if (category){
168     if (strlen (category) != 0){
169       TRACE_category (category);
170       TRACE_sd_set_task_category (task, category);
171     }
172   }
173 }
174
175 /** @brief loads a DOT file describing a DAG
176  * 
177  * See http://www.graphviz.org/doc/info/lang.html
178  * for more details.
179  * To obtain information about transfers and tasks, two attributes are
180  * required : size on task (execution time in Flop) and size on edge
181  * (the amount of data transfer in bit).
182  * if they aren't here, there choose to be equal to zero.
183  */
184 xbt_dynar_t SD_dotload(const char *filename)
185 {
186   FILE *in_file = fopen(filename, "r");
187   xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
188   SD_dotload_FILE(in_file);
189   fclose(in_file);
190   xbt_dynar_t computer = NULL;
191   xbt_dict_cursor_t dict_cursor;
192   char *computer_name;
193   xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
194     xbt_dynar_free(&computer);
195   }
196   xbt_dict_free(&computers);
197   return result;
198 }
199
200 xbt_dynar_t SD_dotload_with_sched(const char *filename){
201   FILE *in_file = fopen(filename, "r");
202   xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
203   SD_dotload_FILE(in_file);
204   fclose(in_file);
205
206   if(schedule == true){
207     xbt_dynar_t computer = NULL;
208     xbt_dict_cursor_t dict_cursor;
209     char *computer_name;
210     const SD_workstation_t *workstations = SD_workstation_get_list ();
211     xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
212       int count_computer = dot_parse_int(computer_name);
213       unsigned int count=0;
214       SD_task_t task;
215       SD_task_t task_previous = NULL;
216       xbt_dynar_foreach(computer,count,task){
217         // add dependency between the previous and the task to avoid
218         // parallel execution
219         if(task != NULL ){
220           if(task_previous != NULL &&
221              !SD_task_dependency_exists(task_previous, task))
222             SD_task_dependency_add(NULL, NULL, task_previous, task);
223           SD_task_schedulel(task, 1, workstations[count_computer]);
224           task_previous = task;
225         }
226       }
227       xbt_dynar_free(&computer);
228     }
229     xbt_dict_free(&computers);
230     if(acyclic_graph_detection(result))
231       return result;
232     else
233       WARN0("There are a cycle in your task graph");
234   }else{
235     WARN0("No scheduling provided");
236   }
237   return NULL;
238 }
239
240 xbt_dynar_t SD_dotload_FILE(FILE * in_file)
241 {
242   xbt_assert0(in_file, "Unable to use a null file descriptor\n");
243   dag_dot = agread(in_file, NIL(Agdisc_t *));
244
245   result = xbt_dynar_new(sizeof(SD_task_t), dot_task_free);
246   files = xbt_dict_new();
247   jobs = xbt_dict_new();
248   computers = xbt_dict_new();
249   root_task = SD_task_create_comp_seq("root", NULL, 0);
250   /* by design the root task is always SCHEDULABLE */
251   __SD_task_set_state(root_task, SD_SCHEDULABLE);
252
253   xbt_dict_set(jobs, "root", root_task, NULL);
254   xbt_dynar_push(result, &root_task);
255   end_task = SD_task_create_comp_seq("end", NULL, 0);
256   xbt_dict_set(jobs, "end", end_task, NULL);
257
258   Agnode_t *dag_node = NULL;
259   for (dag_node = agfstnode(dag_dot); dag_node;
260 #ifdef HAVE_CGRAPH_H
261        dag_node = agnxtnode(dag_dot, dag_node)
262 #elif HAVE_AGRAPH_H
263        dag_node = agnxtnode(dag_node)
264 #endif
265        ) {
266
267   dot_add_task(dag_node);
268   }
269   agclose(dag_dot);
270   xbt_dict_free(&jobs);
271
272   /* And now, post-process the files.
273    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
274    * Files not produced in the system are said to be produced by root task (top of DAG).
275    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
276    */
277   xbt_dict_cursor_t cursor;
278   SD_task_t file;
279   char *name;
280   xbt_dict_foreach(files, cursor, name, file) {
281     unsigned int cpt1, cpt2;
282     SD_task_t newfile = NULL;
283     SD_dependency_t depbefore, depafter;
284     if (xbt_dynar_length(file->tasks_before) == 0) {
285       xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
286         SD_task_t newfile =
287             SD_task_create_comm_e2e(file->name, NULL, file->amount);
288         SD_task_dependency_add(NULL, NULL, root_task, newfile);
289         SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
290         xbt_dynar_push(result, &newfile);
291       }
292     } else if (xbt_dynar_length(file->tasks_after) == 0) {
293       xbt_dynar_foreach(file->tasks_before, cpt2, depbefore) {
294         SD_task_t newfile =
295             SD_task_create_comm_e2e(file->name, NULL, file->amount);
296         SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
297         SD_task_dependency_add(NULL, NULL, newfile, end_task);
298         xbt_dynar_push(result, &newfile);
299       }
300     } else {
301       xbt_dynar_foreach(file->tasks_before, cpt1, depbefore) {
302         xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
303           if (depbefore->src == depafter->dst) {
304             WARN2
305                 ("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
306                  file->name, depbefore->src->name);
307           }
308           newfile =
309               SD_task_create_comm_e2e(file->name, NULL, file->amount);
310           SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
311           SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
312           xbt_dynar_push(result, &newfile);
313         }
314       }
315     }
316   }
317
318   /* Push end task last */
319   xbt_dynar_push(result, &end_task);
320
321   /* Free previous copy of the files */
322   xbt_dict_free(&files);
323   if(acyclic_graph_detection(result))
324     return result;
325   return NULL;
326 }
327
328 /* dot_add_task create a sd_task and all transfers required for this
329  * task. The execution time of the task is given by the attribute size.
330  * The unit of size is the Flop.*/
331 void dot_add_task(Agnode_t * dag_node)
332 {
333   char *name = agnameof(dag_node);
334   SD_task_t current_job;
335   double runtime = dot_parse_double(agget(dag_node, (char *) "size"));
336
337   DEBUG3("See <job id=%s runtime=%s %.0f>", name,
338         agget(dag_node, (char *) "size"), runtime);
339   current_job = xbt_dict_get_or_null(jobs, name);
340   if (current_job == NULL) {
341     current_job =
342         SD_task_create_comp_seq(name, NULL , runtime);
343 #ifdef HAVE_TRACING
344    TRACE_sd_dotloader (current_job, agget (dag_node, (char*)"category"));
345 #endif
346     xbt_dict_set(jobs, name, current_job, NULL);
347     xbt_dynar_push(result, &current_job);
348   }
349   Agedge_t *e;
350   int count = 0;
351
352 #ifdef HAVE_CGRAPH_H
353   for (e = agfstin(dag_dot, dag_node); e; e = agnxtin(dag_dot, e))
354 #elif HAVE_AGRAPH_H
355   for (e = agfstin(dag_node); e; e = agnxtin(e))
356 #endif
357   {
358   dot_add_input_dependencies(current_job, e);
359   count++;
360   }
361   if (count == 0 && current_job != root_task) {
362     SD_task_dependency_add(NULL, NULL, root_task, current_job);
363   }
364   count = 0;
365 #ifdef HAVE_CGRAPH_H
366   for (e = agfstout(dag_dot, dag_node); e; e = agnxtout(dag_dot, e))
367 #elif HAVE_AGRAPH_H
368   for (e = agfstout(dag_node); e; e = agnxtout(e))
369 #endif
370   {
371
372     dot_add_output_dependencies(current_job, e);
373     count++;
374   }
375   if (count == 0 && current_job != end_task) {
376     SD_task_dependency_add(NULL, NULL, current_job, end_task);
377   }
378
379   if(schedule){
380     /* try to take the information to schedule the task only if all is
381      * right*/
382     // performer is the computer which execute the task
383     unsigned long performer = -1;
384     char * char_performer = agget(dag_node, (char *) "performer");
385     if (char_performer != NULL)
386       performer = (long) dot_parse_int(char_performer);
387
388     // order is giving the task order on one computer
389     unsigned long order = -1;
390     char * char_order = agget(dag_node, (char *) "order");
391     if (char_order != NULL)
392       order = (long) dot_parse_int(char_order);
393     xbt_dynar_t computer = NULL;
394     //INFO2("performer = %d, order=%d",performer,order);
395     if(performer != -1 && order != -1){
396       //necessary parameters are given
397       computer = xbt_dict_get_or_null(computers, char_performer);
398       if(computer == NULL){
399         computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
400         xbt_dict_set(computers, char_performer, computer, NULL);
401       }
402       if(performer < sd_global->workstation_count){
403         // the  wanted computer is available
404         SD_task_t *task_test = NULL;
405         if(order < computer->used)
406           task_test = xbt_dynar_get_ptr(computer,order);
407         if(task_test != NULL && *task_test != NULL && *task_test != current_job){
408           /*the user gives the same order to several tasks*/
409           schedule = false;
410           DEBUG0("scheduling does not take into account, several task has\
411                 the same order");
412         }else{
413           //the parameter seems to be ok
414           xbt_dynar_set_as(computer, order, SD_task_t, current_job);
415         }
416       }else{
417         /*the platform has not enough processors to schedule the DAG like
418         *the user wants*/
419         schedule = false;
420         DEBUG0("scheduling does not take into account, not enough computers");
421       }
422     }
423     else if((performer == -1 && order != -1) ||
424             (performer != -1 && order == -1)){
425       //one of necessary parameters are not given
426       schedule = false;
427       DEBUG0("scheduling does not take into account");
428     } else {
429       //No schedule available
430       schedule = false;
431     }
432   }
433 }
434
435 /* dot_add_output_dependencies create the dependencies between a task
436  * and a transfers. This is given by the edges in the dot file. 
437  * The amount of data transfers is given by the attribute size on the
438  * edge. */
439 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge)
440 {
441   SD_task_t file;
442
443   char name[80];
444   sprintf(name, "%s->%s", agnameof(agtail(edge)), agnameof(aghead(edge)));
445   double size = dot_parse_double(agget(edge, (char *) "size"));
446   DEBUG2("size : %e, get size : %s", size, agget(edge, (char *) "size"));
447
448   if (size > 0) {
449     file = xbt_dict_get_or_null(files, name);
450     if (file == NULL) {
451       file = SD_task_create_comm_e2e(name, NULL, size);
452 #ifdef HAVE_TRACING
453       TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
454 #endif
455       xbt_dict_set(files, name, file, &dot_task_free);
456     } else {
457       if (SD_task_get_amount(file) != size) {
458         WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
459               name, SD_task_get_amount(file), size);
460       }
461     }
462     SD_task_dependency_add(NULL, NULL, file, current_job);
463   } else {
464     file = xbt_dict_get_or_null(jobs, agnameof(agtail(edge)));
465     if (file != NULL) {
466       SD_task_dependency_add(NULL, NULL, file, current_job);
467     }
468   }
469 }
470
471 /* dot_add_output_dependencies create the dependencies between a
472  * transfers and a task. This is given by the edges in the dot file.
473  * The amount of data transfers is given by the attribute size on the
474  * edge. */
475 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge)
476 {
477   SD_task_t file;
478   char name[80];
479   sprintf(name, "%s->%s", agnameof(agtail(edge)), agnameof(aghead(edge)));
480   double size = dot_parse_double(agget(edge, (char *) "size"));
481   DEBUG2("size : %e, get size : %s", size, agget(edge, (char *) "size"));
482
483   if (size > 0) {
484     file = xbt_dict_get_or_null(files, name);
485     if (file == NULL) {
486       file = SD_task_create_comm_e2e(name, NULL, size);
487 #ifdef HAVE_TRACING
488       TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
489 #endif
490       xbt_dict_set(files, name, file, &dot_task_free);
491     } else {
492       if (SD_task_get_amount(file) != size) {
493         WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
494               name, SD_task_get_amount(file), size);
495       }
496     }
497     SD_task_dependency_add(NULL, NULL, current_job, file);
498     if (xbt_dynar_length(file->tasks_before) > 1) {
499       WARN1("File %s created at more than one location...", file->name);
500     }
501   } else {
502     file = xbt_dict_get_or_null(jobs, agnameof(aghead(edge)));
503     if (file != NULL) {
504       SD_task_dependency_add(NULL, NULL, current_job, file);
505     }
506   }
507 }