Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
007b233097ac0c6748811a88dd6bcb2d8adf3455
[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 static YY_BUFFER_STATE input_buffer;
44
45 static xbt_dynar_t result;
46 static xbt_dict_t jobs;
47 static xbt_dict_t files;
48 static SD_task_t current_job;
49 static SD_task_t root_task,end_task;
50
51 static void dump_res() {
52   unsigned int cursor;
53   SD_task_t task;
54   xbt_dynar_foreach(result,cursor,task) {
55     INFO1("Task %d",cursor);
56     SD_task_dump(task);
57   }
58 }
59
60 static void dax_task_free(void*task){
61   SD_task_t t=task;
62   SD_task_destroy(t);
63 }
64
65 /** @brief loads a DAX file describing a DAG
66  * 
67  * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator
68  * for more details.
69  */
70 xbt_dynar_t SD_daxload(const char*filename) {
71   xbt_dict_cursor_t cursor;
72   SD_task_t file;
73   char *name;
74   FILE* in_file = fopen(filename,"r");
75   xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
76   input_buffer = dax__create_buffer(in_file, 10);
77   dax__switch_to_buffer(input_buffer);
78   dax_lineno = 1;
79
80   result = xbt_dynar_new(sizeof(SD_task_t),dax_task_free);
81   files=xbt_dict_new();
82   jobs=xbt_dict_new();
83   root_task = SD_task_create_comp_seq("root",NULL,0);
84   /* by design the root task is always READY */
85   __SD_task_set_state(root_task, SD_READY);
86
87   xbt_dynar_push(result,&root_task);
88   end_task = SD_task_create_comp_seq("end",NULL,0);
89
90   xbt_assert2(!dax_lex(),"Parse error in %s: %s",filename,dax__parse_err_msg());
91   dax__delete_buffer(input_buffer);
92   fclose(in_file);
93   xbt_dict_free(&jobs);
94
95   /* And now, post-process the files.
96    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
97    * Files not produced in the system are said to be produced by root task (top of DAG).
98    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
99    */
100
101   xbt_dict_foreach(files,cursor,name,file) {
102     unsigned int cpt1,cpt2;
103     SD_task_t newfile = NULL;
104     SD_dependency_t depbefore,depafter;
105     if (xbt_dynar_length(file->tasks_before) == 0) {
106       xbt_dynar_foreach(file->tasks_after,cpt2,depafter) {
107         SD_task_t newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
108         SD_task_dependency_add(NULL,NULL,root_task,newfile);
109         SD_task_dependency_add(NULL,NULL,newfile,depafter->dst);
110         xbt_dynar_push(result,&newfile);
111       }
112     } else if (xbt_dynar_length(file->tasks_after) == 0) {
113       xbt_dynar_foreach(file->tasks_before,cpt2,depbefore) {
114         SD_task_t newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
115         SD_task_dependency_add(NULL,NULL,depbefore->src,newfile);
116         SD_task_dependency_add(NULL,NULL,newfile,end_task);
117         xbt_dynar_push(result,&newfile);
118       }
119     } else {
120       xbt_dynar_foreach(file->tasks_before,cpt1,depbefore) {
121         xbt_dynar_foreach(file->tasks_after,cpt2,depafter) {
122           if (depbefore->src == depafter->dst) {
123             WARN2("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
124                 file->name,depbefore->src->name);
125           }
126           newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
127           SD_task_dependency_add(NULL,NULL,depbefore->src,newfile);
128           SD_task_dependency_add(NULL,NULL,newfile,depafter->dst);
129           xbt_dynar_push(result,&newfile);
130         }
131       }
132     }
133   }
134
135   /* Push end task last */
136   xbt_dynar_push(result,&end_task);
137
138   /* Free previous copy of the files */
139   xbt_dict_free(&files);
140
141   return result;
142 }
143
144 void STag_dax__adag(void) {
145   double version = dax_parse_double(A_dax__adag_version);
146
147   xbt_assert1((version == 2.1), "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file",version);
148 }
149 void STag_dax__job(void) {
150   double runtime = dax_parse_double(A_dax__job_runtime);
151   char *name=bprintf("%s@%s",A_dax__job_id,A_dax__job_name);
152   runtime*=4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
153 //  INFO3("See <job id=%s runtime=%s %.0f>",A_dax__job_id,A_dax__job_runtime,runtime);
154   current_job = SD_task_create_comp_seq(name,NULL,runtime);
155   xbt_dict_set(jobs,A_dax__job_id,current_job,NULL);
156   free(name);
157   xbt_dynar_push(result,&current_job);
158 }
159 void STag_dax__uses(void) {
160   SD_task_t file;
161   double size = dax_parse_double(A_dax__uses_size);
162   int is_input = (A_dax__uses_link == A_dax__uses_link_input);
163
164 //  INFO2("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
165   file = xbt_dict_get_or_null(files,A_dax__uses_file);
166   if (file==NULL) {
167     file = SD_task_create_comm_e2e(A_dax__uses_file,NULL,size);
168     xbt_dict_set(files,A_dax__uses_file,file,&dax_task_free);
169   } else {
170     if (SD_task_get_amount(file)!=size) {
171       WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
172           A_dax__uses_file,SD_task_get_amount(file),size);
173     }
174   }
175   if (is_input) {
176     SD_task_dependency_add(NULL,NULL,file,current_job);
177   } else {
178     SD_task_dependency_add(NULL,NULL,current_job,file);
179     if (xbt_dynar_length(file->tasks_before)>1) {
180       WARN1("File %s created at more than one location...",file->name);
181     }
182   }
183 }
184 static SD_task_t current_child;
185 void STag_dax__child(void) {
186   current_child = xbt_dict_get_or_null(jobs,A_dax__child_ref);
187   if (current_child==NULL)
188     dax_parse_error(bprintf("Asked to add dependencies to the non-existent %s task",A_dax__child_ref));
189 }
190 void ETag_dax__child(void) {
191   current_child=NULL;
192 }
193 void STag_dax__parent(void) {
194   SD_task_t parent = xbt_dict_get_or_null(jobs,A_dax__parent_ref);
195   if (parent == NULL)
196     dax_parse_error(bprintf("Asked to add a dependency from %s to %s, but %s does not exist",
197         current_child->name,A_dax__parent_ref,A_dax__parent_ref));
198   SD_task_dependency_add(NULL,NULL,parent,current_child);
199   DEBUG2("Control-flow dependency from %s to %s", current_child->name,parent->name);
200 }
201 void ETag_dax__adag(void) {
202 //  INFO0("See </adag>");
203 }
204 void ETag_dax__job(void) {
205   current_job = NULL;
206 //  INFO0("See </job>");
207 }
208 void ETag_dax__parent(void) {
209 //  INFO0("See </parent>");
210 }
211 void ETag_dax__uses(void) {
212 //  INFO0("See </uses>");
213 }