Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove unused variables.
[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
153   result = xbt_dynar_new(sizeof(SD_task_t), dot_task_free);
154   files = xbt_dict_new();
155   jobs = xbt_dict_new();
156   computers = xbt_dict_new();
157   root_task = SD_task_create_comp_seq("root", NULL, 0);
158   /* by design the root task is always SCHEDULABLE */
159   __SD_task_set_state(root_task, SD_SCHEDULABLE);
160
161   xbt_dict_set(jobs, "root", root_task, NULL);
162   xbt_dynar_push(result, &root_task);
163   end_task = SD_task_create_comp_seq("end", NULL, 0);
164   xbt_dict_set(jobs, "end", end_task, NULL);
165
166   Agnode_t *dag_node = NULL;
167   for (dag_node = agfstnode(dag_dot); dag_node;
168 #ifdef HAVE_CGRAPH_H
169        dag_node = agnxtnode(dag_dot, dag_node)
170 #elif HAVE_AGRAPH_H
171        dag_node = agnxtnode(dag_node)
172 #endif
173        ) {
174
175   dot_add_task(dag_node);
176   }
177   agclose(dag_dot);
178   xbt_dict_free(&jobs);
179
180   /* And now, post-process the files.
181    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
182    * Files not produced in the system are said to be produced by root task (top of DAG).
183    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
184    */
185   xbt_dict_cursor_t cursor;
186   SD_task_t file;
187   char *name;
188   xbt_dict_foreach(files, cursor, name, file) {
189     unsigned int cpt1, cpt2;
190     SD_task_t newfile = NULL;
191     SD_dependency_t depbefore, depafter;
192     if (xbt_dynar_length(file->tasks_before) == 0) {
193       xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
194         SD_task_t newfile =
195             SD_task_create_comm_e2e(file->name, NULL, file->amount);
196         SD_task_dependency_add(NULL, NULL, root_task, newfile);
197         SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
198         xbt_dynar_push(result, &newfile);
199       }
200     } else if (xbt_dynar_length(file->tasks_after) == 0) {
201       xbt_dynar_foreach(file->tasks_before, cpt2, depbefore) {
202         SD_task_t newfile =
203             SD_task_create_comm_e2e(file->name, NULL, file->amount);
204         SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
205         SD_task_dependency_add(NULL, NULL, newfile, end_task);
206         xbt_dynar_push(result, &newfile);
207       }
208     } else {
209       xbt_dynar_foreach(file->tasks_before, cpt1, depbefore) {
210         xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
211           if (depbefore->src == depafter->dst) {
212             XBT_WARN
213                 ("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
214                  file->name, depbefore->src->name);
215           }
216           newfile =
217               SD_task_create_comm_e2e(file->name, NULL, file->amount);
218           SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
219           SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
220           xbt_dynar_push(result, &newfile);
221         }
222       }
223     }
224   }
225
226   /* Push end task last */
227   xbt_dynar_push(result, &end_task);
228
229   /* Free previous copy of the files */
230   xbt_dict_free(&files);
231   if(acyclic_graph_detail(result))
232     return result;
233   acyclic_graph_detail(result);
234   free(dag_dot);
235   return NULL;
236 }
237
238 /* dot_add_task create a sd_task and all transfers required for this
239  * task. The execution time of the task is given by the attribute size.
240  * The unit of size is the Flop.*/
241 void dot_add_task(Agnode_t * dag_node)
242 {
243   char *name = agnameof(dag_node);
244   SD_task_t current_job;
245   double runtime = dot_parse_double(agget(dag_node, (char *) "size"));
246
247   XBT_DEBUG("See <job id=%s runtime=%s %.0f>", name,
248         agget(dag_node, (char *) "size"), runtime);
249   current_job = xbt_dict_get_or_null(jobs, name);
250   if (current_job == NULL) {
251     current_job =
252         SD_task_create_comp_seq(name, NULL , runtime);
253 #ifdef HAVE_TRACING
254    TRACE_sd_dotloader (current_job, agget (dag_node, (char*)"category"));
255 #endif
256     xbt_dict_set(jobs, name, current_job, NULL);
257     xbt_dynar_push(result, &current_job);
258   }
259   Agedge_t *e;
260   int count = 0;
261
262 #ifdef HAVE_CGRAPH_H
263   for (e = agfstin(dag_dot, dag_node); e; e = agnxtin(dag_dot, e))
264 #elif HAVE_AGRAPH_H
265   for (e = agfstin(dag_node); e; e = agnxtin(e))
266 #endif
267   {
268   dot_add_input_dependencies(current_job, e);
269   count++;
270   }
271   if (count == 0 && current_job != root_task) {
272     SD_task_dependency_add(NULL, NULL, root_task, current_job);
273   }
274   count = 0;
275 #ifdef HAVE_CGRAPH_H
276   for (e = agfstout(dag_dot, dag_node); e; e = agnxtout(dag_dot, e))
277 #elif HAVE_AGRAPH_H
278   for (e = agfstout(dag_node); e; e = agnxtout(e))
279 #endif
280   {
281
282     dot_add_output_dependencies(current_job, e);
283     count++;
284   }
285   if (count == 0 && current_job != end_task) {
286     SD_task_dependency_add(NULL, NULL, current_job, end_task);
287   }
288
289   if(schedule || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose)){
290     /* try to take the information to schedule the task only if all is
291      * right*/
292     // performer is the computer which execute the task
293     unsigned long performer = -1;
294     char * char_performer = agget(dag_node, (char *) "performer");
295     if (char_performer != NULL)
296       performer = (long) dot_parse_int(char_performer);
297
298     // order is giving the task order on one computer
299     unsigned long order = -1;
300     char * char_order = agget(dag_node, (char *) "order");
301     if (char_order != NULL)
302       order = (long) dot_parse_int(char_order);
303     xbt_dynar_t computer = NULL;
304     //XBT_INFO("performer = %d, order=%d",performer,order);
305     if(performer != -1 && order != -1){
306       //necessary parameters are given
307       computer = xbt_dict_get_or_null(computers, char_performer);
308       if(computer == NULL){
309         computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
310         xbt_dict_set(computers, char_performer, computer, NULL);
311       }
312       if(performer < host_lib->count){
313         // the  wanted computer is available
314         SD_task_t *task_test = NULL;
315         if(order < computer->used)
316           task_test = xbt_dynar_get_ptr(computer,order);
317         if(task_test != NULL && *task_test != NULL && *task_test != current_job){
318           /*the user gives the same order to several tasks*/
319           schedule = false;
320           XBT_VERB("The task %s starts on the computer %s at the position : %s like the task %s",
321                  (*task_test)->name, char_performer, char_order, current_job->name);
322         }else{
323           //the parameter seems to be ok
324           xbt_dynar_set_as(computer, order, SD_task_t, current_job);
325         }
326       }else{
327         /*the platform has not enough processors to schedule the DAG like
328         *the user wants*/
329         schedule = false;
330         XBT_VERB("The schedule is ignored, there are not enough computers");
331       }
332     }
333     else {
334       //one of necessary parameters are not given
335       schedule = false;
336       XBT_VERB("The schedule is ignored, the task %s is not correctly schedule", current_job->name);
337     }
338   }
339 }
340
341 /* dot_add_output_dependencies create the dependencies between a task
342  * and a transfers. This is given by the edges in the dot file. 
343  * The amount of data transfers is given by the attribute size on the
344  * edge. */
345 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge)
346 {
347   SD_task_t file;
348
349   char name[80];
350   sprintf(name, "%s->%s", agnameof(agtail(edge)), agnameof(aghead(edge)));
351   double size = dot_parse_double(agget(edge, (char *) "size"));
352   XBT_DEBUG("size : %e, get size : %s", size, agget(edge, (char *) "size"));
353
354   if (size > 0) {
355     file = xbt_dict_get_or_null(files, name);
356     if (file == NULL) {
357       file = SD_task_create_comm_e2e(name, NULL, size);
358 #ifdef HAVE_TRACING
359       TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
360 #endif
361       xbt_dict_set(files, name, file, &dot_task_free);
362     } else {
363       if (SD_task_get_amount(file) != size) {
364         XBT_WARN("Ignoring file %s size redefinition from %.0f to %.0f",
365               name, SD_task_get_amount(file), size);
366       }
367     }
368     SD_task_dependency_add(NULL, NULL, file, current_job);
369   } else {
370     file = xbt_dict_get_or_null(jobs, agnameof(agtail(edge)));
371     if (file != NULL) {
372       SD_task_dependency_add(NULL, NULL, file, current_job);
373     }
374   }
375 }
376
377 /* dot_add_output_dependencies create the dependencies between a
378  * transfers and a task. This is given by the edges in the dot file.
379  * The amount of data transfers is given by the attribute size on the
380  * edge. */
381 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge)
382 {
383   SD_task_t file;
384   char name[80];
385   sprintf(name, "%s->%s", agnameof(agtail(edge)), agnameof(aghead(edge)));
386   double size = dot_parse_double(agget(edge, (char *) "size"));
387   XBT_DEBUG("size : %e, get size : %s", size, agget(edge, (char *) "size"));
388
389   if (size > 0) {
390     file = xbt_dict_get_or_null(files, name);
391     if (file == NULL) {
392       file = SD_task_create_comm_e2e(name, NULL, size);
393 #ifdef HAVE_TRACING
394       TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
395 #endif
396       xbt_dict_set(files, name, file, &dot_task_free);
397     } else {
398       if (SD_task_get_amount(file) != size) {
399         XBT_WARN("Ignoring file %s size redefinition from %.0f to %.0f",
400               name, SD_task_get_amount(file), size);
401       }
402     }
403     SD_task_dependency_add(NULL, NULL, current_job, file);
404     if (xbt_dynar_length(file->tasks_before) > 1) {
405       XBT_WARN("File %s created at more than one location...", file->name);
406     }
407   } else {
408     file = xbt_dict_get_or_null(jobs, agnameof(aghead(edge)));
409     if (file != NULL) {
410       SD_task_dependency_add(NULL, NULL, current_job, file);
411     }
412   }
413 }