1 /* Copyright (c) 2009-2016. 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. */
7 #include "src/simdag/simdag_private.h"
8 #include "simgrid/simdag.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_dotparse, sd, "Parsing DOT files");
18 #include <graphviz/cgraph.h>
20 #include <graphviz/agraph.h>
21 #define agnxtnode(dot, node) agnxtnode(node)
22 #define agfstout(dot, node) agfstout(node)
23 #define agnxtout(dot, edge) agnxtout(edge)
31 xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par, bool schedule);
34 static void dot_task_p_free(void *task) {
35 SD_task_destroy(*(SD_task_t *)task);
38 /** @brief loads a DOT file describing a DAG
40 * See http://www.graphviz.org/doc/info/lang.html for more details.
41 * The size attribute of a node describes:
42 * - for a compute task: the amount of flops to execute
43 * - for a communication task : the amount of bytes to transfer
44 * If this attribute is ommited, the default value is zero.
46 xbt_dynar_t SD_dotload(const char *filename) {
47 return SD_dotload_generic(filename, sequential, false);
50 xbt_dynar_t SD_PTG_dotload(const char * filename) {
51 return SD_dotload_generic(filename, parallel, false);
54 xbt_dynar_t SD_dotload_with_sched(const char *filename) {
55 return SD_dotload_generic(filename, sequential, true);
59 static int edge_compare(const void *a, const void *b)
61 unsigned va = AGSEQ(*(Agedge_t **)a);
62 unsigned vb = AGSEQ(*(Agedge_t **)b);
63 return va == vb ? 0 : (va < vb ? -1 : 1);
67 xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par, bool schedule){
68 xbt_assert(filename, "Unable to use a null file descriptor\n");
70 SD_task_t root, end, task;
72 xbt_dynar_t computer = NULL;
74 xbt_dict_cursor_t dict_cursor;
75 bool schedule_success = true;
77 xbt_dynar_t result = xbt_dynar_new(sizeof(SD_task_t), dot_task_p_free);
78 xbt_dict_t jobs = xbt_dict_new_homogeneous(NULL);
80 FILE *in_file = fopen(filename, "r");
82 xbt_die("Failed to open file: %s", filename);
84 Agraph_t * dag_dot = agread(in_file, NIL(Agdisc_t *));
87 computers = xbt_dict_new_homogeneous(NULL);
89 /* Create all the nodes */
90 Agnode_t *node = NULL;
91 for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
92 char *name = agnameof(node);
93 double amount = atof(agget(node, (char *) "size"));
96 if (!(task = (SD_task_t)xbt_dict_get_or_null(jobs, name))) {
97 if (seq_or_par == sequential){
98 XBT_DEBUG("See <job id=%s amount =%.0f>", name, amount);
99 task = SD_task_create_comp_seq(name, NULL , amount);
101 alpha = atof(agget(node, (char *) "alpha"));
102 XBT_DEBUG("See <job id=%s amount =%.0f alpha = %.3f>", name, amount, alpha);
103 task = SD_task_create_comp_par_amdahl(name, NULL , amount, alpha);
106 xbt_dict_set(jobs, name, task, NULL);
108 if (!strcmp(name, "root")){
109 /* by design the root task is always SCHEDULABLE */
110 SD_task_set_state(task, SD_SCHEDULABLE);
111 /* Put it at the beginning of the dynar */
112 xbt_dynar_insert_at(result, 0, &task);
114 if (!strcmp(name, "end")){
115 XBT_DEBUG("Declaration of the 'end' node, don't store it yet.");
117 /* Should be inserted later in the dynar */
119 xbt_dynar_push(result, &task);
123 if((seq_or_par == sequential) &&
124 ((schedule && schedule_success) || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose))){
125 /* try to take the information to schedule the task only if all is right*/
126 int performer, order;
127 char *char_performer = agget(node, (char *) "performer");
128 char *char_order = agget(node, (char *) "order");
129 /* performer is the computer which execute the task */
130 performer = ((!char_performer || !strcmp(char_performer,"")) ? -1:atoi(char_performer));
131 /* order is giving the task order on one computer */
132 order = ((!char_order || !strcmp(char_order, ""))? -1:atoi(char_order));
134 XBT_DEBUG ("Task '%s' is scheduled on workstation '%d' in position '%d'", task->name, performer, order);
135 xbt_dynar_t computer = NULL;
136 if(performer != -1 && order != -1){
137 /* required parameters are given */
138 computer = (xbt_dynar_t)xbt_dict_get_or_null(computers, char_performer);
139 if(computer == NULL){
140 computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
141 xbt_dict_set(computers, char_performer, computer, NULL);
143 if(performer < xbt_dict_length(host_list)){
144 /* the wanted computer is available */
145 SD_task_t *task_test = NULL;
146 if((unsigned int)order < computer->used)
147 task_test = (SD_task_t *)xbt_dynar_get_ptr(computer,order);
148 if(task_test != NULL && *task_test != NULL && *task_test != task){
149 /* the user gives the same order to several tasks */
150 schedule_success = false;
151 XBT_VERB("The task %s starts on the computer %s at the position : %s like the task %s",
152 (*task_test)->name, char_performer, char_order, task->name);
154 /* the parameter seems to be ok */
155 xbt_dynar_set_as(computer, order, SD_task_t, task);
158 /* the platform has not enough processors to schedule the DAG like
160 schedule_success = false;
161 XBT_VERB("The schedule is ignored, there are not enough computers");
165 /* one of required parameters is not given */
166 schedule_success = false;
167 XBT_VERB("The schedule is ignored, the task %s is not correctly scheduled", task->name);
171 XBT_WARN("Task '%s' is defined more than once", name);
175 /*Check if 'root' and 'end' nodes have been explicitly declared. If not, create them. */
176 if (!(root = (SD_task_t)xbt_dict_get_or_null(jobs, "root"))){
177 if (seq_or_par == sequential)
178 root = SD_task_create_comp_seq("root", NULL, 0);
180 root = SD_task_create_comp_par_amdahl("root", NULL, 0, 0);
181 /* by design the root task is always SCHEDULABLE */
182 SD_task_set_state(root, SD_SCHEDULABLE);
183 /* Put it at the beginning of the dynar */
184 xbt_dynar_insert_at(result, 0, &root);
187 if (!(end = (SD_task_t)xbt_dict_get_or_null(jobs, "end"))){
188 if (seq_or_par == sequential)
189 end = SD_task_create_comp_seq("end", NULL, 0);
191 end = SD_task_create_comp_par_amdahl("end", NULL, 0, 0);
195 xbt_dynar_t edges = xbt_dynar_new(sizeof(Agedge_t*), NULL);
196 for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
199 xbt_dynar_reset(edges);
200 for (edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge))
201 xbt_dynar_push_as(edges, Agedge_t *, edge);
203 /* Hack: circumvent a bug in libcgraph, where the edges are not always given back in creation order. We sort them
204 * again, according to their sequence id. The problem appears to be solved (i.e.: I did not test it) in graphviz'
205 * mercurial repository by the following changeset:
206 * changeset: 8431:d5f1fb7e8103
207 * user: Emden Gansner <erg@research.att.com>
208 * date: Tue Oct 11 12:38:58 2011 -0400
209 * summary: Make sure edges are stored in node creation order
210 * It should be fixed in graphviz 2.30 and above.
212 xbt_dynar_sort(edges, edge_compare);
214 xbt_dynar_foreach(edges, cursor, edge) {
216 char *src_name=agnameof(agtail(edge));
217 char *dst_name=agnameof(aghead(edge));
218 double size = atof(agget(edge, (char *) "size"));
220 src = (SD_task_t)xbt_dict_get_or_null(jobs, src_name);
221 dst = (SD_task_t)xbt_dict_get_or_null(jobs, dst_name);
224 char *name = (char*)xbt_malloc((strlen(src_name)+strlen(dst_name)+6)*sizeof(char));
225 sprintf(name, "%s->%s", src_name, dst_name);
226 XBT_DEBUG("See <transfer id=%s amount = %.0f>", name, size);
227 if (!(task = (SD_task_t)xbt_dict_get_or_null(jobs, name))) {
228 if (seq_or_par == sequential)
229 task = SD_task_create_comm_e2e(name, NULL , size);
231 task = SD_task_create_comm_par_mxn_1d_block(name, NULL , size);
232 SD_task_dependency_add(NULL, NULL, src, task);
233 SD_task_dependency_add(NULL, NULL, task, dst);
234 xbt_dict_set(jobs, name, task, NULL);
235 xbt_dynar_push(result, &task);
237 XBT_WARN("Task '%s' is defined more than once", name);
241 SD_task_dependency_add(NULL, NULL, src, dst);
245 xbt_dynar_free(&edges);
247 /* all compute and transfer tasks have been created, put the "end" node at the end of dynar */
248 XBT_DEBUG("All tasks have been created, put %s at the end of the dynar", end->name);
249 xbt_dynar_push(result, &end);
251 /* Connect entry tasks to 'root', and exit tasks to 'end'*/
252 xbt_dynar_foreach (result, i, task){
253 if (task == root || task == end)
255 if (xbt_dynar_is_empty(task->tasks_before)) {
256 XBT_DEBUG("file '%s' has no source. Add dependency from 'root'", task->name);
257 SD_task_dependency_add(NULL, NULL, root, task);
260 if (xbt_dynar_is_empty(task->tasks_after)) {
261 XBT_DEBUG("file '%s' has no destination. Add dependency to 'end'", task->name);
262 SD_task_dependency_add(NULL, NULL, task, end);
267 xbt_dict_free(&jobs);
271 if (schedule_success) {
272 const sg_host_t *workstations = sg_host_list ();
273 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
274 int count_computer = atoi(computer_name);
275 unsigned int count=0;
277 SD_task_t task_previous = NULL;
278 xbt_dynar_foreach(computer,count,task){
279 /* add dependency between the previous and the task to avoid parallel execution */
281 if(task_previous != NULL && !SD_task_dependency_exists(task_previous, task))
282 SD_task_dependency_add(NULL, NULL, task_previous, task);
284 SD_task_schedulel(task, 1, workstations[count_computer]);
285 task_previous = task;
288 xbt_dynar_free(&computer);
290 xbt_dict_free(&computers);
292 XBT_WARN("The scheduling is ignored");
293 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
294 xbt_dynar_free(&computer);
297 xbt_dict_free(&computers);
298 xbt_dynar_free(&result);
303 if (!acyclic_graph_detail(result)) {
304 XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.", basename((char*)filename));
305 xbt_dynar_free(&result);