1 /* Copyright (c) 2009, 2010. 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)
30 void dot_add_task(Agnode_t * dag_node);
31 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge);
32 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge);
33 xbt_dynar_t SD_dotload_generic(const char * filename);
35 static double dot_parse_double(const char *string)
42 //ret = sscanf(string, "%lg", &value);
44 value = strtod(string,&err);
47 XBT_WARN("Failed to convert string to double: %s\n",strerror(errno));
54 static int dot_parse_int(const char *string)
61 ret = sscanf(string, "%d", &value);
63 XBT_WARN("%s is not an integer", string);
67 static xbt_dynar_t result;
68 static xbt_dict_t jobs;
69 static xbt_dict_t files;
70 static xbt_dict_t computers;
71 static SD_task_t root_task, end_task;
72 static Agraph_t *dag_dot;
73 static bool schedule = true;
75 static void dump_res()
79 xbt_dynar_foreach(result, cursor, task) {
80 XBT_INFO("Task %d", cursor);
86 static void dot_task_free(void *task)
92 static void TRACE_sd_dotloader (SD_task_t task, const char *category)
95 if (strlen (category) != 0){
96 TRACE_category (category);
97 SD_task_set_category (task, category);
102 /** @brief loads a DOT file describing a DAG
104 * See http://www.graphviz.org/doc/info/lang.html
106 * To obtain information about transfers and tasks, two attributes are
107 * required : size on task (execution time in Flop) and size on edge
108 * (the amount of data transfer in bit).
109 * if they aren't here, there choose to be equal to zero.
111 xbt_dynar_t SD_dotload(const char *filename)
113 SD_dotload_generic(filename);
114 xbt_dynar_t computer = NULL;
115 xbt_dict_cursor_t dict_cursor;
117 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
118 xbt_dynar_free(&computer);
120 xbt_dict_free(&computers);
124 xbt_dynar_t SD_dotload_with_sched(const char *filename){
125 SD_dotload_generic(filename);
127 if(schedule == true){
128 xbt_dynar_t computer = NULL;
129 xbt_dict_cursor_t dict_cursor;
131 const SD_workstation_t *workstations = SD_workstation_get_list ();
132 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
133 int count_computer = dot_parse_int(computer_name);
134 unsigned int count=0;
136 SD_task_t task_previous = NULL;
137 xbt_dynar_foreach(computer,count,task){
138 // add dependency between the previous and the task to avoid
139 // parallel execution
141 if(task_previous != NULL &&
142 !SD_task_dependency_exists(task_previous, task))
143 SD_task_dependency_add(NULL, NULL, task_previous, task);
144 SD_task_schedulel(task, 1, workstations[count_computer]);
145 task_previous = task;
148 xbt_dynar_free(&computer);
150 xbt_dict_free(&computers);
151 if(acyclic_graph_detail(result))
154 XBT_WARN("There is at least one cycle in the provided task graph");
156 XBT_WARN("The scheduling is ignored");
161 xbt_dynar_t SD_dotload_generic(const char * filename)
163 xbt_assert(filename, "Unable to use a null file descriptor\n");
164 //dag_dot = agopen((char*)filename,Agstrictdirected,0);
165 FILE *in_file = fopen(filename, "r");
166 dag_dot = agread(in_file, NIL(Agdisc_t *));
168 result = xbt_dynar_new(sizeof(SD_task_t), dot_task_free);
169 files = xbt_dict_new_homogeneous(&dot_task_free);
170 jobs = xbt_dict_new_homogeneous(NULL);
171 computers = xbt_dict_new_homogeneous(NULL);
172 root_task = SD_task_create_comp_seq("root", NULL, 0);
173 /* by design the root task is always SCHEDULABLE */
174 __SD_task_set_state(root_task, SD_SCHEDULABLE);
176 xbt_dict_set(jobs, "root", root_task, NULL);
177 xbt_dynar_push(result, &root_task);
178 end_task = SD_task_create_comp_seq("end", NULL, 0);
179 xbt_dict_set(jobs, "end", end_task, NULL);
181 Agnode_t *dag_node = NULL;
182 for (dag_node = agfstnode(dag_dot); dag_node; dag_node = agnxtnode(dag_dot, dag_node)) {
183 dot_add_task(dag_node);
186 xbt_dict_free(&jobs);
188 /* And now, post-process the files.
189 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
190 * Files not produced in the system are said to be produced by root task (top of DAG).
191 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
193 xbt_dict_cursor_t cursor;
196 xbt_dict_foreach(files, cursor, name, file) {
197 unsigned int cpt1, cpt2;
198 SD_task_t newfile = NULL;
199 SD_dependency_t depbefore, depafter;
200 if (xbt_dynar_is_empty(file->tasks_before)) {
201 xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
203 SD_task_create_comm_e2e(file->name, NULL, file->amount);
204 SD_task_dependency_add(NULL, NULL, root_task, newfile);
205 SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
206 xbt_dynar_push(result, &newfile);
208 } else if (xbt_dynar_is_empty(file->tasks_after)) {
209 xbt_dynar_foreach(file->tasks_before, cpt2, depbefore) {
211 SD_task_create_comm_e2e(file->name, NULL, file->amount);
212 SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
213 SD_task_dependency_add(NULL, NULL, newfile, end_task);
214 xbt_dynar_push(result, &newfile);
217 xbt_dynar_foreach(file->tasks_before, cpt1, depbefore) {
218 xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
219 if (depbefore->src == depafter->dst) {
221 ("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
222 file->name, depbefore->src->name);
225 SD_task_create_comm_e2e(file->name, NULL, file->amount);
226 SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
227 SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
228 xbt_dynar_push(result, &newfile);
234 /* Push end task last */
235 xbt_dynar_push(result, &end_task);
237 /* Free previous copy of the files */
238 xbt_dict_free(&files);
240 if(acyclic_graph_detail(result))
244 XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.",
245 basename((char*)filename));
246 xbt_dynar_foreach(result, cpt, file)
247 SD_task_destroy(file);
248 xbt_dynar_free_container(&result);
253 /* dot_add_task create a sd_task and all transfers required for this
254 * task. The execution time of the task is given by the attribute size.
255 * The unit of size is the Flop.*/
256 void dot_add_task(Agnode_t * dag_node)
258 char *name = agnameof(dag_node);
259 SD_task_t current_job;
260 double runtime = dot_parse_double(agget(dag_node, (char *) "size"));
262 XBT_DEBUG("See <job id=%s runtime=%s %.0f>", name,
263 agget(dag_node, (char *) "size"), runtime);
264 current_job = xbt_dict_get_or_null(jobs, name);
265 if (current_job == NULL) {
267 SD_task_create_comp_seq(name, NULL , runtime);
269 TRACE_sd_dotloader (current_job, agget (dag_node, (char*)"category"));
271 xbt_dict_set(jobs, name, current_job, NULL);
272 xbt_dynar_push(result, ¤t_job);
277 for (e = agfstin(dag_dot, dag_node); e; e = agnxtin(dag_dot, e)) {
278 dot_add_input_dependencies(current_job, e);
281 if (count == 0 && current_job != root_task) {
282 SD_task_dependency_add(NULL, NULL, root_task, current_job);
285 for (e = agfstout(dag_dot, dag_node); e; e = agnxtout(dag_dot, e)) {
286 dot_add_output_dependencies(current_job, e);
289 if (count == 0 && current_job != end_task) {
290 SD_task_dependency_add(NULL, NULL, current_job, end_task);
293 if(schedule || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose)){
294 /* try to take the information to schedule the task only if all is
296 // performer is the computer which execute the task
297 unsigned long performer = -1;
298 char * char_performer = agget(dag_node, (char *) "performer");
299 if (char_performer != NULL)
300 performer = (long) dot_parse_int(char_performer);
302 // order is giving the task order on one computer
303 unsigned long order = -1;
304 char * char_order = agget(dag_node, (char *) "order");
305 if (char_order != NULL)
306 order = (long) dot_parse_int(char_order);
307 xbt_dynar_t computer = NULL;
308 //XBT_INFO("performer = %d, order=%d",performer,order);
309 if(performer != -1 && order != -1){
310 //necessary parameters are given
311 computer = xbt_dict_get_or_null(computers, char_performer);
312 if(computer == NULL){
313 computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
314 xbt_dict_set(computers, char_performer, computer, NULL);
316 if(performer < xbt_lib_length(host_lib)){
317 // the wanted computer is available
318 SD_task_t *task_test = NULL;
319 if(order < computer->used)
320 task_test = xbt_dynar_get_ptr(computer,order);
321 if(task_test != NULL && *task_test != NULL && *task_test != current_job){
322 /*the user gives the same order to several tasks*/
324 XBT_VERB("The task %s starts on the computer %s at the position : %s like the task %s",
325 (*task_test)->name, char_performer, char_order, current_job->name);
327 //the parameter seems to be ok
328 xbt_dynar_set_as(computer, order, SD_task_t, current_job);
331 /*the platform has not enough processors to schedule the DAG like
334 XBT_VERB("The schedule is ignored, there are not enough computers");
338 //one of necessary parameters are not given
340 XBT_VERB("The schedule is ignored, the task %s is not correctly scheduled", current_job->name);
345 /* dot_add_output_dependencies create the dependencies between a task
346 * and a transfers. This is given by the edges in the dot file.
347 * The amount of data transfers is given by the attribute size on the
349 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge)
351 SD_task_t file = NULL;
352 char *name_tail=agnameof(agtail(edge));
353 char *name_head=agnameof(aghead(edge));
354 char *name = malloc((strlen(name_head)+strlen(name_tail)+6)*sizeof(char));
355 sprintf(name, "%s->%s", name_tail, name_head);
356 double size = dot_parse_double(agget(edge, (char *) "size"));
357 XBT_DEBUG("size : %e, get size : %s", size, agget(edge, (char *) "size"));
360 file = xbt_dict_get_or_null(files, name);
362 file = SD_task_create_comm_e2e(name, NULL, size);
364 TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
366 xbt_dict_set(files, name, file, NULL);
368 if (SD_task_get_amount(file) != size) {
369 XBT_WARN("Ignoring file %s size redefinition from %.0f to %.0f",
370 name, SD_task_get_amount(file), size);
373 SD_task_dependency_add(NULL, NULL, file, current_job);
375 file = xbt_dict_get_or_null(jobs, name_tail);
377 SD_task_dependency_add(NULL, NULL, file, current_job);
383 /* dot_add_output_dependencies create the dependencies between a
384 * transfers and a task. This is given by the edges in the dot file.
385 * The amount of data transfers is given by the attribute size on the
387 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge)
390 char *name_tail=agnameof(agtail(edge));
391 char *name_head=agnameof(aghead(edge));
392 char *name = malloc((strlen(name_head)+strlen(name_tail)+6)*sizeof(char));
393 sprintf(name, "%s->%s", name_tail, name_head);
394 double size = dot_parse_double(agget(edge, (char *) "size"));
395 XBT_DEBUG("size : %e, get size : %s", size, agget(edge, (char *) "size"));
398 file = xbt_dict_get_or_null(files, name);
400 file = SD_task_create_comm_e2e(name, NULL, size);
402 TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
404 xbt_dict_set(files, name, file, NULL);
406 if (SD_task_get_amount(file) != size) {
407 XBT_WARN("Ignoring file %s size redefinition from %.0f to %.0f",
408 name, SD_task_get_amount(file), size);
411 SD_task_dependency_add(NULL, NULL, current_job, file);
412 if (xbt_dynar_length(file->tasks_before) > 1) {
413 XBT_WARN("File %s created at more than one location...", file->name);
416 file = xbt_dict_get_or_null(jobs, name_head);
418 SD_task_dependency_add(NULL, NULL, current_job, file);