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 "src/simdag/simdag_private.h"
8 #include "simgrid/simdag.h"
12 #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 */
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 char *new_name = bprintf("%s_%s_%s", SD_task_get_name(parent), SD_task_get_name(task), SD_task_get_name(child));
33 SD_task_set_name(task, new_name);
38 static bool children_are_marked(SD_task_t task){
39 for (SD_task_t it : *task->successors)
42 for (SD_task_t it : *task->outputs)
48 static bool parents_are_marked(SD_task_t task){
49 for (SD_task_t it : *task->predecessors)
52 for (SD_task_t it : *task->inputs)
58 bool acyclic_graph_detail(xbt_dynar_t dag){
59 unsigned int count, count_current=0;
60 bool all_marked = true;
61 SD_task_t task = nullptr;
62 xbt_dynar_t next = nullptr, current = xbt_dynar_new(sizeof(SD_task_t),nullptr);
64 xbt_dynar_foreach(dag,count,task){
65 if(task->kind == SD_TASK_COMM_E2E)
68 if(task->successors->empty() && task->outputs->empty())
69 xbt_dynar_push(current, &task);
71 //test if something has to be done for the next iteration
72 while(!xbt_dynar_is_empty(current)){
73 next = xbt_dynar_new(sizeof(SD_task_t),nullptr);
74 //test if the current iteration is done
75 xbt_dynar_foreach(current,count_current,task){
80 for (SD_task_t it : *task->inputs){
82 // Inputs are communication, hence they can have only one predecessor
83 SD_task_t input_pred = *(it->predecessors->begin());
84 if (children_are_marked(input_pred))
85 xbt_dynar_push(next, &input_pred);
87 for (SD_task_t it : *task->predecessors) {
88 if (children_are_marked(it))
89 xbt_dynar_push(next, &it);
92 xbt_dynar_free(¤t);
96 xbt_dynar_free(¤t);
98 xbt_dynar_foreach(dag,count,task){
99 if(task->kind == SD_TASK_COMM_E2E)
101 //test if all tasks are marked
102 if(task->marked == 0){
103 XBT_WARN("the task %s is not marked",task->name);
109 XBT_VERB("there is at least one cycle in your task graph");
111 current = xbt_dynar_new(sizeof(SD_task_t),nullptr);
112 xbt_dynar_foreach(dag,count,task){
113 if(task->kind == SD_TASK_COMM_E2E)
115 if(task->predecessors->empty() && task->inputs->empty()){
116 xbt_dynar_push(current, &task);
120 xbt_dynar_foreach(dag,count,task){
121 if(task->kind == SD_TASK_COMM_E2E)
123 if(task->predecessors->empty() && task->inputs->empty()){
125 xbt_dynar_push(current, &task);
128 //test if something has to be done for the next iteration
129 while(!xbt_dynar_is_empty(current)){
130 next = xbt_dynar_new(sizeof(SD_task_t),nullptr);
131 //test if the current iteration is done
132 xbt_dynar_foreach(current,count_current,task){
137 for (SD_task_t it : *task->outputs) {
139 // outputs are communication, hence they can have only one successor
140 SD_task_t output_succ = *(it->successors->begin());
141 if (parents_are_marked(output_succ))
142 xbt_dynar_push(next, &output_succ);
144 for (SD_task_t it : *task->predecessors) {
145 if (parents_are_marked(it))
146 xbt_dynar_push(next, &it);
148 xbt_dynar_free(¤t);
152 xbt_dynar_free(¤t);
154 xbt_dynar_foreach(dag,count,task){
155 if(task->kind == SD_TASK_COMM_E2E)
157 //test if all tasks are marked
158 if(task->marked == 0){
159 XBT_WARN("the task %s is in a cycle",task->name);
168 static YY_BUFFER_STATE input_buffer;
170 static xbt_dynar_t result;
171 static xbt_dict_t jobs;
172 static xbt_dict_t files;
173 static SD_task_t current_job;
174 static SD_task_t root_task;
175 static SD_task_t end_task;
177 static void dax_task_free(void *task)
179 SD_task_destroy(static_cast<SD_task_t>(task));
182 /** @brief loads a DAX file describing a DAG
184 * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
186 xbt_dynar_t SD_daxload(const char *filename)
188 xbt_dict_cursor_t cursor;
191 FILE *in_file = fopen(filename, "r");
192 xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
193 input_buffer = dax__create_buffer(in_file, 10);
194 dax__switch_to_buffer(input_buffer);
197 result = xbt_dynar_new(sizeof(SD_task_t), dax_task_free);
198 files = xbt_dict_new_homogeneous(&dax_task_free);
199 jobs = xbt_dict_new_homogeneous(nullptr);
200 root_task = SD_task_create_comp_seq("root", nullptr, 0);
201 /* by design the root task is always SCHEDULABLE */
202 SD_task_set_state(root_task, SD_SCHEDULABLE);
204 xbt_dynar_push(result, &root_task);
205 end_task = SD_task_create_comp_seq("end", nullptr, 0);
209 xbt_die("Parse error in %s: %s", filename, dax__parse_err_msg());
210 dax__delete_buffer(input_buffer);
213 xbt_dict_free(&jobs);
215 /* And now, post-process the files.
216 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
217 * Files not produced in the system are said to be produced by root task (top of DAG).
218 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
221 xbt_dict_foreach(files, cursor, name, file) {
223 if (file->predecessors->empty()) {
224 for (SD_task_t it : *file->successors) {
225 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
226 SD_task_dependency_add(nullptr, nullptr, root_task, newfile);
227 SD_task_dependency_add(nullptr, nullptr, newfile, it);
228 xbt_dynar_push(result, &newfile);
230 } else if (file->successors->empty()) {
231 for (SD_task_t it : *file->predecessors){
232 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
233 SD_task_dependency_add(nullptr, nullptr, it, newfile);
234 SD_task_dependency_add(nullptr, nullptr, newfile, end_task);
235 xbt_dynar_push(result, &newfile);
238 for (SD_task_t it : *file->predecessors) {
239 for (SD_task_t it2 : *file->successors) {
241 XBT_WARN ("File %s is produced and consumed by task %s."
242 "This loop dependency will prevent the execution of the task.", file->name, it->name);
244 newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
245 SD_task_dependency_add(nullptr, nullptr, it, newfile);
246 SD_task_dependency_add(nullptr, nullptr, newfile, it2);
247 xbt_dynar_push(result, &newfile);
253 /* Push end task last */
254 xbt_dynar_push(result, &end_task);
256 /* Free previous copy of the files */
257 xbt_dict_free(&files);
259 xbt_dynar_foreach(result, cpt, file) {
260 if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
261 uniq_transfer_task_name(file);
262 } else if (SD_task_get_kind(file) == SD_TASK_COMP_SEQ){
263 /* If some tasks do not take files as input, connect them to the root
264 * if they don't produce files, connect them to the end node.
266 if ((file != root_task) && file->inputs->empty())
267 SD_task_dependency_add(nullptr, nullptr, root_task, file);
268 if ((file != end_task) && file->outputs->empty())
269 SD_task_dependency_add(nullptr, nullptr, file, end_task);
275 if (!acyclic_graph_detail(result)) {
276 char* base = xbt_basename(filename);
277 XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.", base);
279 xbt_dynar_foreach(result, cpt, file)
280 SD_task_destroy(file);
281 xbt_dynar_free_container(&result);
288 void STag_dax__adag()
290 XBT_ATTRIB_UNUSED double version;
291 version = xbt_str_parse_double(A_dax__adag_version, "Parse error: %s is not a double");
293 xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
298 double runtime = xbt_str_parse_double(A_dax__job_runtime, "Parse error: %s is not a double");
299 char *name = bprintf("%s@%s", A_dax__job_id, A_dax__job_name);
300 runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
301 XBT_DEBUG("See <job id=%s runtime=%s %.0f>",A_dax__job_id,A_dax__job_runtime,runtime);
302 current_job = SD_task_create_comp_seq(name, nullptr, runtime);
303 xbt_dict_set(jobs, A_dax__job_id, current_job, nullptr);
305 xbt_dynar_push(result, ¤t_job);
308 void STag_dax__uses(void)
310 double size = xbt_str_parse_double(A_dax__uses_size, "Parse error: %s is not a double");
311 int is_input = (A_dax__uses_link == A_dax__uses_link_input);
313 XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
314 SD_task_t file = static_cast<SD_task_t>(xbt_dict_get_or_null(files, A_dax__uses_file));
315 if (file == nullptr) {
316 file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
317 sd_global->initial_tasks->erase(file);
318 xbt_dict_set(files, A_dax__uses_file, file, nullptr);
320 if (file->amount < size || file->amount > size) {
321 XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, SD_task_get_amount(file), size);
325 SD_task_dependency_add(nullptr, nullptr, file, current_job);
327 SD_task_dependency_add(nullptr, nullptr, current_job, file);
328 if ((file->predecessors->size() + file->inputs->size()) > 1) {
329 XBT_WARN("File %s created at more than one location...", file->name);
334 static SD_task_t current_child;
335 void STag_dax__child()
337 current_child = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, A_dax__child_ref));
338 xbt_assert(current_child != nullptr,"Parse error on line %d: Asked to add dependencies to the non-existent %s task",
339 dax_lineno, A_dax__child_ref);
342 void ETag_dax__child()
344 current_child = nullptr;
347 void STag_dax__parent()
349 SD_task_t parent = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, A_dax__parent_ref));
350 xbt_assert(parent != nullptr, "Parse error on line %d: Asked to add a dependency from %s to %s, but %s does not exist",
351 dax_lineno, current_child->name, A_dax__parent_ref, A_dax__parent_ref);
352 SD_task_dependency_add(nullptr, nullptr, parent, current_child);
353 XBT_DEBUG("Control-flow dependency from %s to %s", current_child->name, parent->name);
356 void ETag_dax__adag()
358 XBT_DEBUG("See </adag>");
363 current_job = nullptr;
364 XBT_DEBUG("See </job>");
367 void ETag_dax__parent()
369 XBT_DEBUG("See </parent>");
372 void ETag_dax__uses()
374 XBT_DEBUG("See </uses>");