Logo AND Algorithmique Numérique Distribuée

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