Logo AND Algorithmique Numérique Distribuée

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