Logo AND Algorithmique Numérique Distribuée

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