Logo AND Algorithmique Numérique Distribuée

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