Logo AND Algorithmique Numérique Distribuée

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