Logo AND Algorithmique Numérique Distribuée

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