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/Exec.hpp"
10 #include "simgrid/simdag.h"
11 #include "xbt/file.hpp"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_daxparse, sd, "Parsing DAX files");
23 /* Ensure that transfer tasks have unique names even though a file is used several times */
24 void uniq_transfer_task_name(SD_task_t task)
26 const_SD_task_t child = *(task->get_successors().begin());
27 const_SD_task_t parent = *(task->get_dependencies().begin());
29 std::string new_name = parent->get_name() + "_" + task->get_name() + "_" + child->get_name();
31 task->set_name(new_name);
34 static bool children_are_marked(const_SD_task_t task)
36 return std::none_of(task->get_successors().begin(), task->get_successors().end(),
37 [](const SD_task_t& elm) { return not elm->is_marked(); });
40 static bool parents_are_marked(const_SD_task_t task)
42 return std::none_of(task->get_dependencies().begin(), task->get_dependencies().end(),
43 [](const SD_task_t& elm) { return not elm->is_marked(); });
46 bool acyclic_graph_detail(const_xbt_dynar_t dag)
49 SD_task_t task = nullptr;
50 std::vector<SD_task_t> current;
51 xbt_dynar_foreach (dag, count, task)
52 if (task->get_kind() != SD_TASK_COMM_E2E && task->is_waited_by() == 0)
53 current.push_back(task);
55 while (not current.empty()) {
56 std::vector<SD_task_t> next;
57 for (auto const& t : current) {
60 for (auto const& input : t->get_predecessors()) {
61 if (input->get_kind() == SD_TASK_COMM_E2E || input->get_kind() == SD_TASK_COMM_PAR_MXN_1D_BLOCK) {
63 // Inputs are communication, hence they can have only one predecessor
64 auto input_pred = *(input->get_dependencies().begin());
65 if (children_are_marked(input_pred))
66 next.push_back(input_pred);
69 for (auto const& pred : t->get_dependencies()) {
70 if (children_are_marked(pred))
78 bool all_marked = true;
79 //test if all tasks are marked
80 xbt_dynar_foreach(dag,count,task){
81 if (task->get_kind() != SD_TASK_COMM_E2E && not task->is_marked()) {
82 XBT_WARN("the task %s is not marked", task->get_cname());
89 XBT_VERB("there is at least one cycle in your task graph");
90 xbt_dynar_foreach(dag,count,task){
91 if (task->get_kind() != SD_TASK_COMM_E2E && task->has_unsolved_dependencies() == 0) {
93 current.push_back(task);
96 //test if something has to be done for the next iteration
97 while (not current.empty()) {
98 std::vector<SD_task_t> next;
99 //test if the current iteration is done
100 for (auto const& t : current) {
102 for (auto const& output : t->get_successors()) {
103 if (output->get_kind() == SD_TASK_COMM_E2E || output->get_kind() == SD_TASK_COMM_PAR_MXN_1D_BLOCK) {
105 // outputs are communication, hence they can have only one successor
106 SD_task_t output_succ = output->get_successors().front();
107 if (parents_are_marked(output_succ))
108 next.push_back(output_succ);
111 for (SD_task_t const& succ : t->get_successors()) {
112 if (parents_are_marked(succ))
113 next.push_back(succ);
121 xbt_dynar_foreach(dag,count,task){
122 if (task->get_kind() != SD_TASK_COMM_E2E && not task->is_marked()) {
123 XBT_WARN("the task %s is in a cycle", task->get_cname());
131 bool check_for_cycle(const std::vector<simgrid::s4u::ActivityPtr>& dag)
133 std::vector<simgrid::s4u::ActivityPtr> current;
135 for (const auto& a : dag)
136 if (dynamic_cast<simgrid::s4u::Exec*>(a.get()) != nullptr && a->is_waited_by() == 0)
137 current.push_back(a);
139 while (not current.empty()) {
140 std::vector<simgrid::s4u::ActivityPtr> next;
141 for (auto const& a : current) {
143 for (auto const& pred : a->get_dependencies()) {
144 if (dynamic_cast<simgrid::s4u::Comm*>(pred.get()) != nullptr) {
146 // Comms have only one predecessor
147 auto pred_pred = *(pred->get_dependencies().begin());
148 if (std::none_of(pred_pred->get_successors().begin(), pred_pred->get_successors().end(),
149 [](const simgrid::s4u::ActivityPtr& a) { return not a->is_marked(); }))
150 next.push_back(pred_pred);
152 if (std::none_of(pred->get_successors().begin(), pred->get_successors().end(),
153 [](const simgrid::s4u::ActivityPtr& a) { return not a->is_marked(); }))
154 next.push_back(pred);
162 return not std::any_of(dag.begin(), dag.end(), [](const simgrid::s4u::ActivityPtr& a) { return not a->is_marked(); });
165 static YY_BUFFER_STATE input_buffer;
167 static xbt_dynar_t result;
168 static std::map<std::string, SD_task_t, std::less<>> jobs;
169 static std::map<std::string, SD_task_t, std::less<>> files;
170 static SD_task_t current_job;
172 /** @brief loads a DAX file describing a DAG
174 * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
176 xbt_dynar_t SD_daxload(const char *filename)
179 FILE* in_file = fopen(filename, "r");
180 xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
181 input_buffer = dax__create_buffer(in_file, 10);
182 dax__switch_to_buffer(input_buffer);
185 result = xbt_dynar_new(sizeof(SD_task_t), nullptr);
186 SD_task_t root_task = SD_task_create_comp_seq("root", nullptr, 0);
187 /* by design the root task is always SCHEDULABLE */
188 root_task->set_state(SD_SCHEDULABLE);
190 xbt_dynar_push(result, &root_task);
191 SD_task_t end_task = SD_task_create_comp_seq("end", nullptr, 0);
193 xbt_assert(dax_lex() == 0, "Parse error in %s: %s", filename, dax__parse_err_msg());
194 dax__delete_buffer(input_buffer);
198 /* And now, post-process the files.
199 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
200 * Files not produced in the system are said to be produced by root task (top of DAG).
201 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
204 for (auto const& elm : files) {
207 if (file->has_unsolved_dependencies() == 0) {
208 for (SD_task_t const& it : file->get_successors()) {
209 newfile = SD_task_create_comm_e2e(file->get_cname(), nullptr, file->get_amount());
210 root_task->dependency_add(newfile);
211 newfile->dependency_add(it);
212 xbt_dynar_push(result, &newfile);
215 if (file->is_waited_by() == 0) {
216 for (SD_task_t const& it : file->get_dependencies()) {
217 newfile = SD_task_create_comm_e2e(file->get_cname(), nullptr, file->get_amount());
218 it->dependency_add(newfile);
219 newfile->dependency_add(end_task);
220 xbt_dynar_push(result, &newfile);
223 for (SD_task_t const& it : file->get_dependencies()) {
224 for (SD_task_t const& it2 : file->get_successors()) {
226 XBT_WARN("File %s is produced and consumed by task %s."
227 "This loop dependency will prevent the execution of the task.",
228 file->get_cname(), it->get_cname());
230 newfile = SD_task_create_comm_e2e(file->get_cname(), nullptr, file->get_amount());
231 it->dependency_add(newfile);
232 newfile->dependency_add(it2);
233 xbt_dynar_push(result, &newfile);
236 /* Free previous copy of the files */
240 /* Push end task last */
241 xbt_dynar_push(result, &end_task);
244 xbt_dynar_foreach(result, cpt, file) {
245 if (file->get_kind() == SD_TASK_COMM_E2E) {
246 uniq_transfer_task_name(file);
248 /* If some tasks do not take files as input, connect them to the root
249 * if they don't produce files, connect them to the end node.
251 if ((file != root_task) && (file != end_task)) {
252 if (file->has_unsolved_dependencies() == 0)
253 root_task->dependency_add(file);
254 if (file->is_waited_by() == 0)
255 file->dependency_add(end_task);
260 if (not acyclic_graph_detail(result)) {
261 XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.",
262 simgrid::xbt::Path(filename).get_base_name().c_str());
263 xbt_dynar_foreach(result, cpt, file)
265 xbt_dynar_free_container(&result);
272 void STag_dax__adag()
275 double version = std::stod(std::string(A_dax__adag_version));
276 xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
277 } catch (const std::invalid_argument&) {
278 throw std::invalid_argument(std::string("Parse error: ") + A_dax__adag_version + " is not a double");
285 double runtime = std::stod(std::string(A_dax__job_runtime));
287 std::string name = std::string(A_dax__job_id) + "@" + A_dax__job_name;
288 runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
289 XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
290 current_job = SD_task_create_comp_seq(name.c_str(), nullptr, runtime);
291 jobs.insert({A_dax__job_id, current_job});
292 xbt_dynar_push(result, ¤t_job);
293 } catch (const std::invalid_argument&) {
294 throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
298 void STag_dax__uses()
302 size = std::stod(std::string(A_dax__uses_size));
303 } catch (const std::invalid_argument&) {
304 throw std::invalid_argument(std::string("Parse error: ") + A_dax__uses_size + " is not a double");
306 bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
308 XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
309 auto it = files.find(A_dax__uses_file);
311 if (it == files.end()) {
312 file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
313 sd_global->initial_tasks.erase(file);
314 files[A_dax__uses_file] = file;
317 if (file->get_amount() < size || file->get_amount() > size) {
318 XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, file->get_amount(), size);
322 file->dependency_add(current_job);
324 current_job->dependency_add(file);
325 if (file->has_unsolved_dependencies() > 1) {
326 XBT_WARN("File %s created at more than one location...", file->get_cname());
331 static SD_task_t current_child;
332 void STag_dax__child()
334 auto job = jobs.find(A_dax__child_ref);
335 if (job != jobs.end()) {
336 current_child = job->second;
338 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
339 ": Asked to add dependencies to the non-existent " + A_dax__child_ref + "task");
343 void ETag_dax__child()
345 current_child = nullptr;
348 void STag_dax__parent()
350 auto job = jobs.find(A_dax__parent_ref);
351 if (job != jobs.end()) {
352 auto parent = job->second;
353 parent->dependency_add(current_child);
354 XBT_DEBUG("Control-flow dependency from %s to %s", current_child->get_cname(), parent->get_cname());
356 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
357 ": Asked to add a dependency from " + current_child->get_name() + " to " +
358 A_dax__parent_ref + ", but " + A_dax__parent_ref + " does not exist");
362 void ETag_dax__adag()
364 XBT_DEBUG("See </adag>");
369 current_job = nullptr;
370 XBT_DEBUG("See </job>");
373 void ETag_dax__parent()
375 XBT_DEBUG("See </parent>");
378 void ETag_dax__uses()
380 XBT_DEBUG("See </uses>");