1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
8 #include "simdag/simdag.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_daxparse, sd,"Parsing DAX files");
20 static void dax_parse_error(char *msg) {
21 fprintf(stderr, "Parse error on line %d: %s\n", dax_lineno, msg);
24 static double dax_parse_double(const char *string) {
28 ret = sscanf(string, "%lg", &value);
30 dax_parse_error(bprintf("%s is not a double", string));
33 static int dax_parse_int(const char *string) {
37 ret = sscanf(string, "%d", &value);
39 dax_parse_error(bprintf("%s is not an integer", string));
43 static YY_BUFFER_STATE input_buffer;
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;
51 static void dump_res() {
54 xbt_dynar_foreach(result,cursor,task) {
55 INFO1("Task %d",cursor);
60 static void dax_task_free(void*task){
65 /** @brief loads a DAX file describing a DAG
67 * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator
70 xbt_dynar_t SD_daxload(const char*filename) {
71 FILE* in_file = fopen(filename,"r");
72 xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
73 input_buffer = dax__create_buffer(in_file, 10);
74 dax__switch_to_buffer(input_buffer);
77 result = xbt_dynar_new(sizeof(SD_task_t),dax_task_free);
80 root_task = SD_task_create_comp_seq("root",NULL,0);
81 xbt_dynar_push(result,&root_task);
82 end_task = SD_task_create_comp_seq("end",NULL,0);
84 xbt_assert2(!dax_lex(),"Parse error in %s: %s",filename,dax__parse_err_msg());
85 dax__delete_buffer(input_buffer);
89 /* And now, post-process the files.
90 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
91 * Files not produced in the system are said to be produced by root task (top of DAG).
92 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
94 xbt_dict_cursor_t cursor;
97 xbt_dict_foreach(files,cursor,name,file) {
98 unsigned int cpt1,cpt2;
99 SD_dependency_t depbefore,depafter;
100 if (xbt_dynar_length(file->tasks_before) == 0) {
101 xbt_dynar_foreach(file->tasks_after,cpt2,depafter) {
102 SD_task_t newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
103 SD_task_dependency_add(NULL,NULL,root_task,newfile);
104 SD_task_dependency_add(NULL,NULL,newfile,depafter->dst);
105 xbt_dynar_push(result,&newfile);
107 } else if (xbt_dynar_length(file->tasks_after) == 0) {
108 xbt_dynar_foreach(file->tasks_before,cpt2,depbefore) {
109 SD_task_t newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
110 SD_task_dependency_add(NULL,NULL,depbefore->src,newfile);
111 SD_task_dependency_add(NULL,NULL,newfile,end_task);
112 xbt_dynar_push(result,&newfile);
115 xbt_dynar_foreach(file->tasks_before,cpt1,depbefore) {
116 xbt_dynar_foreach(file->tasks_after,cpt2,depafter) {
117 if (depbefore->src == depafter->dst) {
118 WARN2("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
119 file->name,depbefore->src->name);
121 SD_task_t newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
122 SD_task_dependency_add(NULL,NULL,depbefore->src,newfile);
123 SD_task_dependency_add(NULL,NULL,newfile,depafter->dst);
124 xbt_dynar_push(result,&newfile);
130 /* Push end task last */
131 xbt_dynar_push(result,&end_task);
133 /* Free previous copy of the files */
134 xbt_dict_free(&files);
139 void STag_dax__adag(void) {
140 double version = dax_parse_double(A_dax__adag_version);
142 xbt_assert1((version == 2.1), "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file",version);
144 void STag_dax__job(void) {
145 double runtime = dax_parse_double(A_dax__job_runtime);
146 char *name=bprintf("%s@%s",A_dax__job_id,A_dax__job_name);
147 runtime*=4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
148 // INFO3("See <job id=%s runtime=%s %.0f>",A_dax__job_id,A_dax__job_runtime,runtime);
149 current_job = SD_task_create_comp_seq(name,NULL,runtime);
150 xbt_dict_set(jobs,A_dax__job_id,current_job,NULL);
152 xbt_dynar_push(result,¤t_job);
154 void STag_dax__uses(void) {
156 double size = dax_parse_double(A_dax__uses_size);
157 int is_input = (A_dax__uses_link == A_dax__uses_link_input);
159 // INFO2("See <uses file=%s %s>",A_dax__uses_file,(is_input?"in":"out"));
160 file = xbt_dict_get_or_null(files,A_dax__uses_file);
162 file = SD_task_create_comm_e2e(A_dax__uses_file,NULL,size);
163 xbt_dict_set(files,A_dax__uses_file,file,&dax_task_free);
165 if (SD_task_get_amount(file)!=size) {
166 WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
167 A_dax__uses_file,SD_task_get_amount(file),size);
171 SD_task_dependency_add(NULL,NULL,file,current_job);
173 SD_task_dependency_add(NULL,NULL,current_job,file);
174 if (xbt_dynar_length(file->tasks_before)>1) {
175 WARN1("File %s created at more than one location...",file->name);
179 static SD_task_t current_child;
180 void STag_dax__child(void) {
181 current_child = xbt_dict_get_or_null(jobs,A_dax__child_ref);
182 if (current_child==NULL)
183 dax_parse_error(bprintf("Asked to add dependencies to the non-existent %s task",A_dax__child_ref));
185 void ETag_dax__child(void) {
188 void STag_dax__parent(void) {
189 SD_task_t parent = xbt_dict_get_or_null(jobs,A_dax__parent_ref);
191 dax_parse_error(bprintf("Asked to add a dependency from %s to %s, but %s does not exist",
192 current_child->name,A_dax__parent_ref,A_dax__parent_ref));
193 SD_task_dependency_add(NULL,NULL,parent,current_child);
194 DEBUG2("Control-flow dependency from %s to %s", current_child->name,parent->name);
196 void ETag_dax__adag(void) {
197 // INFO0("See </adag>");
199 void ETag_dax__job(void) {
201 // INFO0("See </job>");
203 void ETag_dax__parent(void) {
204 // INFO0("See </parent>");
206 void ETag_dax__uses(void) {
207 // INFO0("See </uses>");