1 /* Copyright (c) 2009-2020. 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/simdag.h"
9 #include "xbt/file.hpp"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_daxparse, sd, "Parsing DAX files");
21 /* Ensure that transfer tasks have unique names even though a file is used several times */
22 void uniq_transfer_task_name(SD_task_t task)
24 const_SD_task_t child = *(task->successors->begin());
25 const_SD_task_t parent = *(task->predecessors->begin());
27 std::string new_name =
28 std::string(SD_task_get_name(parent)) + "_" + SD_task_get_name(task) + "_" + SD_task_get_name(child);
30 SD_task_set_name(task, new_name.c_str());
33 static bool children_are_marked(const_SD_task_t task)
35 return std::none_of(task->successors->begin(), task->successors->end(),
36 [](const SD_task_t& elm) { return not elm->marked; }) &&
37 std::none_of(task->outputs->begin(), task->outputs->end(),
38 [](const SD_task_t& elm) { return not elm->marked; });
41 static bool parents_are_marked(const_SD_task_t task)
43 return std::none_of(task->predecessors->begin(), task->predecessors->end(),
44 [](const SD_task_t& elm) { return not elm->marked; }) &&
45 std::none_of(task->inputs->begin(), task->inputs->end(), [](const SD_task_t& elm) { return not elm->marked; });
48 bool acyclic_graph_detail(const_xbt_dynar_t dag)
51 bool all_marked = true;
52 SD_task_t task = nullptr;
53 std::vector<SD_task_t> current;
54 xbt_dynar_foreach (dag, count, task)
55 if (task->kind != SD_TASK_COMM_E2E && task->successors->empty() && task->outputs->empty())
56 current.push_back(task);
58 while (not current.empty()) {
59 std::vector<SD_task_t> next;
60 for (auto const& t : current) {
63 for (SD_task_t const& input : *t->inputs) {
65 // Inputs are communication, hence they can have only one predecessor
66 SD_task_t input_pred = *(input->predecessors->begin());
67 if (children_are_marked(input_pred))
68 next.push_back(input_pred);
70 for (SD_task_t const& pred : *t->predecessors) {
71 if (children_are_marked(pred))
80 //test if all tasks are marked
81 xbt_dynar_foreach(dag,count,task){
82 if (task->kind != SD_TASK_COMM_E2E && not task->marked) {
83 XBT_WARN("the task %s is not marked",task->name);
90 XBT_VERB("there is at least one cycle in your task graph");
91 xbt_dynar_foreach(dag,count,task){
92 if(task->kind != SD_TASK_COMM_E2E && task->predecessors->empty() && task->inputs->empty()){
94 current.push_back(task);
97 //test if something has to be done for the next iteration
98 while (not current.empty()) {
99 std::vector<SD_task_t> next;
100 //test if the current iteration is done
101 for (auto const& t : current) {
103 for (SD_task_t const& output : *t->outputs) {
104 output->marked = true;
105 // outputs are communication, hence they can have only one successor
106 SD_task_t output_succ = *(output->successors->begin());
107 if (parents_are_marked(output_succ))
108 next.push_back(output_succ);
110 for (SD_task_t const& succ : *t->successors) {
111 if (parents_are_marked(succ))
112 next.push_back(succ);
120 xbt_dynar_foreach(dag,count,task){
121 if (task->kind != SD_TASK_COMM_E2E && not task->marked) {
122 XBT_WARN("the task %s is in a cycle",task->name);
130 static YY_BUFFER_STATE input_buffer;
132 static xbt_dynar_t result;
133 static std::map<std::string, SD_task_t> jobs;
134 static std::map<std::string, SD_task_t> files;
135 static SD_task_t current_job;
137 /** @brief loads a DAX file describing a DAG
139 * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
141 xbt_dynar_t SD_daxload(const char *filename)
144 FILE* in_file = fopen(filename, "r");
145 xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
146 input_buffer = dax__create_buffer(in_file, 10);
147 dax__switch_to_buffer(input_buffer);
150 result = xbt_dynar_new(sizeof(SD_task_t), nullptr);
151 SD_task_t root_task = SD_task_create_comp_seq("root", nullptr, 0);
152 /* by design the root task is always SCHEDULABLE */
153 SD_task_set_state(root_task, SD_SCHEDULABLE);
155 xbt_dynar_push(result, &root_task);
156 SD_task_t end_task = SD_task_create_comp_seq("end", nullptr, 0);
160 xbt_die("Parse error in %s: %s", filename, dax__parse_err_msg());
161 dax__delete_buffer(input_buffer);
165 /* And now, post-process the files.
166 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
167 * Files not produced in the system are said to be produced by root task (top of DAG).
168 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
171 for (auto const& elm : files) {
174 if (file->predecessors->empty()) {
175 for (SD_task_t const& it : *file->successors) {
176 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
177 SD_task_dependency_add(root_task, newfile);
178 SD_task_dependency_add(newfile, it);
179 xbt_dynar_push(result, &newfile);
182 if (file->successors->empty()) {
183 for (SD_task_t const& it : *file->predecessors) {
184 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
185 SD_task_dependency_add(it, newfile);
186 SD_task_dependency_add(newfile, end_task);
187 xbt_dynar_push(result, &newfile);
190 for (SD_task_t const& it : *file->predecessors) {
191 for (SD_task_t const& it2 : *file->successors) {
193 XBT_WARN("File %s is produced and consumed by task %s."
194 "This loop dependency will prevent the execution of the task.",
195 file->name, it->name);
197 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
198 SD_task_dependency_add(it, newfile);
199 SD_task_dependency_add(newfile, it2);
200 xbt_dynar_push(result, &newfile);
203 /* Free previous copy of the files */
204 SD_task_destroy(file);
207 /* Push end task last */
208 xbt_dynar_push(result, &end_task);
211 xbt_dynar_foreach(result, cpt, file) {
212 if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
213 uniq_transfer_task_name(file);
215 /* If some tasks do not take files as input, connect them to the root
216 * if they don't produce files, connect them to the end node.
218 if ((file != root_task) && (file != end_task)) {
219 if (file->inputs->empty())
220 SD_task_dependency_add(root_task, file);
221 if (file->outputs->empty())
222 SD_task_dependency_add(file, end_task);
227 if (not acyclic_graph_detail(result)) {
228 XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.",
229 simgrid::xbt::Path(filename).get_base_name().c_str());
230 xbt_dynar_foreach(result, cpt, file)
231 SD_task_destroy(file);
232 xbt_dynar_free_container(&result);
239 void STag_dax__adag()
242 double version = std::stod(std::string(A_dax__adag_version));
243 xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
244 } catch (const std::invalid_argument&) {
245 throw std::invalid_argument(std::string("Parse error: ") + A_dax__adag_version + " is not a double");
252 double runtime = std::stod(std::string(A_dax__job_runtime));
254 std::string name = std::string(A_dax__job_id) + "@" + A_dax__job_name;
255 runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
256 XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
257 current_job = SD_task_create_comp_seq(name.c_str(), nullptr, runtime);
258 jobs.insert({A_dax__job_id, current_job});
259 xbt_dynar_push(result, ¤t_job);
260 } catch (const std::invalid_argument&) {
261 throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
265 void STag_dax__uses()
269 size = std::stod(std::string(A_dax__uses_size));
270 } catch (const std::invalid_argument&) {
271 throw std::invalid_argument(std::string("Parse error: ") + A_dax__uses_size + " is not a double");
273 bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
275 XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
276 auto it = files.find(A_dax__uses_file);
278 if (it == files.end()) {
279 file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
280 sd_global->initial_tasks.erase(file);
281 files[A_dax__uses_file] = file;
284 if (file->amount < size || file->amount > size) {
285 XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, SD_task_get_amount(file), size);
289 SD_task_dependency_add(file, current_job);
291 SD_task_dependency_add(current_job, file);
292 if ((file->predecessors->size() + file->inputs->size()) > 1) {
293 XBT_WARN("File %s created at more than one location...", file->name);
298 static SD_task_t current_child;
299 void STag_dax__child()
301 auto job = jobs.find(A_dax__child_ref);
302 if (job != jobs.end()) {
303 current_child = job->second;
305 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
306 ": Asked to add dependencies to the non-existent " + A_dax__child_ref + "task");
310 void ETag_dax__child()
312 current_child = nullptr;
315 void STag_dax__parent()
317 auto job = jobs.find(A_dax__parent_ref);
318 if (job != jobs.end()) {
319 SD_task_t parent = job->second;
320 SD_task_dependency_add(parent, current_child);
321 XBT_DEBUG("Control-flow dependency from %s to %s", current_child->name, parent->name);
323 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
324 ": Asked to add a dependency from " + current_child->name + " to " + A_dax__parent_ref +
325 ", but " + A_dax__parent_ref + " does not exist");
329 void ETag_dax__adag()
331 XBT_DEBUG("See </adag>");
336 current_job = nullptr;
337 XBT_DEBUG("See </job>");
340 void ETag_dax__parent()
342 XBT_DEBUG("See </parent>");
345 void ETag_dax__uses()
347 XBT_DEBUG("See </uses>");