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 xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par);
37 static xbt_dynar_t result;
38 static xbt_dict_t jobs;
39 static xbt_dict_t computers;
40 static Agraph_t *dag_dot;
41 static bool schedule = true;
43 static void dot_task_p_free(void *task) {
49 static void TRACE_sd_dotloader (SD_task_t task, const char *category) {
50 if (category && strlen (category)){
52 XBT_DEBUG("Change the category of %s from %s to %s",
53 task->name, task->category, category);
55 XBT_DEBUG("Set the category of %s to %s",task->name, category);
56 TRACE_category (category);
57 TRACE_sd_set_task_category(task, category);
62 /** @brief loads a DOT file describing a DAG
64 * See http://www.graphviz.org/doc/info/lang.html
66 * To obtain information about transfers and tasks, two attributes are
67 * required : size on task (execution time in Flop) and size on edge
68 * (the amount of data transfer in bit).
69 * if they aren't here, there choose to be equal to zero.
71 xbt_dynar_t SD_dotload(const char *filename) {
72 computers = xbt_dict_new_homogeneous(NULL);
74 SD_dotload_generic(filename, sequential);
75 xbt_dynar_t computer = NULL;
76 xbt_dict_cursor_t dict_cursor;
78 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
79 xbt_dynar_free(&computer);
81 xbt_dict_free(&computers);
85 xbt_dynar_t SD_dotload_with_sched(const char *filename) {
86 computers = xbt_dict_new_homogeneous(NULL);
87 SD_dotload_generic(filename, sequential);
90 xbt_dynar_t computer = NULL;
91 xbt_dict_cursor_t dict_cursor;
93 const SD_workstation_t *workstations = SD_workstation_get_list ();
94 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
95 int count_computer = atoi(computer_name);
98 SD_task_t task_previous = NULL;
99 xbt_dynar_foreach(computer,count,task){
100 /* add dependency between the previous and the task to avoid
101 * parallel execution */
103 if(task_previous != NULL &&
104 !SD_task_dependency_exists(task_previous, task))
105 SD_task_dependency_add(NULL, NULL, task_previous, task);
106 SD_task_schedulel(task, 1, workstations[count_computer]);
107 task_previous = task;
110 xbt_dynar_free(&computer);
112 xbt_dict_free(&computers);
113 if(acyclic_graph_detail(result))
116 XBT_WARN("There is at least one cycle in the provided task graph");
118 XBT_WARN("The scheduling is ignored");
122 xbt_dynar_t computer = NULL;
123 xbt_dict_cursor_t dict_cursor;
125 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
126 xbt_dynar_free(&computer);
128 xbt_dict_free(&computers);
129 xbt_dynar_foreach(result,count,task){
130 SD_task_destroy(task);
135 xbt_dynar_t SD_PTG_dotload(const char * filename) {
136 xbt_dynar_t result = SD_dotload_generic(filename, parallel);
137 if (!acyclic_graph_detail(result)) {
138 XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.",
139 basename((char*)filename));
140 xbt_dynar_free(&result);
141 /* (result == NULL) here */
147 static int edge_compare(const void *a, const void *b)
149 unsigned va = AGSEQ(*(Agedge_t **)a);
150 unsigned vb = AGSEQ(*(Agedge_t **)b);
151 return va == vb ? 0 : (va < vb ? -1 : 1);
155 xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par){
156 xbt_assert(filename, "Unable to use a null file descriptor\n");
158 result = xbt_dynar_new(sizeof(SD_task_t), dot_task_p_free);
159 jobs = xbt_dict_new_homogeneous(NULL);
160 FILE *in_file = fopen(filename, "r");
162 xbt_die("Failed to open file: %s", filename);
163 dag_dot = agread(in_file, NIL(Agdisc_t *));
164 SD_task_t root, end, task;
166 * Create all the nodes
168 Agnode_t *node = NULL;
169 for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
171 char *name = agnameof(node);
172 double amount = atof(agget(node, (char *) "size"));
175 if (seq_or_par == sequential){
176 XBT_DEBUG("See <job id=%s amount =%.0f>", name, amount);
178 if (!strcmp(agget(node, (char *) "alpha"), "")){
179 alpha = atof(agget(node, (char *) "alpha"));
181 XBT_DEBUG("negative alpha value provided. Set to 0.");
185 XBT_DEBUG("no alpha value provided. Set to 0");
189 XBT_DEBUG("See <job id=%s amount =%.0f alpha = %.3f>",
190 name, amount, alpha);
193 if (!(task = xbt_dict_get_or_null(jobs, name))) {
194 if (seq_or_par == sequential){
195 task = SD_task_create_comp_seq(name, NULL , amount);
197 task = SD_task_create_comp_par_amdahl(name, NULL , amount, alpha);
200 TRACE_sd_dotloader (task, agget (node, (char*)"category"));
202 xbt_dict_set(jobs, name, task, NULL);
203 if (!strcmp(name, "root")){
204 /* by design the root task is always SCHEDULABLE */
205 __SD_task_set_state(task, SD_SCHEDULABLE);
206 /* Put it at the beginning of the dynar */
207 xbt_dynar_insert_at(result, 0, &task);
209 if (!strcmp(name, "end")){
210 XBT_DEBUG("Declaration of the 'end' node, don't store it yet.");
212 /* Should be inserted later in the dynar */
214 xbt_dynar_push(result, &task);
218 if((seq_or_par == sequential) &&
220 XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose))){
221 /* try to take the information to schedule the task only if all is
223 int performer, order;
224 char *char_performer = agget(node, (char *) "performer");
225 char *char_order = agget(node, (char *) "order");
226 /* performer is the computer which execute the task */
228 ((!char_performer || !strcmp(char_performer,"")) ? -1:atoi(char_performer));
229 /* order is giving the task order on one computer */
230 order = ((!char_order || !strcmp(char_order, ""))? -1:atoi(char_order));
232 XBT_DEBUG ("Task '%s' is scheduled on workstation '%d' in position '%d'",
233 task->name, performer, order);
234 xbt_dynar_t computer = NULL;
235 if(performer != -1 && order != -1){
236 /* required parameters are given */
237 computer = xbt_dict_get_or_null(computers, char_performer);
238 if(computer == NULL){
239 computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
240 xbt_dict_set(computers, char_performer, computer, NULL);
242 if(performer < xbt_lib_length(host_lib)){
243 /* the wanted computer is available */
244 SD_task_t *task_test = NULL;
245 if(order < computer->used)
246 task_test = xbt_dynar_get_ptr(computer,order);
247 if(task_test != NULL && *task_test != NULL && *task_test != task){
248 /* the user gives the same order to several tasks */
250 XBT_VERB("The task %s starts on the computer %s at the position : %s like the task %s",
251 (*task_test)->name, char_performer, char_order,
254 /* the parameter seems to be ok */
255 xbt_dynar_set_as(computer, order, SD_task_t, task);
258 /* the platform has not enough processors to schedule the DAG like
261 XBT_VERB("The schedule is ignored, there are not enough computers");
265 /* one of required parameters is not given */
267 XBT_VERB("The schedule is ignored, the task %s is not correctly scheduled",
272 XBT_WARN("Task '%s' is defined more than once", name);
277 * Check if 'root' and 'end' nodes have been explicitly declared.
278 * If not, create them.
280 if (!(root = xbt_dict_get_or_null(jobs, "root"))){
281 if (seq_or_par == sequential)
282 root = SD_task_create_comp_seq("root", NULL, 0);
284 root = SD_task_create_comp_par_amdahl("root", NULL, 0, 0);
285 /* by design the root task is always SCHEDULABLE */
286 __SD_task_set_state(root, SD_SCHEDULABLE);
287 /* Put it at the beginning of the dynar */
288 xbt_dynar_insert_at(result, 0, &root);
291 if (!(end = xbt_dict_get_or_null(jobs, "end"))){
292 if (seq_or_par == sequential)
293 end = SD_task_create_comp_seq("end", NULL, 0);
295 end = SD_task_create_comp_par_amdahl("end", NULL, 0, 0);
296 /* Should be inserted later in the dynar */
302 xbt_dynar_t edges = xbt_dynar_new(sizeof(Agedge_t*), NULL);
303 for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
306 xbt_dynar_reset(edges);
307 for (edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge))
308 xbt_dynar_push_as(edges, Agedge_t *, edge);
310 /* Hack: circumvent a bug in libcgraph, where the edges are not always given
311 * back in creation order. We sort them again, according to their sequence
312 * id. The problem appears to be solved (i.e.: I did not test it) in
313 * graphviz' mercurial repository by the following changeset:
314 * changeset: 8431:d5f1fb7e8103
315 * user: Emden Gansner <erg@research.att.com>
316 * date: Tue Oct 11 12:38:58 2011 -0400
317 * summary: Make sure edges are stored in node creation order
318 * It should be fixed in graphviz 2.30 and above.
320 xbt_dynar_sort(edges, edge_compare);
322 xbt_dynar_foreach(edges, cursor, edge) {
324 char *src_name=agnameof(agtail(edge));
325 char *dst_name=agnameof(aghead(edge));
326 double size = atof(agget(edge, (char *) "size"));
328 src = xbt_dict_get_or_null(jobs, src_name);
329 dst = xbt_dict_get_or_null(jobs, dst_name);
333 xbt_malloc((strlen(src_name)+strlen(dst_name)+6)*sizeof(char));
334 sprintf(name, "%s->%s", src_name, dst_name);
335 XBT_DEBUG("See <transfer id=%s amount = %.0f>", name, size);
336 if (!(task = xbt_dict_get_or_null(jobs, name))) {
337 if (seq_or_par == sequential)
338 task = SD_task_create_comm_e2e(name, NULL , size);
340 task = SD_task_create_comm_par_mxn_1d_block(name, NULL , size);
342 TRACE_sd_dotloader (task, agget (node, (char*)"category"));
344 SD_task_dependency_add(NULL, NULL, src, task);
345 SD_task_dependency_add(NULL, NULL, task, dst);
346 xbt_dict_set(jobs, name, task, NULL);
347 xbt_dynar_push(result, &task);
349 XBT_WARN("Task '%s' is defined more than once", name);
353 SD_task_dependency_add(NULL, NULL, src, dst);
357 xbt_dynar_free(&edges);
359 /* all compute and transfer tasks have been created, put the "end" node at
362 XBT_DEBUG("All tasks have been created, put %s at the end of the dynar",
364 xbt_dynar_push(result, &end);
366 /* Connect entry tasks to 'root', and exit tasks to 'end'*/
368 xbt_dynar_foreach (result, i, task){
369 if (task == root || task == end)
371 if (xbt_dynar_is_empty(task->tasks_before)) {
372 XBT_DEBUG("file '%s' has no source. Add dependency from 'root'",
374 SD_task_dependency_add(NULL, NULL, root, task);
375 } else if (xbt_dynar_is_empty(task->tasks_after)) {
376 XBT_DEBUG("file '%s' has no destination. Add dependency to 'end'",
378 SD_task_dependency_add(NULL, NULL, task, end);
383 xbt_dict_free(&jobs);
386 if (!acyclic_graph_detail(result)) {
387 XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.",
388 basename((char*)filename));
389 xbt_dynar_free(&result);
390 /* (result == NULL) here */