1 /* Copyright (c) 2009-2022. 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 "src/internal_config.h"
10 #include <simgrid/s4u/Comm.hpp>
11 #include <simgrid/s4u/Engine.hpp>
12 #include <simgrid/s4u/Exec.hpp>
14 #include <xbt/asserts.h>
15 #include <xbt/file.hpp>
23 #include <graphviz/cgraph.h>
26 XBT_LOG_NEW_DEFAULT_CATEGORY(dag_parsing, "Generation DAGs from files");
28 /* Ensure that transfer tasks have unique names even though a file is used several times */
29 static void uniq_transfer_task_name(simgrid::s4u::Comm* comm)
31 const auto& child = comm->get_successors().front();
32 const auto& parent = *(comm->get_dependencies().begin());
34 std::string new_name = parent->get_name() + "_" + comm->get_name() + "_" + child->get_name();
36 comm->set_name(new_name)->vetoable_start();
39 static bool check_for_cycle(const std::vector<simgrid::s4u::ActivityPtr>& dag)
41 std::vector<simgrid::s4u::ActivityPtr> current;
43 for (const auto& a : dag)
44 if (dynamic_cast<simgrid::s4u::Exec*>(a.get()) != nullptr && a->has_no_successor())
47 while (not current.empty()) {
48 std::vector<simgrid::s4u::ActivityPtr> next;
49 for (auto const& a : current) {
51 for (auto const& pred : a->get_dependencies()) {
52 if (dynamic_cast<simgrid::s4u::Comm*>(pred.get()) != nullptr) {
54 // Comms have only one predecessor
55 auto pred_pred = *(pred->get_dependencies().begin());
56 if (std::none_of(pred_pred->get_successors().begin(), pred_pred->get_successors().end(),
57 [](const simgrid::s4u::ActivityPtr& act) { return not act->is_marked(); }))
58 next.push_back(pred_pred);
60 if (std::none_of(pred->get_successors().begin(), pred->get_successors().end(),
61 [](const simgrid::s4u::ActivityPtr& act) { return not act->is_marked(); }))
70 return not std::any_of(dag.begin(), dag.end(), [](const simgrid::s4u::ActivityPtr& a) { return not a->is_marked(); });
73 static YY_BUFFER_STATE input_buffer;
75 namespace simgrid::s4u {
77 static std::vector<ActivityPtr> result;
78 static std::map<std::string, ExecPtr, std::less<>> jobs;
79 static std::map<std::string, Comm*, std::less<>> files;
80 static ExecPtr current_job;
82 /** @brief loads a DAX file describing a DAG
84 * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
86 std::vector<ActivityPtr> create_DAG_from_DAX(const std::string& filename)
88 FILE* in_file = fopen(filename.c_str(), "r");
89 xbt_assert(in_file, "Unable to open \"%s\"\n", filename.c_str());
90 input_buffer = dax__create_buffer(in_file, 10);
91 dax__switch_to_buffer(input_buffer);
94 auto root_task = Exec::init()->set_name("root")->set_flops_amount(0);
95 root_task->vetoable_start();
97 result.push_back(root_task);
99 auto end_task = Exec::init()->set_name("end")->set_flops_amount(0);
100 end_task->vetoable_start();
102 xbt_assert(dax_lex() == 0, "Parse error in %s: %s", filename.c_str(), dax__parse_err_msg());
103 dax__delete_buffer(input_buffer);
107 /* And now, post-process the files.
108 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
109 * Files not produced in the system are said to be produced by root task (top of DAG).
110 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
112 for (auto const& [_, elm] : files) {
115 if (file->dependencies_solved()) {
116 for (auto const& it : file->get_successors()) {
117 newfile = Comm::sendto_init()->set_name(file->get_name())->set_payload_size(file->get_remaining());
118 root_task->add_successor(newfile);
119 newfile->add_successor(it);
120 result.push_back(newfile);
123 if (file->has_no_successor()) {
124 for (auto const& it : file->get_dependencies()) {
125 newfile = Comm::sendto_init()->set_name(file->get_name())->set_payload_size(file->get_remaining());
126 it->add_successor(newfile);
127 newfile->add_successor(end_task);
128 result.push_back(newfile);
131 for (auto const& it : file->get_dependencies()) {
132 for (auto const& it2 : file->get_successors()) {
134 XBT_WARN("File %s is produced and consumed by task %s."
135 "This loop dependency will prevent the execution of the task.",
136 file->get_cname(), it->get_cname());
138 newfile = Comm::sendto_init()->set_name(file->get_name())->set_payload_size(file->get_remaining());
139 it->add_successor(newfile);
140 newfile->add_successor(it2);
141 result.push_back(newfile);
144 /* Free previous copy of the files */
148 /* Push end task last */
149 result.push_back(end_task);
151 for (const auto& a : result) {
152 auto* comm = dynamic_cast<Comm*>(a.get());
153 if (comm != nullptr) {
154 uniq_transfer_task_name(comm);
156 /* If some tasks do not take files as input, connect them to the root
157 * if they don't produce files, connect them to the end node.
159 if ((a != root_task) && (a != end_task)) {
160 if (a->dependencies_solved())
161 root_task->add_successor(a);
162 if (a->has_no_successor())
163 a->add_successor(end_task);
168 if (not check_for_cycle(result)) {
169 XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.",
170 simgrid::xbt::Path(filename).get_base_name().c_str());
171 for (const auto& a : result)
180 std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename)
182 FILE* in_file = fopen(filename.c_str(), "r");
183 xbt_assert(in_file != nullptr, "Failed to open file: %s", filename.c_str());
185 Agraph_t* dag_dot = agread(in_file, NULL);
187 std::unordered_map<std::string, ActivityPtr> activities;
188 std::vector<ActivityPtr> dag;
193 /* Create all the nodes */
194 Agnode_t* node = nullptr;
195 for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
196 const std::string name = agnameof(node);
197 double amount = atof(agget(node, (char*)"size"));
199 if (activities.find(name) == activities.end()) {
200 XBT_DEBUG("See <Exec id = %s amount = %.0f>", name.c_str(), amount);
201 act = Exec::init()->set_name(name)->set_flops_amount(amount)->vetoable_start();
202 activities.try_emplace(name, act);
203 if (name != "root" && name != "end")
206 XBT_WARN("Exec '%s' is defined more than once", name.c_str());
209 /*Check if 'root' and 'end' nodes have been explicitly declared. If not, create them. */
210 if (activities.find("root") == activities.end())
211 root = Exec::init()->set_name("root")->set_flops_amount(0)->vetoable_start();
213 root = activities.at("root");
215 if (activities.find("end") == activities.end())
216 end = Exec::init()->set_name("end")->set_flops_amount(0)->vetoable_start();
218 end = activities.at("end");
221 std::vector<Agedge_t*> edges;
222 for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
224 for (Agedge_t* edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge))
225 edges.push_back(edge);
227 /* Be sure edges are sorted */
228 std::sort(edges.begin(), edges.end(), [](const Agedge_t* a, const Agedge_t* b) { return AGSEQ(a) < AGSEQ(b); });
230 for (Agedge_t* edge : edges) {
231 const char* src_name = agnameof(agtail(edge));
232 const char* dst_name = agnameof(aghead(edge));
233 double size = atof(agget(edge, (char*)"size"));
235 ActivityPtr src = activities.at(src_name);
236 ActivityPtr dst = activities.at(dst_name);
238 std::string name = std::string(src_name) + "->" + dst_name;
239 XBT_DEBUG("See <Comm id=%s amount = %.0f>", name.c_str(), size);
240 if (activities.find(name) == activities.end()) {
241 act = Comm::sendto_init()->set_name(name)->set_payload_size(size)->vetoable_start();
242 src->add_successor(act);
243 act->add_successor(dst);
244 activities.try_emplace(name, act);
247 XBT_WARN("Comm '%s' is defined more than once", name.c_str());
250 src->add_successor(dst);
255 XBT_DEBUG("All activities have been created, put %s at the beginning and %s at the end", root->get_cname(),
257 dag.insert(dag.begin(), root);
260 /* Connect entry tasks to 'root', and exit tasks to 'end'*/
261 for (const auto& a : dag) {
262 if (a->dependencies_solved() && a != root) {
263 XBT_DEBUG("Activity '%s' has no dependencies. Add dependency from 'root'", a->get_cname());
264 root->add_successor(a);
267 if (a->has_no_successor() && a != end) {
268 XBT_DEBUG("Activity '%s' has no successors. Add dependency to 'end'", a->get_cname());
269 a->add_successor(end);
275 if (not check_for_cycle(dag)) {
276 std::string base = simgrid::xbt::Path(filename).get_base_name();
277 XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.", base.c_str());
278 for (const auto& a : dag)
287 std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename)
289 xbt_die("create_DAG_from_dot() is not usable because graphviz was not found.\n"
290 "Please install graphviz, graphviz-dev, and libgraphviz-dev (and erase CMakeCache.txt) before recompiling.");
293 } // namespace simgrid::s4u
295 void STag_dax__adag()
298 double version = std::stod(std::string(A_dax__adag_version));
299 xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
300 } catch (const std::invalid_argument&) {
301 throw std::invalid_argument(std::string("Parse error: ") + A_dax__adag_version + " is not a double");
308 double runtime = std::stod(std::string(A_dax__job_runtime));
310 std::string name = std::string(A_dax__job_id) + "@" + A_dax__job_name;
311 runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
312 XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
313 simgrid::s4u::current_job = simgrid::s4u::Exec::init()->set_name(name)->set_flops_amount(runtime)->vetoable_start();
314 simgrid::s4u::jobs.try_emplace(A_dax__job_id, simgrid::s4u::current_job);
315 simgrid::s4u::result.push_back(simgrid::s4u::current_job);
316 } catch (const std::invalid_argument&) {
317 throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
321 void STag_dax__uses()
325 size = std::stod(std::string(A_dax__uses_size));
326 } catch (const std::invalid_argument&) {
327 throw std::invalid_argument(std::string("Parse error: ") + A_dax__uses_size + " is not a double");
329 bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
331 XBT_DEBUG("See <uses file=%s %s>", A_dax__uses_file, (is_input ? "in" : "out"));
332 auto it = simgrid::s4u::files.find(A_dax__uses_file);
333 simgrid::s4u::CommPtr file;
334 if (it == simgrid::s4u::files.end()) {
335 file = simgrid::s4u::Comm::sendto_init()->set_name(A_dax__uses_file)->set_payload_size(size);
336 simgrid::s4u::files[A_dax__uses_file] = file.get();
339 if (file->get_remaining() < size || file->get_remaining() > size) {
340 XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, file->get_remaining(), size);
344 file->add_successor(simgrid::s4u::current_job);
346 simgrid::s4u::current_job->add_successor(file);
347 if (file->get_dependencies().size() > 1) {
348 XBT_WARN("File %s created at more than one location...", file->get_cname());
353 static simgrid::s4u::ExecPtr current_child;
354 void STag_dax__child()
356 auto job = simgrid::s4u::jobs.find(A_dax__child_ref);
357 if (job != simgrid::s4u::jobs.end()) {
358 current_child = job->second;
360 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
361 ": Asked to add dependencies to the non-existent " + A_dax__child_ref + "task");
365 void ETag_dax__child()
367 current_child = nullptr;
370 void STag_dax__parent()
372 auto job = simgrid::s4u::jobs.find(A_dax__parent_ref);
373 if (job != simgrid::s4u::jobs.end()) {
374 auto parent = job->second;
375 parent->add_successor(current_child);
376 XBT_DEBUG("Control-flow dependency from %s to %s", current_child->get_cname(), parent->get_cname());
378 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
379 ": Asked to add a dependency from " + current_child->get_name() + " to " +
380 A_dax__parent_ref + ", but " + A_dax__parent_ref + " does not exist");
384 void ETag_dax__adag()
386 XBT_DEBUG("See </adag>");
391 simgrid::s4u::current_job = nullptr;
392 XBT_DEBUG("See </job>");
395 void ETag_dax__parent()
397 XBT_DEBUG("See </parent>");
400 void ETag_dax__uses()
402 XBT_DEBUG("See </uses>");