Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
avoid the complilation of dot_test.c if graphviz wasn't found
[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 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 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 Start_dot_job(Agnode_t *dag_node) ;
58 void input_edge(SD_task_t current_job, Agedge_t *edge) ;
59 void output_edge(SD_task_t current_job, Agedge_t *edge) ;
60
61 /** @brief loads a DAX file describing a DAG
62  * 
63  * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator
64  * for more details.
65  */
66 xbt_dynar_t SD_dotload(const char*filename){
67         FILE* in_file = fopen(filename,"r");
68         xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
69         SD_dotload_FILE(in_file);
70         fclose(in_file);
71         return result;
72 }
73
74 xbt_dynar_t SD_dotload_FILE(FILE* in_file){
75         xbt_assert0(in_file, "Unable to use a null file descriptor\n");
76         dag_dot = agread(in_file,NIL(Agdisc_t*));
77
78         result = xbt_dynar_new(sizeof(SD_task_t),dot_task_free);
79         files=xbt_dict_new();
80         jobs=xbt_dict_new();
81         root_task = SD_task_create_comp_seq("root",NULL,0);
82         /* by design the root task is always SCHEDULABLE */
83         __SD_task_set_state(root_task, SD_SCHEDULABLE);
84
85         xbt_dynar_push(result,&root_task);
86         end_task = SD_task_create_comp_seq("end",NULL,0);
87
88         Agnode_t *dag_node   = NULL;
89         SD_task_t  task_node  = NULL;
90         for (dag_node = agfstnode(dag_dot); dag_node; dag_node = agnxtnode(dag_dot,dag_node)){  
91                 Start_dot_job(dag_node);        
92         }
93         agclose(dag_dot);
94         xbt_dict_free(&jobs);
95
96         /* And now, post-process the files.
97          * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
98          * Files not produced in the system are said to be produced by root task (top of DAG).
99          * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
100          */
101         xbt_dict_cursor_t cursor;
102         SD_task_t file;
103         char *name;
104         xbt_dict_foreach(files,cursor,name,file) {
105                 unsigned int cpt1,cpt2;
106                 SD_task_t newfile = NULL;
107                 SD_dependency_t depbefore,depafter;
108                 if (xbt_dynar_length(file->tasks_before) == 0) {
109                         xbt_dynar_foreach(file->tasks_after,cpt2,depafter) {
110                                 SD_task_t newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
111                                 SD_task_dependency_add(NULL,NULL,root_task,newfile);
112                                 SD_task_dependency_add(NULL,NULL,newfile,depafter->dst);
113                                 xbt_dynar_push(result,&newfile);
114                         }
115                 } else if (xbt_dynar_length(file->tasks_after) == 0) {
116                         xbt_dynar_foreach(file->tasks_before,cpt2,depbefore) {
117                                 SD_task_t newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
118                                 SD_task_dependency_add(NULL,NULL,depbefore->src,newfile);
119                                 SD_task_dependency_add(NULL,NULL,newfile,end_task);
120                                 xbt_dynar_push(result,&newfile);
121                         }
122                 } else {
123                         xbt_dynar_foreach(file->tasks_before,cpt1,depbefore) {
124                                 xbt_dynar_foreach(file->tasks_after,cpt2,depafter) {
125                                         if (depbefore->src == depafter->dst) {
126                                                 WARN2("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
127                                                                 file->name,depbefore->src->name);
128                                         }
129                                         newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
130                                         SD_task_dependency_add(NULL,NULL,depbefore->src,newfile);
131                                         SD_task_dependency_add(NULL,NULL,newfile,depafter->dst);
132                                         xbt_dynar_push(result,&newfile);
133                                 }
134                         }
135                 }
136         }
137
138         /* Push end task last */
139         xbt_dynar_push(result,&end_task);
140
141         /* Free previous copy of the files */
142         xbt_dict_free(&files);
143
144         //INFO2("result : %lld, dot : %lld",result,*dot);
145         return result;
146 }
147
148 void Start_dot_job(Agnode_t *dag_node) {
149         char *name = agnameof(dag_node);
150         double runtime = parse_double(agget(dag_node,"size"));
151         int performer = parse_int(agget(dag_node,"performer"));
152         INFO3("See <job id=%s runtime=%s %.0f>",name,agget(dag_node,"size"),runtime);
153         SD_task_t current_job = SD_task_create_comp_seq(name,(void*)performer,runtime);
154         xbt_dict_set(jobs,name,current_job,NULL);
155         xbt_dynar_push(result,&current_job);
156         Agedge_t    *e;
157         int count = 0;
158         for (e = agfstin(dag_dot,dag_node); e; e = agnxtin(dag_dot,e)) {
159                 input_edge(current_job,e);
160                 count++;
161         }
162         if (count==0){
163                 SD_task_t file;
164                 char *name="root->many";
165                 double size = 0;
166
167                 file = xbt_dict_get_or_null(files,name);
168                 if (file==NULL) {
169                         file = SD_task_create_comm_e2e(name,NULL,size);
170                         xbt_dict_set(files,name,file,&dot_task_free);
171                 } else {
172                         if (SD_task_get_amount(file)!=size) {
173                                 WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
174                                                 name,SD_task_get_amount(file),size);
175                         }
176                 }
177                 SD_task_dependency_add(NULL,NULL,file,current_job);
178         }
179         count = 0;
180         for (e = agfstout(dag_dot,dag_node); e; e = agnxtout(dag_dot,e)) {
181                 output_edge(current_job,e);
182                 count++;
183         }
184         if (count==0){
185                 SD_task_t file;
186                 char *name = "many->end";
187                 double size = 0;
188
189                 //  INFO2("See <uses file=%s %s>",A_dot__uses_file,(is_input?"in":"out"));
190                 file = xbt_dict_get_or_null(files,name);
191                 if (file==NULL) {
192                         file = SD_task_create_comm_e2e(name,NULL,size);
193                         xbt_dict_set(files,name,file,&dot_task_free);
194                 } else {
195                         if (SD_task_get_amount(file)!=size) {
196                                 WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
197                                                 name,SD_task_get_amount(file),size);
198                         }
199                 }
200                 SD_task_dependency_add(NULL,NULL,current_job,file);
201                 if (xbt_dynar_length(file->tasks_before)>1) {
202                         WARN1("File %s created at more than one location...",file->name);
203                 }
204         }
205 }
206
207 void input_edge(SD_task_t current_job, Agedge_t *edge) {
208         SD_task_t file;
209
210         char name[80];
211         sprintf(name ,"%s->%s",agnameof(agtail(edge)) ,agnameof(aghead(edge)));
212         double size = parse_double(agget(edge,"size"));
213         int sender = parse_int(agget(edge,"sender"));
214         int reciever = parse_int(agget(edge,"reciever"));
215
216         file = xbt_dict_get_or_null(files,name);
217         if (file==NULL) {
218                 file = SD_task_create_comm_e2e(name,NULL,size);
219                 xbt_dict_set(files,name,file,&dot_task_free);
220         } else {
221                 if (SD_task_get_amount(file)!=size) {
222                         WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
223                                         name,SD_task_get_amount(file),size);
224                 }
225         }
226         SD_task_dependency_add(NULL,NULL,file,current_job);
227 }
228
229 void output_edge(SD_task_t current_job, Agedge_t *edge) {
230         SD_task_t file;
231         char name[80];
232         sprintf(name ,"%s->%s",agnameof(agtail(edge)) ,agnameof(aghead(edge)));
233         double size = parse_double(agget(edge,"size"));
234         int sender = parse_int(agget(edge,"sender"));
235         int reciever = parse_int(agget(edge,"reciever"));
236
237         //  INFO2("See <uses file=%s %s>",A_dot__uses_file,(is_input?"in":"out"));
238         file = xbt_dict_get_or_null(files,name);
239         if (file==NULL) {
240                 file = SD_task_create_comm_e2e(name,NULL,size);
241                 xbt_dict_set(files,name,file,&dot_task_free);
242         } else {
243                 if (SD_task_get_amount(file)!=size) {
244                         WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
245                                         name,SD_task_get_amount(file),size);
246                 }
247         }
248         SD_task_dependency_add(NULL,NULL,current_job,file);
249         if (xbt_dynar_length(file->tasks_before)>1) {
250                 WARN1("File %s created at more than one location...",file->name);
251         }
252 }
253