Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
194f79efa548b8d02cdc359bb974e2f70e62f156
[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   SD_dotload_generic(filename, sequential);
79   xbt_dynar_t computer = NULL;
80   xbt_dict_cursor_t dict_cursor;
81   char *computer_name;
82   xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
83     xbt_dynar_free(&computer);
84   }
85   xbt_dict_free(&computers);
86   return result;
87 }
88
89 xbt_dynar_t SD_dotload_with_sched(const char *filename) {
90   computers = xbt_dict_new_homogeneous(NULL);
91   SD_dotload_generic(filename, sequential);
92
93   if(schedule){
94     xbt_dynar_t computer = NULL;
95     xbt_dict_cursor_t dict_cursor;
96     char *computer_name;
97     const SD_workstation_t *workstations = SD_workstation_get_list ();
98     xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
99       int count_computer = atoi(computer_name);
100       unsigned int count=0;
101       SD_task_t task;
102       SD_task_t task_previous = NULL;
103       xbt_dynar_foreach(computer,count,task){
104         /* add dependency between the previous and the task to avoid
105          * parallel execution */
106         if(task != NULL ){
107           if(task_previous != NULL &&
108              !SD_task_dependency_exists(task_previous, task))
109             SD_task_dependency_add(NULL, NULL, task_previous, task);
110           SD_task_schedulel(task, 1, workstations[count_computer]);
111           task_previous = task;
112         }
113       }
114       xbt_dynar_free(&computer);
115     }
116     xbt_dict_free(&computers);
117     if(acyclic_graph_detail(result))
118       return result;
119     else
120       XBT_WARN("There is at least one cycle in the provided task graph");
121   }else{
122     XBT_WARN("The scheduling is ignored");
123   }
124   SD_task_t task;
125   unsigned int count;
126   xbt_dynar_t computer = NULL;
127   xbt_dict_cursor_t dict_cursor;
128   char *computer_name;
129   xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
130     xbt_dynar_free(&computer);
131   }
132   xbt_dict_free(&computers);
133   xbt_dynar_foreach(result,count,task){
134      SD_task_destroy(task);
135   }
136   return NULL;
137 }
138
139 xbt_dynar_t SD_PTG_dotload(const char * filename) {
140   xbt_dynar_t result = SD_dotload_generic(filename, parallel);
141   if (!acyclic_graph_detail(result)) {
142     XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.",
143               basename((char*)filename));
144     xbt_dynar_free(&result);
145     /* (result == NULL) here */
146   }
147   return result;
148 }
149
150 xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par){
151   xbt_assert(filename, "Unable to use a null file descriptor\n");
152   unsigned int i;
153   result = xbt_dynar_new(sizeof(SD_task_t), dot_task_p_free);
154   jobs = xbt_dict_new_homogeneous(NULL);
155   FILE *in_file = fopen(filename, "r");
156   dag_dot = agread(in_file, NIL(Agdisc_t *));
157   SD_task_t root, end, task;
158   /*
159    * Create all the nodes
160    */
161   Agnode_t *node = NULL;
162   for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
163
164     char *name = agnameof(node);
165     double amount = atof(agget(node, (char *) "size"));
166     double alpha;
167
168     if (seq_or_par == sequential){
169       XBT_DEBUG("See <job id=%s amount =%.0f>", name, amount);
170     } else {
171       alpha = atof(agget(node, (char *) "alpha"));
172       if (alpha == -1.)
173         alpha = 0.0 ;
174       XBT_DEBUG("See <job id=%s amount =%.0f alpha = %.3f>",
175           name, amount, alpha);
176     }
177
178     if (!(task = xbt_dict_get_or_null(jobs, name))) {
179       if (seq_or_par == sequential){
180         task = SD_task_create_comp_seq(name, NULL , amount);
181       } else {
182         task = SD_task_create_comp_par_amdahl(name, NULL , amount, alpha);
183       }
184 #ifdef HAVE_TRACING
185       TRACE_sd_dotloader (task, agget (node, (char*)"category"));
186 #endif
187       xbt_dict_set(jobs, name, task, NULL);
188       if (!strcmp(name, "root")){
189       /* by design the root task is always SCHEDULABLE */
190       __SD_task_set_state(task, SD_SCHEDULABLE);
191       /* Put it at the beginning of the dynar */
192         xbt_dynar_insert_at(result, 0, &task);
193       } else {
194         if (!strcmp(name, "end")){
195           XBT_DEBUG("Declaration of the 'end' node, don't store it yet.");
196           end = task;
197           /* Should be inserted later in the dynar */
198         } else {
199           xbt_dynar_push(result, &task);
200         }
201       }
202
203       if(schedule && seq_or_par == sequential){
204         /* try to take the information to schedule the task only if all is
205          * right*/
206         /* performer is the computer which execute the task */
207         int performer = -1;
208         char * char_performer = agget(node, (char *) "performer");
209         if (char_performer)
210           performer = atoi(char_performer);
211
212         /* order is giving the task order on one computer */
213         int order = -1;
214         char * char_order = agget(node, (char *) "order");
215         if (char_order)
216           order = atoi(char_order);
217         XBT_DEBUG ("Task '%s' is scheduled on workstation '%d' in position '%d'",
218                     task->name, performer, order);
219         xbt_dynar_t computer = NULL;
220         if(performer != -1 && order != -1){
221           /* required parameters are given */
222           computer = xbt_dict_get_or_null(computers, char_performer);
223           if(computer == NULL){
224             computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
225             xbt_dict_set(computers, char_performer, computer, NULL);
226           }
227           if(performer < xbt_lib_length(host_lib)){
228             /* the wanted computer is available */
229             SD_task_t *task_test = NULL;
230             if(order < computer->used)
231               task_test = xbt_dynar_get_ptr(computer,order);
232             if(task_test != NULL && *task_test != NULL && *task_test != task){
233               /* the user gives the same order to several tasks */
234               schedule = false;
235               XBT_VERB("The task %s starts on the computer %s at the position : %s like the task %s",
236                      (*task_test)->name, char_performer, char_order,
237                      task->name);
238             }else{
239               /* the parameter seems to be ok */
240               xbt_dynar_set_as(computer, order, SD_task_t, task);
241             }
242           }else{
243             /* the platform has not enough processors to schedule the DAG like
244              * the user wants*/
245             schedule = false;
246             XBT_VERB("The schedule is ignored, there are not enough computers");
247           }
248         }
249         else {
250           /* one of required parameters is not given */
251           schedule = false;
252           XBT_VERB("The schedule is ignored, the task %s is not correctly scheduled",
253               task->name);
254         }
255       }
256     } else {
257       XBT_WARN("Task '%s' is defined more than once", name);
258     }
259   }
260
261   /*
262    * Check if 'root' and 'end' nodes have been explicitly declared.
263    * If not, create them.
264    */
265   if (!(root = xbt_dict_get_or_null(jobs, "root"))){
266     if (seq_or_par == sequential)
267       root = SD_task_create_comp_seq("root", NULL, 0);
268     else
269       root = SD_task_create_comp_par_amdahl("root", NULL, 0, 0);
270     /* by design the root task is always SCHEDULABLE */
271     __SD_task_set_state(root, SD_SCHEDULABLE);
272     /* Put it at the beginning of the dynar */
273       xbt_dynar_insert_at(result, 0, &root);
274   }
275
276   if (!(end = xbt_dict_get_or_null(jobs, "end"))){
277     if (seq_or_par == sequential)
278       end = SD_task_create_comp_seq("end", NULL, 0);
279     else
280       end = SD_task_create_comp_par_amdahl("end", NULL, 0, 0);
281     /* Should be inserted later in the dynar */
282   }
283
284   /*
285    * Create edges
286    */
287   node = NULL;
288   for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
289     Agedge_t * edge = NULL;
290     for (edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge)) {
291       SD_task_t src, dst;
292       char *src_name=agnameof(agtail(edge));
293       char *dst_name=agnameof(aghead(edge));
294       double size = atof(agget(edge, (char *) "size"));
295
296       src = xbt_dict_get_or_null(jobs, src_name);
297       dst  = xbt_dict_get_or_null(jobs, dst_name);
298
299       if (size > 0) {
300         char *name =
301             xbt_malloc((strlen(src_name)+strlen(dst_name)+6)*sizeof(char));
302         sprintf(name, "%s->%s", src_name, dst_name);
303         XBT_DEBUG("See <transfer id=%s amount = %.0f>", name, size);
304         if (!(task = xbt_dict_get_or_null(jobs, name))) {
305           if (seq_or_par == sequential)
306             task = SD_task_create_comm_e2e(name, NULL , size);
307           else
308             task = SD_task_create_comm_par_mxn_1d_block(name, NULL , size);
309 #ifdef HAVE_TRACING
310           TRACE_sd_dotloader (task, agget (node, (char*)"category"));
311 #endif
312           SD_task_dependency_add(NULL, NULL, src, task);
313           SD_task_dependency_add(NULL, NULL, task, dst);
314           xbt_dict_set(jobs, name, task, NULL);
315           xbt_dynar_push(result, &task);
316         } else {
317           XBT_WARN("Task '%s' is defined more than once", name);
318         }
319       } else {
320         SD_task_dependency_add(NULL, NULL, src, dst);
321       }
322     }
323   }
324
325   /* all compute and transfer tasks have been created, put the "end" node at
326    * the end of dynar
327    */
328   XBT_DEBUG("All tasks have been created, put %s at the end of the dynar",
329       end->name);
330   xbt_dynar_push(result, &end);
331
332   /* Connect entry tasks to 'root', and exit tasks to 'end'*/
333
334   xbt_dynar_foreach (result, i, task){
335     if (task == root || task == end)
336       continue;
337     if (xbt_dynar_is_empty(task->tasks_before)) {
338       XBT_DEBUG("file '%s' has no source. Add dependency from 'root'",
339           task->name);
340       SD_task_dependency_add(NULL, NULL, root, task);
341     } else if (xbt_dynar_is_empty(task->tasks_after)) {
342       XBT_DEBUG("file '%s' has no destination. Add dependency to 'end'",
343           task->name);
344       SD_task_dependency_add(NULL, NULL, task, end);
345     }
346   }
347
348   agclose(dag_dot);
349   xbt_dict_free(&jobs);
350
351   return result;
352 }