Logo AND Algorithmique Numérique Distribuée

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