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"
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 SD_task_t child = *(task->successors->begin());
25 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(SD_task_t task){
34 return std::none_of(task->successors->begin(), task->successors->end(),
35 [](const SD_task_t& elm) { return not elm->marked; }) &&
36 std::none_of(task->outputs->begin(), task->outputs->end(),
37 [](const SD_task_t& elm) { return not elm->marked; });
40 static bool parents_are_marked(SD_task_t task){
41 return std::none_of(task->predecessors->begin(), task->predecessors->end(),
42 [](const SD_task_t& elm) { return not elm->marked; }) &&
43 std::none_of(task->inputs->begin(), task->inputs->end(), [](const SD_task_t& elm) { return not elm->marked; });
46 bool acyclic_graph_detail(xbt_dynar_t dag){
48 bool all_marked = true;
49 SD_task_t task = nullptr;
50 std::vector<SD_task_t> current;
51 xbt_dynar_foreach (dag, count, task)
52 if (task->kind != SD_TASK_COMM_E2E && task->successors->empty() && task->outputs->empty())
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 (SD_task_t const& input : *t->inputs) {
62 // Inputs are communication, hence they can have only one predecessor
63 SD_task_t input_pred = *(input->predecessors->begin());
64 if (children_are_marked(input_pred))
65 next.push_back(input_pred);
67 for (SD_task_t const& pred : *t->predecessors) {
68 if (children_are_marked(pred))
77 //test if all tasks are marked
78 xbt_dynar_foreach(dag,count,task){
79 if (task->kind != SD_TASK_COMM_E2E && not task->marked) {
80 XBT_WARN("the task %s is not marked",task->name);
87 XBT_VERB("there is at least one cycle in your task graph");
88 xbt_dynar_foreach(dag,count,task){
89 if(task->kind != SD_TASK_COMM_E2E && task->predecessors->empty() && task->inputs->empty()){
91 current.push_back(task);
94 //test if something has to be done for the next iteration
95 while (not current.empty()) {
96 std::vector<SD_task_t> next;
97 //test if the current iteration is done
98 for (auto const& t : current) {
100 for (SD_task_t const& output : *t->outputs) {
101 output->marked = true;
102 // outputs are communication, hence they can have only one successor
103 SD_task_t output_succ = *(output->successors->begin());
104 if (parents_are_marked(output_succ))
105 next.push_back(output_succ);
107 for (SD_task_t const& succ : *t->successors) {
108 if (parents_are_marked(succ))
109 next.push_back(succ);
117 xbt_dynar_foreach(dag,count,task){
118 if (task->kind != SD_TASK_COMM_E2E && not task->marked) {
119 XBT_WARN("the task %s is in a cycle",task->name);
127 static YY_BUFFER_STATE input_buffer;
129 static xbt_dynar_t result;
130 static std::map<std::string, SD_task_t> jobs;
131 static std::map<std::string, SD_task_t> files;
132 static SD_task_t current_job;
134 /** @brief loads a DAX file describing a DAG
136 * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
138 xbt_dynar_t SD_daxload(const char *filename)
141 FILE* in_file = fopen(filename, "r");
142 xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
143 input_buffer = dax__create_buffer(in_file, 10);
144 dax__switch_to_buffer(input_buffer);
147 result = xbt_dynar_new(sizeof(SD_task_t), nullptr);
148 SD_task_t root_task = SD_task_create_comp_seq("root", nullptr, 0);
149 /* by design the root task is always SCHEDULABLE */
150 SD_task_set_state(root_task, SD_SCHEDULABLE);
152 xbt_dynar_push(result, &root_task);
153 SD_task_t end_task = SD_task_create_comp_seq("end", nullptr, 0);
157 xbt_die("Parse error in %s: %s", filename, dax__parse_err_msg());
158 dax__delete_buffer(input_buffer);
162 /* And now, post-process the files.
163 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
164 * Files not produced in the system are said to be produced by root task (top of DAG).
165 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
168 for (auto const& elm : files) {
171 if (file->predecessors->empty()) {
172 for (SD_task_t const& it : *file->successors) {
173 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
174 SD_task_dependency_add(root_task, newfile);
175 SD_task_dependency_add(newfile, it);
176 xbt_dynar_push(result, &newfile);
179 if (file->successors->empty()) {
180 for (SD_task_t const& it : *file->predecessors) {
181 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
182 SD_task_dependency_add(it, newfile);
183 SD_task_dependency_add(newfile, end_task);
184 xbt_dynar_push(result, &newfile);
187 for (SD_task_t const& it : *file->predecessors) {
188 for (SD_task_t const& it2 : *file->successors) {
190 XBT_WARN("File %s is produced and consumed by task %s."
191 "This loop dependency will prevent the execution of the task.",
192 file->name, it->name);
194 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
195 SD_task_dependency_add(it, newfile);
196 SD_task_dependency_add(newfile, it2);
197 xbt_dynar_push(result, &newfile);
200 /* Free previous copy of the files */
201 SD_task_destroy(file);
204 /* Push end task last */
205 xbt_dynar_push(result, &end_task);
208 xbt_dynar_foreach(result, cpt, file) {
209 if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
210 uniq_transfer_task_name(file);
212 /* If some tasks do not take files as input, connect them to the root
213 * if they don't produce files, connect them to the end node.
215 if ((file != root_task) && (file != end_task)) {
216 if (file->inputs->empty())
217 SD_task_dependency_add(root_task, file);
218 if (file->outputs->empty())
219 SD_task_dependency_add(file, end_task);
224 if (not acyclic_graph_detail(result)) {
225 XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.",
226 simgrid::xbt::Path(filename).get_base_name().c_str());
227 xbt_dynar_foreach(result, cpt, file)
228 SD_task_destroy(file);
229 xbt_dynar_free_container(&result);
236 void STag_dax__adag()
239 double version = std::stod(std::string(A_dax__adag_version));
240 xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
241 } catch (const std::invalid_argument&) {
242 throw std::invalid_argument(std::string("Parse error: ") + A_dax__adag_version + " is not a double");
249 double runtime = std::stod(std::string(A_dax__job_runtime));
251 std::string name = std::string(A_dax__job_id) + "@" + A_dax__job_name;
252 runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
253 XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
254 current_job = SD_task_create_comp_seq(name.c_str(), nullptr, runtime);
255 jobs.insert({A_dax__job_id, current_job});
256 xbt_dynar_push(result, ¤t_job);
257 } catch (const std::invalid_argument&) {
258 throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
262 void STag_dax__uses()
266 size = std::stod(std::string(A_dax__uses_size));
267 } catch (const std::invalid_argument&) {
268 throw std::invalid_argument(std::string("Parse error: ") + A_dax__uses_size + " is not a double");
270 bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
272 XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
273 auto it = files.find(A_dax__uses_file);
275 if (it == files.end()) {
276 file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
277 sd_global->initial_tasks.erase(file);
278 files[A_dax__uses_file] = file;
281 if (file->amount < size || file->amount > size) {
282 XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, SD_task_get_amount(file), size);
286 SD_task_dependency_add(file, current_job);
288 SD_task_dependency_add(current_job, file);
289 if ((file->predecessors->size() + file->inputs->size()) > 1) {
290 XBT_WARN("File %s created at more than one location...", file->name);
295 static SD_task_t current_child;
296 void STag_dax__child()
298 auto job = jobs.find(A_dax__child_ref);
299 if (job != jobs.end()) {
300 current_child = job->second;
302 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
303 ": Asked to add dependencies to the non-existent " + A_dax__child_ref + "task");
307 void ETag_dax__child()
309 current_child = nullptr;
312 void STag_dax__parent()
314 auto job = jobs.find(A_dax__parent_ref);
315 if (job != jobs.end()) {
316 SD_task_t parent = job->second;
317 SD_task_dependency_add(parent, current_child);
318 XBT_DEBUG("Control-flow dependency from %s to %s", current_child->name, parent->name);
320 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
321 ": Asked to add a dependency from " + current_child->name + " to " + A_dax__parent_ref +
322 ", but " + A_dax__parent_ref + " does not exist");
326 void ETag_dax__adag()
328 XBT_DEBUG("See </adag>");
333 current_job = nullptr;
334 XBT_DEBUG("See </job>");
337 void ETag_dax__parent()
339 XBT_DEBUG("See </parent>");
342 void ETag_dax__uses()
344 XBT_DEBUG("See </uses>");