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"
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 xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par);
37 static xbt_dict_t jobs;
38 static xbt_dict_t computers;
39 static bool schedule = false;
41 static void dot_task_p_free(void *task) {
42 SD_task_t *t = (SD_task_t *)task;
46 /** @brief loads a DOT file describing a DAG
48 * See http://www.graphviz.org/doc/info/lang.html for more details.
49 * The size attribute of a node describes:
50 * - for a compute task: the amount of flops to execute
51 * - for a communication task : the amount of bytes to transfer
52 * If this attribute is ommited, the default value is zero.
54 xbt_dynar_t SD_dotload(const char *filename) {
55 return SD_dotload_generic(filename, sequential);
58 xbt_dynar_t SD_PTG_dotload(const char * filename) {
59 return SD_dotload_generic(filename, parallel);
62 xbt_dynar_t SD_dotload_with_sched(const char *filename) {
64 xbt_dynar_t result = SD_dotload_generic(filename, sequential);
67 xbt_dynar_t computer = NULL;
68 xbt_dict_cursor_t dict_cursor;
70 const sg_host_t *workstations = sg_host_list ();
71 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
72 int count_computer = atoi(computer_name);
75 SD_task_t task_previous = NULL;
76 xbt_dynar_foreach(computer,count,task){
77 /* add dependency between the previous and the task to avoid parallel execution */
79 if(task_previous != NULL && !SD_task_dependency_exists(task_previous, task))
80 SD_task_dependency_add(NULL, NULL, task_previous, task);
82 SD_task_schedulel(task, 1, workstations[count_computer]);
86 xbt_dynar_free(&computer);
88 xbt_dict_free(&computers);
89 if(acyclic_graph_detail(result))
92 XBT_WARN("There is at least one cycle in the provided task graph");
94 XBT_WARN("The scheduling is ignored");
96 xbt_dynar_t computer = NULL;
97 xbt_dict_cursor_t dict_cursor;
99 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
100 xbt_dynar_free(&computer);
102 xbt_dict_free(&computers);
103 xbt_dynar_free(&result);
108 static int edge_compare(const void *a, const void *b)
110 unsigned va = AGSEQ(*(Agedge_t **)a);
111 unsigned vb = AGSEQ(*(Agedge_t **)b);
112 return va == vb ? 0 : (va < vb ? -1 : 1);
116 xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par){
117 xbt_assert(filename, "Unable to use a null file descriptor\n");
119 xbt_dynar_t result = xbt_dynar_new(sizeof(SD_task_t), dot_task_p_free);
120 jobs = xbt_dict_new_homogeneous(NULL);
121 FILE *in_file = fopen(filename, "r");
123 xbt_die("Failed to open file: %s", filename);
124 Agraph_t * dag_dot = agread(in_file, NIL(Agdisc_t *));
125 SD_task_t root, end, task;
128 computers = xbt_dict_new_homogeneous(NULL);
130 /* Create all the nodes */
131 Agnode_t *node = NULL;
132 for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
133 char *name = agnameof(node);
134 double amount = atof(agget(node, (char *) "size"));
137 if (!(task = (SD_task_t)xbt_dict_get_or_null(jobs, name))) {
138 if (seq_or_par == sequential){
139 XBT_DEBUG("See <job id=%s amount =%.0f>", name, amount);
140 task = SD_task_create_comp_seq(name, NULL , amount);
142 alpha = atof(agget(node, (char *) "alpha"));
143 XBT_DEBUG("See <job id=%s amount =%.0f alpha = %.3f>", name, amount, alpha);
144 task = SD_task_create_comp_par_amdahl(name, NULL , amount, alpha);
147 xbt_dict_set(jobs, name, task, NULL);
149 if (!strcmp(name, "root")){
150 /* by design the root task is always SCHEDULABLE */
151 SD_task_set_state(task, SD_SCHEDULABLE);
152 /* Put it at the beginning of the dynar */
153 xbt_dynar_insert_at(result, 0, &task);
155 if (!strcmp(name, "end")){
156 XBT_DEBUG("Declaration of the 'end' node, don't store it yet.");
158 /* Should be inserted later in the dynar */
160 xbt_dynar_push(result, &task);
164 if((seq_or_par == sequential) && (schedule || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose))){
165 /* try to take the information to schedule the task only if all is right*/
166 int performer, order;
167 char *char_performer = agget(node, (char *) "performer");
168 char *char_order = agget(node, (char *) "order");
169 /* performer is the computer which execute the task */
170 performer = ((!char_performer || !strcmp(char_performer,"")) ? -1:atoi(char_performer));
171 /* order is giving the task order on one computer */
172 order = ((!char_order || !strcmp(char_order, ""))? -1:atoi(char_order));
174 XBT_DEBUG ("Task '%s' is scheduled on workstation '%d' in position '%d'", task->name, performer, order);
175 xbt_dynar_t computer = NULL;
176 if(performer != -1 && order != -1){
177 /* required parameters are given */
178 computer = (xbt_dynar_t)xbt_dict_get_or_null(computers, char_performer);
179 if(computer == NULL){
180 computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
181 xbt_dict_set(computers, char_performer, computer, NULL);
183 if(performer < xbt_dict_length(host_list)){
184 /* the wanted computer is available */
185 SD_task_t *task_test = NULL;
186 if((unsigned int)order < computer->used)
187 task_test = (SD_task_t *)xbt_dynar_get_ptr(computer,order);
188 if(task_test != NULL && *task_test != NULL && *task_test != task){
189 /* the user gives the same order to several tasks */
191 XBT_VERB("The task %s starts on the computer %s at the position : %s like the task %s",
192 (*task_test)->name, char_performer, char_order, task->name);
194 /* the parameter seems to be ok */
195 xbt_dynar_set_as(computer, order, SD_task_t, task);
198 /* the platform has not enough processors to schedule the DAG like
201 XBT_VERB("The schedule is ignored, there are not enough computers");
205 /* one of required parameters is not given */
207 XBT_VERB("The schedule is ignored, the task %s is not correctly scheduled", task->name);
211 XBT_WARN("Task '%s' is defined more than once", name);
215 /*Check if 'root' and 'end' nodes have been explicitly declared. If not, create them. */
216 if (!(root = (SD_task_t)xbt_dict_get_or_null(jobs, "root"))){
217 if (seq_or_par == sequential)
218 root = SD_task_create_comp_seq("root", NULL, 0);
220 root = SD_task_create_comp_par_amdahl("root", NULL, 0, 0);
221 /* by design the root task is always SCHEDULABLE */
222 SD_task_set_state(root, SD_SCHEDULABLE);
223 /* Put it at the beginning of the dynar */
224 xbt_dynar_insert_at(result, 0, &root);
227 if (!(end = (SD_task_t)xbt_dict_get_or_null(jobs, "end"))){
228 if (seq_or_par == sequential)
229 end = SD_task_create_comp_seq("end", NULL, 0);
231 end = SD_task_create_comp_par_amdahl("end", NULL, 0, 0);
235 xbt_dynar_t edges = xbt_dynar_new(sizeof(Agedge_t*), NULL);
236 for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
239 xbt_dynar_reset(edges);
240 for (edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge))
241 xbt_dynar_push_as(edges, Agedge_t *, edge);
243 /* Hack: circumvent a bug in libcgraph, where the edges are not always given back in creation order. We sort them
244 * again, according to their sequence id. The problem appears to be solved (i.e.: I did not test it) in graphviz'
245 * mercurial repository by the following changeset:
246 * changeset: 8431:d5f1fb7e8103
247 * user: Emden Gansner <erg@research.att.com>
248 * date: Tue Oct 11 12:38:58 2011 -0400
249 * summary: Make sure edges are stored in node creation order
250 * It should be fixed in graphviz 2.30 and above.
252 xbt_dynar_sort(edges, edge_compare);
254 xbt_dynar_foreach(edges, cursor, edge) {
256 char *src_name=agnameof(agtail(edge));
257 char *dst_name=agnameof(aghead(edge));
258 double size = atof(agget(edge, (char *) "size"));
260 src = (SD_task_t)xbt_dict_get_or_null(jobs, src_name);
261 dst = (SD_task_t)xbt_dict_get_or_null(jobs, dst_name);
264 char *name = (char*)xbt_malloc((strlen(src_name)+strlen(dst_name)+6)*sizeof(char));
265 sprintf(name, "%s->%s", src_name, dst_name);
266 XBT_DEBUG("See <transfer id=%s amount = %.0f>", name, size);
267 if (!(task = (SD_task_t)xbt_dict_get_or_null(jobs, name))) {
268 if (seq_or_par == sequential)
269 task = SD_task_create_comm_e2e(name, NULL , size);
271 task = SD_task_create_comm_par_mxn_1d_block(name, NULL , size);
272 SD_task_dependency_add(NULL, NULL, src, task);
273 SD_task_dependency_add(NULL, NULL, task, dst);
274 xbt_dict_set(jobs, name, task, NULL);
275 xbt_dynar_push(result, &task);
277 XBT_WARN("Task '%s' is defined more than once", name);
281 SD_task_dependency_add(NULL, NULL, src, dst);
285 xbt_dynar_free(&edges);
287 /* all compute and transfer tasks have been created, put the "end" node at the end of dynar */
288 XBT_DEBUG("All tasks have been created, put %s at the end of the dynar", end->name);
289 xbt_dynar_push(result, &end);
291 /* Connect entry tasks to 'root', and exit tasks to 'end'*/
292 xbt_dynar_foreach (result, i, task){
293 if (task == root || task == end)
295 if (xbt_dynar_is_empty(task->tasks_before)) {
296 XBT_DEBUG("file '%s' has no source. Add dependency from 'root'", task->name);
297 SD_task_dependency_add(NULL, NULL, root, task);
300 if (xbt_dynar_is_empty(task->tasks_after)) {
301 XBT_DEBUG("file '%s' has no destination. Add dependency to 'end'", task->name);
302 SD_task_dependency_add(NULL, NULL, task, end);
307 xbt_dict_free(&jobs);
310 if (!acyclic_graph_detail(result)) {
311 XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.", basename((char*)filename));
312 xbt_dynar_free(&result);