Logo AND Algorithmique Numérique Distribuée

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