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 -10;
27 ret = sscanf(string, "%lg", &value);
29 WARN1("%s is not a double", string);
32 static int dot_parse_int(const char *string) {
33 if (string == NULL) return -10;
37 ret = sscanf(string, "%d", &value);
39 WARN1("%s is not an integer", string);
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;
49 static void dump_res() {
52 xbt_dynar_foreach(result,cursor,task) {
53 INFO1("Task %d",cursor);
58 static void dot_task_free(void*task){
63 /** @brief loads a DOT file describing a DAG
65 * See http://www.graphviz.org/doc/info/lang.html
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.
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);
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*));
84 result = xbt_dynar_new(sizeof(SD_task_t),dot_task_free);
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);
91 xbt_dynar_push(result,&root_task);
92 end_task = SD_task_create_comp_seq("end",NULL,0);
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);
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).
106 xbt_dict_cursor_t cursor;
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);
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);
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);
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);
143 /* Push end task last */
144 xbt_dynar_push(result,&end_task);
146 /* Free previous copy of the files */
147 xbt_dict_free(&files);
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,¤t_job);
165 for (e = agfstin(dag_dot,dag_node); e; e = agnxtin(dag_dot,e)) {
166 dot_add_input_dependencies(current_job,e);
171 char *name= (char*)"root->many";
174 file = xbt_dict_get_or_null(files,name);
176 file = SD_task_create_comm_e2e(name,NULL,size);
177 xbt_dict_set(files,name,file,&dot_task_free);
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);
184 SD_task_dependency_add(NULL,NULL,file,current_job);
187 for (e = agfstout(dag_dot,dag_node); e; e = agnxtout(dag_dot,e)) {
188 dot_add_output_dependencies(current_job,e);
193 char *name = (char*)"many->end";
196 // INFO2("See <uses file=%s %s>",A_dot__uses_file,(is_input?"in":"out"));
197 file = xbt_dict_get_or_null(files,name);
199 file = SD_task_create_comm_e2e(name,NULL,size);
200 xbt_dict_set(files,name,file,&dot_task_free);
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);
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);
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
218 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t *edge) {
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"));
227 file = xbt_dict_get_or_null(files,name);
229 file = SD_task_create_comm_e2e(name,NULL,size);
230 xbt_dict_set(files,name,file,&dot_task_free);
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);
237 SD_task_dependency_add(NULL,NULL,file,current_job);
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
244 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t *edge) {
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"));
252 //INFO2("See <uses file=%s %s>",A_dot__uses_file,(is_input?"in":"out"));
253 file = xbt_dict_get_or_null(files,name);
255 file = SD_task_create_comm_e2e(name,NULL,size);
256 xbt_dict_set(files,name,file,&dot_task_free);
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);
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);