Logo AND Algorithmique Numérique Distribuée

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