Logo AND Algorithmique Numérique Distribuée

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