Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
plug leak
[simgrid.git] / src / simdag / sd_daxloader.cpp
1 /* Copyright (c) 2009-2021. 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 #include <stdexcept>
15
16 #include "dax_dtd.h"
17 #include "dax_dtd.c"
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_daxparse, sd, "Parsing DAX files");
20
21 /* Ensure that transfer tasks have unique names even though a file is used several times */
22 void uniq_transfer_task_name(SD_task_t task)
23 {
24   const_SD_task_t child  = *(task->get_successors().begin());
25   const_SD_task_t parent = *(task->get_predecessors().begin());
26
27   std::string new_name =
28       std::string(SD_task_get_name(parent)) + "_" + SD_task_get_name(task) + "_" + SD_task_get_name(child);
29
30   SD_task_set_name(task, new_name.c_str());
31 }
32
33 static bool children_are_marked(const_SD_task_t task)
34 {
35   return std::none_of(task->get_successors().begin(), task->get_successors().end(),
36                       [](const SD_task_t& elm) { return not elm->is_marked(); }) &&
37          std::none_of(task->get_outputs().begin(), task->get_outputs().end(),
38                       [](const SD_task_t& elm) { return not elm->is_marked(); });
39 }
40
41 static bool parents_are_marked(const_SD_task_t task)
42 {
43   return std::none_of(task->get_predecessors().begin(), task->get_predecessors().end(),
44                       [](const SD_task_t& elm) { return not elm->is_marked(); }) &&
45          std::none_of(task->get_inputs().begin(), task->get_inputs().end(),
46                       [](const SD_task_t& elm) { return not elm->is_marked(); });
47 }
48
49 bool acyclic_graph_detail(const_xbt_dynar_t dag)
50 {
51   unsigned int count;
52   SD_task_t task = nullptr;
53   std::vector<SD_task_t> current;
54   xbt_dynar_foreach (dag, count, task)
55     if (task->get_kind() != SD_TASK_COMM_E2E && task->get_successors().empty() && task->get_outputs().empty())
56       current.push_back(task);
57
58   while (not current.empty()) {
59     std::vector<SD_task_t> next;
60     for (auto const& t : current) {
61       //Mark task
62       t->mark();
63       for (SD_task_t const& input : t->get_inputs()) {
64         input->mark();
65         // Inputs are communication, hence they can have only one predecessor
66         SD_task_t input_pred = *(input->get_predecessors().begin());
67         if (children_are_marked(input_pred))
68           next.push_back(input_pred);
69       }
70       for (SD_task_t const& pred : t->get_predecessors()) {
71         if (children_are_marked(pred))
72           next.push_back(pred);
73       }
74     }
75     current.clear();
76     current = next;
77   }
78
79   bool all_marked = true;
80   //test if all tasks are marked
81   xbt_dynar_foreach(dag,count,task){
82     if (task->get_kind() != SD_TASK_COMM_E2E && not task->is_marked()) {
83       XBT_WARN("the task %s is not marked", task->get_cname());
84       all_marked = false;
85       break;
86     }
87   }
88
89   if (not all_marked) {
90     XBT_VERB("there is at least one cycle in your task graph");
91     xbt_dynar_foreach(dag,count,task){
92       if (task->get_kind() != SD_TASK_COMM_E2E && task->get_predecessors().empty() && task->get_inputs().empty()) {
93         task->mark();
94         current.push_back(task);
95       }
96     }
97     //test if something has to be done for the next iteration
98     while (not current.empty()) {
99       std::vector<SD_task_t> next;
100       //test if the current iteration is done
101       for (auto const& t : current) {
102         t->mark();
103         for (SD_task_t const& output : t->get_outputs()) {
104           output->mark();
105           // outputs are communication, hence they can have only one successor
106           SD_task_t output_succ = *(output->get_successors().begin());
107           if (parents_are_marked(output_succ))
108             next.push_back(output_succ);
109         }
110         for (SD_task_t const& succ : t->get_successors()) {
111           if (parents_are_marked(succ))
112             next.push_back(succ);
113         }
114       }
115       current.clear();
116       current = next;
117     }
118
119     all_marked = true;
120     xbt_dynar_foreach(dag,count,task){
121       if (task->get_kind() != SD_TASK_COMM_E2E && not task->is_marked()) {
122         XBT_WARN("the task %s is in a cycle", task->get_cname());
123         all_marked = false;
124       }
125     }
126   }
127   return all_marked;
128 }
129
130 static YY_BUFFER_STATE input_buffer;
131
132 static xbt_dynar_t result;
133 static std::map<std::string, SD_task_t, std::less<>> jobs;
134 static std::map<std::string, SD_task_t, std::less<>> files;
135 static SD_task_t current_job;
136
137 /** @brief loads a DAX file describing a DAG
138  *
139  * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
140  */
141 xbt_dynar_t SD_daxload(const char *filename)
142 {
143   SD_task_t file;
144   FILE* in_file = fopen(filename, "r");
145   xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
146   input_buffer = dax__create_buffer(in_file, 10);
147   dax__switch_to_buffer(input_buffer);
148   dax_lineno = 1;
149
150   result              = xbt_dynar_new(sizeof(SD_task_t), nullptr);
151   SD_task_t root_task = SD_task_create_comp_seq("root", nullptr, 0);
152   /* by design the root task is always SCHEDULABLE */
153   root_task->set_state(SD_SCHEDULABLE);
154
155   xbt_dynar_push(result, &root_task);
156   SD_task_t end_task = SD_task_create_comp_seq("end", nullptr, 0);
157
158   xbt_assert(dax_lex() == 0, "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->get_predecessors().empty()) {
173       for (SD_task_t const& it : file->get_successors()) {
174         newfile = SD_task_create_comm_e2e(file->get_cname(), nullptr, file->get_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->get_successors().empty()) {
181       for (SD_task_t const& it : file->get_predecessors()) {
182         newfile = SD_task_create_comm_e2e(file->get_cname(), nullptr, file->get_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->get_predecessors()) {
189       for (SD_task_t const& it2 : file->get_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->get_cname(), it->get_cname());
194         }
195         newfile = SD_task_create_comm_e2e(file->get_cname(), nullptr, file->get_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 {
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->get_inputs().empty())
218           SD_task_dependency_add(root_task, file);
219         if (file->get_outputs().empty())
220           SD_task_dependency_add(file, end_task);
221       }
222     }
223   }
224
225   if (not acyclic_graph_detail(result)) {
226     XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.",
227               simgrid::xbt::Path(filename).get_base_name().c_str());
228     xbt_dynar_foreach(result, cpt, file)
229       SD_task_destroy(file);
230     xbt_dynar_free_container(&result);
231     result = nullptr;
232   }
233
234   return result;
235 }
236
237 void STag_dax__adag()
238 {
239   try {
240     double version = std::stod(std::string(A_dax__adag_version));
241     xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
242   } catch (const std::invalid_argument&) {
243     throw std::invalid_argument(std::string("Parse error: ") + A_dax__adag_version + " is not a double");
244   }
245 }
246
247 void STag_dax__job()
248 {
249   try {
250     double runtime = std::stod(std::string(A_dax__job_runtime));
251
252     std::string name = std::string(A_dax__job_id) + "@" + A_dax__job_name;
253     runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
254     XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
255     current_job = SD_task_create_comp_seq(name.c_str(), nullptr, runtime);
256     jobs.insert({A_dax__job_id, current_job});
257     xbt_dynar_push(result, &current_job);
258   } catch (const std::invalid_argument&) {
259     throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
260   }
261 }
262
263 void STag_dax__uses()
264 {
265   double size;
266   try {
267     size = std::stod(std::string(A_dax__uses_size));
268   } catch (const std::invalid_argument&) {
269     throw std::invalid_argument(std::string("Parse error: ") + A_dax__uses_size + " is not a double");
270   }
271   bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
272
273   XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
274   auto it = files.find(A_dax__uses_file);
275   SD_task_t file;
276   if (it == files.end()) {
277     file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
278     sd_global->initial_tasks.erase(file);
279     files[A_dax__uses_file] = file;
280   } else {
281     file = it->second;
282     if (file->get_amount() < size || file->get_amount() > size) {
283       XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, file->get_amount(), size);
284     }
285   }
286   if (is_input) {
287     SD_task_dependency_add(file, current_job);
288   } else {
289     SD_task_dependency_add(current_job, file);
290     if (file->has_unsolved_dependencies() > 1) {
291       XBT_WARN("File %s created at more than one location...", file->get_cname());
292     }
293   }
294 }
295
296 static SD_task_t current_child;
297 void STag_dax__child()
298 {
299   auto job = jobs.find(A_dax__child_ref);
300   if (job != jobs.end()) {
301     current_child = job->second;
302   } else {
303     throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
304                             ": Asked to add dependencies to the non-existent " + A_dax__child_ref + "task");
305   }
306 }
307
308 void ETag_dax__child()
309 {
310   current_child = nullptr;
311 }
312
313 void STag_dax__parent()
314 {
315   auto job = jobs.find(A_dax__parent_ref);
316   if (job != jobs.end()) {
317     SD_task_t parent = job->second;
318     SD_task_dependency_add(parent, current_child);
319     XBT_DEBUG("Control-flow dependency from %s to %s", current_child->get_cname(), parent->get_cname());
320   } else {
321     throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
322                             ": Asked to add a dependency from " + current_child->get_name() + " to " +
323                             A_dax__parent_ref + ", but " + A_dax__parent_ref + " does not exist");
324   }
325 }
326
327 void ETag_dax__adag()
328 {
329   XBT_DEBUG("See </adag>");
330 }
331
332 void ETag_dax__job()
333 {
334   current_job = nullptr;
335   XBT_DEBUG("See </job>");
336 }
337
338 void ETag_dax__parent()
339 {
340   XBT_DEBUG("See </parent>");
341 }
342
343 void ETag_dax__uses()
344 {
345   XBT_DEBUG("See </uses>");
346 }