Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
aeb28a4041450ded2b9193ad0b6ec4ccbf5465a1
[simgrid.git] / src / simdag / sd_daxloader.cpp
1 /* Copyright (c) 2009-2016. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "simgrid/simdag.h"
8 #include "xbt/misc.h"
9 #include "xbt/log.h"
10 #include "xbt/str.h"
11 #include "xbt/file.h" /* xbt_basename() */
12 #include "simdag_private.hpp"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_daxparse, sd, "Parsing DAX files");
15
16 extern "C" {
17 #undef CLEANUP
18 #include "dax_dtd.h"
19 #define register /* g++ don't like register, so don't say it */
20 #include "dax_dtd.c"
21 #undef register
22 }
23
24 static bool children_are_marked(SD_task_t task){
25   for (SD_task_t it : *task->successors)
26     if (it->marked == 0)
27       return false;
28   for (SD_task_t it : *task->outputs)
29     if (it->marked == 0)
30       return false;
31   return true;
32 }
33
34 static bool parents_are_marked(SD_task_t task){
35   for (SD_task_t it : *task->predecessors)
36     if (it->marked == 0)
37       return false;
38   for (SD_task_t it : *task->inputs)
39     if (it->marked == 0)
40       return false;
41   return true;
42 }
43
44 bool acyclic_graph_detail(xbt_dynar_t dag){
45   unsigned int count;
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){
51       task->marked = 0;
52       if(task->successors->empty() && task->outputs->empty())
53         current.push_back(task);
54     }
55   }
56   while(!current.empty()){
57     std::vector<SD_task_t> next;
58     for (auto t: current){
59       //Mark task
60       t->marked = 1;
61       for (SD_task_t input : *t->inputs){
62         input->marked=1;
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);
67       }
68       for (SD_task_t pred : *t->predecessors) {
69         if (children_are_marked(pred))
70           next.push_back(pred);
71       }
72     }
73     current.clear();
74     current = next;
75   }
76
77   all_marked = true;
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());
82       all_marked = false;
83       break;
84     }
85   }
86
87   if(!all_marked){
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 && task->predecessors->empty() && task->inputs->empty()){
91         task->marked = 1;
92         current.push_back(task);
93       }
94     }
95     //test if something has to be done for the next iteration
96     while(!current.empty()){
97       std::vector<SD_task_t> next;
98       //test if the current iteration is done
99       for (auto t: current){
100         t->marked = 1;
101         for (SD_task_t output : *t->outputs) {
102           output->marked = 1;
103           // outputs are communication, hence they can have only one successor
104           SD_task_t output_succ = *(output->successors->begin());
105           if (parents_are_marked(output_succ))
106             next.push_back(output_succ);
107         }
108         for (SD_task_t succ : *t->successors) {
109           if (parents_are_marked(succ))
110             next.push_back(succ);
111         }
112         current.clear();
113         current = next;
114       }
115     }
116
117     all_marked = true;
118     xbt_dynar_foreach(dag,count,task){
119       if(task->kind != SD_TASK_COMM_E2E && task->marked == 0){
120         XBT_WARN("the task %s is in a cycle",task->name.c_str());
121         all_marked = false;
122       }
123     }
124   }
125   return all_marked;
126 }
127
128 static YY_BUFFER_STATE input_buffer;
129
130 static xbt_dynar_t result;
131 static xbt_dict_t files;
132 static xbt_dict_t jobs;
133 static SD_task_t current_job;
134
135 static void dax_task_free(void *task)
136 {
137   SD_task_destroy(static_cast<SD_task_t>(task));
138 }
139
140 /** @brief loads a DAX file describing a DAG
141  * 
142  * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
143  */
144 xbt_dynar_t SD_daxload(const char *filename)
145 {
146   xbt_dict_cursor_t cursor;
147   SD_task_t file;
148   char *name;
149   FILE *in_file = fopen(filename, "r");
150   xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
151   input_buffer = dax__create_buffer(in_file, 10);
152   dax__switch_to_buffer(input_buffer);
153   dax_lineno = 1;
154
155   result = xbt_dynar_new(sizeof(SD_task_t), dax_task_free);
156   files = xbt_dict_new_homogeneous(&dax_task_free);
157   jobs = xbt_dict_new_homogeneous(nullptr);
158   SD_task_t root_task = SD_task_create_comp_seq("root", nullptr, 0);
159   /* by design the root task is always SCHEDULABLE */
160   SD_task_set_state(root_task, SD_SCHEDULABLE);
161
162   xbt_dynar_push(result, &root_task);
163   SD_task_t end_task = SD_task_create_comp_seq("end", nullptr, 0);
164
165   int res = dax_lex();
166   if (res != 0)
167     xbt_die("Parse error in %s: %s", filename, dax__parse_err_msg());
168   dax__delete_buffer(input_buffer);
169   fclose(in_file);
170   dax_lex_destroy();
171   xbt_dict_free(&jobs);
172
173   /* And now, post-process the files.
174    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
175    * Files not produced in the system are said to be produced by root task (top of DAG).
176    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
177    */
178
179   xbt_dict_foreach(files, cursor, name, file) {
180     SD_task_t newfile;
181     if (file->predecessors->empty()) {
182       for (SD_task_t it : *file->successors) {
183         newfile = SD_task_create_comm_e2e(file->name.c_str(), nullptr, file->amount);
184         SD_task_dependency_add(nullptr, nullptr, root_task, newfile);
185         SD_task_dependency_add(nullptr, nullptr, newfile, it);
186         xbt_dynar_push(result, &newfile);
187       }
188     } else if (file->successors->empty()) {
189       for (SD_task_t it : *file->predecessors){
190         newfile = SD_task_create_comm_e2e(file->name.c_str(), nullptr, file->amount);
191         SD_task_dependency_add(nullptr, nullptr, it, newfile);
192         SD_task_dependency_add(nullptr, nullptr, newfile, end_task);
193         xbt_dynar_push(result, &newfile);
194       }
195     } else {
196       for (SD_task_t it : *file->predecessors) {
197         for (SD_task_t it2 : *file->successors) {
198           if (it == it2) {
199             XBT_WARN ("File %s is produced and consumed by task %s."
200                       "This loop dependency will prevent the execution of the task.",
201                       file->name.c_str(), it->name.c_str());
202           }
203           newfile = SD_task_create_comm_e2e(file->name.c_str(), nullptr, file->amount);
204           SD_task_dependency_add(nullptr, nullptr, it, newfile);
205           SD_task_dependency_add(nullptr, nullptr, newfile, it2);
206           xbt_dynar_push(result, &newfile);
207         }
208       }
209     }
210   }
211
212   /* Push end task last */
213   xbt_dynar_push(result, &end_task);
214
215   /* Free previous copy of the files */
216   xbt_dict_free(&files);
217   unsigned int cpt;
218   xbt_dynar_foreach(result, cpt, file) {
219     if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
220       /* Ensure that transfer tasks have unique names even though a file is used several times */
221       file->name = std::string((*file->predecessors->begin())->name+ "_" + file->name + "_" +
222                                (*file->successors->begin())->name);
223     } else if (SD_task_get_kind(file) == SD_TASK_COMP_SEQ){
224       /* If some tasks do not take files as input, connect them to the root
225        * if they don't produce files, connect them to the end node.
226        */
227       if ((file != root_task) && file->inputs->empty())
228         SD_task_dependency_add(nullptr, nullptr, root_task, file);
229       if ((file != end_task) && file->outputs->empty())
230         SD_task_dependency_add(nullptr, nullptr, file, end_task);
231     } else {
232        THROW_IMPOSSIBLE;
233     }
234   }
235
236   if (!acyclic_graph_detail(result)) {
237     char* base = xbt_basename(filename);
238     XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.", base);
239     free(base);
240     xbt_dynar_foreach(result, cpt, file)
241       SD_task_destroy(file);
242     xbt_dynar_free_container(&result);
243     return nullptr;
244   } else {
245     return result;
246   }
247 }
248
249 void STag_dax__adag()
250 {
251   double version = xbt_str_parse_double(A_dax__adag_version, "Parse error: %s is not a double");
252
253   xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
254 }
255
256 void STag_dax__job()
257 {
258   double runtime = xbt_str_parse_double(A_dax__job_runtime, "Parse error: %s is not a double");
259   std::string name = std::string(A_dax__job_id) +"@"+ std::string(A_dax__job_name);
260   runtime *= 4200000000.;       /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
261   XBT_DEBUG("See <job id=%s runtime=%s %.0f>",A_dax__job_id,A_dax__job_runtime,runtime);
262   current_job = SD_task_create_comp_seq(name.c_str(), nullptr, runtime);
263   xbt_dict_set(jobs, A_dax__job_id, current_job, nullptr);
264   xbt_dynar_push(result, &current_job);
265 }
266
267 void STag_dax__uses()
268 {
269   double size = xbt_str_parse_double(A_dax__uses_size, "Parse error: %s is not a double");
270   int is_input = (A_dax__uses_link == A_dax__uses_link_input);
271
272   XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
273   SD_task_t file = static_cast<SD_task_t>(xbt_dict_get_or_null(files, A_dax__uses_file));
274   if (file == nullptr) {
275     file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
276     sd_global->initial_tasks->erase(file);
277     xbt_dict_set(files, A_dax__uses_file, file, nullptr);
278   } else {
279     if (file->amount < size || file->amount > size) {
280       XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, SD_task_get_amount(file), size);
281     }
282   }
283   if (is_input) {
284     SD_task_dependency_add(nullptr, nullptr, file, current_job);
285   } else {
286     SD_task_dependency_add(nullptr, nullptr, current_job, file);
287     if ((file->predecessors->size() + file->inputs->size()) > 1) {
288       XBT_WARN("File %s created at more than one location...", file->name.c_str());
289     }
290   }
291 }
292
293 static SD_task_t current_child;
294 void STag_dax__child()
295 {
296   current_child = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, A_dax__child_ref));
297   xbt_assert(current_child != nullptr,"Parse error on line %d: Asked to add dependencies to the non-existent %s task",
298              dax_lineno, A_dax__child_ref);
299 }
300
301 void ETag_dax__child()
302 {
303   current_child = nullptr;
304 }
305
306 void STag_dax__parent()
307 {
308   SD_task_t parent = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, A_dax__parent_ref));
309   xbt_assert(parent != nullptr, "Parse error on line %d: Asked to add a dependency from %s to %s, but %s does not exist",
310              dax_lineno, current_child->name.c_str(), A_dax__parent_ref, A_dax__parent_ref);
311   SD_task_dependency_add(nullptr, nullptr, parent, current_child);
312   XBT_DEBUG("Control-flow dependency from %s to %s", current_child->name.c_str(), parent->name.c_str());
313 }
314
315 void ETag_dax__adag()
316 {
317   XBT_DEBUG("See </adag>");
318 }
319
320 void ETag_dax__job()
321 {
322   current_job = nullptr;
323   XBT_DEBUG("See </job>");
324 }
325
326 void ETag_dax__parent()
327 {
328   XBT_DEBUG("See </parent>");
329 }
330
331 void ETag_dax__uses()
332 {
333   XBT_DEBUG("See </uses>");
334 }