Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
don't create dependencies if both root and end are not connected to any task
[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 "simgrid/simdag.h"
8 #include "xbt/misc.h"
9 #include "xbt/log.h"
10 #include "xbt/str.h"
11 #include "xbt/file.h" /* xbt_basename() */
12 #include "simdag_private.hpp"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_daxparse, sd, "Parsing DAX files");
15
16 extern "C" {
17   #undef CLEANUP
18   #include "dax_dtd.h"
19   #define register /* g++ don't like register, so don't say it */
20   #include "dax_dtd.c"
21   #undef register
22 }
23
24 /* Ensure that transfer tasks have unique names even though a file is used several times */
25
26 void uniq_transfer_task_name(SD_task_t task)
27 {
28   SD_task_t child = *(task->successors->begin());
29   SD_task_t parent = *(task->predecessors->begin());
30
31   char *new_name = bprintf("%s_%s_%s", SD_task_get_name(parent), SD_task_get_name(task), SD_task_get_name(child));
32
33   SD_task_set_name(task, new_name);
34
35   free(new_name);
36 }
37
38 static bool children_are_marked(SD_task_t task){
39   for (SD_task_t it : *task->successors)
40     if (it->marked == 0)
41       return false;
42   for (SD_task_t it : *task->outputs)
43     if (it->marked == 0)
44       return false;
45   return true;
46 }
47
48 static bool parents_are_marked(SD_task_t task){
49   for (SD_task_t it : *task->predecessors)
50     if (it->marked == 0)
51       return false;
52   for (SD_task_t it : *task->inputs)
53     if (it->marked == 0)
54       return false;
55   return true;
56 }
57
58 bool acyclic_graph_detail(xbt_dynar_t dag){
59   unsigned int count;
60   bool all_marked = true;
61   SD_task_t task = nullptr;
62   std::vector<SD_task_t> current;
63   xbt_dynar_foreach(dag,count,task){
64     if(task->kind != SD_TASK_COMM_E2E){
65       task->marked = 0;
66       if(task->successors->empty() && task->outputs->empty())
67         current.push_back(task);
68     }
69   }
70   while(!current.empty()){
71     std::vector<SD_task_t> next;
72     for (auto t: current){
73       //Mark task
74       t->marked = 1;
75       for (SD_task_t input : *t->inputs){
76         input->marked=1;
77         // Inputs are communication, hence they can have only one predecessor
78         SD_task_t input_pred = *(input->predecessors->begin());
79         if (children_are_marked(input_pred))
80           next.push_back(input_pred);
81       }
82       for (SD_task_t pred : *t->predecessors) {
83         if (children_are_marked(pred))
84           next.push_back(pred);
85       }
86     }
87     current.clear();
88     current = next;
89   }
90
91   all_marked = true;
92   //test if all tasks are marked
93   xbt_dynar_foreach(dag,count,task){
94     if(task->kind != SD_TASK_COMM_E2E && task->marked == 0){
95       XBT_WARN("the task %s is not marked",task->name);
96       all_marked = false;
97       break;
98     }
99   }
100
101   if(!all_marked){
102     XBT_VERB("there is at least one cycle in your task graph");
103     xbt_dynar_foreach(dag,count,task){
104       if(task->kind != SD_TASK_COMM_E2E && task->predecessors->empty() && task->inputs->empty()){
105         task->marked = 1;
106         current.push_back(task);
107       }
108     }
109     //test if something has to be done for the next iteration
110     while(!current.empty()){
111       std::vector<SD_task_t> next;
112       //test if the current iteration is done
113       for (auto t: current){
114         t->marked = 1;
115         for (SD_task_t output : *t->outputs) {
116           output->marked = 1;
117           // outputs are communication, hence they can have only one successor
118           SD_task_t output_succ = *(output->successors->begin());
119           if (parents_are_marked(output_succ))
120             next.push_back(output_succ);
121         }
122         for (SD_task_t succ : *t->successors) {
123           if (parents_are_marked(succ))
124             next.push_back(succ);
125         }
126         current.clear();
127         current = next;
128       }
129     }
130
131     all_marked = true;
132     xbt_dynar_foreach(dag,count,task){
133       if(task->kind != SD_TASK_COMM_E2E && task->marked == 0){
134         XBT_WARN("the task %s is in a cycle",task->name);
135         all_marked = false;
136       }
137     }
138   }
139   return all_marked;
140 }
141
142 static YY_BUFFER_STATE input_buffer;
143
144 static xbt_dynar_t result;
145 static xbt_dict_t jobs;
146 static xbt_dict_t files;
147 static SD_task_t current_job;
148 static SD_task_t root_task;
149 static SD_task_t end_task;
150
151 static void dax_task_free(void *task)
152 {
153   SD_task_destroy(static_cast<SD_task_t>(task));
154 }
155
156 /** @brief loads a DAX file describing a DAG
157  * 
158  * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
159  */
160 xbt_dynar_t SD_daxload(const char *filename)
161 {
162   xbt_dict_cursor_t cursor;
163   SD_task_t file;
164   char *name;
165   FILE *in_file = fopen(filename, "r");
166   xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
167   input_buffer = dax__create_buffer(in_file, 10);
168   dax__switch_to_buffer(input_buffer);
169   dax_lineno = 1;
170
171   result = xbt_dynar_new(sizeof(SD_task_t), dax_task_free);
172   files = xbt_dict_new_homogeneous(&dax_task_free);
173   jobs = xbt_dict_new_homogeneous(nullptr);
174   root_task = SD_task_create_comp_seq("root", nullptr, 0);
175   /* by design the root task is always SCHEDULABLE */
176   SD_task_set_state(root_task, SD_SCHEDULABLE);
177
178   xbt_dynar_push(result, &root_task);
179   end_task = SD_task_create_comp_seq("end", nullptr, 0);
180
181   int res = dax_lex();
182   if (res != 0)
183     xbt_die("Parse error in %s: %s", filename, dax__parse_err_msg());
184   dax__delete_buffer(input_buffer);
185   fclose(in_file);
186   dax_lex_destroy();
187   xbt_dict_free(&jobs);
188
189   /* And now, post-process the files.
190    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
191    * Files not produced in the system are said to be produced by root task (top of DAG).
192    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
193    */
194
195   xbt_dict_foreach(files, cursor, name, file) {
196     SD_task_t newfile;
197     if (file->predecessors->empty()) {
198       for (SD_task_t it : *file->successors) {
199         newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
200         SD_task_dependency_add(nullptr, nullptr, root_task, newfile);
201         SD_task_dependency_add(nullptr, nullptr, newfile, it);
202         xbt_dynar_push(result, &newfile);
203       }
204     } else if (file->successors->empty()) {
205       for (SD_task_t it : *file->predecessors){
206         newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
207         SD_task_dependency_add(nullptr, nullptr, it, newfile);
208         SD_task_dependency_add(nullptr, nullptr, newfile, end_task);
209         xbt_dynar_push(result, &newfile);
210       }
211     } else {
212       for (SD_task_t it : *file->predecessors) {
213         for (SD_task_t it2 : *file->successors) {
214           if (it == it2) {
215             XBT_WARN ("File %s is produced and consumed by task %s."
216                       "This loop dependency will prevent the execution of the task.", file->name, it->name);
217           }
218           newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
219           SD_task_dependency_add(nullptr, nullptr, it, newfile);
220           SD_task_dependency_add(nullptr, nullptr, newfile, it2);
221           xbt_dynar_push(result, &newfile);
222         }
223       }
224     }
225   }
226
227   /* Push end task last */
228   xbt_dynar_push(result, &end_task);
229
230   /* Free previous copy of the files */
231   xbt_dict_free(&files);
232   unsigned int cpt;
233   xbt_dynar_foreach(result, cpt, file) {
234     if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
235       uniq_transfer_task_name(file);
236     } else if (SD_task_get_kind(file) == SD_TASK_COMP_SEQ){
237       /* If some tasks do not take files as input, connect them to the root
238        * if they don't produce files, connect them to the end node.
239        */
240       if ((file != root_task) && (file != end_task)) {
241         if (file->inputs->empty())
242           SD_task_dependency_add(nullptr, nullptr, root_task, file);
243         if (file->outputs->empty())
244           SD_task_dependency_add(nullptr, nullptr, file, end_task);
245       }
246     } else {
247       THROW_IMPOSSIBLE;
248     }
249   }
250
251   if (!acyclic_graph_detail(result)) {
252     char* base = xbt_basename(filename);
253     XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.", base);
254     free(base);
255     xbt_dynar_foreach(result, cpt, file)
256       SD_task_destroy(file);
257     xbt_dynar_free_container(&result);
258     return nullptr;
259   } else {
260     return result;
261   }
262 }
263
264 void STag_dax__adag()
265 {
266   XBT_ATTRIB_UNUSED double version;
267   version = xbt_str_parse_double(A_dax__adag_version, "Parse error: %s is not a double");
268
269   xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
270 }
271
272 void STag_dax__job()
273 {
274   double runtime = xbt_str_parse_double(A_dax__job_runtime, "Parse error: %s is not a double");
275   char *name = bprintf("%s@%s", A_dax__job_id, A_dax__job_name);
276   runtime *= 4200000000.;       /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
277   XBT_DEBUG("See <job id=%s runtime=%s %.0f>",A_dax__job_id,A_dax__job_runtime,runtime);
278   current_job = SD_task_create_comp_seq(name, nullptr, runtime);
279   xbt_dict_set(jobs, A_dax__job_id, current_job, nullptr);
280   free(name);
281   xbt_dynar_push(result, &current_job);
282 }
283
284 void STag_dax__uses()
285 {
286   double size = xbt_str_parse_double(A_dax__uses_size, "Parse error: %s is not a double");
287   int is_input = (A_dax__uses_link == A_dax__uses_link_input);
288
289   XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
290   SD_task_t file = static_cast<SD_task_t>(xbt_dict_get_or_null(files, A_dax__uses_file));
291   if (file == nullptr) {
292     file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
293     sd_global->initial_tasks->erase(file);
294     xbt_dict_set(files, A_dax__uses_file, file, nullptr);
295   } else {
296     if (file->amount < size || file->amount > size) {
297       XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, SD_task_get_amount(file), size);
298     }
299   }
300   if (is_input) {
301     SD_task_dependency_add(nullptr, nullptr, file, current_job);
302   } else {
303     SD_task_dependency_add(nullptr, nullptr, current_job, file);
304     if ((file->predecessors->size() + file->inputs->size()) > 1) {
305       XBT_WARN("File %s created at more than one location...", file->name);
306     }
307   }
308 }
309
310 static SD_task_t current_child;
311 void STag_dax__child()
312 {
313   current_child = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, A_dax__child_ref));
314   xbt_assert(current_child != nullptr,"Parse error on line %d: Asked to add dependencies to the non-existent %s task",
315              dax_lineno, A_dax__child_ref);
316 }
317
318 void ETag_dax__child()
319 {
320   current_child = nullptr;
321 }
322
323 void STag_dax__parent()
324 {
325   SD_task_t parent = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, A_dax__parent_ref));
326   xbt_assert(parent != nullptr, "Parse error on line %d: Asked to add a dependency from %s to %s, but %s does not exist",
327              dax_lineno, current_child->name, A_dax__parent_ref, A_dax__parent_ref);
328   SD_task_dependency_add(nullptr, nullptr, parent, current_child);
329   XBT_DEBUG("Control-flow dependency from %s to %s", current_child->name, parent->name);
330 }
331
332 void ETag_dax__adag()
333 {
334   XBT_DEBUG("See </adag>");
335 }
336
337 void ETag_dax__job()
338 {
339   current_job = nullptr;
340   XBT_DEBUG("See </job>");
341 }
342
343 void ETag_dax__parent()
344 {
345   XBT_DEBUG("See </parent>");
346 }
347
348 void ETag_dax__uses()
349 {
350   XBT_DEBUG("See </uses>");
351 }