Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
test of sd_test
[simgrid.git] / src / simdag / sd_daxloader.c
1 /* Copyright (c) 2009, 2010. 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 "private.h"
8 #include "simdag/simdag.h"
9 #include "xbt/misc.h"
10 #include "xbt/log.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_daxparse, sd,"Parsing DAX files");
13
14 #undef CLEANUP
15 #include "dax_dtd.h"
16 #include "dax_dtd.c"
17
18
19 /* Parsing helpers */
20 static void dax_parse_error(char *msg) {
21   fprintf(stderr, "Parse error on line %d: %s\n", dax_lineno, msg);
22   abort();
23 }
24 static double dax_parse_double(const char *string) {
25   int ret = 0;
26   double value;
27
28   ret = sscanf(string, "%lg", &value);
29   if (ret != 1)
30     dax_parse_error(bprintf("%s is not a double", string));
31   return value;
32 }
33 static int dax_parse_int(const char *string) {
34   int ret = 0;
35   int value;
36
37   ret = sscanf(string, "%d", &value);
38   if (ret != 1)
39     dax_parse_error(bprintf("%s is not an integer", string));
40   return value;
41 }
42
43 /* Ensure that transfer tasks have unique names even though a file is used
44  * several times */
45
46 void uniq_transfer_task_name(SD_task_t task){
47         SD_task_t child, parent;
48         xbt_dynar_t children, parents;
49         char *new_name;
50
51         children = SD_task_get_children(task);
52         parents = SD_task_get_parents(task);
53
54         xbt_dynar_get_cpy(children,0,&child);
55         xbt_dynar_get_cpy(parents,0,&parent);
56
57         new_name = bprintf("%s_%s_%s",
58                         SD_task_get_name(parent),
59                         SD_task_get_name(task),
60                         SD_task_get_name(child));
61
62         SD_task_set_name (task, new_name);
63
64         xbt_dynar_free_container(&children);
65         xbt_dynar_free_container(&parents);
66         free(new_name);
67 }
68
69
70 static YY_BUFFER_STATE input_buffer;
71
72 static xbt_dynar_t result;
73 static xbt_dict_t jobs;
74 static xbt_dict_t files;
75 static SD_task_t current_job;
76 static SD_task_t root_task,end_task;
77
78 static void dump_res() {
79   unsigned int cursor;
80   SD_task_t task;
81   xbt_dynar_foreach(result,cursor,task) {
82     INFO1("Task %d",cursor);
83     SD_task_dump(task);
84   }
85 }
86
87 static void dax_task_free(void*task){
88   SD_task_t t=task;
89   SD_task_destroy(t);
90 }
91
92 /** @brief loads a DAX file describing a DAG
93  * 
94  * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator
95  * for more details.
96  */
97 xbt_dynar_t SD_daxload(const char*filename) {
98   xbt_dict_cursor_t cursor;
99   SD_task_t file;
100   char *name;
101   FILE* in_file = fopen(filename,"r");
102   xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
103   input_buffer = dax__create_buffer(in_file, 10);
104   dax__switch_to_buffer(input_buffer);
105   dax_lineno = 1;
106
107   result = xbt_dynar_new(sizeof(SD_task_t),dax_task_free);
108   files=xbt_dict_new();
109   jobs=xbt_dict_new();
110   root_task = SD_task_create_comp_seq("root",NULL,0);
111   /* by design the root task is always SCHEDULABLE */
112   __SD_task_set_state(root_task, SD_SCHEDULABLE);
113
114   xbt_dynar_push(result,&root_task);
115   end_task = SD_task_create_comp_seq("end",NULL,0);
116
117   xbt_assert2(!dax_lex(),"Parse error in %s: %s",filename,dax__parse_err_msg());
118   dax__delete_buffer(input_buffer);
119   fclose(in_file);
120   xbt_dict_free(&jobs);
121
122   /* And now, post-process the files.
123    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
124    * Files not produced in the system are said to be produced by root task (top of DAG).
125    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
126    */
127
128   xbt_dict_foreach(files,cursor,name,file) {
129     unsigned int cpt1,cpt2;
130     SD_task_t newfile = NULL;
131     SD_dependency_t depbefore,depafter;
132     if (xbt_dynar_length(file->tasks_before) == 0) {
133       xbt_dynar_foreach(file->tasks_after,cpt2,depafter) {
134         SD_task_t newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
135         SD_task_dependency_add(NULL,NULL,root_task,newfile);
136         SD_task_dependency_add(NULL,NULL,newfile,depafter->dst);
137         xbt_dynar_push(result,&newfile);
138       }
139     } else if (xbt_dynar_length(file->tasks_after) == 0) {
140       xbt_dynar_foreach(file->tasks_before,cpt2,depbefore) {
141         SD_task_t newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
142         SD_task_dependency_add(NULL,NULL,depbefore->src,newfile);
143         SD_task_dependency_add(NULL,NULL,newfile,end_task);
144         xbt_dynar_push(result,&newfile);
145       }
146     } else {
147       xbt_dynar_foreach(file->tasks_before,cpt1,depbefore) {
148         xbt_dynar_foreach(file->tasks_after,cpt2,depafter) {
149           if (depbefore->src == depafter->dst) {
150             WARN2("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
151                 file->name,depbefore->src->name);
152           }
153           newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
154           SD_task_dependency_add(NULL,NULL,depbefore->src,newfile);
155           SD_task_dependency_add(NULL,NULL,newfile,depafter->dst);
156           xbt_dynar_push(result,&newfile);
157         }
158       }
159     }
160   }
161
162   /* Push end task last */
163   xbt_dynar_push(result,&end_task);
164
165   /* Free previous copy of the files */
166   xbt_dict_free(&files);
167   unsigned int cpt;
168   xbt_dynar_foreach(result,cpt,file) {
169         if (SD_task_get_kind(file)==SD_TASK_COMM_E2E){
170                 uniq_transfer_task_name(file);
171         }
172   }
173
174   return result;
175 }
176
177 void STag_dax__adag(void) {
178   double version = dax_parse_double(A_dax__adag_version);
179
180   xbt_assert1((version == 2.1), "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file",version);
181 }
182 void STag_dax__job(void) {
183   double runtime = dax_parse_double(A_dax__job_runtime);
184   char *name=bprintf("%s@%s",A_dax__job_id,A_dax__job_name);
185   runtime*=4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
186 //  INFO3("See <job id=%s runtime=%s %.0f>",A_dax__job_id,A_dax__job_runtime,runtime);
187   current_job = SD_task_create_comp_seq(name,NULL,runtime);
188   xbt_dict_set(jobs,A_dax__job_id,current_job,NULL);
189   free(name);
190   xbt_dynar_push(result,&current_job);
191 }
192 void STag_dax__uses(void) {
193   SD_task_t file;
194   double size = dax_parse_double(A_dax__uses_size);
195   int is_input = (A_dax__uses_link == A_dax__uses_link_input);
196
197 //  INFO2("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
198   file = xbt_dict_get_or_null(files,A_dax__uses_file);
199   if (file==NULL) {
200     file = SD_task_create_comm_e2e(A_dax__uses_file,NULL,size);
201     xbt_dict_set(files,A_dax__uses_file,file,&dax_task_free);
202   } else {
203     if (SD_task_get_amount(file)!=size) {
204       WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
205           A_dax__uses_file,SD_task_get_amount(file),size);
206     }
207   }
208   if (is_input) {
209     SD_task_dependency_add(NULL,NULL,file,current_job);
210   } else {
211     SD_task_dependency_add(NULL,NULL,current_job,file);
212     if (xbt_dynar_length(file->tasks_before)>1) {
213       WARN1("File %s created at more than one location...",file->name);
214     }
215   }
216 }
217 static SD_task_t current_child;
218 void STag_dax__child(void) {
219   current_child = xbt_dict_get_or_null(jobs,A_dax__child_ref);
220   if (current_child==NULL)
221     dax_parse_error(bprintf("Asked to add dependencies to the non-existent %s task",A_dax__child_ref));
222 }
223 void ETag_dax__child(void) {
224   current_child=NULL;
225 }
226 void STag_dax__parent(void) {
227   SD_task_t parent = xbt_dict_get_or_null(jobs,A_dax__parent_ref);
228   if (parent == NULL)
229     dax_parse_error(bprintf("Asked to add a dependency from %s to %s, but %s does not exist",
230         current_child->name,A_dax__parent_ref,A_dax__parent_ref));
231   SD_task_dependency_add(NULL,NULL,parent,current_child);
232   DEBUG2("Control-flow dependency from %s to %s", current_child->name,parent->name);
233 }
234 void ETag_dax__adag(void) {
235 //  INFO0("See </adag>");
236 }
237 void ETag_dax__job(void) {
238   current_job = NULL;
239 //  INFO0("See </job>");
240 }
241 void ETag_dax__parent(void) {
242 //  INFO0("See </parent>");
243 }
244 void ETag_dax__uses(void) {
245 //  INFO0("See </uses>");
246 }