Logo AND Algorithmique Numérique Distribuée

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