1 /* Copyright (c) 2009-2023. 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"
11 #include <simgrid/s4u/Host.hpp>
12 #include <simgrid/s4u/Comm.hpp>
13 #include <simgrid/s4u/Engine.hpp>
14 #include <simgrid/s4u/Exec.hpp>
16 #include <xbt/asserts.h>
17 #include <xbt/file.hpp>
24 #include <nlohmann/json.hpp>
27 #include <graphviz/cgraph.h>
30 XBT_LOG_NEW_DEFAULT_CATEGORY(dag_parsing, "Generation DAGs from files");
32 /* Ensure that transfer tasks have unique names even though a file is used several times */
33 static void uniq_transfer_task_name(simgrid::s4u::Comm* comm)
35 const auto& child = comm->get_successors().front();
36 const auto& parent = *(comm->get_dependencies().begin());
38 std::string new_name = parent->get_name() + "_" + comm->get_name() + "_" + child->get_name();
40 comm->set_name(new_name)->start();
43 static bool check_for_cycle(const std::vector<simgrid::s4u::ActivityPtr>& dag)
45 std::vector<simgrid::s4u::ActivityPtr> current;
47 std::copy_if(begin(dag), end(dag), back_inserter(current), [](const auto& a) {
48 return dynamic_cast<simgrid::s4u::Exec*>(a.get()) != nullptr && a->has_no_successor();
51 while (not current.empty()) {
52 std::vector<simgrid::s4u::ActivityPtr> next;
53 for (auto const& a : current) {
55 for (auto const& pred : a->get_dependencies()) {
56 if (dynamic_cast<simgrid::s4u::Comm*>(pred.get()) != nullptr) {
58 // Comms have only one predecessor
59 auto pred_pred = *(pred->get_dependencies().begin());
60 if (std::none_of(pred_pred->get_successors().begin(), pred_pred->get_successors().end(),
61 [](const simgrid::s4u::ActivityPtr& act) { return not act->is_marked(); }))
62 next.push_back(pred_pred);
64 if (std::none_of(pred->get_successors().begin(), pred->get_successors().end(),
65 [](const simgrid::s4u::ActivityPtr& act) { return not act->is_marked(); }))
74 return not std::any_of(dag.begin(), dag.end(), [](const simgrid::s4u::ActivityPtr& a) { return not a->is_marked(); });
77 static YY_BUFFER_STATE input_buffer;
79 namespace simgrid::s4u {
81 static std::vector<ActivityPtr> result;
82 static std::map<std::string, ExecPtr, std::less<>> jobs;
83 static std::map<std::string, Comm*, std::less<>> files;
84 static ExecPtr current_job;
86 /** @brief loads a JSON file describing a DAG
88 * See https://github.com/wfcommons/wfformat for more details.
90 std::vector<ActivityPtr> create_DAG_from_json(const std::string& filename)
92 std::ifstream f(filename);
93 auto data = nlohmann::json::parse(f);
94 std::vector<ActivityPtr> dag = {};
95 std::map<std::string, std::vector<ActivityPtr>> successors = {};
96 std::map<ActivityPtr, Host*> comms_destinations = {};
99 for (auto const& task: data["workflow"]["tasks"]) {
100 if (task["type"] == "compute") {
101 current = Exec::init()->set_name(task["name"])->set_flops_amount(task["runtime"]);
102 if (task.contains("machine"))
103 dynamic_cast<Exec*>(current.get())->set_host(simgrid::s4u::Engine::get_instance()->host_by_name(task["machine"]));
105 else if (task["type"] == "transfer"){
106 current = Comm::sendto_init()->set_name(task["name"])->set_payload_size(task["bytesWritten"]);
107 if (task.contains("machine"))
108 comms_destinations[current] = simgrid::s4u::Engine::get_instance()->host_by_name(task["machine"]);
109 if (task["parents"].size() == 1) {
110 ActivityPtr parent_activity;
111 for (auto const& activity: dag) {
112 if (activity->get_name() == task["parents"][0]) {
113 parent_activity = activity;
117 if (dynamic_cast<Exec*>(parent_activity.get()) != nullptr)
118 dynamic_cast<Comm*>(current.get())->set_source(dynamic_cast<Exec*>(parent_activity.get())->get_host());
119 else if (dynamic_cast<Comm*>(parent_activity.get()) != nullptr)
120 dynamic_cast<Comm*>(current.get())->set_source(dynamic_cast<Comm*>(parent_activity.get())->get_destination());
124 XBT_DEBUG("Task type \"%s\" not supported.", task["type"]);
126 dag.push_back(current);
127 for (auto const& parent: task["parents"]) {
128 auto it = successors.find(parent);
129 if (it == successors.end())
130 successors[parent] = {};
131 successors[parent].push_back(current);
135 for (auto const& [parent, successors_list] : successors)
136 for (auto const& activity: dag)
137 if (activity->get_name() == parent) {
138 for (auto const& successor: successors_list)
139 activity->add_successor(successor);
142 // Assign destinations of Comms (if done before successors are assigned there is a bug)
143 for (auto const& [comm, destination]: comms_destinations)
144 dynamic_cast<Comm*>(comm.get())->set_destination(destination);
146 // Start only Activities with dependencies solved
147 for (auto const& activity: dag) {
148 if (dynamic_cast<Exec*>(activity.get()) != nullptr and activity->dependencies_solved())
154 /** @brief loads a DAX file describing a DAG
156 * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
158 std::vector<ActivityPtr> create_DAG_from_DAX(const std::string& filename)
160 FILE* in_file = fopen(filename.c_str(), "r");
161 xbt_assert(in_file, "Unable to open \"%s\"\n", filename.c_str());
162 input_buffer = dax__create_buffer(in_file, 10);
163 dax__switch_to_buffer(input_buffer);
166 auto root_task = Exec::init()->set_name("root")->set_flops_amount(0);
169 result.push_back(root_task);
171 auto end_task = Exec::init()->set_name("end")->set_flops_amount(0);
174 xbt_assert(dax_lex() == 0, "Parse error in %s: %s", filename.c_str(), dax__parse_err_msg());
175 dax__delete_buffer(input_buffer);
179 /* And now, post-process the files.
180 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
181 * Files not produced in the system are said to be produced by root task (top of DAG).
182 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
184 for (auto const& [_, elm] : files) {
187 if (file->dependencies_solved()) {
188 for (auto const& it : file->get_successors()) {
189 newfile = Comm::sendto_init()->set_name(file->get_name())->set_payload_size(file->get_remaining());
190 root_task->add_successor(newfile);
191 newfile->add_successor(it);
192 result.push_back(newfile);
195 if (file->has_no_successor()) {
196 for (auto const& it : file->get_dependencies()) {
197 newfile = Comm::sendto_init()->set_name(file->get_name())->set_payload_size(file->get_remaining());
198 it->add_successor(newfile);
199 newfile->add_successor(end_task);
200 result.push_back(newfile);
203 for (auto const& it : file->get_dependencies()) {
204 for (auto const& it2 : file->get_successors()) {
206 XBT_WARN("File %s is produced and consumed by task %s."
207 "This loop dependency will prevent the execution of the task.",
208 file->get_cname(), it->get_cname());
210 newfile = Comm::sendto_init()->set_name(file->get_name())->set_payload_size(file->get_remaining());
211 it->add_successor(newfile);
212 newfile->add_successor(it2);
213 result.push_back(newfile);
216 /* Free previous copy of the files */
220 /* Push end task last */
221 result.push_back(end_task);
223 for (const auto& a : result) {
224 auto* comm = dynamic_cast<Comm*>(a.get());
225 if (comm != nullptr) {
226 uniq_transfer_task_name(comm);
228 /* If some tasks do not take files as input, connect them to the root
229 * if they don't produce files, connect them to the end node.
231 if ((a != root_task) && (a != end_task)) {
232 if (a->dependencies_solved())
233 root_task->add_successor(a);
234 if (a->has_no_successor())
235 a->add_successor(end_task);
240 if (not check_for_cycle(result)) {
241 XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.",
242 simgrid::xbt::Path(filename).get_base_name().c_str());
243 for (const auto& a : result)
252 std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename)
254 FILE* in_file = fopen(filename.c_str(), "r");
255 xbt_assert(in_file != nullptr, "Failed to open file: %s", filename.c_str());
257 Agraph_t* dag_dot = agread(in_file, nullptr);
259 std::unordered_map<std::string, ActivityPtr> activities;
260 std::vector<ActivityPtr> dag;
265 /* Create all the nodes */
266 Agnode_t* node = nullptr;
267 for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
268 const std::string name = agnameof(node);
269 double amount = atof(agget(node, (char*)"size"));
271 if (activities.find(name) == activities.end()) {
272 XBT_DEBUG("See <Exec id = %s amount = %.0f>", name.c_str(), amount);
273 act = Exec::init()->set_name(name)->set_flops_amount(amount)->start();
274 activities.try_emplace(name, act);
275 if (name != "root" && name != "end")
278 XBT_WARN("Exec '%s' is defined more than once", name.c_str());
281 /*Check if 'root' and 'end' nodes have been explicitly declared. If not, create them. */
282 if (activities.find("root") == activities.end())
283 root = Exec::init()->set_name("root")->set_flops_amount(0)->start();
285 root = activities.at("root");
287 if (activities.find("end") == activities.end())
288 end = Exec::init()->set_name("end")->set_flops_amount(0)->start();
290 end = activities.at("end");
293 std::vector<Agedge_t*> edges;
294 for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
296 for (Agedge_t* edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge))
297 edges.push_back(edge);
299 /* Be sure edges are sorted */
300 std::sort(edges.begin(), edges.end(), [](const Agedge_t* a, const Agedge_t* b) { return AGSEQ(a) < AGSEQ(b); });
302 for (Agedge_t* edge : edges) {
303 const char* src_name = agnameof(agtail(edge));
304 const char* dst_name = agnameof(aghead(edge));
305 double size = atof(agget(edge, (char*)"size"));
307 ActivityPtr src = activities.at(src_name);
308 ActivityPtr dst = activities.at(dst_name);
310 std::string name = std::string(src_name) + "->" + dst_name;
311 XBT_DEBUG("See <Comm id=%s amount = %.0f>", name.c_str(), size);
312 if (activities.find(name) == activities.end()) {
313 act = Comm::sendto_init()->set_name(name)->set_payload_size(size)->start();
314 src->add_successor(act);
315 act->add_successor(dst);
316 activities.try_emplace(name, act);
319 XBT_WARN("Comm '%s' is defined more than once", name.c_str());
322 src->add_successor(dst);
327 XBT_DEBUG("All activities have been created, put %s at the beginning and %s at the end", root->get_cname(),
329 dag.insert(dag.begin(), root);
332 /* Connect entry tasks to 'root', and exit tasks to 'end'*/
333 for (const auto& a : dag) {
334 if (a->dependencies_solved() && a != root) {
335 XBT_DEBUG("Activity '%s' has no dependencies. Add dependency from 'root'", a->get_cname());
336 root->add_successor(a);
339 if (a->has_no_successor() && a != end) {
340 XBT_DEBUG("Activity '%s' has no successors. Add dependency to 'end'", a->get_cname());
341 a->add_successor(end);
347 if (not check_for_cycle(dag)) {
348 std::string base = simgrid::xbt::Path(filename).get_base_name();
349 XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.", base.c_str());
350 for (const auto& a : dag)
359 std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename)
361 xbt_die("create_DAG_from_dot() is not usable because graphviz was not found.\n"
362 "Please install graphviz, graphviz-dev, and libgraphviz-dev (and erase CMakeCache.txt) before recompiling.");
365 } // namespace simgrid::s4u
367 void STag_dax__adag()
370 double version = std::stod(A_dax__adag_version);
371 xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
372 } catch (const std::invalid_argument&) {
373 throw std::invalid_argument(std::string("Parse error: ") + A_dax__adag_version + " is not a double");
380 double runtime = std::stod(A_dax__job_runtime);
382 std::string name = std::string(A_dax__job_id) + "@" + A_dax__job_name;
383 runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
384 XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
385 simgrid::s4u::current_job = simgrid::s4u::Exec::init()->set_name(name)->set_flops_amount(runtime)->start();
386 simgrid::s4u::jobs.try_emplace(A_dax__job_id, simgrid::s4u::current_job);
387 simgrid::s4u::result.push_back(simgrid::s4u::current_job);
388 } catch (const std::invalid_argument&) {
389 throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
393 void STag_dax__uses()
397 size = std::stod(A_dax__uses_size);
398 } catch (const std::invalid_argument&) {
399 throw std::invalid_argument(std::string("Parse error: ") + A_dax__uses_size + " is not a double");
401 bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
403 XBT_DEBUG("See <uses file=%s %s>", A_dax__uses_file, (is_input ? "in" : "out"));
404 auto it = simgrid::s4u::files.find(A_dax__uses_file);
405 simgrid::s4u::CommPtr file;
406 if (it == simgrid::s4u::files.end()) {
407 file = simgrid::s4u::Comm::sendto_init()->set_name(A_dax__uses_file)->set_payload_size(size);
408 simgrid::s4u::files[A_dax__uses_file] = file.get();
411 if (file->get_remaining() < size || file->get_remaining() > size) {
412 XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, file->get_remaining(), size);
416 file->add_successor(simgrid::s4u::current_job);
418 simgrid::s4u::current_job->add_successor(file);
419 if (file->get_dependencies().size() > 1) {
420 XBT_WARN("File %s created at more than one location...", file->get_cname());
425 static simgrid::s4u::ExecPtr current_child;
426 void STag_dax__child()
428 auto job = simgrid::s4u::jobs.find(A_dax__child_ref);
429 if (job != simgrid::s4u::jobs.end()) {
430 current_child = job->second;
432 throw std::out_of_range("Parse error on line " + std::to_string(dax_lineno) +
433 ": Asked to add dependencies to the non-existent " + A_dax__child_ref + "task");
437 void ETag_dax__child()
439 current_child = nullptr;
442 void STag_dax__parent()
444 auto job = simgrid::s4u::jobs.find(A_dax__parent_ref);
445 if (job != simgrid::s4u::jobs.end()) {
446 auto parent = job->second;
447 parent->add_successor(current_child);
448 XBT_DEBUG("Control-flow dependency from %s to %s", current_child->get_cname(), parent->get_cname());
450 throw std::out_of_range("Parse error on line " + std::to_string(dax_lineno) + ": Asked to add a dependency from " +
451 current_child->get_name() + " to " + A_dax__parent_ref + ", but " + A_dax__parent_ref +
456 void ETag_dax__adag()
458 XBT_DEBUG("See </adag>");
463 simgrid::s4u::current_job = nullptr;
464 XBT_DEBUG("See </job>");
467 void ETag_dax__parent()
469 XBT_DEBUG("See </parent>");
472 void ETag_dax__uses()
474 XBT_DEBUG("See </uses>");