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_free(void *task) {
48 static void dot_task_p_free(void *task) {
54 static void TRACE_sd_dotloader (SD_task_t task, const char *category) {
55 if (category && strlen (category)){
57 XBT_DEBUG("Change the category of %s from %s to %s",
58 task->name, task->category, category);
60 XBT_DEBUG("Set the category of %s to %s",task->name, category);
61 TRACE_category (category);
62 TRACE_sd_set_task_category(task, category);
67 /** @brief loads a DOT file describing a DAG
69 * See http://www.graphviz.org/doc/info/lang.html
71 * To obtain information about transfers and tasks, two attributes are
72 * required : size on task (execution time in Flop) and size on edge
73 * (the amount of data transfer in bit).
74 * if they aren't here, there choose to be equal to zero.
76 xbt_dynar_t SD_dotload(const char *filename) {
77 computers = xbt_dict_new_homogeneous(NULL);
79 SD_dotload_generic(filename, sequential);
80 xbt_dynar_t computer = NULL;
81 xbt_dict_cursor_t dict_cursor;
83 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
84 xbt_dynar_free(&computer);
86 xbt_dict_free(&computers);
90 xbt_dynar_t SD_dotload_with_sched(const char *filename) {
91 computers = xbt_dict_new_homogeneous(NULL);
92 SD_dotload_generic(filename, sequential);
95 xbt_dynar_t computer = NULL;
96 xbt_dict_cursor_t dict_cursor;
98 const SD_workstation_t *workstations = SD_workstation_get_list ();
99 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
100 int count_computer = atoi(computer_name);
101 unsigned int count=0;
103 SD_task_t task_previous = NULL;
104 xbt_dynar_foreach(computer,count,task){
105 /* add dependency between the previous and the task to avoid
106 * parallel execution */
108 if(task_previous != NULL &&
109 !SD_task_dependency_exists(task_previous, task))
110 SD_task_dependency_add(NULL, NULL, task_previous, task);
111 SD_task_schedulel(task, 1, workstations[count_computer]);
112 task_previous = task;
115 xbt_dynar_free(&computer);
117 xbt_dict_free(&computers);
118 if(acyclic_graph_detail(result))
121 XBT_WARN("There is at least one cycle in the provided task graph");
123 XBT_WARN("The scheduling is ignored");
127 xbt_dynar_t computer = NULL;
128 xbt_dict_cursor_t dict_cursor;
130 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
131 xbt_dynar_free(&computer);
133 xbt_dict_free(&computers);
134 xbt_dynar_foreach(result,count,task){
135 SD_task_destroy(task);
140 xbt_dynar_t SD_PTG_dotload(const char * filename) {
141 xbt_dynar_t result = SD_dotload_generic(filename, parallel);
142 if (!acyclic_graph_detail(result)) {
143 XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.",
144 basename((char*)filename));
145 xbt_dynar_free(&result);
146 /* (result == NULL) here */
151 xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par){
152 xbt_assert(filename, "Unable to use a null file descriptor\n");
154 result = xbt_dynar_new(sizeof(SD_task_t), dot_task_p_free);
155 jobs = xbt_dict_new_homogeneous(NULL);
156 FILE *in_file = fopen(filename, "r");
157 dag_dot = agread(in_file, NIL(Agdisc_t *));
158 SD_task_t root, end, task;
160 * Create all the nodes
162 Agnode_t *node = NULL;
163 for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
165 char *name = agnameof(node);
166 double amount = atof(agget(node, (char *) "size"));
169 if (seq_or_par == sequential){
170 XBT_DEBUG("See <job id=%s amount =%.0f>", name, amount);
172 if (!strcmp(agget(node, (char *) "alpha"), "")){
173 alpha = atof(agget(node, (char *) "alpha"));
175 XBT_DEBUG("negative alpha value provided. Set to 0.");
179 XBT_DEBUG("no alpha value provided. Set to 0");
183 XBT_DEBUG("See <job id=%s amount =%.0f alpha = %.3f>",
184 name, amount, alpha);
187 if (!(task = xbt_dict_get_or_null(jobs, name))) {
188 if (seq_or_par == sequential){
189 task = SD_task_create_comp_seq(name, NULL , amount);
191 task = SD_task_create_comp_par_amdahl(name, NULL , amount, alpha);
194 TRACE_sd_dotloader (task, agget (node, (char*)"category"));
196 xbt_dict_set(jobs, name, task, NULL);
197 if (!strcmp(name, "root")){
198 /* by design the root task is always SCHEDULABLE */
199 __SD_task_set_state(task, SD_SCHEDULABLE);
200 /* Put it at the beginning of the dynar */
201 xbt_dynar_insert_at(result, 0, &task);
203 if (!strcmp(name, "end")){
204 XBT_DEBUG("Declaration of the 'end' node, don't store it yet.");
206 /* Should be inserted later in the dynar */
208 xbt_dynar_push(result, &task);
212 if((seq_or_par == sequential) &&
214 XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose))){
215 /* try to take the information to schedule the task only if all is
217 int performer, order;
218 char *char_performer = NULL, *char_order=NULL;
219 /* performer is the computer which execute the task */
222 !strcmp((char_performer = agget(node, (char *) "performer")),
223 "")) ? -1:atoi(char_performer));
224 /* order is giving the task order on one computer */
225 order = ((!char_order ||
226 !strcmp((char_order = agget(node, (char *) "order")), ""))?
227 -1:atoi(char_order));
229 XBT_DEBUG ("Task '%s' is scheduled on workstation '%d' in position '%d'",
230 task->name, performer, order);
231 xbt_dynar_t computer = NULL;
232 if(performer != -1 && order != -1){
233 /* required parameters are given */
234 computer = xbt_dict_get_or_null(computers, char_performer);
235 if(computer == NULL){
236 computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
237 xbt_dict_set(computers, char_performer, computer, NULL);
239 if(performer < xbt_lib_length(host_lib)){
240 /* the wanted computer is available */
241 SD_task_t *task_test = NULL;
242 if(order < computer->used)
243 task_test = xbt_dynar_get_ptr(computer,order);
244 if(task_test != NULL && *task_test != NULL && *task_test != task){
245 /* the user gives the same order to several tasks */
247 XBT_VERB("The task %s starts on the computer %s at the position : %s like the task %s",
248 (*task_test)->name, char_performer, char_order,
251 /* the parameter seems to be ok */
252 xbt_dynar_set_as(computer, order, SD_task_t, task);
255 /* the platform has not enough processors to schedule the DAG like
258 XBT_VERB("The schedule is ignored, there are not enough computers");
262 /* one of required parameters is not given */
264 XBT_VERB("The schedule is ignored, the task %s is not correctly scheduled",
269 XBT_WARN("Task '%s' is defined more than once", name);
274 * Check if 'root' and 'end' nodes have been explicitly declared.
275 * If not, create them.
277 if (!(root = xbt_dict_get_or_null(jobs, "root"))){
278 if (seq_or_par == sequential)
279 root = SD_task_create_comp_seq("root", NULL, 0);
281 root = SD_task_create_comp_par_amdahl("root", NULL, 0, 0);
282 /* by design the root task is always SCHEDULABLE */
283 __SD_task_set_state(root, SD_SCHEDULABLE);
284 /* Put it at the beginning of the dynar */
285 xbt_dynar_insert_at(result, 0, &root);
288 if (!(end = xbt_dict_get_or_null(jobs, "end"))){
289 if (seq_or_par == sequential)
290 end = SD_task_create_comp_seq("end", NULL, 0);
292 end = SD_task_create_comp_par_amdahl("end", NULL, 0, 0);
293 /* Should be inserted later in the dynar */
300 for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
301 Agedge_t * edge = NULL;
302 for (edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge)) {
304 char *src_name=agnameof(agtail(edge));
305 char *dst_name=agnameof(aghead(edge));
306 double size = atof(agget(edge, (char *) "size"));
308 src = xbt_dict_get_or_null(jobs, src_name);
309 dst = xbt_dict_get_or_null(jobs, dst_name);
313 xbt_malloc((strlen(src_name)+strlen(dst_name)+6)*sizeof(char));
314 sprintf(name, "%s->%s", src_name, dst_name);
315 XBT_DEBUG("See <transfer id=%s amount = %.0f>", name, size);
316 if (!(task = xbt_dict_get_or_null(jobs, name))) {
317 if (seq_or_par == sequential)
318 task = SD_task_create_comm_e2e(name, NULL , size);
320 task = SD_task_create_comm_par_mxn_1d_block(name, NULL , size);
322 TRACE_sd_dotloader (task, agget (node, (char*)"category"));
324 SD_task_dependency_add(NULL, NULL, src, task);
325 SD_task_dependency_add(NULL, NULL, task, dst);
326 xbt_dict_set(jobs, name, task, NULL);
327 xbt_dynar_push(result, &task);
329 XBT_WARN("Task '%s' is defined more than once", name);
333 SD_task_dependency_add(NULL, NULL, src, dst);
338 /* all compute and transfer tasks have been created, put the "end" node at
341 XBT_DEBUG("All tasks have been created, put %s at the end of the dynar",
343 xbt_dynar_push(result, &end);
345 /* Connect entry tasks to 'root', and exit tasks to 'end'*/
347 xbt_dynar_foreach (result, i, task){
348 if (task == root || task == end)
350 if (xbt_dynar_is_empty(task->tasks_before)) {
351 XBT_DEBUG("file '%s' has no source. Add dependency from 'root'",
353 SD_task_dependency_add(NULL, NULL, root, task);
354 } else if (xbt_dynar_is_empty(task->tasks_after)) {
355 XBT_DEBUG("file '%s' has no destination. Add dependency to 'end'",
357 SD_task_dependency_add(NULL, NULL, task, end);
362 xbt_dict_free(&jobs);
364 if (!acyclic_graph_detail(result)) {
365 XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.",
366 basename((char*)filename));
367 xbt_dynar_free(&result);
368 /* (result == NULL) here */