Logo AND Algorithmique Numérique Distribuée

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