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_dotparse, sd,"Parsing DOT files");
15 #include <graphviz/cgraph.h>
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);
22 static double dot_parse_double(const char *string) {
23 if (string == NULL) return -1;
27 ret = sscanf(string, "%lg", &value);
29 WARN1("%s is not a double", string);
33 static int dot_parse_int(const char *string) {
34 if (string == NULL) return -10;
38 ret = sscanf(string, "%d", &value);
40 WARN1("%s is not an integer", string);
44 static xbt_dynar_t result;
45 static xbt_dict_t jobs;
46 static xbt_dict_t files;
47 static SD_task_t root_task,end_task;
48 static Agraph_t * dag_dot;
50 static void dump_res() {
53 xbt_dynar_foreach(result,cursor,task) {
54 INFO1("Task %d",cursor);
59 static void dot_task_free(void*task){
64 /** @brief loads a DOT file describing a DAG
66 * See http://www.graphviz.org/doc/info/lang.html
68 * To obtain information about transfers and tasks, two attributes are
69 * required : size on task (execution time in Flop) and size on edge
70 * (the amount of data transfer in bit).
71 * if they aren't here, there choose to be equal to zero.
73 xbt_dynar_t SD_dotload(const char*filename){
74 FILE* in_file = fopen(filename,"r");
75 xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
76 SD_dotload_FILE(in_file);
81 xbt_dynar_t SD_dotload_FILE(FILE* in_file){
82 xbt_assert0(in_file, "Unable to use a null file descriptor\n");
83 dag_dot = agread(in_file,NIL(Agdisc_t*));
85 result = xbt_dynar_new(sizeof(SD_task_t),dot_task_free);
88 root_task = SD_task_create_comp_seq("root",NULL,0);
89 /* by design the root task is always SCHEDULABLE */
90 __SD_task_set_state(root_task, SD_SCHEDULABLE);
92 xbt_dict_set(jobs,"root",root_task,NULL);
93 xbt_dynar_push(result,&root_task);
94 end_task = SD_task_create_comp_seq("end",NULL,0);
95 xbt_dict_set(jobs,"end",end_task,NULL);
97 Agnode_t *dag_node = NULL;
98 for (dag_node = agfstnode(dag_dot); dag_node; dag_node = agnxtnode(dag_dot,dag_node)){
99 dot_add_task(dag_node);
102 xbt_dict_free(&jobs);
104 /* And now, post-process the files.
105 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
106 * Files not produced in the system are said to be produced by root task (top of DAG).
107 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
109 xbt_dict_cursor_t cursor;
112 xbt_dict_foreach(files,cursor,name,file) {
113 unsigned int cpt1,cpt2;
114 SD_task_t newfile = NULL;
115 SD_dependency_t depbefore,depafter;
116 if (xbt_dynar_length(file->tasks_before) == 0) {
117 xbt_dynar_foreach(file->tasks_after,cpt2,depafter) {
118 SD_task_t newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
119 SD_task_dependency_add(NULL,NULL,root_task,newfile);
120 SD_task_dependency_add(NULL,NULL,newfile,depafter->dst);
121 xbt_dynar_push(result,&newfile);
123 } else if (xbt_dynar_length(file->tasks_after) == 0) {
124 xbt_dynar_foreach(file->tasks_before,cpt2,depbefore) {
125 SD_task_t newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
126 SD_task_dependency_add(NULL,NULL,depbefore->src,newfile);
127 SD_task_dependency_add(NULL,NULL,newfile,end_task);
128 xbt_dynar_push(result,&newfile);
131 xbt_dynar_foreach(file->tasks_before,cpt1,depbefore) {
132 xbt_dynar_foreach(file->tasks_after,cpt2,depafter) {
133 if (depbefore->src == depafter->dst) {
134 WARN2("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
135 file->name,depbefore->src->name);
137 newfile = SD_task_create_comm_e2e(file->name,NULL,file->amount);
138 SD_task_dependency_add(NULL,NULL,depbefore->src,newfile);
139 SD_task_dependency_add(NULL,NULL,newfile,depafter->dst);
140 xbt_dynar_push(result,&newfile);
146 /* Push end task last */
147 xbt_dynar_push(result,&end_task);
149 /* Free previous copy of the files */
150 xbt_dict_free(&files);
155 /* dot_add_task create a sd_task and all transfers required for this
156 * task. The execution time of the task is given by the attribute size.
157 * The unit of size is the Flop.*/
158 void dot_add_task(Agnode_t *dag_node) {
159 char *name = agnameof(dag_node);
160 SD_task_t current_job;
161 double runtime = dot_parse_double(agget(dag_node,(char*)"size"));
162 long performer = (long)dot_parse_int((char *) agget(dag_node,(char*)"performer"));
163 INFO3("See <job id=%s runtime=%s %.0f>",name,agget(dag_node,(char*)"size"),runtime);
164 current_job = xbt_dict_get_or_null(jobs,name);
165 if (current_job == NULL) {
166 current_job = SD_task_create_comp_seq(name,(void*)performer,runtime);
167 xbt_dict_set(jobs,name,current_job,NULL);
168 xbt_dynar_push(result,¤t_job);
172 for (e = agfstin(dag_dot,dag_node); e; e = agnxtin(dag_dot,e)) {
173 dot_add_input_dependencies(current_job,e);
176 if (count==0 && current_job != root_task){
178 char *name= (char*)"root->many";
181 file = xbt_dict_get_or_null(files,name);
183 file = SD_task_create_comm_e2e(name,NULL,size);
184 xbt_dict_set(files,name,file,&dot_task_free);
186 if (SD_task_get_amount(file)!=size) {
187 WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
188 name,SD_task_get_amount(file),size);
191 SD_task_dependency_add(NULL,NULL,file,current_job);
194 for (e = agfstout(dag_dot,dag_node); e; e = agnxtout(dag_dot,e)) {
195 dot_add_output_dependencies(current_job,e);
198 if (count==0 && current_job != end_task){
200 char *name = (char*)"many->end";
203 // INFO2("See <uses file=%s %s>",A_dot__uses_file,(is_input?"in":"out"));
204 file = xbt_dict_get_or_null(files,name);
206 file = SD_task_create_comm_e2e(name,NULL,size);
207 xbt_dict_set(files,name,file,&dot_task_free);
209 if (SD_task_get_amount(file)!=size) {
210 WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
211 name,SD_task_get_amount(file),size);
214 SD_task_dependency_add(NULL,NULL,current_job,file);
215 if (xbt_dynar_length(file->tasks_before)>1) {
216 WARN1("File %s created at more than one location...",file->name);
221 /* dot_add_output_dependencies create the dependencies between a task
222 * and a transfers. This is given by the edges in the dot file.
223 * The amount of data transfers is given by the attribute size on the
225 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t *edge) {
229 sprintf(name ,"%s->%s",agnameof(agtail(edge)) ,agnameof(aghead(edge)));
230 double size = dot_parse_double(agget(edge,(char*)"size"));
231 INFO2("size : %e, get size : %s",size,agget(edge,(char*)"size"));
232 //int sender = dot_parse_int(agget(edge,(char*)"sender"));
233 //int reciever = dot_parse_int(agget(edge,(char*)"reciever"));
235 file = xbt_dict_get_or_null(files,name);
237 file = SD_task_create_comm_e2e(name,NULL,size);
238 xbt_dict_set(files,name,file,&dot_task_free);
240 if (SD_task_get_amount(file)!=size) {
241 WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
242 name,SD_task_get_amount(file),size);
245 SD_task_dependency_add(NULL,NULL,file,current_job);
247 file = xbt_dict_get_or_null(jobs,agnameof(agtail(edge)));
249 SD_task_dependency_add(NULL,NULL,file,current_job);
254 /* dot_add_output_dependencies create the dependencies between a
255 * transfers and a task. This is given by the edges in the dot file.
256 * The amount of data transfers is given by the attribute size on the
258 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t *edge) {
261 sprintf(name ,"%s->%s",agnameof(agtail(edge)) ,agnameof(aghead(edge)));
262 double size = dot_parse_double(agget(edge,(char*)"size"));
263 INFO2("size : %e, get size : %s",size,agget(edge,(char*)"size"));
264 //int sender = dot_parse_int(agget(edge,(char*)"sender"));
265 //int reciever = dot_parse_int(agget(edge,(char*)"reciever"));
267 //INFO2("See <uses file=%s %s>",A_dot__uses_file,(is_input?"in":"out"));
269 file = xbt_dict_get_or_null(files,name);
271 file = SD_task_create_comm_e2e(name,NULL,size);
272 xbt_dict_set(files,name,file,&dot_task_free);
274 if (SD_task_get_amount(file)!=size) {
275 WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
276 name,SD_task_get_amount(file),size);
279 SD_task_dependency_add(NULL,NULL,current_job,file);
280 if (xbt_dynar_length(file->tasks_before)>1) {
281 WARN1("File %s created at more than one location...",file->name);
284 file = xbt_dict_get_or_null(jobs,agnameof(aghead(edge)));
286 SD_task_dependency_add(NULL,NULL,current_job,file);