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"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_dotparse, sd, "Parsing DOT files");
18 #include <graphviz/cgraph.h>
20 #include <graphviz/agraph.h>
23 void dot_add_task(Agnode_t * dag_node);
24 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge);
25 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge);
26 xbt_dynar_t SD_dotload_generic(const char * filename);
28 static double dot_parse_double(const char *string)
35 //ret = sscanf(string, "%lg", &value);
37 value = strtod(string,&err);
40 XBT_WARN("Failed to convert string to double: %s\n",strerror(errno));
47 static int dot_parse_int(const char *string)
54 ret = sscanf(string, "%d", &value);
56 XBT_WARN("%s is not an integer", string);
60 static xbt_dynar_t result;
61 static xbt_dict_t jobs;
62 static xbt_dict_t files;
63 static xbt_dict_t computers;
64 static SD_task_t root_task, end_task;
65 static Agraph_t *dag_dot;
66 static bool schedule = true;
68 static void dump_res()
72 xbt_dynar_foreach(result, cursor, task) {
73 XBT_INFO("Task %d", cursor);
79 static void dot_task_free(void *task)
85 static void TRACE_sd_dotloader (SD_task_t task, const char *category)
88 if (strlen (category) != 0){
89 TRACE_category (category);
90 SD_task_set_category (task, category);
95 /** @brief loads a DOT file describing a DAG
97 * See http://www.graphviz.org/doc/info/lang.html
99 * To obtain information about transfers and tasks, two attributes are
100 * required : size on task (execution time in Flop) and size on edge
101 * (the amount of data transfer in bit).
102 * if they aren't here, there choose to be equal to zero.
104 xbt_dynar_t SD_dotload(const char *filename)
106 SD_dotload_generic(filename);
107 xbt_dynar_t computer = NULL;
108 xbt_dict_cursor_t dict_cursor;
110 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
111 xbt_dynar_free(&computer);
113 xbt_dict_free(&computers);
117 xbt_dynar_t SD_dotload_with_sched(const char *filename){
118 SD_dotload_generic(filename);
120 if(schedule == true){
121 xbt_dynar_t computer = NULL;
122 xbt_dict_cursor_t dict_cursor;
124 const SD_workstation_t *workstations = SD_workstation_get_list ();
125 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
126 int count_computer = dot_parse_int(computer_name);
127 unsigned int count=0;
129 SD_task_t task_previous = NULL;
130 xbt_dynar_foreach(computer,count,task){
131 // add dependency between the previous and the task to avoid
132 // parallel execution
134 if(task_previous != NULL &&
135 !SD_task_dependency_exists(task_previous, task))
136 SD_task_dependency_add(NULL, NULL, task_previous, task);
137 SD_task_schedulel(task, 1, workstations[count_computer]);
138 task_previous = task;
141 xbt_dynar_free(&computer);
143 xbt_dict_free(&computers);
144 if(acyclic_graph_detail(result))
147 XBT_WARN("There is at least one cycle in the provided task graph");
149 XBT_WARN("The scheduling is ignored");
154 xbt_dynar_t SD_dotload_generic(const char * filename)
156 xbt_assert(filename, "Unable to use a null file descriptor\n");
157 //dag_dot = agopen((char*)filename,Agstrictdirected,0);
158 FILE *in_file = fopen(filename, "r");
159 dag_dot = agread(in_file, NIL(Agdisc_t *));
161 result = xbt_dynar_new(sizeof(SD_task_t), dot_task_free);
162 files = xbt_dict_new_homogeneous(&dot_task_free);
163 jobs = xbt_dict_new_homogeneous(NULL);
164 computers = xbt_dict_new_homogeneous(NULL);
165 root_task = SD_task_create_comp_seq("root", NULL, 0);
166 /* by design the root task is always SCHEDULABLE */
167 __SD_task_set_state(root_task, SD_SCHEDULABLE);
169 xbt_dict_set(jobs, "root", root_task, NULL);
170 xbt_dynar_push(result, &root_task);
171 end_task = SD_task_create_comp_seq("end", NULL, 0);
172 xbt_dict_set(jobs, "end", end_task, NULL);
174 Agnode_t *dag_node = NULL;
175 for (dag_node = agfstnode(dag_dot); dag_node;
177 dag_node = agnxtnode(dag_dot, dag_node)
179 dag_node = agnxtnode(dag_node)
183 dot_add_task(dag_node);
186 xbt_dict_free(&jobs);
188 /* And now, post-process the files.
189 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
190 * Files not produced in the system are said to be produced by root task (top of DAG).
191 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
193 xbt_dict_cursor_t cursor;
196 xbt_dict_foreach(files, cursor, name, file) {
197 unsigned int cpt1, cpt2;
198 SD_task_t newfile = NULL;
199 SD_dependency_t depbefore, depafter;
200 if (xbt_dynar_is_empty(file->tasks_before)) {
201 xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
203 SD_task_create_comm_e2e(file->name, NULL, file->amount);
204 SD_task_dependency_add(NULL, NULL, root_task, newfile);
205 SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
206 xbt_dynar_push(result, &newfile);
208 } else if (xbt_dynar_is_empty(file->tasks_after)) {
209 xbt_dynar_foreach(file->tasks_before, cpt2, depbefore) {
211 SD_task_create_comm_e2e(file->name, NULL, file->amount);
212 SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
213 SD_task_dependency_add(NULL, NULL, newfile, end_task);
214 xbt_dynar_push(result, &newfile);
217 xbt_dynar_foreach(file->tasks_before, cpt1, depbefore) {
218 xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
219 if (depbefore->src == depafter->dst) {
221 ("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
222 file->name, depbefore->src->name);
225 SD_task_create_comm_e2e(file->name, NULL, file->amount);
226 SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
227 SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
228 xbt_dynar_push(result, &newfile);
234 /* Push end task last */
235 xbt_dynar_push(result, &end_task);
237 /* Free previous copy of the files */
238 xbt_dict_free(&files);
240 if(acyclic_graph_detail(result))
242 acyclic_graph_detail(result);
247 /* dot_add_task create a sd_task and all transfers required for this
248 * task. The execution time of the task is given by the attribute size.
249 * The unit of size is the Flop.*/
250 void dot_add_task(Agnode_t * dag_node)
252 char *name = agnameof(dag_node);
253 SD_task_t current_job;
254 double runtime = dot_parse_double(agget(dag_node, (char *) "size"));
256 XBT_DEBUG("See <job id=%s runtime=%s %.0f>", name,
257 agget(dag_node, (char *) "size"), runtime);
258 current_job = xbt_dict_get_or_null(jobs, name);
259 if (current_job == NULL) {
261 SD_task_create_comp_seq(name, NULL , runtime);
263 TRACE_sd_dotloader (current_job, agget (dag_node, (char*)"category"));
265 xbt_dict_set(jobs, name, current_job, NULL);
266 xbt_dynar_push(result, ¤t_job);
272 for (e = agfstin(dag_dot, dag_node); e; e = agnxtin(dag_dot, e))
274 for (e = agfstin(dag_node); e; e = agnxtin(e))
277 dot_add_input_dependencies(current_job, e);
280 if (count == 0 && current_job != root_task) {
281 SD_task_dependency_add(NULL, NULL, root_task, current_job);
285 for (e = agfstout(dag_dot, dag_node); e; e = agnxtout(dag_dot, e))
287 for (e = agfstout(dag_node); e; e = agnxtout(e))
291 dot_add_output_dependencies(current_job, e);
294 if (count == 0 && current_job != end_task) {
295 SD_task_dependency_add(NULL, NULL, current_job, end_task);
298 if(schedule || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose)){
299 /* try to take the information to schedule the task only if all is
301 // performer is the computer which execute the task
302 unsigned long performer = -1;
303 char * char_performer = agget(dag_node, (char *) "performer");
304 if (char_performer != NULL)
305 performer = (long) dot_parse_int(char_performer);
307 // order is giving the task order on one computer
308 unsigned long order = -1;
309 char * char_order = agget(dag_node, (char *) "order");
310 if (char_order != NULL)
311 order = (long) dot_parse_int(char_order);
312 xbt_dynar_t computer = NULL;
313 //XBT_INFO("performer = %d, order=%d",performer,order);
314 if(performer != -1 && order != -1){
315 //necessary parameters are given
316 computer = xbt_dict_get_or_null(computers, char_performer);
317 if(computer == NULL){
318 computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
319 xbt_dict_set(computers, char_performer, computer, NULL);
321 if(performer < xbt_lib_length(host_lib)){
322 // the wanted computer is available
323 SD_task_t *task_test = NULL;
324 if(order < computer->used)
325 task_test = xbt_dynar_get_ptr(computer,order);
326 if(task_test != NULL && *task_test != NULL && *task_test != current_job){
327 /*the user gives the same order to several tasks*/
329 XBT_VERB("The task %s starts on the computer %s at the position : %s like the task %s",
330 (*task_test)->name, char_performer, char_order, current_job->name);
332 //the parameter seems to be ok
333 xbt_dynar_set_as(computer, order, SD_task_t, current_job);
336 /*the platform has not enough processors to schedule the DAG like
339 XBT_VERB("The schedule is ignored, there are not enough computers");
343 //one of necessary parameters are not given
345 XBT_VERB("The schedule is ignored, the task %s is not correctly schedule", current_job->name);
350 /* dot_add_output_dependencies create the dependencies between a task
351 * and a transfers. This is given by the edges in the dot file.
352 * The amount of data transfers is given by the attribute size on the
354 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge)
357 char *name_tail=agnameof(agtail(edge));
358 char *name_head=agnameof(aghead(edge));
359 char *name = malloc((strlen(name_head)+strlen(name_tail)+3)*sizeof(char));
360 sprintf(name, "%s->%s", name_tail, name_head);
361 double size = dot_parse_double(agget(edge, (char *) "size"));
362 XBT_DEBUG("size : %e, get size : %s", size, agget(edge, (char *) "size"));
365 file = xbt_dict_get_or_null(files, name);
367 file = SD_task_create_comm_e2e(name, NULL, size);
369 TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
371 xbt_dict_set(files, name, file, NULL);
373 if (SD_task_get_amount(file) != size) {
374 XBT_WARN("Ignoring file %s size redefinition from %.0f to %.0f",
375 name, SD_task_get_amount(file), size);
378 SD_task_dependency_add(NULL, NULL, file, current_job);
380 file = xbt_dict_get_or_null(jobs, agnameof(agtail(edge)));
382 SD_task_dependency_add(NULL, NULL, file, current_job);
388 /* dot_add_output_dependencies create the dependencies between a
389 * transfers and a task. This is given by the edges in the dot file.
390 * The amount of data transfers is given by the attribute size on the
392 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge)
395 char *name_tail=agnameof(agtail(edge));
396 char *name_head=agnameof(aghead(edge));
397 char *name = malloc((strlen(name_head)+strlen(name_tail)+3)*sizeof(char));
398 sprintf(name, "%s->%s", name_tail, name_head);
399 double size = dot_parse_double(agget(edge, (char *) "size"));
400 XBT_DEBUG("size : %e, get size : %s", size, agget(edge, (char *) "size"));
403 file = xbt_dict_get_or_null(files, name);
405 file = SD_task_create_comm_e2e(name, NULL, size);
407 TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
409 xbt_dict_set(files, name, file, NULL);
411 if (SD_task_get_amount(file) != size) {
412 XBT_WARN("Ignoring file %s size redefinition from %.0f to %.0f",
413 name, SD_task_get_amount(file), size);
416 SD_task_dependency_add(NULL, NULL, current_job, file);
417 if (xbt_dynar_length(file->tasks_before) > 1) {
418 XBT_WARN("File %s created at more than one location...", file->name);
421 file = xbt_dict_get_or_null(jobs, agnameof(aghead(edge)));
423 SD_task_dependency_add(NULL, NULL, current_job, file);