1 /* Copyright (c) 2009-2013. 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"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_dotparse, sd, "Parsing DOT files");
20 #include <graphviz/cgraph.h>
22 #include <graphviz/agraph.h>
23 #define agnxtnode(dot, node) agnxtnode(node)
24 #define agfstin(dot, node) agfstin(node)
25 #define agnxtin(dot, edge) agnxtin(edge)
26 #define agfstout(dot, node) agfstout(node)
27 #define agnxtout(dot, edge) agnxtout(edge)
35 void dot_add_task(Agnode_t * dag_node);
36 void dot_add_parallel_task(Agnode_t * dag_node);
37 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge,
38 seq_par_t seq_or_par);
39 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge,
40 seq_par_t seq_or_par);
41 xbt_dynar_t SD_dotload_generic(const char * filename);
43 static double dot_parse_double(const char *string) {
50 value = strtod(string,&err);
52 XBT_WARN("Failed to convert string to double: %s\n",strerror(errno));
59 static int dot_parse_int(const char *string) {
65 ret = sscanf(string, "%d", &value);
67 XBT_WARN("%s is not an integer", string);
71 static xbt_dynar_t result;
72 static xbt_dict_t jobs;
73 static xbt_dict_t files;
74 static xbt_dict_t computers;
75 static SD_task_t root_task, end_task;
76 static Agraph_t *dag_dot;
77 static bool schedule = true;
79 static void dump_res() {
82 xbt_dynar_foreach(result, cursor, task) {
83 XBT_INFO("Task %d", cursor);
89 static void dot_task_free(void *task) {
94 static void dot_task_p_free(void *task) {
100 static void TRACE_sd_dotloader (SD_task_t task, const char *category) {
101 if (category && strlen (category)){
103 XBT_DEBUG("Change the category of %s from %s to %s",
104 task->name, task->category, category);
106 XBT_DEBUG("Set the category of %s to %s",task->name, category);
107 TRACE_category (category);
108 TRACE_sd_set_task_category(task, category);
113 /** @brief loads a DOT file describing a DAG
115 * See http://www.graphviz.org/doc/info/lang.html
117 * To obtain information about transfers and tasks, two attributes are
118 * required : size on task (execution time in Flop) and size on edge
119 * (the amount of data transfer in bit).
120 * if they aren't here, there choose to be equal to zero.
122 xbt_dynar_t SD_dotload(const char *filename) {
123 SD_dotload_generic(filename);
124 xbt_dynar_t computer = NULL;
125 xbt_dict_cursor_t dict_cursor;
127 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
128 xbt_dynar_free(&computer);
130 xbt_dict_free(&computers);
134 xbt_dynar_t SD_dotload_with_sched(const char *filename) {
135 SD_dotload_generic(filename);
137 if(schedule == true){
138 xbt_dynar_t computer = NULL;
139 xbt_dict_cursor_t dict_cursor;
141 const SD_workstation_t *workstations = SD_workstation_get_list ();
142 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
143 int count_computer = dot_parse_int(computer_name);
144 unsigned int count=0;
146 SD_task_t task_previous = NULL;
147 xbt_dynar_foreach(computer,count,task){
148 /* add dependency between the previous and the task to avoid
149 * parallel execution */
151 if(task_previous != NULL &&
152 !SD_task_dependency_exists(task_previous, task))
153 SD_task_dependency_add(NULL, NULL, task_previous, task);
154 SD_task_schedulel(task, 1, workstations[count_computer]);
155 task_previous = task;
158 xbt_dynar_free(&computer);
160 xbt_dict_free(&computers);
161 if(acyclic_graph_detail(result))
164 XBT_WARN("There is at least one cycle in the provided task graph");
166 XBT_WARN("The scheduling is ignored");
170 xbt_dynar_t computer = NULL;
171 xbt_dict_cursor_t dict_cursor;
173 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
174 xbt_dynar_free(&computer);
176 xbt_dict_free(&computers);
177 xbt_dynar_foreach(result,count,task){
178 SD_task_destroy(task);
183 xbt_dynar_t SD_PTG_dotload(const char * filename) {
184 xbt_assert(filename, "Unable to use a null file descriptor\n");
185 FILE *in_file = fopen(filename, "r");
186 dag_dot = agread(in_file, NIL(Agdisc_t *));
188 result = xbt_dynar_new(sizeof(SD_task_t), dot_task_p_free);
189 files = xbt_dict_new_homogeneous(&dot_task_free);
190 jobs = xbt_dict_new_homogeneous(NULL);
191 computers = xbt_dict_new_homogeneous(NULL);
192 root_task = SD_task_create_comp_par_amdahl("root", NULL, 0., 0.);
193 /* by design the root task is always SCHEDULABLE */
194 __SD_task_set_state(root_task, SD_SCHEDULABLE);
196 xbt_dict_set(jobs, "root", root_task, NULL);
197 xbt_dynar_push(result, &root_task);
198 end_task = SD_task_create_comp_par_amdahl("end", NULL, 0., 0.);
199 xbt_dict_set(jobs, "end", end_task, NULL);
201 Agnode_t *dag_node = NULL;
202 for (dag_node = agfstnode(dag_dot); dag_node; dag_node = agnxtnode(dag_dot,
204 dot_add_parallel_task(dag_node);
207 xbt_dict_free(&jobs);
209 /* And now, post-process the files.
210 * We want a file task per pair of computation tasks exchanging the file.
212 * Files not produced in the system are said to be produced by root task
214 * Files not consumed in the system are said to be consumed by end task
217 xbt_dict_cursor_t cursor;
220 xbt_dict_foreach(files, cursor, name, file) {
221 unsigned int cpt1, cpt2;
222 SD_task_t newfile = NULL;
223 SD_dependency_t depbefore, depafter;
224 if (xbt_dynar_is_empty(file->tasks_before)) {
225 xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
227 SD_task_create_comm_par_mxn_1d_block(file->name, NULL, file->amount);
228 SD_task_dependency_add(NULL, NULL, root_task, newfile);
229 SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
230 xbt_dynar_push(result, &newfile);
232 } else if (xbt_dynar_is_empty(file->tasks_after)) {
233 xbt_dynar_foreach(file->tasks_before, cpt2, depbefore) {
235 SD_task_create_comm_par_mxn_1d_block(file->name, NULL,
237 SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
238 SD_task_dependency_add(NULL, NULL, newfile, end_task);
239 xbt_dynar_push(result, &newfile);
242 xbt_dynar_foreach(file->tasks_before, cpt1, depbefore) {
243 xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
244 if (depbefore->src == depafter->dst) {
246 ("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
247 file->name, depbefore->src->name);
250 SD_task_create_comm_par_mxn_1d_block(file->name, NULL,
252 SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
253 SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
254 xbt_dynar_push(result, &newfile);
260 /* Push end task last */
261 xbt_dynar_push(result, &end_task);
263 /* Free previous copy of the files */
264 xbt_dict_free(&files);
265 xbt_dict_free(&computers);
267 if (!acyclic_graph_detail(result)) {
268 XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.",
269 basename((char*)filename));
270 xbt_dynar_free(&result);
271 /* (result == NULL) here */
277 xbt_dynar_t SD_dotload_generic(const char * filename) {
278 xbt_assert(filename, "Unable to use a null file descriptor\n");
279 FILE *in_file = fopen(filename, "r");
280 dag_dot = agread(in_file, NIL(Agdisc_t *));
282 result = xbt_dynar_new(sizeof(SD_task_t), dot_task_p_free);
283 files = xbt_dict_new_homogeneous(NULL);
284 jobs = xbt_dict_new_homogeneous(NULL);
285 computers = xbt_dict_new_homogeneous(NULL);
286 root_task = SD_task_create_comp_seq("root", NULL, 0);
287 /* by design the root task is always SCHEDULABLE */
288 __SD_task_set_state(root_task, SD_SCHEDULABLE);
290 xbt_dict_set(jobs, "root", root_task, NULL);
291 xbt_dynar_push(result, &root_task);
292 end_task = SD_task_create_comp_seq("end", NULL, 0);
293 xbt_dict_set(jobs, "end", end_task, NULL);
295 Agnode_t *dag_node = NULL;
296 for (dag_node = agfstnode(dag_dot); dag_node; dag_node = agnxtnode(dag_dot, dag_node)) {
297 dot_add_task(dag_node);
300 xbt_dict_free(&jobs);
302 /* And now, post-process the files.
303 * We want a file task per pair of computation tasks exchanging the file.
305 * Files not produced in the system are said to be produced by root task
307 * Files not consumed in the system are said to be consumed by end task
310 xbt_dict_cursor_t cursor;
313 xbt_dict_foreach(files, cursor, name, file) {
314 XBT_DEBUG("Considering file '%s' stored in the dictionary",
316 if (xbt_dynar_is_empty(file->tasks_before)) {
317 XBT_DEBUG("file '%s' has no source. Add dependency from 'root'",
319 SD_task_dependency_add(NULL, NULL, root_task, file);
320 } else if (xbt_dynar_is_empty(file->tasks_after)) {
321 XBT_DEBUG("file '%s' has no destination. Add dependency to 'end'",
323 SD_task_dependency_add(NULL, NULL, file, end_task);
325 xbt_dynar_push(result, &file);
328 /* Push end task last */
329 xbt_dynar_push(result, &end_task);
331 xbt_dict_free(&files);
333 if (!acyclic_graph_detail(result)) {
334 XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.",
335 basename((char*)filename));
336 xbt_dynar_free(&result);
337 /* (result == NULL) here */
342 /* dot_add_parallel_task create a sd_task of SD_TASK_COMP_PAR_AMDHAL type and
343 * all transfers required for this task. The execution time of the task is
344 * given by the attribute size. The unit of size is the Flop.*/
345 void dot_add_parallel_task(Agnode_t * dag_node) {
346 char *name = agnameof(dag_node);
347 SD_task_t current_job;
348 double amount = dot_parse_double(agget(dag_node, (char *) "size"));
349 double alpha = dot_parse_double(agget(dag_node, (char *) "alpha"));
354 XBT_DEBUG("See <job id=%s amount=%s %.0f alpha=%.2f>", name,
355 agget(dag_node, (char *) "size"), amount, alpha);
356 if (!strcmp(name, "root")){
357 XBT_WARN("'root' node is explicitly declared in the DOT file. Update it");
358 root_task->amount = amount;
359 root_task->alpha = alpha;
361 TRACE_sd_dotloader (root_task, agget (dag_node, (char*)"category"));
365 if (!strcmp(name, "end")){
366 XBT_WARN("'end' node is explicitly declared in the DOT file. Update it");
367 end_task->amount = amount;
368 end_task->alpha = alpha;
370 TRACE_sd_dotloader (end_task, agget (dag_node, (char*)"category"));
374 current_job = xbt_dict_get_or_null(jobs, name);
375 if (current_job == NULL) {
377 SD_task_create_comp_par_amdahl(name, NULL , amount, alpha);
379 TRACE_sd_dotloader (current_job, agget (dag_node, (char*)"category"));
381 xbt_dict_set(jobs, name, current_job, NULL);
382 xbt_dynar_push(result, ¤t_job);
387 for (e = agfstin(dag_dot, dag_node); e; e = agnxtin(dag_dot, e)) {
388 dot_add_input_dependencies(current_job, e, parallel);
391 if (count == 0 && current_job != root_task) {
392 SD_task_dependency_add(NULL, NULL, root_task, current_job);
395 for (e = agfstout(dag_dot, dag_node); e; e = agnxtout(dag_dot, e)) {
396 dot_add_output_dependencies(current_job, e, parallel);
399 if (count == 0 && current_job != end_task) {
400 SD_task_dependency_add(NULL, NULL, current_job, end_task);
404 /* dot_add_task create a sd_task and all transfers required for this
405 * task. The execution time of the task is given by the attribute size.
406 * The unit of size is the Flop.*/
407 void dot_add_task(Agnode_t * dag_node) {
408 char *name = agnameof(dag_node);
409 SD_task_t current_job;
410 double runtime = dot_parse_double(agget(dag_node, (char *) "size"));
412 XBT_DEBUG("See <job id=%s runtime=%s %.0f>", name,
413 agget(dag_node, (char *) "size"), runtime);
415 if (!strcmp(name, "root")){
416 XBT_WARN("'root' node is explicitly declared in the DOT file. Update it");
417 root_task->amount = runtime;
419 TRACE_sd_dotloader (root_task, agget (dag_node, (char*)"category"));
423 if (!strcmp(name, "end")){
424 XBT_WARN("'end' node is explicitly declared in the DOT file. Update it");
425 end_task->amount = runtime;
427 TRACE_sd_dotloader (end_task, agget (dag_node, (char*)"category"));
431 current_job = xbt_dict_get_or_null(jobs, name);
434 SD_task_create_comp_seq(name, NULL , runtime);
436 TRACE_sd_dotloader (current_job, agget (dag_node, (char*)"category"));
438 xbt_dict_set(jobs, name, current_job, NULL);
439 xbt_dynar_push(result, ¤t_job);
444 for (e = agfstin(dag_dot, dag_node); e; e = agnxtin(dag_dot, e)) {
445 dot_add_input_dependencies(current_job, e, sequential);
448 if (count == 0 && current_job != root_task) {
449 SD_task_dependency_add(NULL, NULL, root_task, current_job);
452 for (e = agfstout(dag_dot, dag_node); e; e = agnxtout(dag_dot, e)) {
453 dot_add_output_dependencies(current_job, e, sequential);
456 if (count == 0 && current_job != end_task) {
457 SD_task_dependency_add(NULL, NULL, current_job, end_task);
460 if(schedule || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose)){
461 /* try to take the information to schedule the task only if all is
463 /* performer is the computer which execute the task */
464 unsigned long performer = -1;
465 char * char_performer = agget(dag_node, (char *) "performer");
466 if (char_performer != NULL)
467 performer = (long) dot_parse_int(char_performer);
469 /* order is giving the task order on one computer */
470 unsigned long order = -1;
471 char * char_order = agget(dag_node, (char *) "order");
472 if (char_order != NULL)
473 order = (long) dot_parse_int(char_order);
474 xbt_dynar_t computer = NULL;
475 if(performer != -1 && order != -1){
476 /* required parameters are given */
477 computer = xbt_dict_get_or_null(computers, char_performer);
478 if(computer == NULL){
479 computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
480 xbt_dict_set(computers, char_performer, computer, NULL);
482 if(performer < xbt_lib_length(host_lib)){
483 /* the wanted computer is available */
484 SD_task_t *task_test = NULL;
485 if(order < computer->used)
486 task_test = xbt_dynar_get_ptr(computer,order);
487 if(task_test != NULL && *task_test != NULL && *task_test != current_job){
488 /* the user gives the same order to several tasks */
490 XBT_VERB("The task %s starts on the computer %s at the position : %s like the task %s",
491 (*task_test)->name, char_performer, char_order,
494 /* the parameter seems to be ok */
495 xbt_dynar_set_as(computer, order, SD_task_t, current_job);
498 /* the platform has not enough processors to schedule the DAG like
501 XBT_VERB("The schedule is ignored, there are not enough computers");
505 /* one of required parameters is not given */
507 XBT_VERB("The schedule is ignored, the task %s is not correctly scheduled",
513 /* dot_add_output_dependencies create the dependencies between a task
514 * and a transfers. This is given by the edges in the dot file.
515 * The amount of data transfers is given by the attribute size on the
517 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge,
518 seq_par_t seq_or_par) {
519 SD_task_t file = NULL;
520 char *name_tail=agnameof(agtail(edge));
521 char *name_head=agnameof(aghead(edge));
522 char *name = xbt_malloc((strlen(name_head)+strlen(name_tail)+6)*sizeof(char));
523 sprintf(name, "%s->%s", name_tail, name_head);
524 double size = dot_parse_double(agget(edge, (char *) "size"));
525 XBT_DEBUG("add input -- edge: %s, size : %e, get size : %s",
526 name, size, agget(edge, (char *) "size"));
529 file = xbt_dict_get_or_null(files, name);
531 if (seq_or_par == sequential){
532 file = SD_task_create_comm_e2e(name, NULL, size);
534 file = SD_task_create_comm_par_mxn_1d_block(name, NULL, size);
537 TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
539 XBT_DEBUG("add input -- adding %s to the dict as new file", name);
540 xbt_dict_set(files, name, file, NULL);
542 XBT_WARN("%s already exists", name);
543 if (SD_task_get_amount(file) != size) {
544 XBT_WARN("Ignoring file %s size redefinition from %.0f to %.0f",
545 name, SD_task_get_amount(file), size);
548 SD_task_dependency_add(NULL, NULL, file, current_job);
550 file = xbt_dict_get_or_null(jobs, name_tail);
552 SD_task_dependency_add(NULL, NULL, file, current_job);
558 /* dot_add_output_dependencies create the dependencies between a
559 * transfers and a task. This is given by the edges in the dot file.
560 * The amount of data transfers is given by the attribute size on the
562 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge,
563 seq_par_t seq_or_par) {
565 char *name_tail=agnameof(agtail(edge));
566 char *name_head=agnameof(aghead(edge));
567 char *name = xbt_malloc((strlen(name_head)+strlen(name_tail)+6)*sizeof(char));
568 sprintf(name, "%s->%s", name_tail, name_head);
569 double size = dot_parse_double(agget(edge, (char *) "size"));
570 XBT_DEBUG("add_output -- edge: %s, size : %e, get size : %s",
571 name, size, agget(edge, (char *) "size"));
574 file = xbt_dict_get_or_null(files, name);
576 if (seq_or_par == sequential){
577 file = SD_task_create_comm_e2e(name, NULL, size);
579 file = SD_task_create_comm_par_mxn_1d_block(name, NULL, size);
582 TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
584 XBT_DEBUG("add output -- adding %s to the dict as new file", name);
585 xbt_dict_set(files, name, file, NULL);
587 XBT_WARN("%s already exists", name);
588 if (SD_task_get_amount(file) != size) {
589 XBT_WARN("Ignoring file %s size redefinition from %.0f to %.0f",
590 name, SD_task_get_amount(file), size);
593 SD_task_dependency_add(NULL, NULL, current_job, file);
594 if (xbt_dynar_length(file->tasks_before) > 1) {
595 XBT_WARN("File %s created at more than one location...", file->name);
598 file = xbt_dict_get_or_null(jobs, name_head);
600 SD_task_dependency_add(NULL, NULL, current_job, file);