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.hpp"
14 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 for (SD_task_t const& it : *task->successors)
37 for (SD_task_t const& it : *task->outputs)
43 static bool parents_are_marked(SD_task_t task){
44 for (SD_task_t const& it : *task->predecessors)
47 for (SD_task_t const& it : *task->inputs)
53 bool acyclic_graph_detail(xbt_dynar_t dag){
55 bool all_marked = true;
56 SD_task_t task = nullptr;
57 std::vector<SD_task_t> current;
58 xbt_dynar_foreach(dag,count,task){
59 if(task->kind != SD_TASK_COMM_E2E){
61 if(task->successors->empty() && task->outputs->empty())
62 current.push_back(task);
65 while (not current.empty()) {
66 std::vector<SD_task_t> next;
67 for (auto const& t : current) {
70 for (SD_task_t const& input : *t->inputs) {
72 // Inputs are communication, hence they can have only one predecessor
73 SD_task_t input_pred = *(input->predecessors->begin());
74 if (children_are_marked(input_pred))
75 next.push_back(input_pred);
77 for (SD_task_t const& pred : *t->predecessors) {
78 if (children_are_marked(pred))
87 //test if all tasks are marked
88 xbt_dynar_foreach(dag,count,task){
89 if(task->kind != SD_TASK_COMM_E2E && task->marked == 0){
90 XBT_WARN("the task %s is not marked",task->name);
97 XBT_VERB("there is at least one cycle in your task graph");
98 xbt_dynar_foreach(dag,count,task){
99 if(task->kind != SD_TASK_COMM_E2E && task->predecessors->empty() && task->inputs->empty()){
101 current.push_back(task);
104 //test if something has to be done for the next iteration
105 while (not current.empty()) {
106 std::vector<SD_task_t> next;
107 //test if the current iteration is done
108 for (auto const& t : current) {
110 for (SD_task_t const& output : *t->outputs) {
112 // outputs are communication, hence they can have only one successor
113 SD_task_t output_succ = *(output->successors->begin());
114 if (parents_are_marked(output_succ))
115 next.push_back(output_succ);
117 for (SD_task_t const& succ : *t->successors) {
118 if (parents_are_marked(succ))
119 next.push_back(succ);
127 xbt_dynar_foreach(dag,count,task){
128 if(task->kind != SD_TASK_COMM_E2E && task->marked == 0){
129 XBT_WARN("the task %s is in a cycle",task->name);
137 static YY_BUFFER_STATE input_buffer;
139 static xbt_dynar_t result;
140 static std::map<std::string, SD_task_t> jobs;
141 static std::map<std::string, SD_task_t> files;
142 static SD_task_t current_job;
143 static SD_task_t root_task;
144 static SD_task_t end_task;
146 static void dax_task_free(void *task)
148 SD_task_destroy(static_cast<SD_task_t>(task));
151 /** @brief loads a DAX file describing a DAG
153 * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
155 xbt_dynar_t SD_daxload(const char *filename)
158 FILE* in_file = fopen(filename, "r");
159 xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
160 input_buffer = dax__create_buffer(in_file, 10);
161 dax__switch_to_buffer(input_buffer);
164 result = xbt_dynar_new(sizeof(SD_task_t), dax_task_free);
165 root_task = SD_task_create_comp_seq("root", nullptr, 0);
166 /* by design the root task is always SCHEDULABLE */
167 SD_task_set_state(root_task, SD_SCHEDULABLE);
169 xbt_dynar_push(result, &root_task);
170 end_task = SD_task_create_comp_seq("end", nullptr, 0);
174 xbt_die("Parse error in %s: %s", filename, dax__parse_err_msg());
175 dax__delete_buffer(input_buffer);
179 /* And now, post-process the files.
180 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
181 * Files not produced in the system are said to be produced by root task (top of DAG).
182 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
185 for (auto const& elm : files) {
188 if (file->predecessors->empty()) {
189 for (SD_task_t const& it : *file->successors) {
190 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
191 SD_task_dependency_add(nullptr, nullptr, root_task, newfile);
192 SD_task_dependency_add(nullptr, nullptr, newfile, it);
193 xbt_dynar_push(result, &newfile);
195 } else if (file->successors->empty()) {
196 for (SD_task_t const& it : *file->predecessors) {
197 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
198 SD_task_dependency_add(nullptr, nullptr, it, newfile);
199 SD_task_dependency_add(nullptr, nullptr, newfile, end_task);
200 xbt_dynar_push(result, &newfile);
203 for (SD_task_t const& it : *file->predecessors) {
204 for (SD_task_t const& it2 : *file->successors) {
206 XBT_WARN ("File %s is produced and consumed by task %s."
207 "This loop dependency will prevent the execution of the task.", file->name, it->name);
209 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
210 SD_task_dependency_add(nullptr, nullptr, it, newfile);
211 SD_task_dependency_add(nullptr, nullptr, newfile, it2);
212 xbt_dynar_push(result, &newfile);
218 /* Push end task last */
219 xbt_dynar_push(result, &end_task);
221 /* Free previous copy of the files */
222 for (auto const& elm : files)
223 SD_task_destroy(elm.second);
225 xbt_dynar_foreach(result, cpt, file) {
226 if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
227 uniq_transfer_task_name(file);
228 } else if (SD_task_get_kind(file) == SD_TASK_COMP_SEQ){
229 /* If some tasks do not take files as input, connect them to the root
230 * if they don't produce files, connect them to the end node.
232 if ((file != root_task) && (file != end_task)) {
233 if (file->inputs->empty())
234 SD_task_dependency_add(nullptr, nullptr, root_task, file);
235 if (file->outputs->empty())
236 SD_task_dependency_add(nullptr, nullptr, file, end_task);
243 if (not acyclic_graph_detail(result)) {
244 std::string base = simgrid::xbt::Path(filename).getBasename();
245 XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.", base.c_str());
246 xbt_dynar_foreach(result, cpt, file)
247 SD_task_destroy(file);
248 xbt_dynar_free_container(&result);
255 void STag_dax__adag()
258 double version = std::stod(std::string(A_dax__adag_version));
259 xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
260 } catch (std::invalid_argument& ia) {
261 throw std::invalid_argument(std::string("Parse error: ") + A_dax__adag_version + " is not a double");
268 double runtime = std::stod(std::string(A_dax__job_runtime));
270 std::string name = std::string(A_dax__job_id) + "@" + A_dax__job_name;
271 runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
272 XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
273 current_job = SD_task_create_comp_seq(name.c_str(), nullptr, runtime);
274 jobs.insert({A_dax__job_id, current_job});
275 xbt_dynar_push(result, ¤t_job);
276 } catch (std::invalid_argument& ia) {
277 throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
281 void STag_dax__uses()
285 size = std::stod(std::string(A_dax__uses_size));
286 } catch (std::invalid_argument& ia) {
287 throw std::invalid_argument(std::string("Parse error: ") + A_dax__uses_size + " is not a double");
289 bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
291 XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
292 auto it = files.find(A_dax__uses_file);
294 if (it == files.end()) {
295 file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
296 sd_global->initial_tasks->erase(file);
297 files[A_dax__uses_file] = file;
300 if (file->amount < size || file->amount > size) {
301 XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, SD_task_get_amount(file), size);
305 SD_task_dependency_add(nullptr, nullptr, file, current_job);
307 SD_task_dependency_add(nullptr, nullptr, current_job, file);
308 if ((file->predecessors->size() + file->inputs->size()) > 1) {
309 XBT_WARN("File %s created at more than one location...", file->name);
314 static SD_task_t current_child;
315 void STag_dax__child()
317 auto job = jobs.find(A_dax__child_ref);
318 if (job != jobs.end()) {
319 current_child = job->second;
321 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
322 ": Asked to add dependencies to the non-existent " + A_dax__child_ref + "task");
326 void ETag_dax__child()
328 current_child = nullptr;
331 void STag_dax__parent()
333 auto job = jobs.find(A_dax__parent_ref);
334 if (job != jobs.end()) {
335 SD_task_t parent = job->second;
336 SD_task_dependency_add(nullptr, nullptr, parent, current_child);
337 XBT_DEBUG("Control-flow dependency from %s to %s", current_child->name, parent->name);
339 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
340 ": Asked to add a dependency from " + current_child->name + " to " + A_dax__parent_ref +
341 ", but " + A_dax__parent_ref + " does not exist");
345 void ETag_dax__adag()
347 XBT_DEBUG("See </adag>");
352 current_job = nullptr;
353 XBT_DEBUG("See </job>");
356 void ETag_dax__parent()
358 XBT_DEBUG("See </parent>");
361 void ETag_dax__uses()
363 XBT_DEBUG("See </uses>");