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() */
13 #include <unordered_map>
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 it : *task->successors)
41 for (SD_task_t it : *task->outputs)
47 static bool parents_are_marked(SD_task_t task){
48 for (SD_task_t it : *task->predecessors)
51 for (SD_task_t 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 t: current){
74 for (SD_task_t 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 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 t: current){
114 for (SD_task_t 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 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::unordered_map<std::string, SD_task_t> jobs;
145 static xbt_dict_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)
161 xbt_dict_cursor_t cursor;
164 FILE* in_file = fopen(filename, "r");
165 xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
166 input_buffer = dax__create_buffer(in_file, 10);
167 dax__switch_to_buffer(input_buffer);
170 result = xbt_dynar_new(sizeof(SD_task_t), dax_task_free);
171 files = xbt_dict_new_homogeneous(&dax_task_free);
172 root_task = SD_task_create_comp_seq("root", nullptr, 0);
173 /* by design the root task is always SCHEDULABLE */
174 SD_task_set_state(root_task, SD_SCHEDULABLE);
176 xbt_dynar_push(result, &root_task);
177 end_task = SD_task_create_comp_seq("end", nullptr, 0);
181 xbt_die("Parse error in %s: %s", filename, dax__parse_err_msg());
182 dax__delete_buffer(input_buffer);
186 /* And now, post-process the files.
187 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
188 * Files not produced in the system are said to be produced by root task (top of DAG).
189 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
192 xbt_dict_foreach(files, cursor, name, file) {
194 if (file->predecessors->empty()) {
195 for (SD_task_t it : *file->successors) {
196 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
197 SD_task_dependency_add(nullptr, nullptr, root_task, newfile);
198 SD_task_dependency_add(nullptr, nullptr, newfile, it);
199 xbt_dynar_push(result, &newfile);
201 } else if (file->successors->empty()) {
202 for (SD_task_t it : *file->predecessors){
203 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
204 SD_task_dependency_add(nullptr, nullptr, it, newfile);
205 SD_task_dependency_add(nullptr, nullptr, newfile, end_task);
206 xbt_dynar_push(result, &newfile);
209 for (SD_task_t it : *file->predecessors) {
210 for (SD_task_t it2 : *file->successors) {
212 XBT_WARN ("File %s is produced and consumed by task %s."
213 "This loop dependency will prevent the execution of the task.", file->name, it->name);
215 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
216 SD_task_dependency_add(nullptr, nullptr, it, newfile);
217 SD_task_dependency_add(nullptr, nullptr, newfile, it2);
218 xbt_dynar_push(result, &newfile);
224 /* Push end task last */
225 xbt_dynar_push(result, &end_task);
227 /* Free previous copy of the files */
228 xbt_dict_free(&files);
230 xbt_dynar_foreach(result, cpt, file) {
231 if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
232 uniq_transfer_task_name(file);
233 } else if (SD_task_get_kind(file) == SD_TASK_COMP_SEQ){
234 /* If some tasks do not take files as input, connect them to the root
235 * if they don't produce files, connect them to the end node.
237 if ((file != root_task) && (file != end_task)) {
238 if (file->inputs->empty())
239 SD_task_dependency_add(nullptr, nullptr, root_task, file);
240 if (file->outputs->empty())
241 SD_task_dependency_add(nullptr, nullptr, file, end_task);
248 if (not acyclic_graph_detail(result)) {
249 char* base = xbt_basename(filename);
250 XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.", base);
252 xbt_dynar_foreach(result, cpt, file)
253 SD_task_destroy(file);
254 xbt_dynar_free_container(&result);
261 void STag_dax__adag()
264 double version = std::stod(std::string(A_dax__adag_version));
265 xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
266 } catch (std::invalid_argument& ia) {
267 throw std::invalid_argument(std::string("Parse error: ") + A_dax__adag_version + " is not a double");
274 double runtime = std::stod(std::string(A_dax__job_runtime));
276 std::string name = std::string(A_dax__job_id) + "@" + A_dax__job_name;
277 runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
278 XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
279 current_job = SD_task_create_comp_seq(name.c_str(), nullptr, runtime);
280 jobs.insert({A_dax__job_id, current_job});
281 xbt_dynar_push(result, ¤t_job);
282 } catch (std::invalid_argument& ia) {
283 throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
287 void STag_dax__uses()
291 size = std::stod(std::string(A_dax__uses_size));
292 } catch (std::invalid_argument& ia) {
293 throw std::invalid_argument(std::string("Parse error: ") + A_dax__uses_size + " is not a double");
295 bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
297 XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
298 SD_task_t file = static_cast<SD_task_t>(xbt_dict_get_or_null(files, A_dax__uses_file));
299 if (file == nullptr) {
300 file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
301 sd_global->initial_tasks->erase(file);
302 xbt_dict_set(files, A_dax__uses_file, file, nullptr);
304 if (file->amount < size || file->amount > size) {
305 XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, SD_task_get_amount(file), size);
309 SD_task_dependency_add(nullptr, nullptr, file, current_job);
311 SD_task_dependency_add(nullptr, nullptr, current_job, file);
312 if ((file->predecessors->size() + file->inputs->size()) > 1) {
313 XBT_WARN("File %s created at more than one location...", file->name);
318 static SD_task_t current_child;
319 void STag_dax__child()
321 auto job = jobs.find(A_dax__child_ref);
322 if (job != jobs.end()) {
323 current_child = job->second;
325 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
326 ": Asked to add dependencies to the non-existent " + A_dax__child_ref + "task");
330 void ETag_dax__child()
332 current_child = nullptr;
335 void STag_dax__parent()
337 auto job = jobs.find(A_dax__parent_ref);
338 if (job != jobs.end()) {
339 SD_task_t parent = job->second;
340 SD_task_dependency_add(nullptr, nullptr, parent, current_child);
341 XBT_DEBUG("Control-flow dependency from %s to %s", current_child->name, parent->name);
343 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
344 ": Asked to add a dependency from " + current_child->name + " to " + A_dax__parent_ref +
345 ", but " + A_dax__parent_ref + " does not exist");
349 void ETag_dax__adag()
351 XBT_DEBUG("See </adag>");
356 current_job = nullptr;
357 XBT_DEBUG("See </job>");
360 void ETag_dax__parent()
362 XBT_DEBUG("See </parent>");
365 void ETag_dax__uses()
367 XBT_DEBUG("See </uses>");