Logo AND Algorithmique Numérique Distribuée

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