Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c17e7f015995d3dc5ae8eebf3b9821a374f308ff
[simgrid.git] / src / simdag / sd_daxloader.c
1 /* Copyright (c) 2009, 2010. 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 "simdag/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
19 /* Parsing helpers */
20 static void dax_parse_error(char *msg)
21 {
22   fprintf(stderr, "Parse error on line %d: %s\n", dax_lineno, msg);
23   abort();
24 }
25
26 static double dax_parse_double(const char *string)
27 {
28   int ret = 0;
29   double value;
30
31   ret = sscanf(string, "%lg", &value);
32   if (ret != 1)
33     dax_parse_error(bprintf("%s is not a double", string));
34   return value;
35 }
36
37 static int dax_parse_int(const char *string)
38 {
39   int ret = 0;
40   int value;
41
42   ret = sscanf(string, "%d", &value);
43   if (ret != 1)
44     dax_parse_error(bprintf("%s is not an integer", string));
45   return value;
46 }
47
48 /* Ensure that transfer tasks have unique names even though a file is used
49  * several times */
50
51 void uniq_transfer_task_name(SD_task_t task)
52 {
53   SD_task_t child, parent;
54   xbt_dynar_t children, parents;
55   char *new_name;
56
57   children = SD_task_get_children(task);
58   parents = SD_task_get_parents(task);
59
60   xbt_dynar_get_cpy(children, 0, &child);
61   xbt_dynar_get_cpy(parents, 0, &parent);
62
63   new_name = bprintf("%s_%s_%s",
64                      SD_task_get_name(parent),
65                      SD_task_get_name(task), SD_task_get_name(child));
66
67   SD_task_set_name(task, new_name);
68
69   xbt_dynar_free_container(&children);
70   xbt_dynar_free_container(&parents);
71   free(new_name);
72 }
73
74
75 static YY_BUFFER_STATE input_buffer;
76
77 static xbt_dynar_t result;
78 static xbt_dict_t jobs;
79 static xbt_dict_t files;
80 static SD_task_t current_job;
81 static SD_task_t root_task, end_task;
82
83 static void dump_res()
84 {
85   unsigned int cursor;
86   SD_task_t task;
87   xbt_dynar_foreach(result, cursor, task) {
88     INFO1("Task %d", cursor);
89     SD_task_dump(task);
90   }
91 }
92
93 static void dax_task_free(void *task)
94 {
95   SD_task_t t = task;
96   SD_task_destroy(t);
97 }
98
99 /** @brief loads a DAX file describing a DAG
100  * 
101  * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator
102  * for more details.
103  */
104 xbt_dynar_t SD_daxload(const char *filename)
105 {
106   xbt_dict_cursor_t cursor;
107   SD_task_t file;
108   char *name;
109   FILE *in_file = fopen(filename, "r");
110   xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
111   input_buffer = dax__create_buffer(in_file, 10);
112   dax__switch_to_buffer(input_buffer);
113   dax_lineno = 1;
114
115   result = xbt_dynar_new(sizeof(SD_task_t), dax_task_free);
116   files = xbt_dict_new();
117   jobs = xbt_dict_new();
118   root_task = SD_task_create_comp_seq("root", NULL, 0);
119   /* by design the root task is always SCHEDULABLE */
120   __SD_task_set_state(root_task, SD_SCHEDULABLE);
121
122   xbt_dynar_push(result, &root_task);
123   end_task = SD_task_create_comp_seq("end", NULL, 0);
124
125   xbt_assert2(!dax_lex(), "Parse error in %s: %s", filename,
126               dax__parse_err_msg());
127   dax__delete_buffer(input_buffer);
128   fclose(in_file);
129   xbt_dict_free(&jobs);
130
131   /* And now, post-process the files.
132    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
133    * Files not produced in the system are said to be produced by root task (top of DAG).
134    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
135    */
136
137   xbt_dict_foreach(files, cursor, name, file) {
138     unsigned int cpt1, cpt2;
139     SD_task_t newfile = NULL;
140     SD_dependency_t depbefore, depafter;
141     if (xbt_dynar_length(file->tasks_before) == 0) {
142       xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
143         SD_task_t newfile =
144             SD_task_create_comm_e2e(file->name, NULL, file->amount);
145         SD_task_dependency_add(NULL, NULL, root_task, newfile);
146         SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
147 #ifdef HAVE_TRACING
148         const char *category = depbefore->src->category;
149         if (category){
150           TRACE_category (category);
151           TRACE_sd_set_task_category (newfile, category);
152         }
153 #endif
154         xbt_dynar_push(result, &newfile);
155       }
156     } else if (xbt_dynar_length(file->tasks_after) == 0) {
157       xbt_dynar_foreach(file->tasks_before, cpt2, depbefore) {
158         SD_task_t newfile =
159             SD_task_create_comm_e2e(file->name, NULL, file->amount);
160         SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
161         SD_task_dependency_add(NULL, NULL, newfile, end_task);
162 #ifdef HAVE_TRACING
163         const char *category = depbefore->src->category;
164         if (category){
165           TRACE_category (category);
166           TRACE_sd_set_task_category (newfile, category);
167         }
168 #endif
169         xbt_dynar_push(result, &newfile);
170       }
171     } else {
172       xbt_dynar_foreach(file->tasks_before, cpt1, depbefore) {
173         xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
174           if (depbefore->src == depafter->dst) {
175             WARN2
176                 ("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
177                  file->name, depbefore->src->name);
178           }
179           newfile =
180               SD_task_create_comm_e2e(file->name, NULL, file->amount);
181           SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
182           SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
183 #ifdef HAVE_TRACING
184           const char *category = depbefore->src->category;
185           if (category){
186             TRACE_category (category);
187             TRACE_sd_set_task_category (newfile, category);
188           }
189 #endif
190           xbt_dynar_push(result, &newfile);
191         }
192       }
193     }
194   }
195
196   /* Push end task last */
197   xbt_dynar_push(result, &end_task);
198
199   /* Free previous copy of the files */
200   xbt_dict_free(&files);
201   unsigned int cpt;
202   xbt_dynar_foreach(result, cpt, file) {
203     if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
204       uniq_transfer_task_name(file);
205     }
206   }
207
208   acyclic_graph_detection(result);
209   return result;
210 }
211
212 void STag_dax__adag(void)
213 {
214   double version = dax_parse_double(A_dax__adag_version);
215
216   xbt_assert1((version == 2.1),
217               "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file",
218               version);
219 }
220
221 void STag_dax__job(void)
222 {
223   double runtime = dax_parse_double(A_dax__job_runtime);
224   char *name = bprintf("%s@%s", A_dax__job_id, A_dax__job_name);
225   runtime *= 4200000000.;       /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
226 //  INFO3("See <job id=%s runtime=%s %.0f>",A_dax__job_id,A_dax__job_runtime,runtime);
227   current_job = SD_task_create_comp_seq(name, NULL, runtime);
228 #ifdef HAVE_TRACING
229   char *category = A_dax__job_name;
230   if (category){
231     TRACE_category (category);
232     TRACE_sd_set_task_category(current_job, category);
233   }
234 #endif
235   xbt_dict_set(jobs, A_dax__job_id, current_job, NULL);
236   free(name);
237   xbt_dynar_push(result, &current_job);
238 }
239
240 void STag_dax__uses(void)
241 {
242   SD_task_t file;
243   double size = dax_parse_double(A_dax__uses_size);
244   int is_input = (A_dax__uses_link == A_dax__uses_link_input);
245
246 //  INFO2("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
247   file = xbt_dict_get_or_null(files, A_dax__uses_file);
248   if (file == NULL) {
249     file = SD_task_create_comm_e2e(A_dax__uses_file, NULL, size);
250     xbt_dict_set(files, A_dax__uses_file, file, &dax_task_free);
251   } else {
252     if (SD_task_get_amount(file) != size) {
253       WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
254             A_dax__uses_file, SD_task_get_amount(file), size);
255     }
256   }
257   if (is_input) {
258     SD_task_dependency_add(NULL, NULL, file, current_job);
259   } else {
260     SD_task_dependency_add(NULL, NULL, current_job, file);
261     if (xbt_dynar_length(file->tasks_before) > 1) {
262       WARN1("File %s created at more than one location...", file->name);
263     }
264   }
265 }
266
267 static SD_task_t current_child;
268 void STag_dax__child(void)
269 {
270   current_child = xbt_dict_get_or_null(jobs, A_dax__child_ref);
271   if (current_child == NULL)
272     dax_parse_error(bprintf
273                     ("Asked to add dependencies to the non-existent %s task",
274                      A_dax__child_ref));
275 }
276
277 void ETag_dax__child(void)
278 {
279   current_child = NULL;
280 }
281
282 void STag_dax__parent(void)
283 {
284   SD_task_t parent = xbt_dict_get_or_null(jobs, A_dax__parent_ref);
285   if (parent == NULL)
286     dax_parse_error(bprintf
287                     ("Asked to add a dependency from %s to %s, but %s does not exist",
288                      current_child->name, A_dax__parent_ref,
289                      A_dax__parent_ref));
290   SD_task_dependency_add(NULL, NULL, parent, current_child);
291   DEBUG2("Control-flow dependency from %s to %s", current_child->name,
292          parent->name);
293 }
294
295 void ETag_dax__adag(void)
296 {
297 //  INFO0("See </adag>");
298 }
299
300 void ETag_dax__job(void)
301 {
302   current_job = NULL;
303 //  INFO0("See </job>");
304 }
305
306 void ETag_dax__parent(void)
307 {
308 //  INFO0("See </parent>");
309 }
310
311 void ETag_dax__uses(void)
312 {
313 //  INFO0("See </uses>");
314 }