1 /* Copyright (c) 2009-2021. The SimGrid Team.
2 * All rights reserved. */
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. */
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"
21 XBT_LOG_NEW_DEFAULT_CATEGORY(daxparse, "Parsing DAX files");
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)
26 const auto& child = comm->get_successors().front();
27 const auto& parent = *(comm->get_dependencies().begin());
29 std::string new_name = parent->get_name() + "_" + comm->get_name() + "_" + child->get_name();
31 comm->set_name(new_name)->vetoable_start();
34 bool check_for_cycle(const std::vector<simgrid::s4u::ActivityPtr>& dag)
36 std::vector<simgrid::s4u::ActivityPtr> current;
38 for (const auto& a : dag)
39 if (dynamic_cast<simgrid::s4u::Exec*>(a.get()) != nullptr && a->is_waited_by() == 0)
42 while (not current.empty()) {
43 std::vector<simgrid::s4u::ActivityPtr> next;
44 for (auto const& a : current) {
46 for (auto const& pred : a->get_dependencies()) {
47 if (dynamic_cast<simgrid::s4u::Comm*>(pred.get()) != nullptr) {
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);
55 if (std::none_of(pred->get_successors().begin(), pred->get_successors().end(),
56 [](const simgrid::s4u::ActivityPtr& act) { return not act->is_marked(); }))
65 return not std::any_of(dag.begin(), dag.end(), [](const simgrid::s4u::ActivityPtr& a) { return not a->is_marked(); });
68 static YY_BUFFER_STATE input_buffer;
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;
78 /** @brief loads a DAX file describing a DAG
80 * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
82 std::vector<ActivityPtr> create_DAG_from_DAX(const std::string& filename)
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);
90 auto root_task = Exec::init()->set_name("root")->set_flops_amount(0);
91 root_task->vetoable_start();
93 result.push_back(root_task);
95 auto end_task = Exec::init()->set_name("end")->set_flops_amount(0);
96 end_task->vetoable_start();
98 xbt_assert(dax_lex() == 0, "Parse error in %s: %s", filename.c_str(), dax__parse_err_msg());
99 dax__delete_buffer(input_buffer);
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).
110 for (auto const& elm : files) {
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);
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);
129 for (auto const& it : file->get_dependencies()) {
130 for (auto const& it2 : file->get_successors()) {
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());
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);
142 /* Free previous copy of the files */
146 /* Push end task last */
147 result.push_back(end_task);
149 for (const auto& a : result) {
150 auto* comm = dynamic_cast<Comm*>(a.get());
151 if (comm != nullptr) {
152 uniq_transfer_task_name(comm);
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.
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);
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)
178 } // namespace simgrid
180 void STag_dax__adag()
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");
193 double runtime = std::stod(std::string(A_dax__job_runtime));
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");
206 void STag_dax__uses()
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");
214 bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
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();
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);
229 file->add_successor(simgrid::s4u::current_job);
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());
238 static simgrid::s4u::ExecPtr current_child;
239 void STag_dax__child()
241 auto job = simgrid::s4u::jobs.find(A_dax__child_ref);
242 if (job != simgrid::s4u::jobs.end()) {
243 current_child = job->second;
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");
250 void ETag_dax__child()
252 current_child = nullptr;
255 void STag_dax__parent()
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());
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");
269 void ETag_dax__adag()
271 XBT_DEBUG("See </adag>");
276 simgrid::s4u::current_job = nullptr;
277 XBT_DEBUG("See </job>");
280 void ETag_dax__parent()
282 XBT_DEBUG("See </parent>");
285 void ETag_dax__uses()
287 XBT_DEBUG("See </uses>");