Logo AND Algorithmique Numérique Distribuée

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