Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] cleanup some recent smells
[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/Engine.hpp"
10 #include "simgrid/s4u/Exec.hpp"
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 static void uniq_transfer_task_name(simgrid::s4u::Comm* comm)
25 {
26   const auto& child  = comm->get_successors().front();
27   const auto& parent = *(comm->get_dependencies().begin());
28
29   std::string new_name = parent->get_name() + "_" + comm->get_name() + "_" + child->get_name();
30
31   comm->set_name(new_name)->vetoable_start();
32 }
33
34 bool check_for_cycle(const std::vector<simgrid::s4u::ActivityPtr>& dag)
35 {
36   std::vector<simgrid::s4u::ActivityPtr> current;
37
38   for (const auto& a : dag)
39     if (dynamic_cast<simgrid::s4u::Exec*>(a.get()) != nullptr && a->is_waited_by() == 0)
40       current.push_back(a);
41
42   while (not current.empty()) {
43     std::vector<simgrid::s4u::ActivityPtr> next;
44     for (auto const& a : current) {
45       a->mark();
46       for (auto const& pred : a->get_dependencies()) {
47         if (dynamic_cast<simgrid::s4u::Comm*>(pred.get()) != nullptr) {
48           pred->mark();
49           // Comms have only one predecessor
50           auto pred_pred = *(pred->get_dependencies().begin());
51           if (std::none_of(pred_pred->get_successors().begin(), pred_pred->get_successors().end(),
52                            [](const simgrid::s4u::ActivityPtr& act) { return not act->is_marked(); }))
53             next.push_back(pred_pred);
54         } else {
55           if (std::none_of(pred->get_successors().begin(), pred->get_successors().end(),
56                            [](const simgrid::s4u::ActivityPtr& act) { return not act->is_marked(); }))
57             next.push_back(pred);
58         }
59       }
60     }
61     current.clear();
62     current = next;
63   }
64
65   return not std::any_of(dag.begin(), dag.end(), [](const simgrid::s4u::ActivityPtr& a) { return not a->is_marked(); });
66 }
67
68 static YY_BUFFER_STATE input_buffer;
69
70 namespace simgrid {
71 namespace s4u {
72
73 static std::vector<ActivityPtr> result;
74 static std::map<std::string, ExecPtr, std::less<>> jobs;
75 static std::map<std::string, Comm*, std::less<>> files;
76 static ExecPtr current_job;
77
78 /** @brief loads a DAX file describing a DAG
79  *
80  * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
81  */
82 std::vector<ActivityPtr> create_DAG_from_DAX(const std::string& filename)
83 {
84   FILE* in_file = fopen(filename.c_str(), "r");
85   xbt_assert(in_file, "Unable to open \"%s\"\n", filename.c_str());
86   input_buffer = dax__create_buffer(in_file, 10);
87   dax__switch_to_buffer(input_buffer);
88   dax_lineno = 1;
89
90   auto root_task = Exec::init()->set_name("root")->set_flops_amount(0);
91   root_task->vetoable_start();
92
93   result.push_back(root_task);
94
95   auto end_task = Exec::init()->set_name("end")->set_flops_amount(0);
96   end_task->vetoable_start();
97
98   xbt_assert(dax_lex() == 0, "Parse error in %s: %s", filename.c_str(), dax__parse_err_msg());
99   dax__delete_buffer(input_buffer);
100   fclose(in_file);
101   dax_lex_destroy();
102
103   /* And now, post-process the files.
104    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
105    * Files not produced in the system are said to be produced by root task (top of DAG).
106    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
107    */
108   CommPtr file;
109
110   for (auto const& elm : files) {
111     file = elm.second;
112     CommPtr newfile;
113     if (file->dependencies_solved()) {
114       for (auto const& it : file->get_successors()) {
115         newfile = Comm::sendto_init()->set_name(file->get_name())->set_payload_size(file->get_remaining());
116         root_task->add_successor(newfile);
117         newfile->add_successor(it);
118         result.push_back(newfile);
119       }
120     }
121     if (file->is_waited_by() == 0) {
122       for (auto const& it : file->get_dependencies()) {
123         newfile = Comm::sendto_init()->set_name(file->get_name())->set_payload_size(file->get_remaining());
124         it->add_successor(newfile);
125         newfile->add_successor(end_task);
126         result.push_back(newfile);
127       }
128     }
129     for (auto const& it : file->get_dependencies()) {
130       for (auto const& it2 : file->get_successors()) {
131         if (it == it2) {
132           XBT_WARN("File %s is produced and consumed by task %s."
133                    "This loop dependency will prevent the execution of the task.",
134                    file->get_cname(), it->get_cname());
135         }
136         newfile = Comm::sendto_init()->set_name(file->get_name())->set_payload_size(file->get_remaining());
137         it->add_successor(newfile);
138         newfile->add_successor(it2);
139         result.push_back(newfile);
140       }
141     }
142     /* Free previous copy of the files */
143     file->destroy();
144   }
145
146   /* Push end task last */
147   result.push_back(end_task);
148
149   for (const auto& a : result) {
150     auto* comm = dynamic_cast<Comm*>(a.get());
151     if (comm != nullptr) {
152       uniq_transfer_task_name(comm);
153     } else {
154       /* If some tasks do not take files as input, connect them to the root
155        * if they don't produce files, connect them to the end node.
156        */
157       if ((a != root_task) && (a != end_task)) {
158         if (a->dependencies_solved())
159           root_task->add_successor(a);
160         if (a->is_waited_by() == 0)
161           a->add_successor(end_task);
162       }
163     }
164   }
165
166   if (not check_for_cycle(result)) {
167     XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.",
168               simgrid::xbt::Path(filename).get_base_name().c_str());
169     for (const auto& a : result)
170       a->destroy();
171     result.clear();
172   }
173
174   return result;
175 }
176
177 } // namespace s4u
178 } // namespace simgrid
179
180 void STag_dax__adag()
181 {
182   try {
183     double version = std::stod(std::string(A_dax__adag_version));
184     xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
185   } catch (const std::invalid_argument&) {
186     throw std::invalid_argument(std::string("Parse error: ") + A_dax__adag_version + " is not a double");
187   }
188 }
189
190 void STag_dax__job()
191 {
192   try {
193     double runtime = std::stod(std::string(A_dax__job_runtime));
194
195     std::string name = std::string(A_dax__job_id) + "@" + A_dax__job_name;
196     runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
197     XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
198     simgrid::s4u::current_job = simgrid::s4u::Exec::init()->set_name(name)->set_flops_amount(runtime)->vetoable_start();
199     simgrid::s4u::jobs.insert({A_dax__job_id, simgrid::s4u::current_job});
200     simgrid::s4u::result.push_back(simgrid::s4u::current_job);
201   } catch (const std::invalid_argument&) {
202     throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
203   }
204 }
205
206 void STag_dax__uses()
207 {
208   double size;
209   try {
210     size = std::stod(std::string(A_dax__uses_size));
211   } catch (const std::invalid_argument&) {
212     throw std::invalid_argument(std::string("Parse error: ") + A_dax__uses_size + " is not a double");
213   }
214   bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
215
216   XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
217   auto it = simgrid::s4u::files.find(A_dax__uses_file);
218   simgrid::s4u::CommPtr file;
219   if (it == simgrid::s4u::files.end()) {
220     file = simgrid::s4u::Comm::sendto_init()->set_name(A_dax__uses_file)->set_payload_size(size);
221     simgrid::s4u::files[A_dax__uses_file] = file.get();
222   } else {
223     file = it->second;
224     if (file->get_remaining() < size || file->get_remaining() > size) {
225       XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, file->get_remaining(), size);
226     }
227   }
228   if (is_input) {
229     file->add_successor(simgrid::s4u::current_job);
230   } else {
231     simgrid::s4u::current_job->add_successor(file);
232     if (file->get_dependencies().size() > 1) {
233       XBT_WARN("File %s created at more than one location...", file->get_cname());
234     }
235   }
236 }
237
238 static simgrid::s4u::ExecPtr current_child;
239 void STag_dax__child()
240 {
241   auto job = simgrid::s4u::jobs.find(A_dax__child_ref);
242   if (job != simgrid::s4u::jobs.end()) {
243     current_child = job->second;
244   } else {
245     throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
246                             ": Asked to add dependencies to the non-existent " + A_dax__child_ref + "task");
247   }
248 }
249
250 void ETag_dax__child()
251 {
252   current_child = nullptr;
253 }
254
255 void STag_dax__parent()
256 {
257   auto job = simgrid::s4u::jobs.find(A_dax__parent_ref);
258   if (job != simgrid::s4u::jobs.end()) {
259     auto parent = job->second;
260     parent->add_successor(current_child);
261     XBT_DEBUG("Control-flow dependency from %s to %s", current_child->get_cname(), parent->get_cname());
262   } else {
263     throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
264                             ": Asked to add a dependency from " + current_child->get_name() + " to " +
265                             A_dax__parent_ref + ", but " + A_dax__parent_ref + " does not exist");
266   }
267 }
268
269 void ETag_dax__adag()
270 {
271   XBT_DEBUG("See </adag>");
272 }
273
274 void ETag_dax__job()
275 {
276   simgrid::s4u::current_job = nullptr;
277   XBT_DEBUG("See </job>");
278 }
279
280 void ETag_dax__parent()
281 {
282   XBT_DEBUG("See </parent>");
283 }
284
285 void ETag_dax__uses()
286 {
287   XBT_DEBUG("See </uses>");
288 }