Logo AND Algorithmique Numérique Distribuée

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