Logo AND Algorithmique Numérique Distribuée

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