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 bool child_are_marked(SD_task_t task);
27 xbt_dynar_t SD_dotload_FILE(FILE * in_file);
29 static double dot_parse_double(const char *string)
36 ret = sscanf(string, "%lg", &value);
38 WARN1("%s is not a double", string);
42 static int dot_parse_int(const char *string)
49 ret = sscanf(string, "%d", &value);
51 WARN1("%s is not an integer", string);
55 static xbt_dynar_t result;
56 static xbt_dict_t jobs;
57 static xbt_dict_t files;
58 static xbt_dict_t computers;
59 static SD_task_t root_task, end_task;
60 static Agraph_t *dag_dot;
61 static bool schedule = true;
63 static void dump_res()
67 xbt_dynar_foreach(result, cursor, task) {
68 INFO1("Task %d", cursor);
73 bool child_are_marked(SD_task_t task){
74 SD_task_t child_task = NULL;
75 bool all_marked = true;
76 SD_dependency_t depafter = NULL;
78 xbt_dynar_foreach(task->tasks_after,count,depafter){
79 child_task = depafter->dst;
81 if(child_task->marked == 0) {
90 bool acyclic_graph_detection(xbt_dynar_t dag){
91 unsigned int count=0, count_current=0;
92 bool all_marked = true;
93 SD_task_t task = NULL, parent_task = NULL;
94 SD_dependency_t depbefore = NULL;
95 xbt_dynar_t next = NULL, current = xbt_dynar_new(sizeof(SD_task_t),NULL);
97 xbt_dynar_foreach(dag,count,task){
98 if(task->kind == SD_TASK_COMM_E2E) continue;
100 if(xbt_dynar_length(task->tasks_after) == 0){
101 xbt_dynar_push(current, &task);
106 //test if something has to be done for the next iteration
107 while(xbt_dynar_length(current) != 0){
108 next = xbt_dynar_new(sizeof(SD_task_t),NULL);
109 //test if the current iteration is done
111 xbt_dynar_foreach(current,count_current,task){
112 if (task == NULL) continue;
117 xbt_dynar_foreach(task->tasks_before,count,depbefore){
118 parent_task = depbefore->src;
119 if(parent_task->kind == SD_TASK_COMM_E2E){
121 parent_task->marked = 1;
122 xbt_dynar_foreach(parent_task->tasks_before,j,depbefore){
123 parent_task = depbefore->src;
124 if(child_are_marked(parent_task))
125 xbt_dynar_push(next, &parent_task);
128 if(child_are_marked(parent_task))
129 xbt_dynar_push(next, &parent_task);
136 xbt_dynar_free(¤t);
140 xbt_dynar_free(¤t);
143 xbt_dynar_foreach(dag,count,task){
144 if(task->kind == SD_TASK_COMM_E2E) continue;
145 //test if all tasks are marked
146 if(task->marked == 0){
147 WARN1("test %s",task->name);
154 DEBUG0("there are a cycle in your DAG");
159 static void dot_task_free(void *task)
165 static void TRACE_sd_dotloader (SD_task_t task, const char *category)
168 if (strlen (category) != 0){
169 TRACE_category (category);
170 TRACE_sd_set_task_category (task, category);
175 /** @brief loads a DOT file describing a DAG
177 * See http://www.graphviz.org/doc/info/lang.html
179 * To obtain information about transfers and tasks, two attributes are
180 * required : size on task (execution time in Flop) and size on edge
181 * (the amount of data transfer in bit).
182 * if they aren't here, there choose to be equal to zero.
184 xbt_dynar_t SD_dotload(const char *filename)
186 FILE *in_file = fopen(filename, "r");
187 xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
188 SD_dotload_FILE(in_file);
190 xbt_dynar_t computer = NULL;
191 xbt_dict_cursor_t dict_cursor;
193 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
194 xbt_dynar_free(&computer);
196 xbt_dict_free(&computers);
200 xbt_dynar_t SD_dotload_with_sched(const char *filename){
201 FILE *in_file = fopen(filename, "r");
202 xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
203 SD_dotload_FILE(in_file);
206 if(schedule == true){
207 xbt_dynar_t computer = NULL;
208 xbt_dict_cursor_t dict_cursor;
210 const SD_workstation_t *workstations = SD_workstation_get_list ();
211 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
212 int count_computer = dot_parse_int(computer_name);
213 unsigned int count=0;
215 SD_task_t task_previous = NULL;
216 xbt_dynar_foreach(computer,count,task){
217 // add dependency between the previous and the task to avoid
218 // parallel execution
220 if(task_previous != NULL &&
221 !SD_task_dependency_exists(task_previous, task))
222 SD_task_dependency_add(NULL, NULL, task_previous, task);
223 SD_task_schedulel(task, 1, workstations[count_computer]);
224 task_previous = task;
227 xbt_dynar_free(&computer);
229 xbt_dict_free(&computers);
230 if(acyclic_graph_detection(result))
233 WARN0("There are a cycle in your task graph");
235 WARN0("No scheduling provided");
240 xbt_dynar_t SD_dotload_FILE(FILE * in_file)
242 xbt_assert0(in_file, "Unable to use a null file descriptor\n");
243 dag_dot = agread(in_file, NIL(Agdisc_t *));
245 result = xbt_dynar_new(sizeof(SD_task_t), dot_task_free);
246 files = xbt_dict_new();
247 jobs = xbt_dict_new();
248 computers = xbt_dict_new();
249 root_task = SD_task_create_comp_seq("root", NULL, 0);
250 /* by design the root task is always SCHEDULABLE */
251 __SD_task_set_state(root_task, SD_SCHEDULABLE);
253 xbt_dict_set(jobs, "root", root_task, NULL);
254 xbt_dynar_push(result, &root_task);
255 end_task = SD_task_create_comp_seq("end", NULL, 0);
256 xbt_dict_set(jobs, "end", end_task, NULL);
258 Agnode_t *dag_node = NULL;
259 for (dag_node = agfstnode(dag_dot); dag_node;
261 dag_node = agnxtnode(dag_dot, dag_node)
263 dag_node = agnxtnode(dag_node)
267 dot_add_task(dag_node);
270 xbt_dict_free(&jobs);
272 /* And now, post-process the files.
273 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
274 * Files not produced in the system are said to be produced by root task (top of DAG).
275 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
277 xbt_dict_cursor_t cursor;
280 xbt_dict_foreach(files, cursor, name, file) {
281 unsigned int cpt1, cpt2;
282 SD_task_t newfile = NULL;
283 SD_dependency_t depbefore, depafter;
284 if (xbt_dynar_length(file->tasks_before) == 0) {
285 xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
287 SD_task_create_comm_e2e(file->name, NULL, file->amount);
288 SD_task_dependency_add(NULL, NULL, root_task, newfile);
289 SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
290 xbt_dynar_push(result, &newfile);
292 } else if (xbt_dynar_length(file->tasks_after) == 0) {
293 xbt_dynar_foreach(file->tasks_before, cpt2, depbefore) {
295 SD_task_create_comm_e2e(file->name, NULL, file->amount);
296 SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
297 SD_task_dependency_add(NULL, NULL, newfile, end_task);
298 xbt_dynar_push(result, &newfile);
301 xbt_dynar_foreach(file->tasks_before, cpt1, depbefore) {
302 xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
303 if (depbefore->src == depafter->dst) {
305 ("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
306 file->name, depbefore->src->name);
309 SD_task_create_comm_e2e(file->name, NULL, file->amount);
310 SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
311 SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
312 xbt_dynar_push(result, &newfile);
318 /* Push end task last */
319 xbt_dynar_push(result, &end_task);
321 /* Free previous copy of the files */
322 xbt_dict_free(&files);
323 if(acyclic_graph_detection(result))
328 /* dot_add_task create a sd_task and all transfers required for this
329 * task. The execution time of the task is given by the attribute size.
330 * The unit of size is the Flop.*/
331 void dot_add_task(Agnode_t * dag_node)
333 char *name = agnameof(dag_node);
334 SD_task_t current_job;
335 double runtime = dot_parse_double(agget(dag_node, (char *) "size"));
337 DEBUG3("See <job id=%s runtime=%s %.0f>", name,
338 agget(dag_node, (char *) "size"), runtime);
339 current_job = xbt_dict_get_or_null(jobs, name);
340 if (current_job == NULL) {
342 SD_task_create_comp_seq(name, NULL , runtime);
344 TRACE_sd_dotloader (current_job, agget (dag_node, (char*)"category"));
346 xbt_dict_set(jobs, name, current_job, NULL);
347 xbt_dynar_push(result, ¤t_job);
353 for (e = agfstin(dag_dot, dag_node); e; e = agnxtin(dag_dot, e))
355 for (e = agfstin(dag_node); e; e = agnxtin(e))
358 dot_add_input_dependencies(current_job, e);
361 if (count == 0 && current_job != root_task) {
362 SD_task_dependency_add(NULL, NULL, root_task, current_job);
366 for (e = agfstout(dag_dot, dag_node); e; e = agnxtout(dag_dot, e))
368 for (e = agfstout(dag_node); e; e = agnxtout(e))
372 dot_add_output_dependencies(current_job, e);
375 if (count == 0 && current_job != end_task) {
376 SD_task_dependency_add(NULL, NULL, current_job, end_task);
380 /* try to take the information to schedule the task only if all is
382 // performer is the computer which execute the task
383 unsigned long performer = -1;
384 char * char_performer = agget(dag_node, (char *) "performer");
385 if (char_performer != NULL)
386 performer = (long) dot_parse_int(char_performer);
388 // order is giving the task order on one computer
389 unsigned long order = -1;
390 char * char_order = agget(dag_node, (char *) "order");
391 if (char_order != NULL)
392 order = (long) dot_parse_int(char_order);
393 xbt_dynar_t computer = NULL;
394 //INFO2("performer = %d, order=%d",performer,order);
395 if(performer != -1 && order != -1){
396 //necessary parameters are given
397 computer = xbt_dict_get_or_null(computers, char_performer);
398 if(computer == NULL){
399 computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
400 xbt_dict_set(computers, char_performer, computer, NULL);
402 if(performer < sd_global->workstation_count){
403 // the wanted computer is available
404 SD_task_t *task_test = NULL;
405 if(order < computer->used)
406 task_test = xbt_dynar_get_ptr(computer,order);
407 if(task_test != NULL && *task_test != NULL && *task_test != current_job){
408 /*the user gives the same order to several tasks*/
410 DEBUG0("scheduling does not take into account, several task has\
413 //the parameter seems to be ok
414 xbt_dynar_set_as(computer, order, SD_task_t, current_job);
417 /*the platform has not enough processors to schedule the DAG like
420 DEBUG0("scheduling does not take into account, not enough computers");
423 else if((performer == -1 && order != -1) ||
424 (performer != -1 && order == -1)){
425 //one of necessary parameters are not given
427 DEBUG0("scheduling does not take into account");
429 //No schedule available
435 /* dot_add_output_dependencies create the dependencies between a task
436 * and a transfers. This is given by the edges in the dot file.
437 * The amount of data transfers is given by the attribute size on the
439 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge)
444 sprintf(name, "%s->%s", agnameof(agtail(edge)), agnameof(aghead(edge)));
445 double size = dot_parse_double(agget(edge, (char *) "size"));
446 DEBUG2("size : %e, get size : %s", size, agget(edge, (char *) "size"));
449 file = xbt_dict_get_or_null(files, name);
451 file = SD_task_create_comm_e2e(name, NULL, size);
453 TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
455 xbt_dict_set(files, name, file, &dot_task_free);
457 if (SD_task_get_amount(file) != size) {
458 WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
459 name, SD_task_get_amount(file), size);
462 SD_task_dependency_add(NULL, NULL, file, current_job);
464 file = xbt_dict_get_or_null(jobs, agnameof(agtail(edge)));
466 SD_task_dependency_add(NULL, NULL, file, current_job);
471 /* dot_add_output_dependencies create the dependencies between a
472 * transfers and a task. This is given by the edges in the dot file.
473 * The amount of data transfers is given by the attribute size on the
475 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge)
479 sprintf(name, "%s->%s", agnameof(agtail(edge)), agnameof(aghead(edge)));
480 double size = dot_parse_double(agget(edge, (char *) "size"));
481 DEBUG2("size : %e, get size : %s", size, agget(edge, (char *) "size"));
484 file = xbt_dict_get_or_null(files, name);
486 file = SD_task_create_comm_e2e(name, NULL, size);
488 TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
490 xbt_dict_set(files, name, file, &dot_task_free);
492 if (SD_task_get_amount(file) != size) {
493 WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
494 name, SD_task_get_amount(file), size);
497 SD_task_dependency_add(NULL, NULL, current_job, file);
498 if (xbt_dynar_length(file->tasks_before) > 1) {
499 WARN1("File %s created at more than one location...", file->name);
502 file = xbt_dict_get_or_null(jobs, agnameof(aghead(edge)));
504 SD_task_dependency_add(NULL, NULL, current_job, file);