Logo AND Algorithmique Numérique Distribuée

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