Logo AND Algorithmique Numérique Distribuée

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