Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge 'master' into mc
[simgrid.git] / src / simdag / sd_daxloader.c
1 /* Copyright (c) 2009-2014. 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 <libgen.h>
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_daxparse, sd, "Parsing DAX files");
14
15 #undef CLEANUP
16 #include "dax_dtd.h"
17 #include "dax_dtd.c"
18
19 bool children_are_marked(SD_task_t task);
20 bool parents_are_marked(SD_task_t task);
21
22 /* Parsing helpers */
23 static void dax_parse_error(char *msg)
24 {
25   fprintf(stderr, "Parse error on line %d: %s\n", dax_lineno, msg);
26   xbt_abort();
27 }
28
29 static double dax_parse_double(const char *string)
30 {
31   int ret = 0;
32   double value;
33
34   ret = sscanf(string, "%lg", &value);
35   if (ret != 1)
36     dax_parse_error(bprintf("%s is not a double", string));
37   return value;
38 }
39
40 /* Ensure that transfer tasks have unique names even though a file is used
41  * several times */
42
43 void uniq_transfer_task_name(SD_task_t task)
44 {
45   SD_task_t child, parent;
46   xbt_dynar_t children, parents;
47   char *new_name;
48
49   children = SD_task_get_children(task);
50   parents = SD_task_get_parents(task);
51
52   xbt_dynar_get_cpy(children, 0, &child);
53   xbt_dynar_get_cpy(parents, 0, &parent);
54
55   new_name = bprintf("%s_%s_%s",
56                      SD_task_get_name(parent),
57                      SD_task_get_name(task), SD_task_get_name(child));
58
59   SD_task_set_name(task, new_name);
60
61   xbt_dynar_free_container(&children);
62   xbt_dynar_free_container(&parents);
63   free(new_name);
64 }
65
66 bool children_are_marked(SD_task_t task){
67   SD_task_t child_task = NULL;
68   bool all_marked = true;
69   SD_dependency_t depafter = NULL;
70   unsigned int count;
71   xbt_dynar_foreach(task->tasks_after,count,depafter){
72     child_task = depafter->dst;
73     //test marked
74     if(child_task->marked == 0) {
75       all_marked = false;
76       break;
77     }
78     child_task = NULL;
79   }
80   return all_marked;
81 }
82
83 bool parents_are_marked(SD_task_t task){
84   SD_task_t parent_task = NULL;
85   bool all_marked = true;
86   SD_dependency_t depbefore = NULL;
87   unsigned int count;
88   xbt_dynar_foreach(task->tasks_before,count,depbefore){
89     parent_task = depbefore->src;
90     //test marked
91     if(parent_task->marked == 0) {
92       all_marked = false;
93       break;
94     }
95     parent_task = NULL;
96   }
97   return all_marked;
98 }
99
100 bool acyclic_graph_detail(xbt_dynar_t dag){
101   unsigned int count=0, count_current=0;
102   bool all_marked = true;
103   SD_task_t task = NULL, parent_task = NULL, child_task = NULL;
104   SD_dependency_t depbefore = NULL, depafter = NULL;
105   xbt_dynar_t next = NULL, current = xbt_dynar_new(sizeof(SD_task_t),NULL);
106
107   xbt_dynar_foreach(dag,count,task){
108     if(task->kind == SD_TASK_COMM_E2E) continue;
109     task->marked = 0;
110     if(xbt_dynar_is_empty(task->tasks_after)){
111       xbt_dynar_push(current, &task);
112     }
113   }
114   task = NULL;
115   count = 0;
116   //test if something has to be done for the next iteration
117   while(!xbt_dynar_is_empty(current)){
118     next = xbt_dynar_new(sizeof(SD_task_t),NULL);
119     //test if the current iteration is done
120     count_current=0;
121     xbt_dynar_foreach(current,count_current,task){
122       if (task == NULL) continue;
123       count = 0;
124       //push task in next
125       task->marked = 1;
126       count = 0;
127       xbt_dynar_foreach(task->tasks_before,count,depbefore){
128         parent_task = depbefore->src;
129         if(parent_task->kind == SD_TASK_COMM_E2E){
130           unsigned int j=0;
131           parent_task->marked = 1;
132           SD_task_t parent_task_2 = NULL;
133           xbt_dynar_foreach(parent_task->tasks_before,j,depbefore){
134             parent_task_2 = depbefore->src;
135             if(children_are_marked(parent_task_2))
136               xbt_dynar_push(next, &parent_task_2);
137           }
138         } else{
139           if(children_are_marked(parent_task))
140             xbt_dynar_push(next, &parent_task);
141         }
142         parent_task = NULL;
143       }
144       task = NULL;
145       count = 0;
146     }
147     xbt_dynar_free(&current);
148     current = next;
149     next = NULL;
150   }
151   xbt_dynar_free(&current);
152   current = NULL;
153   all_marked = true;
154   xbt_dynar_foreach(dag,count,task){
155     if(task->kind == SD_TASK_COMM_E2E) continue;
156     //test if all tasks are marked
157     if(task->marked == 0){
158       XBT_WARN("the task %s is not marked",task->name);
159       all_marked = false;
160       break;
161     }
162   }
163   task = NULL;
164   if(!all_marked){
165     XBT_VERB("there is at least one cycle in your task graph");
166
167     current = xbt_dynar_new(sizeof(SD_task_t),NULL);
168     xbt_dynar_foreach(dag,count,task){
169       if(task->kind == SD_TASK_COMM_E2E) continue;
170       if(xbt_dynar_is_empty(task->tasks_before)){
171         xbt_dynar_push(current, &task);
172       }
173     }
174
175     count = 0;
176     task = NULL;
177     xbt_dynar_foreach(dag,count,task){
178       if(task->kind == SD_TASK_COMM_E2E) continue;
179       if(xbt_dynar_is_empty(task->tasks_before)){
180         task->marked = 1;
181         xbt_dynar_push(current, &task);
182       }
183     }
184     task = NULL;
185     count = 0;
186     //test if something has to be done for the next iteration
187     while(!xbt_dynar_is_empty(current)){
188       next = xbt_dynar_new(sizeof(SD_task_t),NULL);
189       //test if the current iteration is done
190       count_current=0;
191       xbt_dynar_foreach(current,count_current,task){
192         if (task == NULL) continue;
193         count = 0;
194         //push task in next
195         task->marked = 1;
196         count = 0;
197         xbt_dynar_foreach(task->tasks_after,count,depafter){
198           child_task = depbefore->dst;
199           if(child_task->kind == SD_TASK_COMM_E2E){
200             unsigned int j=0;
201             child_task->marked = 1;
202             SD_task_t child_task_2 = NULL;
203             xbt_dynar_foreach(child_task->tasks_after,j,depafter){
204               child_task_2 = depbefore->dst;
205               if(parents_are_marked(child_task_2))
206                 xbt_dynar_push(next, &child_task_2);
207             }
208           } else{
209             if(parents_are_marked(child_task))
210               xbt_dynar_push(next, &child_task);
211           }
212           child_task = NULL;
213         }
214         task = NULL;
215         count = 0;
216       }
217       xbt_dynar_free(&current);
218       current = next;
219       next = NULL;
220     }
221     xbt_dynar_free(&current);
222     current = NULL;
223     all_marked = true;
224     xbt_dynar_foreach(dag,count,task){
225       if(task->kind == SD_TASK_COMM_E2E) continue;
226       //test if all tasks are marked
227       if(task->marked == 0){
228         XBT_WARN("the task %s is in a cycle",task->name);
229         all_marked = false;
230       }
231     }
232   }
233   return all_marked;
234 }
235
236
237
238 static YY_BUFFER_STATE input_buffer;
239
240 static xbt_dynar_t result;
241 static xbt_dict_t jobs;
242 static xbt_dict_t files;
243 static SD_task_t current_job;
244 static SD_task_t root_task, end_task;
245
246 static void dax_task_free(void *task)
247 {
248   SD_task_t t = task;
249   SD_task_destroy(t);
250 }
251
252 /** @brief loads a DAX file describing a DAG
253  * 
254  * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator
255  * for more details.
256  */
257 xbt_dynar_t SD_daxload(const char *filename)
258 {
259   xbt_dict_cursor_t cursor;
260   SD_task_t file;
261   char *name;
262   FILE *in_file = fopen(filename, "r");
263   xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
264   input_buffer = dax__create_buffer(in_file, 10);
265   dax__switch_to_buffer(input_buffer);
266   dax_lineno = 1;
267
268   result = xbt_dynar_new(sizeof(SD_task_t), dax_task_free);
269   files = xbt_dict_new_homogeneous(&dax_task_free);
270   jobs = xbt_dict_new_homogeneous(NULL);
271   root_task = SD_task_create_comp_seq("root", NULL, 0);
272   /* by design the root task is always SCHEDULABLE */
273   __SD_task_set_state(root_task, SD_SCHEDULABLE);
274
275   xbt_dynar_push(result, &root_task);
276   end_task = SD_task_create_comp_seq("end", NULL, 0);
277
278   int res = dax_lex();
279   if (res != 0)
280     xbt_die("Parse error in %s: %s", filename, dax__parse_err_msg());
281   dax__delete_buffer(input_buffer);
282   fclose(in_file);
283   dax_lex_destroy();
284   xbt_dict_free(&jobs);
285
286   /* And now, post-process the files.
287    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
288    * Files not produced in the system are said to be produced by root task (top of DAG).
289    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
290    */
291
292   xbt_dict_foreach(files, cursor, name, file) {
293     unsigned int cpt1, cpt2;
294     SD_task_t newfile;
295     SD_dependency_t depbefore, depafter;
296     if (xbt_dynar_is_empty(file->tasks_before)) {
297       xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
298         newfile = SD_task_create_comm_e2e(file->name, NULL, file->amount);
299         SD_task_dependency_add(NULL, NULL, root_task, newfile);
300         SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
301 #ifdef HAVE_TRACING
302         if (depafter->src){
303           const char *category = depafter->src->category;
304           if (category){
305             TRACE_category (category);
306             TRACE_sd_set_task_category(newfile, category);
307           }
308         }
309 #endif
310         xbt_dynar_push(result, &newfile);
311       }
312     } else if (xbt_dynar_is_empty(file->tasks_after)) {
313       xbt_dynar_foreach(file->tasks_before, cpt2, depbefore) {
314         newfile = SD_task_create_comm_e2e(file->name, NULL, file->amount);
315         SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
316         SD_task_dependency_add(NULL, NULL, newfile, end_task);
317 #ifdef HAVE_TRACING
318         if (depbefore->src){
319           const char *category = depbefore->src->category;
320           if (category){
321             TRACE_category (category);
322             TRACE_sd_set_task_category(newfile, category);
323           }
324         }
325 #endif
326         xbt_dynar_push(result, &newfile);
327       }
328     } else {
329       xbt_dynar_foreach(file->tasks_before, cpt1, depbefore) {
330         xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
331           if (depbefore->src == depafter->dst) {
332             XBT_WARN
333                 ("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
334                  file->name, depbefore->src->name);
335           }
336           newfile = SD_task_create_comm_e2e(file->name, NULL, file->amount);
337           SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
338           SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
339 #ifdef HAVE_TRACING
340           if (depbefore->src){
341             const char *category = depbefore->src->category;
342             if (category){
343               TRACE_category (category);
344               TRACE_sd_set_task_category(newfile, category);
345             }
346           }
347 #endif
348           xbt_dynar_push(result, &newfile);
349         }
350       }
351     }
352   }
353
354   /* Push end task last */
355   xbt_dynar_push(result, &end_task);
356
357   /* Free previous copy of the files */
358   xbt_dict_free(&files);
359   unsigned int cpt;
360   xbt_dynar_foreach(result, cpt, file) {
361     if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
362       uniq_transfer_task_name(file);
363     } else if (SD_task_get_kind(file) == SD_TASK_COMP_SEQ){
364       /* If some tasks do not take files as input, connect them to the root, if
365        * they don't produce files, connect them to the end node.
366        */
367       if ((file != root_task) && xbt_dynar_is_empty(file->tasks_before)) {
368         SD_task_dependency_add(NULL, NULL, root_task, file);
369       }
370       if ((file != end_task) && xbt_dynar_is_empty(file->tasks_after)) {
371         SD_task_dependency_add(NULL, NULL, file, end_task);
372       }
373     }
374   }
375
376   if (!acyclic_graph_detail(result)){
377     XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.",
378               basename((char*)filename));
379     xbt_dynar_foreach(result, cpt, file)
380       SD_task_destroy(file);
381      xbt_dynar_free_container(&result);
382     return NULL;
383   } else {
384     return result;
385   }
386 }
387
388 void STag_dax__adag(void)
389 {
390   _XBT_GNUC_UNUSED double version;
391   version = dax_parse_double(A_dax__adag_version);
392
393   xbt_assert(version == 2.1,
394               "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file",
395               version);
396 }
397
398 void STag_dax__job(void)
399 {
400   double runtime = dax_parse_double(A_dax__job_runtime);
401   char *name = bprintf("%s@%s", A_dax__job_id, A_dax__job_name);
402   runtime *= 4200000000.;       /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
403 //  XBT_INFO("See <job id=%s runtime=%s %.0f>",A_dax__job_id,A_dax__job_runtime,runtime);
404   current_job = SD_task_create_comp_seq(name, NULL, runtime);
405 #ifdef HAVE_TRACING
406   char *category = A_dax__job_name;
407   if (category){
408     TRACE_category (category);
409     TRACE_sd_set_task_category(current_job, category);
410   }
411 #endif
412   xbt_dict_set(jobs, A_dax__job_id, current_job, NULL);
413   free(name);
414   xbt_dynar_push(result, &current_job);
415 }
416
417 void STag_dax__uses(void)
418 {
419   SD_task_t file;
420   double size = dax_parse_double(A_dax__uses_size);
421   int is_input = (A_dax__uses_link == A_dax__uses_link_input);
422
423 //  XBT_INFO("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
424   file = xbt_dict_get_or_null(files, A_dax__uses_file);
425   if (file == NULL) {
426     file = SD_task_create_comm_e2e(A_dax__uses_file, NULL, size);
427     xbt_dict_set(files, A_dax__uses_file, file, NULL);
428   } else {
429     if (SD_task_get_amount(file) != size) {
430       XBT_WARN("Ignoring file %s size redefinition from %.0f to %.0f",
431             A_dax__uses_file, SD_task_get_amount(file), size);
432     }
433   }
434   if (is_input) {
435     SD_task_dependency_add(NULL, NULL, file, current_job);
436   } else {
437     SD_task_dependency_add(NULL, NULL, current_job, file);
438     if (xbt_dynar_length(file->tasks_before) > 1) {
439       XBT_WARN("File %s created at more than one location...", file->name);
440     }
441   }
442 }
443
444 static SD_task_t current_child;
445 void STag_dax__child(void)
446 {
447   current_child = xbt_dict_get_or_null(jobs, A_dax__child_ref);
448   if (current_child == NULL)
449     dax_parse_error(bprintf
450                     ("Asked to add dependencies to the non-existent %s task",
451                      A_dax__child_ref));
452 }
453
454 void ETag_dax__child(void)
455 {
456   current_child = NULL;
457 }
458
459 void STag_dax__parent(void)
460 {
461   SD_task_t parent = xbt_dict_get_or_null(jobs, A_dax__parent_ref);
462   if (parent == NULL)
463     dax_parse_error(bprintf
464                     ("Asked to add a dependency from %s to %s, but %s does not exist",
465                      current_child->name, A_dax__parent_ref,
466                      A_dax__parent_ref));
467   SD_task_dependency_add(NULL, NULL, parent, current_child);
468   XBT_DEBUG("Control-flow dependency from %s to %s", current_child->name,
469          parent->name);
470 }
471
472 void ETag_dax__adag(void)
473 {
474 //  XBT_INFO("See </adag>");
475 }
476
477 void ETag_dax__job(void)
478 {
479   current_job = NULL;
480 //  XBT_INFO("See </job>");
481 }
482
483 void ETag_dax__parent(void)
484 {
485 //  XBT_INFO("See </parent>");
486 }
487
488 void ETag_dax__uses(void)
489 {
490 //  XBT_INFO("See </uses>");
491 }