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_dotloader.c
1 /* Copyright (c) 2009-2013. 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 #include <string.h>
13 #include <libgen.h>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_dotparse, sd, "Parsing DOT files");
16
17 #undef CLEANUP
18
19 #ifdef HAVE_CGRAPH_H
20 #include <graphviz/cgraph.h>
21 #elif HAVE_AGRAPH_H
22 #include <graphviz/agraph.h>
23 #define agnxtnode(dot, node)    agnxtnode(node)
24 #define agfstin(dot, node)      agfstin(node)
25 #define agnxtin(dot, edge)      agnxtin(edge)
26 #define agfstout(dot, node)     agfstout(node)
27 #define agnxtout(dot, edge)     agnxtout(edge)
28 #endif
29
30 typedef enum {
31   sequential =0,
32   parallel
33 } seq_par_t;
34
35 xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par);
36
37 static xbt_dynar_t result;
38 static xbt_dict_t jobs;
39 static xbt_dict_t computers;
40 static Agraph_t *dag_dot;
41 static bool schedule = true;
42
43 static void dot_task_free(void *task) {
44   SD_task_t t = task;
45   SD_task_destroy(t);
46 }
47
48 static void dot_task_p_free(void *task) {
49   SD_task_t *t = task;
50   SD_task_destroy(*t);
51 }
52
53 #ifdef HAVE_TRACING
54 static void TRACE_sd_dotloader (SD_task_t task, const char *category) {
55   if (category && strlen (category)){
56     if (task->category)
57       XBT_DEBUG("Change the category of %s from %s to %s",
58           task->name, task->category, category);
59     else
60       XBT_DEBUG("Set the category of %s to %s",task->name, category);
61     TRACE_category (category);
62     TRACE_sd_set_task_category(task, category);
63   }
64 }
65 #endif
66
67 /** @brief loads a DOT file describing a DAG
68  * 
69  * See http://www.graphviz.org/doc/info/lang.html
70  * for more details.
71  * To obtain information about transfers and tasks, two attributes are
72  * required : size on task (execution time in Flop) and size on edge
73  * (the amount of data transfer in bit).
74  * if they aren't here, there choose to be equal to zero.
75  */
76 xbt_dynar_t SD_dotload(const char *filename) {
77   computers = xbt_dict_new_homogeneous(NULL);
78   schedule = false;
79   SD_dotload_generic(filename, sequential);
80   xbt_dynar_t computer = NULL;
81   xbt_dict_cursor_t dict_cursor;
82   char *computer_name;
83   xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
84     xbt_dynar_free(&computer);
85   }
86   xbt_dict_free(&computers);
87   return result;
88 }
89
90 xbt_dynar_t SD_dotload_with_sched(const char *filename) {
91   computers = xbt_dict_new_homogeneous(NULL);
92   SD_dotload_generic(filename, sequential);
93
94   if(schedule){
95     xbt_dynar_t computer = NULL;
96     xbt_dict_cursor_t dict_cursor;
97     char *computer_name;
98     const SD_workstation_t *workstations = SD_workstation_get_list ();
99     xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
100       int count_computer = atoi(computer_name);
101       unsigned int count=0;
102       SD_task_t task;
103       SD_task_t task_previous = NULL;
104       xbt_dynar_foreach(computer,count,task){
105         /* add dependency between the previous and the task to avoid
106          * parallel execution */
107         if(task != NULL ){
108           if(task_previous != NULL &&
109              !SD_task_dependency_exists(task_previous, task))
110             SD_task_dependency_add(NULL, NULL, task_previous, task);
111           SD_task_schedulel(task, 1, workstations[count_computer]);
112           task_previous = task;
113         }
114       }
115       xbt_dynar_free(&computer);
116     }
117     xbt_dict_free(&computers);
118     if(acyclic_graph_detail(result))
119       return result;
120     else
121       XBT_WARN("There is at least one cycle in the provided task graph");
122   }else{
123     XBT_WARN("The scheduling is ignored");
124   }
125   SD_task_t task;
126   unsigned int count;
127   xbt_dynar_t computer = NULL;
128   xbt_dict_cursor_t dict_cursor;
129   char *computer_name;
130   xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
131     xbt_dynar_free(&computer);
132   }
133   xbt_dict_free(&computers);
134   xbt_dynar_foreach(result,count,task){
135      SD_task_destroy(task);
136   }
137   return NULL;
138 }
139
140 xbt_dynar_t SD_PTG_dotload(const char * filename) {
141   xbt_dynar_t result = SD_dotload_generic(filename, parallel);
142   if (!acyclic_graph_detail(result)) {
143     XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.",
144               basename((char*)filename));
145     xbt_dynar_free(&result);
146     /* (result == NULL) here */
147   }
148   return result;
149 }
150
151 #ifdef HAVE_CGRAPH_H
152 static int edge_compare(const void *a, const void *b)
153 {
154   unsigned va = AGSEQ(*(Agedge_t **)a);
155   unsigned vb = AGSEQ(*(Agedge_t **)b);
156   return va == vb ? 0 : (va < vb ? -1 : 1);
157 }
158 #endif
159
160 xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par){
161   xbt_assert(filename, "Unable to use a null file descriptor\n");
162   unsigned int i;
163   result = xbt_dynar_new(sizeof(SD_task_t), dot_task_p_free);
164   jobs = xbt_dict_new_homogeneous(NULL);
165   FILE *in_file = fopen(filename, "r");
166   if (in_file == NULL)
167     xbt_die("Failed to open file: %s", filename);
168   dag_dot = agread(in_file, NIL(Agdisc_t *));
169   SD_task_t root, end, task;
170   /*
171    * Create all the nodes
172    */
173   Agnode_t *node = NULL;
174   for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
175
176     char *name = agnameof(node);
177     double amount = atof(agget(node, (char *) "size"));
178     double alpha;
179
180     if (seq_or_par == sequential){
181       XBT_DEBUG("See <job id=%s amount =%.0f>", name, amount);
182     } else {
183       if (!strcmp(agget(node, (char *) "alpha"), "")){
184         alpha = atof(agget(node, (char *) "alpha"));
185         if (alpha == -1.){
186           XBT_DEBUG("negative alpha value provided. Set to 0.");
187           alpha = 0.0 ;
188         }
189       } else {
190         XBT_DEBUG("no alpha value provided. Set to 0");
191         alpha = 0.0 ;
192       }
193
194       XBT_DEBUG("See <job id=%s amount =%.0f alpha = %.3f>",
195           name, amount, alpha);
196     }
197
198     if (!(task = xbt_dict_get_or_null(jobs, name))) {
199       if (seq_or_par == sequential){
200         task = SD_task_create_comp_seq(name, NULL , amount);
201       } else {
202         task = SD_task_create_comp_par_amdahl(name, NULL , amount, alpha);
203       }
204 #ifdef HAVE_TRACING
205       TRACE_sd_dotloader (task, agget (node, (char*)"category"));
206 #endif
207       xbt_dict_set(jobs, name, task, NULL);
208       if (!strcmp(name, "root")){
209       /* by design the root task is always SCHEDULABLE */
210       __SD_task_set_state(task, SD_SCHEDULABLE);
211       /* Put it at the beginning of the dynar */
212         xbt_dynar_insert_at(result, 0, &task);
213       } else {
214         if (!strcmp(name, "end")){
215           XBT_DEBUG("Declaration of the 'end' node, don't store it yet.");
216           end = task;
217           /* Should be inserted later in the dynar */
218         } else {
219           xbt_dynar_push(result, &task);
220         }
221       }
222
223       if((seq_or_par == sequential) &&
224           (schedule ||
225               XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose))){
226         /* try to take the information to schedule the task only if all is
227          * right*/
228         int performer, order;
229         char *char_performer = agget(node, (char *) "performer");
230         char *char_order = agget(node, (char *) "order");
231         /* performer is the computer which execute the task */
232         performer =
233             ((!char_performer || !strcmp(char_performer,"")) ? -1:atoi(char_performer));
234         /* order is giving the task order on one computer */
235         order = ((!char_order || !strcmp(char_order, ""))? -1:atoi(char_order));
236
237         XBT_DEBUG ("Task '%s' is scheduled on workstation '%d' in position '%d'",
238                     task->name, performer, order);
239         xbt_dynar_t computer = NULL;
240         if(performer != -1 && order != -1){
241           /* required parameters are given */
242           computer = xbt_dict_get_or_null(computers, char_performer);
243           if(computer == NULL){
244             computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
245             xbt_dict_set(computers, char_performer, computer, NULL);
246           }
247           if(performer < xbt_lib_length(host_lib)){
248             /* the wanted computer is available */
249             SD_task_t *task_test = NULL;
250             if(order < computer->used)
251               task_test = xbt_dynar_get_ptr(computer,order);
252             if(task_test != NULL && *task_test != NULL && *task_test != task){
253               /* the user gives the same order to several tasks */
254               schedule = false;
255               XBT_VERB("The task %s starts on the computer %s at the position : %s like the task %s",
256                      (*task_test)->name, char_performer, char_order,
257                      task->name);
258             }else{
259               /* the parameter seems to be ok */
260               xbt_dynar_set_as(computer, order, SD_task_t, task);
261             }
262           }else{
263             /* the platform has not enough processors to schedule the DAG like
264              * the user wants*/
265             schedule = false;
266             XBT_VERB("The schedule is ignored, there are not enough computers");
267           }
268         }
269         else {
270           /* one of required parameters is not given */
271           schedule = false;
272           XBT_VERB("The schedule is ignored, the task %s is not correctly scheduled",
273               task->name);
274         }
275       }
276     } else {
277       XBT_WARN("Task '%s' is defined more than once", name);
278     }
279   }
280
281   /*
282    * Check if 'root' and 'end' nodes have been explicitly declared.
283    * If not, create them.
284    */
285   if (!(root = xbt_dict_get_or_null(jobs, "root"))){
286     if (seq_or_par == sequential)
287       root = SD_task_create_comp_seq("root", NULL, 0);
288     else
289       root = SD_task_create_comp_par_amdahl("root", NULL, 0, 0);
290     /* by design the root task is always SCHEDULABLE */
291     __SD_task_set_state(root, SD_SCHEDULABLE);
292     /* Put it at the beginning of the dynar */
293       xbt_dynar_insert_at(result, 0, &root);
294   }
295
296   if (!(end = xbt_dict_get_or_null(jobs, "end"))){
297     if (seq_or_par == sequential)
298       end = SD_task_create_comp_seq("end", NULL, 0);
299     else
300       end = SD_task_create_comp_par_amdahl("end", NULL, 0, 0);
301     /* Should be inserted later in the dynar */
302   }
303
304   /*
305    * Create edges
306    */
307   xbt_dynar_t edges = xbt_dynar_new(sizeof(Agedge_t*), NULL);
308   for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
309     unsigned cursor;
310     Agedge_t * edge;
311     xbt_dynar_reset(edges);
312     for (edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge))
313       xbt_dynar_push_as(edges, Agedge_t *, edge);
314 #ifdef HAVE_CGRAPH_H
315     /* Hack: circumvent a bug in libcgraph, where the edges are not always given
316      * back in creation order.  We sort them again, according to their sequence
317      * id.  The problem appears to be solved (i.e.: I did not test it) in
318      * graphviz' mercurial repository by the following changeset:
319      *    changeset:   8431:d5f1fb7e8103
320      *    user:        Emden Gansner <erg@research.att.com>
321      *    date:        Tue Oct 11 12:38:58 2011 -0400
322      *    summary:     Make sure edges are stored in node creation order
323      * It should be fixed in graphviz 2.30 and above.
324      */
325     xbt_dynar_sort(edges, edge_compare);
326 #endif
327     xbt_dynar_foreach(edges, cursor, edge) {
328       SD_task_t src, dst;
329       char *src_name=agnameof(agtail(edge));
330       char *dst_name=agnameof(aghead(edge));
331       double size = atof(agget(edge, (char *) "size"));
332
333       src = xbt_dict_get_or_null(jobs, src_name);
334       dst  = xbt_dict_get_or_null(jobs, dst_name);
335
336       if (size > 0) {
337         char *name =
338             xbt_malloc((strlen(src_name)+strlen(dst_name)+6)*sizeof(char));
339         sprintf(name, "%s->%s", src_name, dst_name);
340         XBT_DEBUG("See <transfer id=%s amount = %.0f>", name, size);
341         if (!(task = xbt_dict_get_or_null(jobs, name))) {
342           if (seq_or_par == sequential)
343             task = SD_task_create_comm_e2e(name, NULL , size);
344           else
345             task = SD_task_create_comm_par_mxn_1d_block(name, NULL , size);
346 #ifdef HAVE_TRACING
347           TRACE_sd_dotloader (task, agget (node, (char*)"category"));
348 #endif
349           SD_task_dependency_add(NULL, NULL, src, task);
350           SD_task_dependency_add(NULL, NULL, task, dst);
351           xbt_dict_set(jobs, name, task, NULL);
352           xbt_dynar_push(result, &task);
353         } else {
354           XBT_WARN("Task '%s' is defined more than once", name);
355         }
356         xbt_free(name);
357       } else {
358         SD_task_dependency_add(NULL, NULL, src, dst);
359       }
360     }
361   }
362   xbt_dynar_free(&edges);
363
364   /* all compute and transfer tasks have been created, put the "end" node at
365    * the end of dynar
366    */
367   XBT_DEBUG("All tasks have been created, put %s at the end of the dynar",
368       end->name);
369   xbt_dynar_push(result, &end);
370
371   /* Connect entry tasks to 'root', and exit tasks to 'end'*/
372
373   xbt_dynar_foreach (result, i, task){
374     if (task == root || task == end)
375       continue;
376     if (xbt_dynar_is_empty(task->tasks_before)) {
377       XBT_DEBUG("file '%s' has no source. Add dependency from 'root'",
378           task->name);
379       SD_task_dependency_add(NULL, NULL, root, task);
380     } else if (xbt_dynar_is_empty(task->tasks_after)) {
381       XBT_DEBUG("file '%s' has no destination. Add dependency to 'end'",
382           task->name);
383       SD_task_dependency_add(NULL, NULL, task, end);
384     }
385   }
386
387   agclose(dag_dot);
388   xbt_dict_free(&jobs);
389   fclose(in_file);
390
391   if (!acyclic_graph_detail(result)) {
392     XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.",
393               basename((char*)filename));
394     xbt_dynar_free(&result);
395     /* (result == NULL) here */
396   }
397   return result;
398 }