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 static double dot_parse_double(const char *string) {
18 if (string == NULL) return -10;
22 ret = sscanf(string, "%lg", &value);
24 WARN1("%s is not a double", string);
27 static int dot_parse_int(const char *string) {
28 if (string == NULL) return -10;
32 ret = sscanf(string, "%d", &value);
34 WARN1("%s is not an integer", string);
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;
44 static void dump_res() {
47 xbt_dynar_foreach(result,cursor,task) {
48 INFO1("Task %d",cursor);
53 static void dot_task_free(void*task){
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) ;
61 /** @brief loads a DOT file describing a DAG
63 * See http://www.graphviz.org/doc/info/lang.html
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.
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);
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*));
82 result = xbt_dynar_new(sizeof(SD_task_t),dot_task_free);
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);
89 xbt_dynar_push(result,&root_task);
90 end_task = SD_task_create_comp_seq("end",NULL,0);
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);
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).
104 xbt_dict_cursor_t cursor;
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);
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);
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);
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);
141 /* Push end task last */
142 xbt_dynar_push(result,&end_task);
144 /* Free previous copy of the files */
145 xbt_dict_free(&files);
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,¤t_job);
163 for (e = agfstin(dag_dot,dag_node); e; e = agnxtin(dag_dot,e)) {
164 dot_add_input_dependencies(current_job,e);
169 char *name= (char*)"root->many";
172 file = xbt_dict_get_or_null(files,name);
174 file = SD_task_create_comm_e2e(name,NULL,size);
175 xbt_dict_set(files,name,file,&dot_task_free);
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);
182 SD_task_dependency_add(NULL,NULL,file,current_job);
185 for (e = agfstout(dag_dot,dag_node); e; e = agnxtout(dag_dot,e)) {
186 dot_add_output_dependencies(current_job,e);
191 char *name = (char*)"many->end";
194 // INFO2("See <uses file=%s %s>",A_dot__uses_file,(is_input?"in":"out"));
195 file = xbt_dict_get_or_null(files,name);
197 file = SD_task_create_comm_e2e(name,NULL,size);
198 xbt_dict_set(files,name,file,&dot_task_free);
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);
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);
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
216 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t *edge) {
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"));
225 file = xbt_dict_get_or_null(files,name);
227 file = SD_task_create_comm_e2e(name,NULL,size);
228 xbt_dict_set(files,name,file,&dot_task_free);
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);
235 SD_task_dependency_add(NULL,NULL,file,current_job);
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
242 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t *edge) {
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"));
250 //INFO2("See <uses file=%s %s>",A_dot__uses_file,(is_input?"in":"out"));
251 file = xbt_dict_get_or_null(files,name);
253 file = SD_task_create_comm_e2e(name,NULL,size);
254 xbt_dict_set(files,name,file,&dot_task_free);
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);
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);