Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid costly exceptions when looking into a map.
[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 <unordered_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 it : *task->successors)
39     if (it->marked == 0)
40       return false;
41   for (SD_task_t 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 it : *task->predecessors)
49     if (it->marked == 0)
50       return false;
51   for (SD_task_t 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 t: current){
72       //Mark task
73       t->marked = 1;
74       for (SD_task_t 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 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 t: current){
113         t->marked = 1;
114         for (SD_task_t 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 succ : *t->successors) {
122           if (parents_are_marked(succ))
123             next.push_back(succ);
124         }
125         current.clear();
126         current = next;
127       }
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::unordered_map<std::string, SD_task_t> jobs;
145 static xbt_dict_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   xbt_dict_cursor_t cursor;
162   SD_task_t file;
163   char* name;
164   FILE* in_file = fopen(filename, "r");
165   xbt_assert(in_file, "Unable to open \"%s\"\n", filename);
166   input_buffer = dax__create_buffer(in_file, 10);
167   dax__switch_to_buffer(input_buffer);
168   dax_lineno = 1;
169
170   result = xbt_dynar_new(sizeof(SD_task_t), dax_task_free);
171   files = xbt_dict_new_homogeneous(&dax_task_free);
172   root_task = SD_task_create_comp_seq("root", nullptr, 0);
173   /* by design the root task is always SCHEDULABLE */
174   SD_task_set_state(root_task, SD_SCHEDULABLE);
175
176   xbt_dynar_push(result, &root_task);
177   end_task = SD_task_create_comp_seq("end", nullptr, 0);
178
179   int res = dax_lex();
180   if (res != 0)
181     xbt_die("Parse error in %s: %s", filename, dax__parse_err_msg());
182   dax__delete_buffer(input_buffer);
183   fclose(in_file);
184   dax_lex_destroy();
185
186   /* And now, post-process the files.
187    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
188    * Files not produced in the system are said to be produced by root task (top of DAG).
189    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
190    */
191
192   xbt_dict_foreach(files, cursor, name, file) {
193     SD_task_t newfile;
194     if (file->predecessors->empty()) {
195       for (SD_task_t it : *file->successors) {
196         newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
197         SD_task_dependency_add(nullptr, nullptr, root_task, newfile);
198         SD_task_dependency_add(nullptr, nullptr, newfile, it);
199         xbt_dynar_push(result, &newfile);
200       }
201     } else if (file->successors->empty()) {
202       for (SD_task_t it : *file->predecessors){
203         newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
204         SD_task_dependency_add(nullptr, nullptr, it, newfile);
205         SD_task_dependency_add(nullptr, nullptr, newfile, end_task);
206         xbt_dynar_push(result, &newfile);
207       }
208     } else {
209       for (SD_task_t it : *file->predecessors) {
210         for (SD_task_t it2 : *file->successors) {
211           if (it == it2) {
212             XBT_WARN ("File %s is produced and consumed by task %s."
213                       "This loop dependency will prevent the execution of the task.", file->name, it->name);
214           }
215           newfile = SD_task_create_comm_e2e(file->name, nullptr, file->amount);
216           SD_task_dependency_add(nullptr, nullptr, it, newfile);
217           SD_task_dependency_add(nullptr, nullptr, newfile, it2);
218           xbt_dynar_push(result, &newfile);
219         }
220       }
221     }
222   }
223
224   /* Push end task last */
225   xbt_dynar_push(result, &end_task);
226
227   /* Free previous copy of the files */
228   xbt_dict_free(&files);
229   unsigned int cpt;
230   xbt_dynar_foreach(result, cpt, file) {
231     if (SD_task_get_kind(file) == SD_TASK_COMM_E2E) {
232       uniq_transfer_task_name(file);
233     } else if (SD_task_get_kind(file) == SD_TASK_COMP_SEQ){
234       /* If some tasks do not take files as input, connect them to the root
235        * if they don't produce files, connect them to the end node.
236        */
237       if ((file != root_task) && (file != end_task)) {
238         if (file->inputs->empty())
239           SD_task_dependency_add(nullptr, nullptr, root_task, file);
240         if (file->outputs->empty())
241           SD_task_dependency_add(nullptr, nullptr, file, end_task);
242       }
243     } else {
244       THROW_IMPOSSIBLE;
245     }
246   }
247
248   if (not acyclic_graph_detail(result)) {
249     char* base = xbt_basename(filename);
250     XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.", base);
251     free(base);
252     xbt_dynar_foreach(result, cpt, file)
253       SD_task_destroy(file);
254     xbt_dynar_free_container(&result);
255     return nullptr;
256   } else {
257     return result;
258   }
259 }
260
261 void STag_dax__adag()
262 {
263   try {
264     double version = std::stod(std::string(A_dax__adag_version));
265     xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
266   } catch (std::invalid_argument& ia) {
267     throw std::invalid_argument(std::string("Parse error: ") + A_dax__adag_version + " is not a double");
268   }
269 }
270
271 void STag_dax__job()
272 {
273   try {
274     double runtime = std::stod(std::string(A_dax__job_runtime));
275
276     std::string name = std::string(A_dax__job_id) + "@" + A_dax__job_name;
277     runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
278     XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
279     current_job = SD_task_create_comp_seq(name.c_str(), nullptr, runtime);
280     jobs.insert({A_dax__job_id, current_job});
281     xbt_dynar_push(result, &current_job);
282   } catch (std::invalid_argument& ia) {
283     throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
284   }
285 }
286
287 void STag_dax__uses()
288 {
289   double size;
290   try {
291     size = std::stod(std::string(A_dax__uses_size));
292   } catch (std::invalid_argument& ia) {
293     throw std::invalid_argument(std::string("Parse error: ") + A_dax__uses_size + " is not a double");
294   }
295   bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
296
297   XBT_DEBUG("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
298   SD_task_t file = static_cast<SD_task_t>(xbt_dict_get_or_null(files, A_dax__uses_file));
299   if (file == nullptr) {
300     file = SD_task_create_comm_e2e(A_dax__uses_file, nullptr, size);
301     sd_global->initial_tasks->erase(file);
302     xbt_dict_set(files, A_dax__uses_file, file, nullptr);
303   } else {
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 }