1 /* Copyright (c) 2009-2019. 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"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_daxparse, sd, "Parsing DAX files");
20 /* Ensure that transfer tasks have unique names even though a file is used several times */
21 void uniq_transfer_task_name(SD_task_t task)
23 SD_task_t child = *(task->successors->begin());
24 SD_task_t parent = *(task->predecessors->begin());
26 std::string new_name =
27 std::string(SD_task_get_name(parent)) + "_" + SD_task_get_name(task) + "_" + SD_task_get_name(child);
29 SD_task_set_name(task, new_name.c_str());
32 static bool children_are_marked(SD_task_t task){
33 return std::none_of(task->successors->begin(), task->successors->end(),
34 [](const SD_task_t& elm) { return not elm->marked; }) &&
35 std::none_of(task->outputs->begin(), task->outputs->end(),
36 [](const SD_task_t& elm) { return not elm->marked; });
39 static bool parents_are_marked(SD_task_t task){
40 return std::none_of(task->predecessors->begin(), task->predecessors->end(),
41 [](const SD_task_t& elm) { return not elm->marked; }) &&
42 std::none_of(task->inputs->begin(), task->inputs->end(), [](const SD_task_t& elm) { return not elm->marked; });
45 bool acyclic_graph_detail(xbt_dynar_t dag){
47 bool all_marked = true;
48 SD_task_t task = nullptr;
49 std::vector<SD_task_t> current;
50 xbt_dynar_foreach (dag, count, task)
51 if (task->kind != SD_TASK_COMM_E2E && task->successors->empty() && task->outputs->empty())
52 current.push_back(task);
54 while (not current.empty()) {
55 std::vector<SD_task_t> next;
56 for (auto const& t : current) {
59 for (SD_task_t const& input : *t->inputs) {
61 // Inputs are communication, hence they can have only one predecessor
62 SD_task_t input_pred = *(input->predecessors->begin());
63 if (children_are_marked(input_pred))
64 next.push_back(input_pred);
66 for (SD_task_t const& pred : *t->predecessors) {
67 if (children_are_marked(pred))
76 //test if all tasks are marked
77 xbt_dynar_foreach(dag,count,task){
78 if (task->kind != SD_TASK_COMM_E2E && not task->marked) {
79 XBT_WARN("the task %s is not marked",task->name);
86 XBT_VERB("there is at least one cycle in your task graph");
87 xbt_dynar_foreach(dag,count,task){
88 if(task->kind != SD_TASK_COMM_E2E && task->predecessors->empty() && task->inputs->empty()){
90 current.push_back(task);
93 //test if something has to be done for the next iteration
94 while (not current.empty()) {
95 std::vector<SD_task_t> next;
96 //test if the current iteration is done
97 for (auto const& t : current) {
99 for (SD_task_t const& output : *t->outputs) {
100 output->marked = true;
101 // outputs are communication, hence they can have only one successor
102 SD_task_t output_succ = *(output->successors->begin());
103 if (parents_are_marked(output_succ))
104 next.push_back(output_succ);
106 for (SD_task_t const& succ : *t->successors) {
107 if (parents_are_marked(succ))
108 next.push_back(succ);
116 xbt_dynar_foreach(dag,count,task){
117 if (task->kind != SD_TASK_COMM_E2E && not task->marked) {
118 XBT_WARN("the task %s is in a cycle",task->name);
126 static YY_BUFFER_STATE input_buffer;
128 static xbt_dynar_t result;
129 static std::map<std::string, SD_task_t> jobs;
130 static std::map<std::string, SD_task_t> files;
131 static SD_task_t current_job;
133 /** @brief loads a DAX file describing a DAG
135 * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
137 xbt_dynar_t SD_daxload(const char *filename)
140 FILE* in_file = fopen(filename, "r");
141 xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
142 input_buffer = dax__create_buffer(in_file, 10);
143 dax__switch_to_buffer(input_buffer);
146 result = xbt_dynar_new(sizeof(SD_task_t), nullptr);
147 SD_task_t root_task = SD_task_create_comp_seq("root", nullptr, 0);
148 /* by design the root task is always SCHEDULABLE */
149 SD_task_set_state(root_task, SD_SCHEDULABLE);
151 xbt_dynar_push(result, &root_task);
152 SD_task_t end_task = SD_task_create_comp_seq("end", nullptr, 0);
156 xbt_die("Parse error in %s: %s", filename, dax__parse_err_msg());
157 dax__delete_buffer(input_buffer);
161 /* And now, post-process the files.
162 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
163 * Files not produced in the system are said to be produced by root task (top of DAG).
164 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
167 for (auto const& elm : files) {
170 if (file->predecessors->empty()) {
171 for (SD_task_t const& it : *file->successors) {
172 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
173 SD_task_dependency_add(root_task, newfile);
174 SD_task_dependency_add(newfile, it);
175 xbt_dynar_push(result, &newfile);
178 if (file->successors->empty()) {
179 for (SD_task_t const& it : *file->predecessors) {
180 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
181 SD_task_dependency_add(it, newfile);
182 SD_task_dependency_add(newfile, end_task);
183 xbt_dynar_push(result, &newfile);
186 for (SD_task_t const& it : *file->predecessors) {
187 for (SD_task_t const& it2 : *file->successors) {
189 XBT_WARN("File %s is produced and consumed by task %s."
190 "This loop dependency will prevent the execution of the task.",
191 file->name, it->name);
193 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
194 SD_task_dependency_add(it, newfile);
195 SD_task_dependency_add(newfile, it2);
196 xbt_dynar_push(result, &newfile);
199 /* Free previous copy of the files */
200 SD_task_destroy(file);
203 /* Push end task last */
204 xbt_dynar_push(result, &end_task);
207 xbt_dynar_foreach(result, cpt, file) {
208 if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
209 uniq_transfer_task_name(file);
211 /* If some tasks do not take files as input, connect them to the root
212 * if they don't produce files, connect them to the end node.
214 if ((file != root_task) && (file != end_task)) {
215 if (file->inputs->empty())
216 SD_task_dependency_add(root_task, file);
217 if (file->outputs->empty())
218 SD_task_dependency_add(file, end_task);
223 if (not acyclic_graph_detail(result)) {
224 XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.",
225 simgrid::xbt::Path(filename).get_base_name().c_str());
226 xbt_dynar_foreach(result, cpt, file)
227 SD_task_destroy(file);
228 xbt_dynar_free_container(&result);
235 void STag_dax__adag()
238 double version = std::stod(std::string(A_dax__adag_version));
239 xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
240 } catch (std::invalid_argument& ia) {
241 throw std::invalid_argument(std::string("Parse error: ") + A_dax__adag_version + " is not a double");
248 double runtime = std::stod(std::string(A_dax__job_runtime));
250 std::string name = std::string(A_dax__job_id) + "@" + A_dax__job_name;
251 runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
252 XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
253 current_job = SD_task_create_comp_seq(name.c_str(), nullptr, runtime);
254 jobs.insert({A_dax__job_id, current_job});
255 xbt_dynar_push(result, ¤t_job);
256 } catch (std::invalid_argument& ia) {
257 throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
261 void STag_dax__uses()
265 size = std::stod(std::string(A_dax__uses_size));
266 } catch (std::invalid_argument& ia) {
267 throw std::invalid_argument(std::string("Parse error: ") + A_dax__uses_size + " is not a double");
269 bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
271 XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
272 auto it = files.find(A_dax__uses_file);
274 if (it == files.end()) {
275 file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
276 sd_global->initial_tasks.erase(file);
277 files[A_dax__uses_file] = file;
280 if (file->amount < size || file->amount > size) {
281 XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, SD_task_get_amount(file), size);
285 SD_task_dependency_add(file, current_job);
287 SD_task_dependency_add(current_job, file);
288 if ((file->predecessors->size() + file->inputs->size()) > 1) {
289 XBT_WARN("File %s created at more than one location...", file->name);
294 static SD_task_t current_child;
295 void STag_dax__child()
297 auto job = jobs.find(A_dax__child_ref);
298 if (job != jobs.end()) {
299 current_child = job->second;
301 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
302 ": Asked to add dependencies to the non-existent " + A_dax__child_ref + "task");
306 void ETag_dax__child()
308 current_child = nullptr;
311 void STag_dax__parent()
313 auto job = jobs.find(A_dax__parent_ref);
314 if (job != jobs.end()) {
315 SD_task_t parent = job->second;
316 SD_task_dependency_add(parent, current_child);
317 XBT_DEBUG("Control-flow dependency from %s to %s", current_child->name, parent->name);
319 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
320 ": Asked to add a dependency from " + current_child->name + " to " + A_dax__parent_ref +
321 ", but " + A_dax__parent_ref + " does not exist");
325 void ETag_dax__adag()
327 XBT_DEBUG("See </adag>");
332 current_job = nullptr;
333 XBT_DEBUG("See </job>");
336 void ETag_dax__parent()
338 XBT_DEBUG("See </parent>");
341 void ETag_dax__uses()
343 XBT_DEBUG("See </uses>");