1 /* Copyright (c) 2009-2016. 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 "simgrid/simdag.h"
11 #include "xbt/file.h" /* xbt_basename() */
12 #include "simdag_private.hpp"
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 static bool children_are_marked(SD_task_t task){
25 for (SD_task_t it : *task->successors)
28 for (SD_task_t it : *task->outputs)
34 static bool parents_are_marked(SD_task_t task){
35 for (SD_task_t it : *task->predecessors)
38 for (SD_task_t it : *task->inputs)
44 bool acyclic_graph_detail(xbt_dynar_t dag){
46 bool all_marked = true;
47 SD_task_t task = nullptr;
48 std::vector<SD_task_t> current;
49 xbt_dynar_foreach(dag,count,task){
50 if(task->kind != SD_TASK_COMM_E2E){
52 if(task->successors->empty() && task->outputs->empty())
53 current.push_back(task);
56 while(!current.empty()){
57 std::vector<SD_task_t> next;
58 for (auto t: current){
61 for (SD_task_t input : *t->inputs){
63 // Inputs are communication, hence they can have only one predecessor
64 SD_task_t input_pred = *(input->predecessors->begin());
65 if (children_are_marked(input_pred))
66 next.push_back(input_pred);
68 for (SD_task_t pred : *t->predecessors) {
69 if (children_are_marked(pred))
78 //test if all tasks are marked
79 xbt_dynar_foreach(dag,count,task){
80 if(task->kind != SD_TASK_COMM_E2E && task->marked == 0){
81 XBT_WARN("the task %s is not marked",task->name.c_str());
88 XBT_VERB("there is at least one cycle in your task graph");
89 xbt_dynar_foreach(dag,count,task){
90 if(task->kind != SD_TASK_COMM_E2E) {
91 if(task->predecessors->empty() && task->inputs->empty()){
93 current.push_back(task);
97 //test if something has to be done for the next iteration
98 while(!current.empty()){
99 std::vector<SD_task_t> next;
100 //test if the current iteration is done
101 for (auto t: current){
103 for (SD_task_t output : *t->outputs) {
105 // outputs are communication, hence they can have only one successor
106 SD_task_t output_succ = *(output->successors->begin());
107 if (parents_are_marked(output_succ))
108 next.push_back(output_succ);
110 for (SD_task_t succ : *t->successors) {
111 if (parents_are_marked(succ))
112 next.push_back(succ);
120 xbt_dynar_foreach(dag,count,task){
121 if(task->kind != SD_TASK_COMM_E2E && task->marked == 0){
122 XBT_WARN("the task %s is in a cycle",task->name.c_str());
130 static YY_BUFFER_STATE input_buffer;
132 static xbt_dynar_t result;
133 static xbt_dict_t files;
134 static xbt_dict_t jobs;
135 static SD_task_t current_job;
137 static void dax_task_free(void *task)
139 SD_task_destroy(static_cast<SD_task_t>(task));
142 /** @brief loads a DAX file describing a DAG
144 * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
146 xbt_dynar_t SD_daxload(const char *filename)
148 xbt_dict_cursor_t cursor;
151 FILE *in_file = fopen(filename, "r");
152 xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
153 input_buffer = dax__create_buffer(in_file, 10);
154 dax__switch_to_buffer(input_buffer);
157 result = xbt_dynar_new(sizeof(SD_task_t), dax_task_free);
158 files = xbt_dict_new_homogeneous(&dax_task_free);
159 jobs = xbt_dict_new_homogeneous(nullptr);
160 SD_task_t root_task = SD_task_create_comp_seq("root", nullptr, 0);
161 /* by design the root task is always SCHEDULABLE */
162 SD_task_set_state(root_task, SD_SCHEDULABLE);
164 xbt_dynar_push(result, &root_task);
165 SD_task_t end_task = SD_task_create_comp_seq("end", nullptr, 0);
169 xbt_die("Parse error in %s: %s", filename, dax__parse_err_msg());
170 dax__delete_buffer(input_buffer);
173 xbt_dict_free(&jobs);
175 /* And now, post-process the files.
176 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
177 * Files not produced in the system are said to be produced by root task (top of DAG).
178 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
181 xbt_dict_foreach(files, cursor, name, file) {
183 if (file->predecessors->empty()) {
184 for (SD_task_t it : *file->successors) {
185 newfile = SD_task_create_comm_e2e(file->name.c_str(), nullptr, file->amount);
186 SD_task_dependency_add(nullptr, nullptr, root_task, newfile);
187 SD_task_dependency_add(nullptr, nullptr, newfile, it);
188 xbt_dynar_push(result, &newfile);
190 } else if (file->successors->empty()) {
191 for (SD_task_t it : *file->predecessors){
192 newfile = SD_task_create_comm_e2e(file->name.c_str(), nullptr, file->amount);
193 SD_task_dependency_add(nullptr, nullptr, it, newfile);
194 SD_task_dependency_add(nullptr, nullptr, newfile, end_task);
195 xbt_dynar_push(result, &newfile);
198 for (SD_task_t it : *file->predecessors) {
199 for (SD_task_t it2 : *file->successors) {
201 XBT_WARN ("File %s is produced and consumed by task %s."
202 "This loop dependency will prevent the execution of the task.",
203 file->name.c_str(), it->name.c_str());
205 newfile = SD_task_create_comm_e2e(file->name.c_str(), nullptr, file->amount);
206 SD_task_dependency_add(nullptr, nullptr, it, newfile);
207 SD_task_dependency_add(nullptr, nullptr, newfile, it2);
208 xbt_dynar_push(result, &newfile);
214 /* Push end task last */
215 xbt_dynar_push(result, &end_task);
217 /* Free previous copy of the files */
218 xbt_dict_free(&files);
220 xbt_dynar_foreach(result, cpt, file) {
221 if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
222 /* Ensure that transfer tasks have unique names even though a file is used several times */
223 file->name = std::string((*file->predecessors->begin())->name+ "_" + file->name + "_" +
224 (*file->successors->begin())->name);
225 } else if (SD_task_get_kind(file) == SD_TASK_COMP_SEQ){
226 /* If some tasks do not take files as input, connect them to the root
227 * if they don't produce files, connect them to the end node.
229 if ((file != root_task) && file->inputs->empty())
230 SD_task_dependency_add(nullptr, nullptr, root_task, file);
231 if ((file != end_task) && file->outputs->empty())
232 SD_task_dependency_add(nullptr, nullptr, file, end_task);
238 if (!acyclic_graph_detail(result)) {
239 char* base = xbt_basename(filename);
240 XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.", base);
242 xbt_dynar_foreach(result, cpt, file)
243 SD_task_destroy(file);
244 xbt_dynar_free_container(&result);
251 void STag_dax__adag()
253 double version = xbt_str_parse_double(A_dax__adag_version, "Parse error: %s is not a double");
255 xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
260 double runtime = xbt_str_parse_double(A_dax__job_runtime, "Parse error: %s is not a double");
261 std::string name = std::string(A_dax__job_id) +"@"+ std::string(A_dax__job_name);
262 runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
263 XBT_DEBUG("See <job id=%s runtime=%s %.0f>",A_dax__job_id,A_dax__job_runtime,runtime);
264 current_job = SD_task_create_comp_seq(name.c_str(), nullptr, runtime);
265 xbt_dict_set(jobs, A_dax__job_id, current_job, nullptr);
266 xbt_dynar_push(result, ¤t_job);
269 void STag_dax__uses()
271 double size = xbt_str_parse_double(A_dax__uses_size, "Parse error: %s is not a double");
272 int is_input = (A_dax__uses_link == A_dax__uses_link_input);
274 XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
275 SD_task_t file = static_cast<SD_task_t>(xbt_dict_get_or_null(files, A_dax__uses_file));
276 if (file == nullptr) {
277 file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
278 sd_global->initial_tasks->erase(file);
279 xbt_dict_set(files, A_dax__uses_file, file, nullptr);
281 if (file->amount < size || file->amount > size) {
282 XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, SD_task_get_amount(file), size);
286 SD_task_dependency_add(nullptr, nullptr, file, current_job);
288 SD_task_dependency_add(nullptr, nullptr, current_job, file);
289 if ((file->predecessors->size() + file->inputs->size()) > 1) {
290 XBT_WARN("File %s created at more than one location...", file->name.c_str());
295 static SD_task_t current_child;
296 void STag_dax__child()
298 current_child = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, A_dax__child_ref));
299 xbt_assert(current_child != nullptr,"Parse error on line %d: Asked to add dependencies to the non-existent %s task",
300 dax_lineno, A_dax__child_ref);
303 void ETag_dax__child()
305 current_child = nullptr;
308 void STag_dax__parent()
310 SD_task_t parent = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, A_dax__parent_ref));
311 xbt_assert(parent != nullptr, "Parse error on line %d: Asked to add a dependency from %s to %s, but %s does not exist",
312 dax_lineno, current_child->name.c_str(), A_dax__parent_ref, A_dax__parent_ref);
313 SD_task_dependency_add(nullptr, nullptr, parent, current_child);
314 XBT_DEBUG("Control-flow dependency from %s to %s", current_child->name.c_str(), parent->name.c_str());
317 void ETag_dax__adag()
319 XBT_DEBUG("See </adag>");
324 current_job = nullptr;
325 XBT_DEBUG("See </job>");
328 void ETag_dax__parent()
330 XBT_DEBUG("See </parent>");
333 void ETag_dax__uses()
335 XBT_DEBUG("See </uses>");