Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix performance issue in simdag tests
[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_predecessors()) {
59         if (input->get_kind() == SD_TASK_COMM_E2E || input->get_kind() == SD_TASK_COMM_PAR_MXN_1D_BLOCK) {
60           input->mark();
61           // Inputs are communication, hence they can have only one predecessor
62           auto input_pred = *(input->get_dependencies().begin());
63           if (children_are_marked(input_pred))
64             next.push_back(input_pred);
65         }
66       }
67       for (auto const& pred : t->get_dependencies()) {
68         if (children_are_marked(pred))
69           next.push_back(pred);
70       }
71     }
72     current.clear();
73     current = next;
74   }
75
76   bool all_marked = true;
77   //test if all tasks are marked
78   xbt_dynar_foreach(dag,count,task){
79     if (task->get_kind() != SD_TASK_COMM_E2E && not task->is_marked()) {
80       XBT_WARN("the task %s is not marked", task->get_cname());
81       all_marked = false;
82       break;
83     }
84   }
85
86   if (not all_marked) {
87     XBT_VERB("there is at least one cycle in your task graph");
88     xbt_dynar_foreach(dag,count,task){
89       if (task->get_kind() != SD_TASK_COMM_E2E && task->has_unsolved_dependencies() == 0) {
90         task->mark();
91         current.push_back(task);
92       }
93     }
94     //test if something has to be done for the next iteration
95     while (not current.empty()) {
96       std::vector<SD_task_t> next;
97       //test if the current iteration is done
98       for (auto const& t : current) {
99         t->mark();
100         for (auto const& output : t->get_successors()) {
101           if (output->get_kind() == SD_TASK_COMM_E2E || output->get_kind() == SD_TASK_COMM_PAR_MXN_1D_BLOCK) {
102             output->mark();
103             // outputs are communication, hence they can have only one successor
104             SD_task_t output_succ = output->get_successors().front();
105             if (parents_are_marked(output_succ))
106               next.push_back(output_succ);
107           }
108         }
109         for (SD_task_t const& succ : t->get_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->get_kind() != SD_TASK_COMM_E2E && not task->is_marked()) {
121         XBT_WARN("the task %s is in a cycle", task->get_cname());
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   root_task->set_state(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   xbt_assert(dax_lex() == 0, "Parse error in %s: %s", filename, dax__parse_err_msg());
158   dax__delete_buffer(input_buffer);
159   fclose(in_file);
160   dax_lex_destroy();
161
162   /* And now, post-process the files.
163    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
164    * Files not produced in the system are said to be produced by root task (top of DAG).
165    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
166    */
167
168   for (auto const& elm : files) {
169     file = elm.second;
170     SD_task_t newfile;
171     if (file->has_unsolved_dependencies() == 0) {
172       for (SD_task_t const& it : file->get_successors()) {
173         newfile = SD_task_create_comm_e2e(file->get_cname(), nullptr, file->get_amount());
174         root_task->dependency_add(newfile);
175         newfile->dependency_add(it);
176         xbt_dynar_push(result, &newfile);
177       }
178     }
179     if (file->is_waited_by() == 0) {
180       for (SD_task_t const& it : file->get_dependencies()) {
181         newfile = SD_task_create_comm_e2e(file->get_cname(), nullptr, file->get_amount());
182         it->dependency_add(newfile);
183         newfile->dependency_add(end_task);
184         xbt_dynar_push(result, &newfile);
185       }
186     }
187     for (SD_task_t const& it : file->get_dependencies()) {
188       for (SD_task_t const& it2 : file->get_successors()) {
189         if (it == it2) {
190           XBT_WARN("File %s is produced and consumed by task %s."
191                    "This loop dependency will prevent the execution of the task.",
192                    file->get_cname(), it->get_cname());
193         }
194         newfile = SD_task_create_comm_e2e(file->get_cname(), nullptr, file->get_amount());
195         it->dependency_add(newfile);
196         newfile->dependency_add(it2);
197         xbt_dynar_push(result, &newfile);
198       }
199     }
200     /* Free previous copy of the files */
201     file->destroy();
202   }
203
204   /* Push end task last */
205   xbt_dynar_push(result, &end_task);
206
207   unsigned int cpt;
208   xbt_dynar_foreach(result, cpt, file) {
209     if (file->get_kind() == SD_TASK_COMM_E2E) {
210       uniq_transfer_task_name(file);
211     } else {
212       /* If some tasks do not take files as input, connect them to the root
213        * if they don't produce files, connect them to the end node.
214        */
215       if ((file != root_task) && (file != end_task)) {
216         if (file->has_unsolved_dependencies() == 0)
217           root_task->dependency_add(file);
218         if (file->is_waited_by() == 0)
219           file->dependency_add(end_task);
220       }
221     }
222   }
223
224   if (not acyclic_graph_detail(result)) {
225     XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.",
226               simgrid::xbt::Path(filename).get_base_name().c_str());
227     xbt_dynar_foreach(result, cpt, file)
228       file->destroy();
229     xbt_dynar_free_container(&result);
230     result = nullptr;
231   }
232
233   return result;
234 }
235
236 void STag_dax__adag()
237 {
238   try {
239     double version = std::stod(std::string(A_dax__adag_version));
240     xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
241   } catch (const std::invalid_argument&) {
242     throw std::invalid_argument(std::string("Parse error: ") + A_dax__adag_version + " is not a double");
243   }
244 }
245
246 void STag_dax__job()
247 {
248   try {
249     double runtime = std::stod(std::string(A_dax__job_runtime));
250
251     std::string name = std::string(A_dax__job_id) + "@" + A_dax__job_name;
252     runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
253     XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
254     current_job = SD_task_create_comp_seq(name.c_str(), nullptr, runtime);
255     jobs.insert({A_dax__job_id, current_job});
256     xbt_dynar_push(result, &current_job);
257   } catch (const std::invalid_argument&) {
258     throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
259   }
260 }
261
262 void STag_dax__uses()
263 {
264   double size;
265   try {
266     size = std::stod(std::string(A_dax__uses_size));
267   } catch (const std::invalid_argument&) {
268     throw std::invalid_argument(std::string("Parse error: ") + A_dax__uses_size + " is not a double");
269   }
270   bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
271
272   XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
273   auto it = files.find(A_dax__uses_file);
274   SD_task_t file;
275   if (it == files.end()) {
276     file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
277     sd_global->initial_tasks.erase(file);
278     files[A_dax__uses_file] = file;
279   } else {
280     file = it->second;
281     if (file->get_amount() < size || file->get_amount() > size) {
282       XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, file->get_amount(), size);
283     }
284   }
285   if (is_input) {
286     file->dependency_add(current_job);
287   } else {
288     current_job->dependency_add(file);
289     if (file->has_unsolved_dependencies() > 1) {
290       XBT_WARN("File %s created at more than one location...", file->get_cname());
291     }
292   }
293 }
294
295 static SD_task_t current_child;
296 void STag_dax__child()
297 {
298   auto job = jobs.find(A_dax__child_ref);
299   if (job != jobs.end()) {
300     current_child = job->second;
301   } else {
302     throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
303                             ": Asked to add dependencies to the non-existent " + A_dax__child_ref + "task");
304   }
305 }
306
307 void ETag_dax__child()
308 {
309   current_child = nullptr;
310 }
311
312 void STag_dax__parent()
313 {
314   auto job = jobs.find(A_dax__parent_ref);
315   if (job != jobs.end()) {
316     auto parent = job->second;
317     parent->dependency_add(current_child);
318     XBT_DEBUG("Control-flow dependency from %s to %s", current_child->get_cname(), parent->get_cname());
319   } else {
320     throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
321                             ": Asked to add a dependency from " + current_child->get_name() + " to " +
322                             A_dax__parent_ref + ", but " + A_dax__parent_ref + " does not exist");
323   }
324 }
325
326 void ETag_dax__adag()
327 {
328   XBT_DEBUG("See </adag>");
329 }
330
331 void ETag_dax__job()
332 {
333   current_job = nullptr;
334   XBT_DEBUG("See </job>");
335 }
336
337 void ETag_dax__parent()
338 {
339   XBT_DEBUG("See </parent>");
340 }
341
342 void ETag_dax__uses()
343 {
344   XBT_DEBUG("See </uses>");
345 }