Logo AND Algorithmique Numérique Distribuée

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