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>
25 void dot_add_task(Agnode_t * dag_node);
26 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge);
27 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge);
28 xbt_dynar_t SD_dotload_generic(const char * filename);
30 static double dot_parse_double(const char *string)
37 //ret = sscanf(string, "%lg", &value);
39 value = strtod(string,&err);
42 XBT_WARN("Failed to convert string to double: %s\n",strerror(errno));
49 static int dot_parse_int(const char *string)
56 ret = sscanf(string, "%d", &value);
58 XBT_WARN("%s is not an integer", string);
62 static xbt_dynar_t result;
63 static xbt_dict_t jobs;
64 static xbt_dict_t files;
65 static xbt_dict_t computers;
66 static SD_task_t root_task, end_task;
67 static Agraph_t *dag_dot;
68 static bool schedule = true;
70 static void dump_res()
74 xbt_dynar_foreach(result, cursor, task) {
75 XBT_INFO("Task %d", cursor);
81 static void dot_task_free(void *task)
87 static void TRACE_sd_dotloader (SD_task_t task, const char *category)
90 if (strlen (category) != 0){
91 TRACE_category (category);
92 SD_task_set_category (task, category);
97 /** @brief loads a DOT file describing a DAG
99 * See http://www.graphviz.org/doc/info/lang.html
101 * To obtain information about transfers and tasks, two attributes are
102 * required : size on task (execution time in Flop) and size on edge
103 * (the amount of data transfer in bit).
104 * if they aren't here, there choose to be equal to zero.
106 xbt_dynar_t SD_dotload(const char *filename)
108 SD_dotload_generic(filename);
109 xbt_dynar_t computer = NULL;
110 xbt_dict_cursor_t dict_cursor;
112 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
113 xbt_dynar_free(&computer);
115 xbt_dict_free(&computers);
119 xbt_dynar_t SD_dotload_with_sched(const char *filename){
120 SD_dotload_generic(filename);
122 if(schedule == true){
123 xbt_dynar_t computer = NULL;
124 xbt_dict_cursor_t dict_cursor;
126 const SD_workstation_t *workstations = SD_workstation_get_list ();
127 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
128 int count_computer = dot_parse_int(computer_name);
129 unsigned int count=0;
131 SD_task_t task_previous = NULL;
132 xbt_dynar_foreach(computer,count,task){
133 // add dependency between the previous and the task to avoid
134 // parallel execution
136 if(task_previous != NULL &&
137 !SD_task_dependency_exists(task_previous, task))
138 SD_task_dependency_add(NULL, NULL, task_previous, task);
139 SD_task_schedulel(task, 1, workstations[count_computer]);
140 task_previous = task;
143 xbt_dynar_free(&computer);
145 xbt_dict_free(&computers);
146 if(acyclic_graph_detail(result))
149 XBT_WARN("There is at least one cycle in the provided task graph");
151 XBT_WARN("The scheduling is ignored");
156 xbt_dynar_t SD_dotload_generic(const char * filename)
158 xbt_assert(filename, "Unable to use a null file descriptor\n");
159 //dag_dot = agopen((char*)filename,Agstrictdirected,0);
160 FILE *in_file = fopen(filename, "r");
161 dag_dot = agread(in_file, NIL(Agdisc_t *));
163 result = xbt_dynar_new(sizeof(SD_task_t), dot_task_free);
164 files = xbt_dict_new_homogeneous(&dot_task_free);
165 jobs = xbt_dict_new_homogeneous(NULL);
166 computers = xbt_dict_new_homogeneous(NULL);
167 root_task = SD_task_create_comp_seq("root", NULL, 0);
168 /* by design the root task is always SCHEDULABLE */
169 __SD_task_set_state(root_task, SD_SCHEDULABLE);
171 xbt_dict_set(jobs, "root", root_task, NULL);
172 xbt_dynar_push(result, &root_task);
173 end_task = SD_task_create_comp_seq("end", NULL, 0);
174 xbt_dict_set(jobs, "end", end_task, NULL);
176 Agnode_t *dag_node = NULL;
177 for (dag_node = agfstnode(dag_dot); dag_node;
179 dag_node = agnxtnode(dag_dot, dag_node)
181 dag_node = agnxtnode(dag_node)
185 dot_add_task(dag_node);
188 xbt_dict_free(&jobs);
190 /* And now, post-process the files.
191 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
192 * Files not produced in the system are said to be produced by root task (top of DAG).
193 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
195 xbt_dict_cursor_t cursor;
198 xbt_dict_foreach(files, cursor, name, file) {
199 unsigned int cpt1, cpt2;
200 SD_task_t newfile = NULL;
201 SD_dependency_t depbefore, depafter;
202 if (xbt_dynar_is_empty(file->tasks_before)) {
203 xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
205 SD_task_create_comm_e2e(file->name, NULL, file->amount);
206 SD_task_dependency_add(NULL, NULL, root_task, newfile);
207 SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
208 xbt_dynar_push(result, &newfile);
210 } else if (xbt_dynar_is_empty(file->tasks_after)) {
211 xbt_dynar_foreach(file->tasks_before, cpt2, depbefore) {
213 SD_task_create_comm_e2e(file->name, NULL, file->amount);
214 SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
215 SD_task_dependency_add(NULL, NULL, newfile, end_task);
216 xbt_dynar_push(result, &newfile);
219 xbt_dynar_foreach(file->tasks_before, cpt1, depbefore) {
220 xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
221 if (depbefore->src == depafter->dst) {
223 ("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
224 file->name, depbefore->src->name);
227 SD_task_create_comm_e2e(file->name, NULL, file->amount);
228 SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
229 SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
230 xbt_dynar_push(result, &newfile);
236 /* Push end task last */
237 xbt_dynar_push(result, &end_task);
239 /* Free previous copy of the files */
240 xbt_dict_free(&files);
242 if(acyclic_graph_detail(result))
246 XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.",
247 basename((char*)filename));
248 xbt_dynar_foreach(result, cpt, file)
249 SD_task_destroy(file);
250 xbt_dynar_free_container(&result);
256 /* dot_add_task create a sd_task and all transfers required for this
257 * task. The execution time of the task is given by the attribute size.
258 * The unit of size is the Flop.*/
259 void dot_add_task(Agnode_t * dag_node)
261 char *name = agnameof(dag_node);
262 SD_task_t current_job;
263 double runtime = dot_parse_double(agget(dag_node, (char *) "size"));
265 XBT_DEBUG("See <job id=%s runtime=%s %.0f>", name,
266 agget(dag_node, (char *) "size"), runtime);
267 current_job = xbt_dict_get_or_null(jobs, name);
268 if (current_job == NULL) {
270 SD_task_create_comp_seq(name, NULL , runtime);
272 TRACE_sd_dotloader (current_job, agget (dag_node, (char*)"category"));
274 xbt_dict_set(jobs, name, current_job, NULL);
275 xbt_dynar_push(result, ¤t_job);
281 for (e = agfstin(dag_dot, dag_node); e; e = agnxtin(dag_dot, e))
283 for (e = agfstin(dag_node); e; e = agnxtin(e))
286 dot_add_input_dependencies(current_job, e);
289 if (count == 0 && current_job != root_task) {
290 SD_task_dependency_add(NULL, NULL, root_task, current_job);
294 for (e = agfstout(dag_dot, dag_node); e; e = agnxtout(dag_dot, e))
296 for (e = agfstout(dag_node); e; e = agnxtout(e))
300 dot_add_output_dependencies(current_job, e);
303 if (count == 0 && current_job != end_task) {
304 SD_task_dependency_add(NULL, NULL, current_job, end_task);
307 if(schedule || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose)){
308 /* try to take the information to schedule the task only if all is
310 // performer is the computer which execute the task
311 unsigned long performer = -1;
312 char * char_performer = agget(dag_node, (char *) "performer");
313 if (char_performer != NULL)
314 performer = (long) dot_parse_int(char_performer);
316 // order is giving the task order on one computer
317 unsigned long order = -1;
318 char * char_order = agget(dag_node, (char *) "order");
319 if (char_order != NULL)
320 order = (long) dot_parse_int(char_order);
321 xbt_dynar_t computer = NULL;
322 //XBT_INFO("performer = %d, order=%d",performer,order);
323 if(performer != -1 && order != -1){
324 //necessary parameters are given
325 computer = xbt_dict_get_or_null(computers, char_performer);
326 if(computer == NULL){
327 computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
328 xbt_dict_set(computers, char_performer, computer, NULL);
330 if(performer < xbt_lib_length(host_lib)){
331 // the wanted computer is available
332 SD_task_t *task_test = NULL;
333 if(order < computer->used)
334 task_test = xbt_dynar_get_ptr(computer,order);
335 if(task_test != NULL && *task_test != NULL && *task_test != current_job){
336 /*the user gives the same order to several tasks*/
338 XBT_VERB("The task %s starts on the computer %s at the position : %s like the task %s",
339 (*task_test)->name, char_performer, char_order, current_job->name);
341 //the parameter seems to be ok
342 xbt_dynar_set_as(computer, order, SD_task_t, current_job);
345 /*the platform has not enough processors to schedule the DAG like
348 XBT_VERB("The schedule is ignored, there are not enough computers");
352 //one of necessary parameters are not given
354 XBT_VERB("The schedule is ignored, the task %s is not correctly scheduled", current_job->name);
359 /* dot_add_output_dependencies create the dependencies between a task
360 * and a transfers. This is given by the edges in the dot file.
361 * The amount of data transfers is given by the attribute size on the
363 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge)
365 SD_task_t file = NULL;
366 char *name_tail=agnameof(agtail(edge));
367 char *name_head=agnameof(aghead(edge));
368 char *name = malloc((strlen(name_head)+strlen(name_tail)+6)*sizeof(char));
369 sprintf(name, "%s->%s", name_tail, name_head);
370 double size = dot_parse_double(agget(edge, (char *) "size"));
371 XBT_DEBUG("size : %e, get size : %s", size, agget(edge, (char *) "size"));
374 file = xbt_dict_get_or_null(files, name);
376 file = SD_task_create_comm_e2e(name, NULL, size);
378 TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
380 xbt_dict_set(files, name, file, NULL);
382 if (SD_task_get_amount(file) != size) {
383 XBT_WARN("Ignoring file %s size redefinition from %.0f to %.0f",
384 name, SD_task_get_amount(file), size);
387 SD_task_dependency_add(NULL, NULL, file, current_job);
389 file = xbt_dict_get_or_null(jobs, name_tail);
391 SD_task_dependency_add(NULL, NULL, file, current_job);
397 /* dot_add_output_dependencies create the dependencies between a
398 * transfers and a task. This is given by the edges in the dot file.
399 * The amount of data transfers is given by the attribute size on the
401 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge)
404 char *name_tail=agnameof(agtail(edge));
405 char *name_head=agnameof(aghead(edge));
406 char *name = malloc((strlen(name_head)+strlen(name_tail)+6)*sizeof(char));
407 sprintf(name, "%s->%s", name_tail, name_head);
408 double size = dot_parse_double(agget(edge, (char *) "size"));
409 XBT_DEBUG("size : %e, get size : %s", size, agget(edge, (char *) "size"));
412 file = xbt_dict_get_or_null(files, name);
414 file = SD_task_create_comm_e2e(name, NULL, size);
416 TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
418 xbt_dict_set(files, name, file, NULL);
420 if (SD_task_get_amount(file) != size) {
421 XBT_WARN("Ignoring file %s size redefinition from %.0f to %.0f",
422 name, SD_task_get_amount(file), size);
425 SD_task_dependency_add(NULL, NULL, current_job, file);
426 if (xbt_dynar_length(file->tasks_before) > 1) {
427 XBT_WARN("File %s created at more than one location...", file->name);
430 file = xbt_dict_get_or_null(jobs, name_head);
432 SD_task_dependency_add(NULL, NULL, current_job, file);