Logo AND Algorithmique Numérique Distribuée

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