Logo AND Algorithmique Numérique Distribuée

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