1 /* Copyright (c) 2009-2018. 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");
19 /* Ensure that transfer tasks have unique names even though a file is used several times */
20 void uniq_transfer_task_name(SD_task_t task)
22 SD_task_t child = *(task->successors->begin());
23 SD_task_t parent = *(task->predecessors->begin());
25 std::string new_name =
26 std::string(SD_task_get_name(parent)) + "_" + SD_task_get_name(task) + "_" + SD_task_get_name(child);
28 SD_task_set_name(task, new_name.c_str());
31 static bool children_are_marked(SD_task_t task){
32 for (SD_task_t const& it : *task->successors)
35 for (SD_task_t const& it : *task->outputs)
41 static bool parents_are_marked(SD_task_t task){
42 for (SD_task_t const& it : *task->predecessors)
45 for (SD_task_t const& it : *task->inputs)
51 bool acyclic_graph_detail(xbt_dynar_t dag){
53 bool all_marked = true;
54 SD_task_t task = nullptr;
55 std::vector<SD_task_t> current;
56 xbt_dynar_foreach(dag,count,task){
57 if(task->kind != SD_TASK_COMM_E2E){
59 if(task->successors->empty() && task->outputs->empty())
60 current.push_back(task);
63 while (not current.empty()) {
64 std::vector<SD_task_t> next;
65 for (auto const& t : current) {
68 for (SD_task_t const& input : *t->inputs) {
70 // Inputs are communication, hence they can have only one predecessor
71 SD_task_t input_pred = *(input->predecessors->begin());
72 if (children_are_marked(input_pred))
73 next.push_back(input_pred);
75 for (SD_task_t const& pred : *t->predecessors) {
76 if (children_are_marked(pred))
85 //test if all tasks are marked
86 xbt_dynar_foreach(dag,count,task){
87 if(task->kind != SD_TASK_COMM_E2E && task->marked == 0){
88 XBT_WARN("the task %s is not marked",task->name);
95 XBT_VERB("there is at least one cycle in your task graph");
96 xbt_dynar_foreach(dag,count,task){
97 if(task->kind != SD_TASK_COMM_E2E && task->predecessors->empty() && task->inputs->empty()){
99 current.push_back(task);
102 //test if something has to be done for the next iteration
103 while (not current.empty()) {
104 std::vector<SD_task_t> next;
105 //test if the current iteration is done
106 for (auto const& t : current) {
108 for (SD_task_t const& output : *t->outputs) {
110 // outputs are communication, hence they can have only one successor
111 SD_task_t output_succ = *(output->successors->begin());
112 if (parents_are_marked(output_succ))
113 next.push_back(output_succ);
115 for (SD_task_t const& succ : *t->successors) {
116 if (parents_are_marked(succ))
117 next.push_back(succ);
125 xbt_dynar_foreach(dag,count,task){
126 if(task->kind != SD_TASK_COMM_E2E && task->marked == 0){
127 XBT_WARN("the task %s is in a cycle",task->name);
135 static YY_BUFFER_STATE input_buffer;
137 static xbt_dynar_t result;
138 static std::map<std::string, SD_task_t> jobs;
139 static std::map<std::string, SD_task_t> files;
140 static SD_task_t current_job;
141 static SD_task_t root_task;
142 static SD_task_t end_task;
144 static void dax_task_free(void *task)
146 SD_task_destroy(static_cast<SD_task_t>(task));
149 /** @brief loads a DAX file describing a DAG
151 * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
153 xbt_dynar_t SD_daxload(const char *filename)
156 FILE* in_file = fopen(filename, "r");
157 xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
158 input_buffer = dax__create_buffer(in_file, 10);
159 dax__switch_to_buffer(input_buffer);
162 result = xbt_dynar_new(sizeof(SD_task_t), dax_task_free);
163 root_task = SD_task_create_comp_seq("root", nullptr, 0);
164 /* by design the root task is always SCHEDULABLE */
165 SD_task_set_state(root_task, SD_SCHEDULABLE);
167 xbt_dynar_push(result, &root_task);
168 end_task = SD_task_create_comp_seq("end", nullptr, 0);
172 xbt_die("Parse error in %s: %s", filename, dax__parse_err_msg());
173 dax__delete_buffer(input_buffer);
177 /* And now, post-process the files.
178 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
179 * Files not produced in the system are said to be produced by root task (top of DAG).
180 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
183 for (auto const& elm : files) {
186 if (file->predecessors->empty()) {
187 for (SD_task_t const& it : *file->successors) {
188 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
189 SD_task_dependency_add(root_task, newfile);
190 SD_task_dependency_add(newfile, it);
191 xbt_dynar_push(result, &newfile);
193 } else if (file->successors->empty()) {
194 for (SD_task_t const& it : *file->predecessors) {
195 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
196 SD_task_dependency_add(it, newfile);
197 SD_task_dependency_add(newfile, end_task);
198 xbt_dynar_push(result, &newfile);
201 for (SD_task_t const& it : *file->predecessors) {
202 for (SD_task_t const& it2 : *file->successors) {
204 XBT_WARN ("File %s is produced and consumed by task %s."
205 "This loop dependency will prevent the execution of the task.", file->name, it->name);
207 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
208 SD_task_dependency_add(it, newfile);
209 SD_task_dependency_add(newfile, it2);
210 xbt_dynar_push(result, &newfile);
216 /* Push end task last */
217 xbt_dynar_push(result, &end_task);
219 /* Free previous copy of the files */
220 for (auto const& elm : files)
221 SD_task_destroy(elm.second);
223 xbt_dynar_foreach(result, cpt, file) {
224 if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
225 uniq_transfer_task_name(file);
226 } else if (SD_task_get_kind(file) == SD_TASK_COMP_SEQ){
227 /* If some tasks do not take files as input, connect them to the root
228 * if they don't produce files, connect them to the end node.
230 if ((file != root_task) && (file != end_task)) {
231 if (file->inputs->empty())
232 SD_task_dependency_add(root_task, file);
233 if (file->outputs->empty())
234 SD_task_dependency_add(file, end_task);
241 if (not acyclic_graph_detail(result)) {
242 std::string base = simgrid::xbt::Path(filename).getBasename();
243 XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.", base.c_str());
244 xbt_dynar_foreach(result, cpt, file)
245 SD_task_destroy(file);
246 xbt_dynar_free_container(&result);
253 void STag_dax__adag()
256 double version = std::stod(std::string(A_dax__adag_version));
257 xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
258 } catch (std::invalid_argument& ia) {
259 throw std::invalid_argument(std::string("Parse error: ") + A_dax__adag_version + " is not a double");
266 double runtime = std::stod(std::string(A_dax__job_runtime));
268 std::string name = std::string(A_dax__job_id) + "@" + A_dax__job_name;
269 runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
270 XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
271 current_job = SD_task_create_comp_seq(name.c_str(), nullptr, runtime);
272 jobs.insert({A_dax__job_id, current_job});
273 xbt_dynar_push(result, ¤t_job);
274 } catch (std::invalid_argument& ia) {
275 throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
279 void STag_dax__uses()
283 size = std::stod(std::string(A_dax__uses_size));
284 } catch (std::invalid_argument& ia) {
285 throw std::invalid_argument(std::string("Parse error: ") + A_dax__uses_size + " is not a double");
287 bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
289 XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
290 auto it = files.find(A_dax__uses_file);
292 if (it == files.end()) {
293 file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
294 sd_global->initial_tasks->erase(file);
295 files[A_dax__uses_file] = file;
298 if (file->amount < size || file->amount > size) {
299 XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, SD_task_get_amount(file), size);
303 SD_task_dependency_add(file, current_job);
305 SD_task_dependency_add(current_job, file);
306 if ((file->predecessors->size() + file->inputs->size()) > 1) {
307 XBT_WARN("File %s created at more than one location...", file->name);
312 static SD_task_t current_child;
313 void STag_dax__child()
315 auto job = jobs.find(A_dax__child_ref);
316 if (job != jobs.end()) {
317 current_child = job->second;
319 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
320 ": Asked to add dependencies to the non-existent " + A_dax__child_ref + "task");
324 void ETag_dax__child()
326 current_child = nullptr;
329 void STag_dax__parent()
331 auto job = jobs.find(A_dax__parent_ref);
332 if (job != jobs.end()) {
333 SD_task_t parent = job->second;
334 SD_task_dependency_add(parent, current_child);
335 XBT_DEBUG("Control-flow dependency from %s to %s", current_child->name, parent->name);
337 throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
338 ": Asked to add a dependency from " + current_child->name + " to " + A_dax__parent_ref +
339 ", but " + A_dax__parent_ref + " does not exist");
343 void ETag_dax__adag()
345 XBT_DEBUG("See </adag>");
350 current_job = nullptr;
351 XBT_DEBUG("See </job>");
354 void ETag_dax__parent()
356 XBT_DEBUG("See </parent>");
359 void ETag_dax__uses()
361 XBT_DEBUG("See </uses>");