Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
29803e9f75dfa150567ddeb15258c98fb8d717c6
[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 #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   _XBT_GNUC_UNUSED int res;
279   res = dax_lex();
280   xbt_assert(!res, "Parse error in %s: %s", filename,
281               dax__parse_err_msg());
282   dax__delete_buffer(input_buffer);
283   fclose(in_file);
284   dax_lex_destroy();
285   xbt_dict_free(&jobs);
286
287   /* And now, post-process the files.
288    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
289    * Files not produced in the system are said to be produced by root task (top of DAG).
290    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
291    */
292
293   xbt_dict_foreach(files, cursor, name, file) {
294     unsigned int cpt1, cpt2;
295     SD_task_t newfile;
296     SD_dependency_t depbefore, depafter;
297     if (xbt_dynar_is_empty(file->tasks_before)) {
298       xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
299         newfile = SD_task_create_comm_e2e(file->name, NULL, file->amount);
300         SD_task_dependency_add(NULL, NULL, root_task, newfile);
301         SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
302 #ifdef HAVE_TRACING
303         if (depafter->src){
304           const char *category = depafter->src->category;
305           if (category){
306             TRACE_category (category);
307             TRACE_sd_set_task_category(newfile, category);
308           }
309         }
310 #endif
311         xbt_dynar_push(result, &newfile);
312       }
313     } else if (xbt_dynar_is_empty(file->tasks_after)) {
314       xbt_dynar_foreach(file->tasks_before, cpt2, depbefore) {
315         newfile = SD_task_create_comm_e2e(file->name, NULL, file->amount);
316         SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
317         SD_task_dependency_add(NULL, NULL, newfile, end_task);
318 #ifdef HAVE_TRACING
319         if (depbefore->src){
320           const char *category = depbefore->src->category;
321           if (category){
322             TRACE_category (category);
323             TRACE_sd_set_task_category(newfile, category);
324           }
325         }
326 #endif
327         xbt_dynar_push(result, &newfile);
328       }
329     } else {
330       xbt_dynar_foreach(file->tasks_before, cpt1, depbefore) {
331         xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
332           if (depbefore->src == depafter->dst) {
333             XBT_WARN
334                 ("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
335                  file->name, depbefore->src->name);
336           }
337           newfile = SD_task_create_comm_e2e(file->name, NULL, file->amount);
338           SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
339           SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
340 #ifdef HAVE_TRACING
341           if (depbefore->src){
342             const char *category = depbefore->src->category;
343             if (category){
344               TRACE_category (category);
345               TRACE_sd_set_task_category(newfile, category);
346             }
347           }
348 #endif
349           xbt_dynar_push(result, &newfile);
350         }
351       }
352     }
353   }
354
355   /* Push end task last */
356   xbt_dynar_push(result, &end_task);
357
358   /* Free previous copy of the files */
359   xbt_dict_free(&files);
360   unsigned int cpt;
361   xbt_dynar_foreach(result, cpt, file) {
362     if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
363       uniq_transfer_task_name(file);
364     }
365   }
366
367   if (!acyclic_graph_detail(result)){
368     XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.",
369               basename((char*)filename));
370     xbt_dynar_foreach(result, cpt, file)
371       SD_task_destroy(file);
372      xbt_dynar_free_container(&result);
373     return NULL;
374   } else {
375     return result;
376   }
377 }
378
379 void STag_dax__adag(void)
380 {
381   _XBT_GNUC_UNUSED double version;
382   version = dax_parse_double(A_dax__adag_version);
383
384   xbt_assert(version == 2.1,
385               "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file",
386               version);
387 }
388
389 void STag_dax__job(void)
390 {
391   double runtime = dax_parse_double(A_dax__job_runtime);
392   char *name = bprintf("%s@%s", A_dax__job_id, A_dax__job_name);
393   runtime *= 4200000000.;       /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
394 //  XBT_INFO("See <job id=%s runtime=%s %.0f>",A_dax__job_id,A_dax__job_runtime,runtime);
395   current_job = SD_task_create_comp_seq(name, NULL, runtime);
396 #ifdef HAVE_TRACING
397   char *category = A_dax__job_name;
398   if (category){
399     TRACE_category (category);
400     TRACE_sd_set_task_category(current_job, category);
401   }
402 #endif
403   xbt_dict_set(jobs, A_dax__job_id, current_job, NULL);
404   free(name);
405   xbt_dynar_push(result, &current_job);
406 }
407
408 void STag_dax__uses(void)
409 {
410   SD_task_t file;
411   double size = dax_parse_double(A_dax__uses_size);
412   int is_input = (A_dax__uses_link == A_dax__uses_link_input);
413
414 //  XBT_INFO("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
415   file = xbt_dict_get_or_null(files, A_dax__uses_file);
416   if (file == NULL) {
417     file = SD_task_create_comm_e2e(A_dax__uses_file, NULL, size);
418     xbt_dict_set(files, A_dax__uses_file, file, NULL);
419   } else {
420     if (SD_task_get_amount(file) != size) {
421       XBT_WARN("Ignoring file %s size redefinition from %.0f to %.0f",
422             A_dax__uses_file, SD_task_get_amount(file), size);
423     }
424   }
425   if (is_input) {
426     SD_task_dependency_add(NULL, NULL, file, current_job);
427   } else {
428     SD_task_dependency_add(NULL, NULL, current_job, file);
429     if (xbt_dynar_length(file->tasks_before) > 1) {
430       XBT_WARN("File %s created at more than one location...", file->name);
431     }
432   }
433 }
434
435 static SD_task_t current_child;
436 void STag_dax__child(void)
437 {
438   current_child = xbt_dict_get_or_null(jobs, A_dax__child_ref);
439   if (current_child == NULL)
440     dax_parse_error(bprintf
441                     ("Asked to add dependencies to the non-existent %s task",
442                      A_dax__child_ref));
443 }
444
445 void ETag_dax__child(void)
446 {
447   current_child = NULL;
448 }
449
450 void STag_dax__parent(void)
451 {
452   SD_task_t parent = xbt_dict_get_or_null(jobs, A_dax__parent_ref);
453   if (parent == NULL)
454     dax_parse_error(bprintf
455                     ("Asked to add a dependency from %s to %s, but %s does not exist",
456                      current_child->name, A_dax__parent_ref,
457                      A_dax__parent_ref));
458   SD_task_dependency_add(NULL, NULL, parent, current_child);
459   XBT_DEBUG("Control-flow dependency from %s to %s", current_child->name,
460          parent->name);
461 }
462
463 void ETag_dax__adag(void)
464 {
465 //  XBT_INFO("See </adag>");
466 }
467
468 void ETag_dax__job(void)
469 {
470   current_job = NULL;
471 //  XBT_INFO("See </job>");
472 }
473
474 void ETag_dax__parent(void)
475 {
476 //  XBT_INFO("See </parent>");
477 }
478
479 void ETag_dax__uses(void)
480 {
481 //  XBT_INFO("See </uses>");
482 }