Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
23da93e19b7a8b759d2c452549dabe7ef51c83f3
[simgrid.git] / src / simdag / sd_dotloader.c
1 /* Copyright (c) 2009, 2010. 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 void dot_add_task(Agnode_t * dag_node);
31 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge);
32 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge);
33 xbt_dynar_t SD_dotload_generic(const char * filename);
34
35 static double dot_parse_double(const char *string)
36 {
37   if (string == NULL)
38     return -1;
39   double value = -1;
40   char *err;
41
42   //ret = sscanf(string, "%lg", &value);
43   errno = 0;
44   value = strtod(string,&err);
45   if(errno)
46   {
47     XBT_WARN("Failed to convert string to double: %s\n",strerror(errno));
48     return -1;
49   }
50   return value;
51 }
52
53
54 static int dot_parse_int(const char *string)
55 {
56   if (string == NULL)
57     return -10;
58   int ret = 0;
59   int value = -1;
60
61   ret = sscanf(string, "%d", &value);
62   if (ret != 1)
63     XBT_WARN("%s is not an integer", string);
64   return value;
65 }
66
67 static xbt_dynar_t result;
68 static xbt_dict_t jobs;
69 static xbt_dict_t files;
70 static xbt_dict_t computers;
71 static SD_task_t root_task, end_task;
72 static Agraph_t *dag_dot;
73 static bool schedule = true;
74
75 static void dump_res()
76 {
77   unsigned int cursor;
78   SD_task_t task;
79   xbt_dynar_foreach(result, cursor, task) {
80     XBT_INFO("Task %d", cursor);
81     SD_task_dump(task);
82   }
83 }
84
85
86 static void dot_task_free(void *task)
87 {
88   SD_task_t t = task;
89   SD_task_destroy(t);
90 }
91
92 static void TRACE_sd_dotloader (SD_task_t task, const char *category)
93 {
94   if (category){
95     if (strlen (category) != 0){
96       TRACE_category (category);
97       SD_task_set_category (task, category);
98     }
99   }
100 }
101
102 /** @brief loads a DOT file describing a DAG
103  * 
104  * See http://www.graphviz.org/doc/info/lang.html
105  * for more details.
106  * To obtain information about transfers and tasks, two attributes are
107  * required : size on task (execution time in Flop) and size on edge
108  * (the amount of data transfer in bit).
109  * if they aren't here, there choose to be equal to zero.
110  */
111 xbt_dynar_t SD_dotload(const char *filename)
112 {
113   SD_dotload_generic(filename);
114   xbt_dynar_t computer = NULL;
115   xbt_dict_cursor_t dict_cursor;
116   char *computer_name;
117   xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
118     xbt_dynar_free(&computer);
119   }
120   xbt_dict_free(&computers);
121   return result;
122 }
123
124 xbt_dynar_t SD_dotload_with_sched(const char *filename){
125   SD_dotload_generic(filename);
126
127   if(schedule == true){
128     xbt_dynar_t computer = NULL;
129     xbt_dict_cursor_t dict_cursor;
130     char *computer_name;
131     const SD_workstation_t *workstations = SD_workstation_get_list ();
132     xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
133       int count_computer = dot_parse_int(computer_name);
134       unsigned int count=0;
135       SD_task_t task;
136       SD_task_t task_previous = NULL;
137       xbt_dynar_foreach(computer,count,task){
138         // add dependency between the previous and the task to avoid
139         // parallel execution
140         if(task != NULL ){
141           if(task_previous != NULL &&
142              !SD_task_dependency_exists(task_previous, task))
143             SD_task_dependency_add(NULL, NULL, task_previous, task);
144           SD_task_schedulel(task, 1, workstations[count_computer]);
145           task_previous = task;
146         }
147       }
148       xbt_dynar_free(&computer);
149     }
150     xbt_dict_free(&computers);
151     if(acyclic_graph_detail(result))
152       return result;
153     else
154       XBT_WARN("There is at least one cycle in the provided task graph");
155   }else{
156     XBT_WARN("The scheduling is ignored");
157   }
158   return NULL;
159 }
160
161 xbt_dynar_t SD_dotload_generic(const char * filename)
162 {
163   xbt_assert(filename, "Unable to use a null file descriptor\n");
164   //dag_dot =  agopen((char*)filename,Agstrictdirected,0);
165   FILE *in_file = fopen(filename, "r");
166   dag_dot = agread(in_file, NIL(Agdisc_t *));
167
168   result = xbt_dynar_new(sizeof(SD_task_t), dot_task_free);
169   files = xbt_dict_new_homogeneous(&dot_task_free);
170   jobs = xbt_dict_new_homogeneous(NULL);
171   computers = xbt_dict_new_homogeneous(NULL);
172   root_task = SD_task_create_comp_seq("root", NULL, 0);
173   /* by design the root task is always SCHEDULABLE */
174   __SD_task_set_state(root_task, SD_SCHEDULABLE);
175
176   xbt_dict_set(jobs, "root", root_task, NULL);
177   xbt_dynar_push(result, &root_task);
178   end_task = SD_task_create_comp_seq("end", NULL, 0);
179   xbt_dict_set(jobs, "end", end_task, NULL);
180
181   Agnode_t *dag_node = NULL;
182   for (dag_node = agfstnode(dag_dot); dag_node; dag_node = agnxtnode(dag_dot, dag_node)) {
183     dot_add_task(dag_node);
184   }
185   agclose(dag_dot);
186   xbt_dict_free(&jobs);
187
188   /* And now, post-process the files.
189    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
190    * Files not produced in the system are said to be produced by root task (top of DAG).
191    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
192    */
193   xbt_dict_cursor_t cursor;
194   SD_task_t file;
195   char *name;
196   xbt_dict_foreach(files, cursor, name, file) {
197     unsigned int cpt1, cpt2;
198     SD_task_t newfile = NULL;
199     SD_dependency_t depbefore, depafter;
200     if (xbt_dynar_is_empty(file->tasks_before)) {
201       xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
202         SD_task_t newfile =
203             SD_task_create_comm_e2e(file->name, NULL, file->amount);
204         SD_task_dependency_add(NULL, NULL, root_task, newfile);
205         SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
206         xbt_dynar_push(result, &newfile);
207       }
208     } else if (xbt_dynar_is_empty(file->tasks_after)) {
209       xbt_dynar_foreach(file->tasks_before, cpt2, depbefore) {
210         SD_task_t newfile =
211             SD_task_create_comm_e2e(file->name, NULL, file->amount);
212         SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
213         SD_task_dependency_add(NULL, NULL, newfile, end_task);
214         xbt_dynar_push(result, &newfile);
215       }
216     } else {
217       xbt_dynar_foreach(file->tasks_before, cpt1, depbefore) {
218         xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
219           if (depbefore->src == depafter->dst) {
220             XBT_WARN
221                 ("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
222                  file->name, depbefore->src->name);
223           }
224           newfile =
225               SD_task_create_comm_e2e(file->name, NULL, file->amount);
226           SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
227           SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
228           xbt_dynar_push(result, &newfile);
229         }
230       }
231     }
232   }
233
234   /* Push end task last */
235   xbt_dynar_push(result, &end_task);
236
237   /* Free previous copy of the files */
238   xbt_dict_free(&files);
239   fclose(in_file);
240   if(acyclic_graph_detail(result))
241     return result;
242   else {
243     unsigned int cpt;
244     XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.",
245               basename((char*)filename));
246     xbt_dynar_foreach(result, cpt, file)
247       SD_task_destroy(file);
248      xbt_dynar_free_container(&result);
249   }
250   free(dag_dot);
251   return NULL;
252 }
253
254 /* dot_add_task create a sd_task and all transfers required for this
255  * task. The execution time of the task is given by the attribute size.
256  * The unit of size is the Flop.*/
257 void dot_add_task(Agnode_t * dag_node)
258 {
259   char *name = agnameof(dag_node);
260   SD_task_t current_job;
261   double runtime = dot_parse_double(agget(dag_node, (char *) "size"));
262
263   XBT_DEBUG("See <job id=%s runtime=%s %.0f>", name,
264         agget(dag_node, (char *) "size"), runtime);
265   current_job = xbt_dict_get_or_null(jobs, name);
266   if (current_job == NULL) {
267     current_job =
268         SD_task_create_comp_seq(name, NULL , runtime);
269 #ifdef HAVE_TRACING
270    TRACE_sd_dotloader (current_job, agget (dag_node, (char*)"category"));
271 #endif
272     xbt_dict_set(jobs, name, current_job, NULL);
273     xbt_dynar_push(result, &current_job);
274   }
275   Agedge_t *e;
276   int count = 0;
277
278   for (e = agfstin(dag_dot, dag_node); e; e = agnxtin(dag_dot, e)) {
279     dot_add_input_dependencies(current_job, e);
280     count++;
281   }
282   if (count == 0 && current_job != root_task) {
283     SD_task_dependency_add(NULL, NULL, root_task, current_job);
284   }
285   count = 0;
286   for (e = agfstout(dag_dot, dag_node); e; e = agnxtout(dag_dot, e)) {
287     dot_add_output_dependencies(current_job, e);
288     count++;
289   }
290   if (count == 0 && current_job != end_task) {
291     SD_task_dependency_add(NULL, NULL, current_job, end_task);
292   }
293
294   if(schedule || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose)){
295     /* try to take the information to schedule the task only if all is
296      * right*/
297     // performer is the computer which execute the task
298     unsigned long performer = -1;
299     char * char_performer = agget(dag_node, (char *) "performer");
300     if (char_performer != NULL)
301       performer = (long) dot_parse_int(char_performer);
302
303     // order is giving the task order on one computer
304     unsigned long order = -1;
305     char * char_order = agget(dag_node, (char *) "order");
306     if (char_order != NULL)
307       order = (long) dot_parse_int(char_order);
308     xbt_dynar_t computer = NULL;
309     //XBT_INFO("performer = %d, order=%d",performer,order);
310     if(performer != -1 && order != -1){
311       //necessary parameters are given
312       computer = xbt_dict_get_or_null(computers, char_performer);
313       if(computer == NULL){
314         computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
315         xbt_dict_set(computers, char_performer, computer, NULL);
316       }
317       if(performer < xbt_lib_length(host_lib)){
318         // the  wanted computer is available
319         SD_task_t *task_test = NULL;
320         if(order < computer->used)
321           task_test = xbt_dynar_get_ptr(computer,order);
322         if(task_test != NULL && *task_test != NULL && *task_test != current_job){
323           /*the user gives the same order to several tasks*/
324           schedule = false;
325           XBT_VERB("The task %s starts on the computer %s at the position : %s like the task %s",
326                  (*task_test)->name, char_performer, char_order, current_job->name);
327         }else{
328           //the parameter seems to be ok
329           xbt_dynar_set_as(computer, order, SD_task_t, current_job);
330         }
331       }else{
332         /*the platform has not enough processors to schedule the DAG like
333         *the user wants*/
334         schedule = false;
335         XBT_VERB("The schedule is ignored, there are not enough computers");
336       }
337     }
338     else {
339       //one of necessary parameters are not given
340       schedule = false;
341       XBT_VERB("The schedule is ignored, the task %s is not correctly scheduled", current_job->name);
342     }
343   }
344 }
345
346 /* dot_add_output_dependencies create the dependencies between a task
347  * and a transfers. This is given by the edges in the dot file. 
348  * The amount of data transfers is given by the attribute size on the
349  * edge. */
350 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge)
351 {
352   SD_task_t file = NULL;
353   char *name_tail=agnameof(agtail(edge));
354   char *name_head=agnameof(aghead(edge));
355   char *name = malloc((strlen(name_head)+strlen(name_tail)+6)*sizeof(char));
356   sprintf(name, "%s->%s", name_tail, name_head);
357   double size = dot_parse_double(agget(edge, (char *) "size"));
358   XBT_DEBUG("size : %e, get size : %s", size, agget(edge, (char *) "size"));
359
360   if (size > 0) {
361     file = xbt_dict_get_or_null(files, name);
362     if (file == NULL) {
363       file = SD_task_create_comm_e2e(name, NULL, size);
364 #ifdef HAVE_TRACING
365       TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
366 #endif
367       xbt_dict_set(files, name, file, NULL);
368     } else {
369       if (SD_task_get_amount(file) != size) {
370         XBT_WARN("Ignoring file %s size redefinition from %.0f to %.0f",
371               name, SD_task_get_amount(file), size);
372       }
373     }
374     SD_task_dependency_add(NULL, NULL, file, current_job);
375   } else {
376     file = xbt_dict_get_or_null(jobs, name_tail);
377     if (file != NULL) {
378       SD_task_dependency_add(NULL, NULL, file, current_job);
379     }
380   }
381   free(name);
382 }
383
384 /* dot_add_output_dependencies create the dependencies between a
385  * transfers and a task. This is given by the edges in the dot file.
386  * The amount of data transfers is given by the attribute size on the
387  * edge. */
388 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge)
389 {
390   SD_task_t file;
391   char *name_tail=agnameof(agtail(edge));
392   char *name_head=agnameof(aghead(edge));
393   char *name = malloc((strlen(name_head)+strlen(name_tail)+6)*sizeof(char));
394   sprintf(name, "%s->%s", name_tail, name_head);
395   double size = dot_parse_double(agget(edge, (char *) "size"));
396   XBT_DEBUG("size : %e, get size : %s", size, agget(edge, (char *) "size"));
397
398   if (size > 0) {
399     file = xbt_dict_get_or_null(files, name);
400     if (file == NULL) {
401       file = SD_task_create_comm_e2e(name, NULL, size);
402 #ifdef HAVE_TRACING
403       TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
404 #endif
405       xbt_dict_set(files, name, file, NULL);
406     } else {
407       if (SD_task_get_amount(file) != size) {
408         XBT_WARN("Ignoring file %s size redefinition from %.0f to %.0f",
409               name, SD_task_get_amount(file), size);
410       }
411     }
412     SD_task_dependency_add(NULL, NULL, current_job, file);
413     if (xbt_dynar_length(file->tasks_before) > 1) {
414       XBT_WARN("File %s created at more than one location...", file->name);
415     }
416   } else {
417     file = xbt_dict_get_or_null(jobs, name_head);
418     if (file != NULL) {
419       SD_task_dependency_add(NULL, NULL, current_job, file);
420     }
421   }
422   free(name);
423 }