Logo AND Algorithmique Numérique Distribuée

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