Logo AND Algorithmique Numérique Distribuée

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