Logo AND Algorithmique Numérique Distribuée

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