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"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_dotparse, sd, "Parsing DOT files");
18 #include <graphviz/cgraph.h>
20 #include <graphviz/agraph.h>
23 void dot_add_task(Agnode_t * dag_node);
24 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge);
25 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge);
26 xbt_dynar_t SD_dotload_FILE(FILE * in_file);
28 static double dot_parse_double(const char *string)
35 ret = sscanf(string, "%lg", &value);
37 WARN1("%s is not a double", string);
41 static int dot_parse_int(const char *string)
48 ret = sscanf(string, "%d", &value);
50 WARN1("%s is not an integer", string);
54 static xbt_dynar_t result;
55 static xbt_dict_t jobs;
56 static xbt_dict_t files;
57 static xbt_dict_t computers;
58 static SD_task_t root_task, end_task;
59 static Agraph_t *dag_dot;
60 static bool schedule = true;
62 static void dump_res()
66 xbt_dynar_foreach(result, cursor, task) {
67 INFO1("Task %d", cursor);
73 static void dot_task_free(void *task)
79 static void TRACE_sd_dotloader (SD_task_t task, const char *category)
82 if (strlen (category) != 0){
83 TRACE_category (category);
84 TRACE_sd_set_task_category (task, category);
89 /** @brief loads a DOT file describing a DAG
91 * See http://www.graphviz.org/doc/info/lang.html
93 * To obtain information about transfers and tasks, two attributes are
94 * required : size on task (execution time in Flop) and size on edge
95 * (the amount of data transfer in bit).
96 * if they aren't here, there choose to be equal to zero.
98 xbt_dynar_t SD_dotload(const char *filename)
100 FILE *in_file = fopen(filename, "r");
101 xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
102 SD_dotload_FILE(in_file);
104 xbt_dynar_t computer = NULL;
105 xbt_dict_cursor_t dict_cursor;
107 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
108 xbt_dynar_free(&computer);
110 xbt_dict_free(&computers);
114 xbt_dynar_t SD_dotload_with_sched(const char *filename){
115 FILE *in_file = fopen(filename, "r");
116 xbt_assert1(in_file, "Unable to open \"%s\"\n", filename);
117 SD_dotload_FILE(in_file);
120 if(schedule == true){
121 xbt_dynar_t computer = NULL;
122 xbt_dict_cursor_t dict_cursor;
124 const SD_workstation_t *workstations = SD_workstation_get_list ();
125 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
126 int count_computer = dot_parse_int(computer_name);
127 unsigned int count=0;
129 SD_task_t task_previous = NULL;
130 xbt_dynar_foreach(computer,count,task){
131 // add dependency between the previous and the task to avoid
132 // parallel execution
134 if(task_previous != NULL &&
135 !SD_task_dependency_exists(task_previous, task))
136 SD_task_dependency_add(NULL, NULL, task_previous, task);
137 SD_task_schedulel(task, 1, workstations[count_computer]);
138 task_previous = task;
141 xbt_dynar_free(&computer);
143 xbt_dict_free(&computers);
144 if(acyclic_graph_detail(result))
147 WARN0("There is at least one cycle in the provided task graph");
149 WARN0("The scheduling is ignored");
154 xbt_dynar_t SD_dotload_FILE(FILE * in_file)
156 xbt_assert0(in_file, "Unable to use a null file descriptor\n");
157 dag_dot = agread(in_file, NIL(Agdisc_t *));
159 result = xbt_dynar_new(sizeof(SD_task_t), dot_task_free);
160 files = xbt_dict_new();
161 jobs = xbt_dict_new();
162 computers = xbt_dict_new();
163 root_task = SD_task_create_comp_seq("root", NULL, 0);
164 /* by design the root task is always SCHEDULABLE */
165 __SD_task_set_state(root_task, SD_SCHEDULABLE);
167 xbt_dict_set(jobs, "root", root_task, NULL);
168 xbt_dynar_push(result, &root_task);
169 end_task = SD_task_create_comp_seq("end", NULL, 0);
170 xbt_dict_set(jobs, "end", end_task, NULL);
172 Agnode_t *dag_node = NULL;
173 for (dag_node = agfstnode(dag_dot); dag_node;
175 dag_node = agnxtnode(dag_dot, dag_node)
177 dag_node = agnxtnode(dag_node)
181 dot_add_task(dag_node);
184 xbt_dict_free(&jobs);
186 /* And now, post-process the files.
187 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
188 * Files not produced in the system are said to be produced by root task (top of DAG).
189 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
191 xbt_dict_cursor_t cursor;
194 xbt_dict_foreach(files, cursor, name, file) {
195 unsigned int cpt1, cpt2;
196 SD_task_t newfile = NULL;
197 SD_dependency_t depbefore, depafter;
198 if (xbt_dynar_length(file->tasks_before) == 0) {
199 xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
201 SD_task_create_comm_e2e(file->name, NULL, file->amount);
202 SD_task_dependency_add(NULL, NULL, root_task, newfile);
203 SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
204 xbt_dynar_push(result, &newfile);
206 } else if (xbt_dynar_length(file->tasks_after) == 0) {
207 xbt_dynar_foreach(file->tasks_before, cpt2, depbefore) {
209 SD_task_create_comm_e2e(file->name, NULL, file->amount);
210 SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
211 SD_task_dependency_add(NULL, NULL, newfile, end_task);
212 xbt_dynar_push(result, &newfile);
215 xbt_dynar_foreach(file->tasks_before, cpt1, depbefore) {
216 xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
217 if (depbefore->src == depafter->dst) {
219 ("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
220 file->name, depbefore->src->name);
223 SD_task_create_comm_e2e(file->name, NULL, file->amount);
224 SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
225 SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
226 xbt_dynar_push(result, &newfile);
232 /* Push end task last */
233 xbt_dynar_push(result, &end_task);
235 /* Free previous copy of the files */
236 xbt_dict_free(&files);
237 if(acyclic_graph_detail(result))
239 acyclic_graph_detail(result);
243 /* dot_add_task create a sd_task and all transfers required for this
244 * task. The execution time of the task is given by the attribute size.
245 * The unit of size is the Flop.*/
246 void dot_add_task(Agnode_t * dag_node)
248 char *name = agnameof(dag_node);
249 SD_task_t current_job;
250 double runtime = dot_parse_double(agget(dag_node, (char *) "size"));
252 DEBUG3("See <job id=%s runtime=%s %.0f>", name,
253 agget(dag_node, (char *) "size"), runtime);
254 current_job = xbt_dict_get_or_null(jobs, name);
255 if (current_job == NULL) {
257 SD_task_create_comp_seq(name, NULL , runtime);
259 TRACE_sd_dotloader (current_job, agget (dag_node, (char*)"category"));
261 xbt_dict_set(jobs, name, current_job, NULL);
262 xbt_dynar_push(result, ¤t_job);
268 for (e = agfstin(dag_dot, dag_node); e; e = agnxtin(dag_dot, e))
270 for (e = agfstin(dag_node); e; e = agnxtin(e))
273 dot_add_input_dependencies(current_job, e);
276 if (count == 0 && current_job != root_task) {
277 SD_task_dependency_add(NULL, NULL, root_task, current_job);
281 for (e = agfstout(dag_dot, dag_node); e; e = agnxtout(dag_dot, e))
283 for (e = agfstout(dag_node); e; e = agnxtout(e))
287 dot_add_output_dependencies(current_job, e);
290 if (count == 0 && current_job != end_task) {
291 SD_task_dependency_add(NULL, NULL, current_job, end_task);
294 if(schedule || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose)){
295 /* try to take the information to schedule the task only if all is
297 // performer is the computer which execute the task
298 unsigned long performer = -1;
299 char * char_performer = agget(dag_node, (char *) "performer");
300 if (char_performer != NULL)
301 performer = (long) dot_parse_int(char_performer);
303 // order is giving the task order on one computer
304 unsigned long order = -1;
305 char * char_order = agget(dag_node, (char *) "order");
306 if (char_order != NULL)
307 order = (long) dot_parse_int(char_order);
308 xbt_dynar_t computer = NULL;
309 //INFO2("performer = %d, order=%d",performer,order);
310 if(performer != -1 && order != -1){
311 //necessary parameters are given
312 computer = xbt_dict_get_or_null(computers, char_performer);
313 if(computer == NULL){
314 computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
315 xbt_dict_set(computers, char_performer, computer, NULL);
317 if(performer < sd_global->workstation_count){
318 // the wanted computer is available
319 SD_task_t *task_test = NULL;
320 if(order < computer->used)
321 task_test = xbt_dynar_get_ptr(computer,order);
322 if(task_test != NULL && *task_test != NULL && *task_test != current_job){
323 /*the user gives the same order to several tasks*/
325 VERB4("The task %s starts on the computer %s at the position : %s like the task %s",
326 (*task_test)->name, char_performer, char_order, current_job->name);
328 //the parameter seems to be ok
329 xbt_dynar_set_as(computer, order, SD_task_t, current_job);
332 /*the platform has not enough processors to schedule the DAG like
335 VERB0("The schedule is ignored, there are not enough computers");
339 //one of necessary parameters are not given
341 VERB1("The schedule is ignored, the task %s is not correctly schedule", current_job->name);
346 /* dot_add_output_dependencies create the dependencies between a task
347 * and a transfers. This is given by the edges in the dot file.
348 * The amount of data transfers is given by the attribute size on the
350 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge)
355 sprintf(name, "%s->%s", agnameof(agtail(edge)), agnameof(aghead(edge)));
356 double size = dot_parse_double(agget(edge, (char *) "size"));
357 DEBUG2("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, &dot_task_free);
368 if (SD_task_get_amount(file) != size) {
369 WARN3("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, agnameof(agtail(edge)));
377 SD_task_dependency_add(NULL, NULL, file, current_job);
382 /* dot_add_output_dependencies create the dependencies between a
383 * transfers and a task. This is given by the edges in the dot file.
384 * The amount of data transfers is given by the attribute size on the
386 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge)
390 sprintf(name, "%s->%s", agnameof(agtail(edge)), agnameof(aghead(edge)));
391 double size = dot_parse_double(agget(edge, (char *) "size"));
392 DEBUG2("size : %e, get size : %s", size, agget(edge, (char *) "size"));
395 file = xbt_dict_get_or_null(files, name);
397 file = SD_task_create_comm_e2e(name, NULL, size);
399 TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
401 xbt_dict_set(files, name, file, &dot_task_free);
403 if (SD_task_get_amount(file) != size) {
404 WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
405 name, SD_task_get_amount(file), size);
408 SD_task_dependency_add(NULL, NULL, current_job, file);
409 if (xbt_dynar_length(file->tasks_before) > 1) {
410 WARN1("File %s created at more than one location...", file->name);
413 file = xbt_dict_get_or_null(jobs, agnameof(aghead(edge)));
415 SD_task_dependency_add(NULL, NULL, current_job, file);