Logo AND Algorithmique Numérique Distribuée

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