Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Delete the dependencies with stdio in simdag.h. Correction and add of comments.
[simgrid.git] / src / simdag / sd_dotloader.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_dotparse, sd,"Parsing DOT files");
13
14 #undef CLEANUP
15 #include <graphviz/cgraph.h>
16
17 static double dot_parse_double(const char *string) {
18     if (string == NULL) return -10;
19     int ret = 0;
20     double value;
21
22     ret = sscanf(string, "%lg", &value);
23     if (ret != 1)
24         WARN1("%s is not a double", string);
25     return value;
26 }
27 static int dot_parse_int(const char *string) {
28     if (string == NULL) return -10;
29     int ret = 0;
30     int value;
31
32     ret = sscanf(string, "%d", &value);
33     if (ret != 1)
34         WARN1("%s is not an integer", string);
35     return value;
36 }
37
38 static xbt_dynar_t result;
39 static xbt_dict_t jobs;
40 static xbt_dict_t files;
41 static SD_task_t root_task,end_task;
42 static Agraph_t * dag_dot;
43
44 static void dump_res() {
45     unsigned int cursor;
46     SD_task_t task;
47     xbt_dynar_foreach(result,cursor,task) {
48         INFO1("Task %d",cursor);
49         SD_task_dump(task);
50     }
51 }
52
53 static void dot_task_free(void*task){
54     SD_task_t t=task;
55     SD_task_destroy(t);
56 }
57 void dot_add_task(Agnode_t *dag_node) ;
58 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t *edge) ;
59 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t *edge) ;
60
61 /** @brief loads a DOT file describing a DAG
62  * 
63  * See http://www.graphviz.org/doc/info/lang.html
64  * for more details.
65  * To obtain information about transfers and tasks, two attributes are
66  * required : size on task (execution time in Flop) and size on edge
67  * (the amount of data transfer in bit).
68  * if they aren't here, there choose to be equal to zero.
69  */
70 xbt_dynar_t SD_dotload(const char*filename){
71     FILE* in_file = fopen(filename,"r");
72     xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
73     SD_dotload_FILE(in_file);
74     fclose(in_file);
75     return result;
76 }
77
78 xbt_dynar_t SD_dotload_FILE(FILE* in_file){
79     xbt_assert0(in_file, "Unable to use a null file descriptor\n");
80     dag_dot = agread(in_file,NIL(Agdisc_t*));
81
82     result = xbt_dynar_new(sizeof(SD_task_t),dot_task_free);
83     files=xbt_dict_new();
84     jobs=xbt_dict_new();
85     root_task = SD_task_create_comp_seq("root",NULL,0);
86     /* by design the root task is always SCHEDULABLE */
87     __SD_task_set_state(root_task, SD_SCHEDULABLE);
88
89     xbt_dynar_push(result,&root_task);
90     end_task = SD_task_create_comp_seq("end",NULL,0);
91
92     Agnode_t *dag_node   = NULL;
93     for (dag_node = agfstnode(dag_dot); dag_node; dag_node = agnxtnode(dag_dot,dag_node)){      
94         dot_add_task(dag_node); 
95     }
96     agclose(dag_dot);
97     xbt_dict_free(&jobs);
98
99     /* And now, post-process the files.
100      * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
101      * Files not produced in the system are said to be produced by root task (top of DAG).
102      * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
103      */
104     xbt_dict_cursor_t cursor;
105     SD_task_t file;
106     char *name;
107     xbt_dict_foreach(files,cursor,name,file) {
108         unsigned int cpt1,cpt2;
109         SD_task_t newfile = NULL;
110         SD_dependency_t depbefore,depafter;
111         if (xbt_dynar_length(file->tasks_before) == 0) {
112             xbt_dynar_foreach(file->tasks_after,cpt2,depafter) {
113                 SD_task_t newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
114                 SD_task_dependency_add(NULL,NULL,root_task,newfile);
115                 SD_task_dependency_add(NULL,NULL,newfile,depafter->dst);
116                 xbt_dynar_push(result,&newfile);
117             }
118         } else if (xbt_dynar_length(file->tasks_after) == 0) {
119             xbt_dynar_foreach(file->tasks_before,cpt2,depbefore) {
120                 SD_task_t newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
121                 SD_task_dependency_add(NULL,NULL,depbefore->src,newfile);
122                 SD_task_dependency_add(NULL,NULL,newfile,end_task);
123                 xbt_dynar_push(result,&newfile);
124             }
125         } else {
126             xbt_dynar_foreach(file->tasks_before,cpt1,depbefore) {
127                 xbt_dynar_foreach(file->tasks_after,cpt2,depafter) {
128                     if (depbefore->src == depafter->dst) {
129                         WARN2("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
130                               file->name,depbefore->src->name);
131                     }
132                     newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
133                     SD_task_dependency_add(NULL,NULL,depbefore->src,newfile);
134                     SD_task_dependency_add(NULL,NULL,newfile,depafter->dst);
135                     xbt_dynar_push(result,&newfile);
136                 }
137             }
138         }
139     }
140
141     /* Push end task last */
142     xbt_dynar_push(result,&end_task);
143
144     /* Free previous copy of the files */
145     xbt_dict_free(&files);
146
147     return result;
148 }
149
150 /* dot_add_task create a sd_task and all transfers required for this
151  * task. The execution time of the task is given by the attribute size.
152  * The unit of size is the Flop.*/
153 void dot_add_task(Agnode_t *dag_node) {
154     char *name = agnameof(dag_node);
155     double runtime = dot_parse_double(agget(dag_node,(char*)"size"));
156     long performer = (long)dot_parse_int((char *) agget(dag_node,(char*)"performer"));
157     INFO3("See <job id=%s runtime=%s %.0f>",name,agget(dag_node,(char*)"size"),runtime);
158     SD_task_t current_job = SD_task_create_comp_seq(name,(void*)performer,runtime);
159     xbt_dict_set(jobs,name,current_job,NULL);
160     xbt_dynar_push(result,&current_job);
161     Agedge_t    *e;
162     int count = 0;
163     for (e = agfstin(dag_dot,dag_node); e; e = agnxtin(dag_dot,e)) {
164         dot_add_input_dependencies(current_job,e);
165         count++;
166     }
167     if (count==0){
168         SD_task_t file;
169         char *name= (char*)"root->many";
170         double size = 0;
171
172         file = xbt_dict_get_or_null(files,name);
173         if (file==NULL) {
174             file = SD_task_create_comm_e2e(name,NULL,size);
175             xbt_dict_set(files,name,file,&dot_task_free);
176         } else {
177             if (SD_task_get_amount(file)!=size) {
178                 WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
179                       name,SD_task_get_amount(file),size);
180             }
181         }
182         SD_task_dependency_add(NULL,NULL,file,current_job);
183     }
184     count = 0;
185     for (e = agfstout(dag_dot,dag_node); e; e = agnxtout(dag_dot,e)) {
186         dot_add_output_dependencies(current_job,e);
187         count++;
188     }
189     if (count==0){
190         SD_task_t file;
191         char *name = (char*)"many->end";
192         double size = 0;
193
194         //  INFO2("See <uses file=%s %s>",A_dot__uses_file,(is_input?"in":"out"));
195         file = xbt_dict_get_or_null(files,name);
196         if (file==NULL) {
197             file = SD_task_create_comm_e2e(name,NULL,size);
198             xbt_dict_set(files,name,file,&dot_task_free);
199         } else {
200             if (SD_task_get_amount(file)!=size) {
201                 WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
202                       name,SD_task_get_amount(file),size);
203             }
204         }
205         SD_task_dependency_add(NULL,NULL,current_job,file);
206         if (xbt_dynar_length(file->tasks_before)>1) {
207             WARN1("File %s created at more than one location...",file->name);
208         }
209     }
210 }
211
212 /* dot_add_output_dependencies create the dependencies between a task
213  * and a transfers. This is given by the edges in the dot file. 
214  * The amount of data transfers is given by the attribute size on the
215  * edge. */
216 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t *edge) {
217     SD_task_t file;
218
219     char name[80];
220     sprintf(name ,"%s->%s",agnameof(agtail(edge)) ,agnameof(aghead(edge)));
221     double size = dot_parse_double(agget(edge,(char*)"size"));
222     //int sender = dot_parse_int(agget(edge,(char*)"sender"));
223     //int reciever = dot_parse_int(agget(edge,(char*)"reciever"));
224
225     file = xbt_dict_get_or_null(files,name);
226     if (file==NULL) {
227         file = SD_task_create_comm_e2e(name,NULL,size);
228         xbt_dict_set(files,name,file,&dot_task_free);
229     } else {
230         if (SD_task_get_amount(file)!=size) {
231             WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
232                   name,SD_task_get_amount(file),size);
233         }
234     }
235     SD_task_dependency_add(NULL,NULL,file,current_job);
236 }
237
238 /* dot_add_output_dependencies create the dependencies between a
239  * transfers and a task. This is given by the edges in the dot file.
240  * The amount of data transfers is given by the attribute size on the
241  * edge. */
242 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t *edge) {
243     SD_task_t file;
244     char name[80];
245     sprintf(name ,"%s->%s",agnameof(agtail(edge)) ,agnameof(aghead(edge)));
246     double size = dot_parse_double(agget(edge,(char*)"size"));
247     //int sender = dot_parse_int(agget(edge,(char*)"sender"));
248     //int reciever = dot_parse_int(agget(edge,(char*)"reciever"));
249
250     //INFO2("See <uses file=%s %s>",A_dot__uses_file,(is_input?"in":"out"));
251     file = xbt_dict_get_or_null(files,name);
252     if (file==NULL) {
253         file = SD_task_create_comm_e2e(name,NULL,size);
254         xbt_dict_set(files,name,file,&dot_task_free);
255     } else {
256         if (SD_task_get_amount(file)!=size) {
257             WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
258                   name,SD_task_get_amount(file),size);
259         }
260     }
261     SD_task_dependency_add(NULL,NULL,current_job,file);
262     if (xbt_dynar_length(file->tasks_before)>1) {
263         WARN1("File %s created at more than one location...",file->name);
264     }
265 }
266