1 /* Copyright (c) 2009-2017. 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.h" /* xbt_basename() */
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_daxparse, sd, "Parsing DAX files");
20 #define register /* g++ don't like register, so don't say it */
25 /* Ensure that transfer tasks have unique names even though a file is used several times */
26 void uniq_transfer_task_name(SD_task_t task)
28 SD_task_t child = *(task->successors->begin());
29 SD_task_t parent = *(task->predecessors->begin());
31 std::string new_name =
32 std::string(SD_task_get_name(parent)) + "_" + SD_task_get_name(task) + "_" + SD_task_get_name(child);
34 SD_task_set_name(task, new_name.c_str());
37 static bool children_are_marked(SD_task_t task){
38 for (SD_task_t const& it : *task->successors)
41 for (SD_task_t const& it : *task->outputs)
47 static bool parents_are_marked(SD_task_t task){
48 for (SD_task_t const& it : *task->predecessors)
51 for (SD_task_t const& it : *task->inputs)
57 bool acyclic_graph_detail(xbt_dynar_t dag){
59 bool all_marked = true;
60 SD_task_t task = nullptr;
61 std::vector<SD_task_t> current;
62 xbt_dynar_foreach(dag,count,task){
63 if(task->kind != SD_TASK_COMM_E2E){
65 if(task->successors->empty() && task->outputs->empty())
66 current.push_back(task);
69 while (not current.empty()) {
70 std::vector<SD_task_t> next;
71 for (auto const& t : current) {
74 for (SD_task_t const& input : *t->inputs) {
76 // Inputs are communication, hence they can have only one predecessor
77 SD_task_t input_pred = *(input->predecessors->begin());
78 if (children_are_marked(input_pred))
79 next.push_back(input_pred);
81 for (SD_task_t const& pred : *t->predecessors) {
82 if (children_are_marked(pred))
91 //test if all tasks are marked
92 xbt_dynar_foreach(dag,count,task){
93 if(task->kind != SD_TASK_COMM_E2E && task->marked == 0){
94 XBT_WARN("the task %s is not marked",task->name);
100 if (not all_marked) {
101 XBT_VERB("there is at least one cycle in your task graph");
102 xbt_dynar_foreach(dag,count,task){
103 if(task->kind != SD_TASK_COMM_E2E && task->predecessors->empty() && task->inputs->empty()){
105 current.push_back(task);
108 //test if something has to be done for the next iteration
109 while (not current.empty()) {
110 std::vector<SD_task_t> next;
111 //test if the current iteration is done
112 for (auto const& t : current) {
114 for (SD_task_t const& output : *t->outputs) {
116 // outputs are communication, hence they can have only one successor
117 SD_task_t output_succ = *(output->successors->begin());
118 if (parents_are_marked(output_succ))
119 next.push_back(output_succ);
121 for (SD_task_t const& succ : *t->successors) {
122 if (parents_are_marked(succ))
123 next.push_back(succ);
131 xbt_dynar_foreach(dag,count,task){
132 if(task->kind != SD_TASK_COMM_E2E && task->marked == 0){
133 XBT_WARN("the task %s is in a cycle",task->name);
141 static YY_BUFFER_STATE input_buffer;
143 static xbt_dynar_t result;
144 static std::map<std::string, SD_task_t> jobs;
145 static std::map<std::string, SD_task_t> files;
146 static SD_task_t current_job;
147 static SD_task_t root_task;
148 static SD_task_t end_task;
150 static void dax_task_free(void *task)
152 SD_task_destroy(static_cast<SD_task_t>(task));
155 /** @brief loads a DAX file describing a DAG
157 * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
159 xbt_dynar_t SD_daxload(const char *filename)
162 FILE* in_file = fopen(filename, "r");
163 xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
164 input_buffer = dax__create_buffer(in_file, 10);
165 dax__switch_to_buffer(input_buffer);
168 result = xbt_dynar_new(sizeof(SD_task_t), dax_task_free);
169 root_task = SD_task_create_comp_seq("root", nullptr, 0);
170 /* by design the root task is always SCHEDULABLE */
171 SD_task_set_state(root_task, SD_SCHEDULABLE);
173 xbt_dynar_push(result, &root_task);
174 end_task = SD_task_create_comp_seq("end", nullptr, 0);
178 xbt_die("Parse error in %s: %s", filename, dax__parse_err_msg());
179 dax__delete_buffer(input_buffer);
183 /* And now, post-process the files.
184 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
185 * Files not produced in the system are said to be produced by root task (top of DAG).
186 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
189 for (auto const& elm : files) {
192 if (file->predecessors->empty()) {
193 for (SD_task_t const& it : *file->successors) {
194 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
195 SD_task_dependency_add(nullptr, nullptr, root_task, newfile);
196 SD_task_dependency_add(nullptr, nullptr, newfile, it);
197 xbt_dynar_push(result, &newfile);
199 } else if (file->successors->empty()) {
200 for (SD_task_t const& it : *file->predecessors) {
201 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
202 SD_task_dependency_add(nullptr, nullptr, it, newfile);
203 SD_task_dependency_add(nullptr, nullptr, newfile, end_task);
204 xbt_dynar_push(result, &newfile);
207 for (SD_task_t const& it : *file->predecessors) {
208 for (SD_task_t const& it2 : *file->successors) {
210 XBT_WARN ("File %s is produced and consumed by task %s."
211 "This loop dependency will prevent the execution of the task.", file->name, it->name);
213 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
214 SD_task_dependency_add(nullptr, nullptr, it, newfile);
215 SD_task_dependency_add(nullptr, nullptr, newfile, it2);
216 xbt_dynar_push(result, &newfile);
222 /* Push end task last */
223 xbt_dynar_push(result, &end_task);
225 /* Free previous copy of the files */
226 for (auto const& elm : files)
227 SD_task_destroy(elm.second);
229 xbt_dynar_foreach(result, cpt, file) {
230 if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
231 uniq_transfer_task_name(file);
232 } else if (SD_task_get_kind(file) == SD_TASK_COMP_SEQ){
233 /* If some tasks do not take files as input, connect them to the root
234 * if they don't produce files, connect them to the end node.
236 if ((file != root_task) && (file != end_task)) {
237 if (file->inputs->empty())
238 SD_task_dependency_add(nullptr, nullptr, root_task, file);
239 if (file->outputs->empty())
240 SD_task_dependency_add(nullptr, nullptr, file, end_task);
247 if (not acyclic_graph_detail(result)) {
248 char* base = xbt_basename(filename);
249 XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.", base);
251 xbt_dynar_foreach(result, cpt, file)
252 SD_task_destroy(file);
253 xbt_dynar_free_container(&result);
260 void STag_dax__adag()
263 double version = std::stod(std::string(A_dax__adag_version));
264 xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
265 } catch (std::invalid_argument& ia) {
266 throw std::invalid_argument(std::string("Parse error: ") + A_dax__adag_version + " is not a double");
273 double runtime = std::stod(std::string(A_dax__job_runtime));
275 std::string name = std::string(A_dax__job_id) + "@" + A_dax__job_name;
276 runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
277 XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
278 current_job = SD_task_create_comp_seq(name.c_str(), nullptr, runtime);
279 jobs.insert({A_dax__job_id, current_job});
280 xbt_dynar_push(result, ¤t_job);
281 } catch (std::invalid_argument& ia) {
282 throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
286 void STag_dax__uses()
290 size = std::stod(std::string(A_dax__uses_size));
291 } catch (std::invalid_argument& ia) {
292 throw std::invalid_argument(std::string("Parse error: ") + A_dax__uses_size + " is not a double");
294 bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
296 XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
297 auto it = files.find(A_dax__uses_file);
299 if (it == files.end()) {
300 file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
301 sd_global->initial_tasks->erase(file);
302 files[A_dax__uses_file] = file;
305 if (file->amount < size || file->amount > size) {
306 XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, SD_task_get_amount(file), size);
310 SD_task_dependency_add(nullptr, nullptr, file, current_job);
312 SD_task_dependency_add(nullptr, nullptr, current_job, file);
313 if ((file->predecessors->size() + file->inputs->size()) > 1) {
314 XBT_WARN("File %s created at more than one location...", file->name);
319 static SD_task_t current_child;
320 void STag_dax__child()
322 auto job = jobs.find(A_dax__child_ref);
323 if (job != jobs.end()) {
324 current_child = job->second;
326 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
327 ": Asked to add dependencies to the non-existent " + A_dax__child_ref + "task");
331 void ETag_dax__child()
333 current_child = nullptr;
336 void STag_dax__parent()
338 auto job = jobs.find(A_dax__parent_ref);
339 if (job != jobs.end()) {
340 SD_task_t parent = job->second;
341 SD_task_dependency_add(nullptr, nullptr, parent, current_child);
342 XBT_DEBUG("Control-flow dependency from %s to %s", current_child->name, parent->name);
344 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
345 ": Asked to add a dependency from " + current_child->name + " to " + A_dax__parent_ref +
346 ", but " + A_dax__parent_ref + " does not exist");
350 void ETag_dax__adag()
352 XBT_DEBUG("See </adag>");
357 current_job = nullptr;
358 XBT_DEBUG("See </job>");
361 void ETag_dax__parent()
363 XBT_DEBUG("See </parent>");
366 void ETag_dax__uses()
368 XBT_DEBUG("See </uses>");