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() */
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_daxparse, sd, "Parsing DAX files");
19 # define register /* g++ don't like register, so don't say it */
24 /* Ensure that transfer tasks have unique names even though a file is used several times */
25 void uniq_transfer_task_name(SD_task_t task)
27 SD_task_t child = *(task->successors->begin());
28 SD_task_t parent = *(task->predecessors->begin());
30 std::string new_name =
31 std::string(SD_task_get_name(parent)) + "_" + SD_task_get_name(task) + "_" + SD_task_get_name(child);
33 SD_task_set_name(task, new_name.c_str());
36 static bool children_are_marked(SD_task_t task){
37 for (SD_task_t const& it : *task->successors)
40 for (SD_task_t const& it : *task->outputs)
46 static bool parents_are_marked(SD_task_t task){
47 for (SD_task_t const& it : *task->predecessors)
50 for (SD_task_t const& it : *task->inputs)
56 bool acyclic_graph_detail(xbt_dynar_t dag){
58 bool all_marked = true;
59 SD_task_t task = nullptr;
60 std::vector<SD_task_t> current;
61 xbt_dynar_foreach(dag,count,task){
62 if(task->kind != SD_TASK_COMM_E2E){
64 if(task->successors->empty() && task->outputs->empty())
65 current.push_back(task);
68 while (not current.empty()) {
69 std::vector<SD_task_t> next;
70 for (auto const& t : current) {
73 for (SD_task_t const& input : *t->inputs) {
75 // Inputs are communication, hence they can have only one predecessor
76 SD_task_t input_pred = *(input->predecessors->begin());
77 if (children_are_marked(input_pred))
78 next.push_back(input_pred);
80 for (SD_task_t const& pred : *t->predecessors) {
81 if (children_are_marked(pred))
90 //test if all tasks are marked
91 xbt_dynar_foreach(dag,count,task){
92 if(task->kind != SD_TASK_COMM_E2E && task->marked == 0){
93 XBT_WARN("the task %s is not marked",task->name);
100 XBT_VERB("there is at least one cycle in your task graph");
101 xbt_dynar_foreach(dag,count,task){
102 if(task->kind != SD_TASK_COMM_E2E && task->predecessors->empty() && task->inputs->empty()){
104 current.push_back(task);
107 //test if something has to be done for the next iteration
108 while (not current.empty()) {
109 std::vector<SD_task_t> next;
110 //test if the current iteration is done
111 for (auto const& t : current) {
113 for (SD_task_t const& output : *t->outputs) {
115 // outputs are communication, hence they can have only one successor
116 SD_task_t output_succ = *(output->successors->begin());
117 if (parents_are_marked(output_succ))
118 next.push_back(output_succ);
120 for (SD_task_t const& succ : *t->successors) {
121 if (parents_are_marked(succ))
122 next.push_back(succ);
130 xbt_dynar_foreach(dag,count,task){
131 if(task->kind != SD_TASK_COMM_E2E && task->marked == 0){
132 XBT_WARN("the task %s is in a cycle",task->name);
140 static YY_BUFFER_STATE input_buffer;
142 static xbt_dynar_t result;
143 static std::map<std::string, SD_task_t> jobs;
144 static std::map<std::string, SD_task_t> files;
145 static SD_task_t current_job;
146 static SD_task_t root_task;
147 static SD_task_t end_task;
149 static void dax_task_free(void *task)
151 SD_task_destroy(static_cast<SD_task_t>(task));
154 /** @brief loads a DAX file describing a DAG
156 * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
158 xbt_dynar_t SD_daxload(const char *filename)
161 FILE* in_file = fopen(filename, "r");
162 xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
163 input_buffer = dax__create_buffer(in_file, 10);
164 dax__switch_to_buffer(input_buffer);
167 result = xbt_dynar_new(sizeof(SD_task_t), dax_task_free);
168 root_task = SD_task_create_comp_seq("root", nullptr, 0);
169 /* by design the root task is always SCHEDULABLE */
170 SD_task_set_state(root_task, SD_SCHEDULABLE);
172 xbt_dynar_push(result, &root_task);
173 end_task = SD_task_create_comp_seq("end", nullptr, 0);
177 xbt_die("Parse error in %s: %s", filename, dax__parse_err_msg());
178 dax__delete_buffer(input_buffer);
182 /* And now, post-process the files.
183 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
184 * Files not produced in the system are said to be produced by root task (top of DAG).
185 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
188 for (auto const& elm : files) {
191 if (file->predecessors->empty()) {
192 for (SD_task_t const& it : *file->successors) {
193 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
194 SD_task_dependency_add(nullptr, nullptr, root_task, newfile);
195 SD_task_dependency_add(nullptr, nullptr, newfile, it);
196 xbt_dynar_push(result, &newfile);
198 } else if (file->successors->empty()) {
199 for (SD_task_t const& it : *file->predecessors) {
200 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
201 SD_task_dependency_add(nullptr, nullptr, it, newfile);
202 SD_task_dependency_add(nullptr, nullptr, newfile, end_task);
203 xbt_dynar_push(result, &newfile);
206 for (SD_task_t const& it : *file->predecessors) {
207 for (SD_task_t const& it2 : *file->successors) {
209 XBT_WARN ("File %s is produced and consumed by task %s."
210 "This loop dependency will prevent the execution of the task.", file->name, it->name);
212 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
213 SD_task_dependency_add(nullptr, nullptr, it, newfile);
214 SD_task_dependency_add(nullptr, nullptr, newfile, it2);
215 xbt_dynar_push(result, &newfile);
221 /* Push end task last */
222 xbt_dynar_push(result, &end_task);
224 /* Free previous copy of the files */
225 for (auto const& elm : files)
226 SD_task_destroy(elm.second);
228 xbt_dynar_foreach(result, cpt, file) {
229 if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
230 uniq_transfer_task_name(file);
231 } else if (SD_task_get_kind(file) == SD_TASK_COMP_SEQ){
232 /* If some tasks do not take files as input, connect them to the root
233 * if they don't produce files, connect them to the end node.
235 if ((file != root_task) && (file != end_task)) {
236 if (file->inputs->empty())
237 SD_task_dependency_add(nullptr, nullptr, root_task, file);
238 if (file->outputs->empty())
239 SD_task_dependency_add(nullptr, nullptr, file, end_task);
246 if (not acyclic_graph_detail(result)) {
247 char* base = xbt_basename(filename);
248 XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.", base);
250 xbt_dynar_foreach(result, cpt, file)
251 SD_task_destroy(file);
252 xbt_dynar_free_container(&result);
259 void STag_dax__adag()
262 double version = std::stod(std::string(A_dax__adag_version));
263 xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
264 } catch (std::invalid_argument& ia) {
265 throw std::invalid_argument(std::string("Parse error: ") + A_dax__adag_version + " is not a double");
272 double runtime = std::stod(std::string(A_dax__job_runtime));
274 std::string name = std::string(A_dax__job_id) + "@" + A_dax__job_name;
275 runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
276 XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
277 current_job = SD_task_create_comp_seq(name.c_str(), nullptr, runtime);
278 jobs.insert({A_dax__job_id, current_job});
279 xbt_dynar_push(result, ¤t_job);
280 } catch (std::invalid_argument& ia) {
281 throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
285 void STag_dax__uses()
289 size = std::stod(std::string(A_dax__uses_size));
290 } catch (std::invalid_argument& ia) {
291 throw std::invalid_argument(std::string("Parse error: ") + A_dax__uses_size + " is not a double");
293 bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
295 XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
296 auto it = files.find(A_dax__uses_file);
298 if (it == files.end()) {
299 file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
300 sd_global->initial_tasks->erase(file);
301 files[A_dax__uses_file] = file;
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>");