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_generic(const char * filename);
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 SD_dotload_generic(filename);
101 xbt_dynar_t computer = NULL;
102 xbt_dict_cursor_t dict_cursor;
104 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
105 xbt_dynar_free(&computer);
107 xbt_dict_free(&computers);
111 xbt_dynar_t SD_dotload_with_sched(const char *filename){
112 SD_dotload_generic(filename);
114 if(schedule == true){
115 xbt_dynar_t computer = NULL;
116 xbt_dict_cursor_t dict_cursor;
118 const SD_workstation_t *workstations = SD_workstation_get_list ();
119 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
120 int count_computer = dot_parse_int(computer_name);
121 unsigned int count=0;
123 SD_task_t task_previous = NULL;
124 xbt_dynar_foreach(computer,count,task){
125 // add dependency between the previous and the task to avoid
126 // parallel execution
128 if(task_previous != NULL &&
129 !SD_task_dependency_exists(task_previous, task))
130 SD_task_dependency_add(NULL, NULL, task_previous, task);
131 SD_task_schedulel(task, 1, workstations[count_computer]);
132 task_previous = task;
135 xbt_dynar_free(&computer);
137 xbt_dict_free(&computers);
138 if(acyclic_graph_detail(result))
141 WARN0("There is at least one cycle in the provided task graph");
143 WARN0("The scheduling is ignored");
148 xbt_dynar_t SD_dotload_generic(const char * filename)
150 xbt_assert0(filename, "Unable to use a null file descriptor\n");
151 dag_dot = agopen((char*)filename,Agstrictdirected,0);
153 result = xbt_dynar_new(sizeof(SD_task_t), dot_task_free);
154 files = xbt_dict_new();
155 jobs = xbt_dict_new();
156 computers = xbt_dict_new();
157 root_task = SD_task_create_comp_seq("root", NULL, 0);
158 /* by design the root task is always SCHEDULABLE */
159 __SD_task_set_state(root_task, SD_SCHEDULABLE);
161 xbt_dict_set(jobs, "root", root_task, NULL);
162 xbt_dynar_push(result, &root_task);
163 end_task = SD_task_create_comp_seq("end", NULL, 0);
164 xbt_dict_set(jobs, "end", end_task, NULL);
166 Agnode_t *dag_node = NULL;
167 for (dag_node = agfstnode(dag_dot); dag_node;
169 dag_node = agnxtnode(dag_dot, dag_node)
171 dag_node = agnxtnode(dag_node)
175 dot_add_task(dag_node);
178 xbt_dict_free(&jobs);
180 /* And now, post-process the files.
181 * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
182 * Files not produced in the system are said to be produced by root task (top of DAG).
183 * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
185 xbt_dict_cursor_t cursor;
188 xbt_dict_foreach(files, cursor, name, file) {
189 unsigned int cpt1, cpt2;
190 SD_task_t newfile = NULL;
191 SD_dependency_t depbefore, depafter;
192 if (xbt_dynar_length(file->tasks_before) == 0) {
193 xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
195 SD_task_create_comm_e2e(file->name, NULL, file->amount);
196 SD_task_dependency_add(NULL, NULL, root_task, newfile);
197 SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
198 xbt_dynar_push(result, &newfile);
200 } else if (xbt_dynar_length(file->tasks_after) == 0) {
201 xbt_dynar_foreach(file->tasks_before, cpt2, depbefore) {
203 SD_task_create_comm_e2e(file->name, NULL, file->amount);
204 SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
205 SD_task_dependency_add(NULL, NULL, newfile, end_task);
206 xbt_dynar_push(result, &newfile);
209 xbt_dynar_foreach(file->tasks_before, cpt1, depbefore) {
210 xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
211 if (depbefore->src == depafter->dst) {
213 ("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
214 file->name, depbefore->src->name);
217 SD_task_create_comm_e2e(file->name, NULL, file->amount);
218 SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
219 SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
220 xbt_dynar_push(result, &newfile);
226 /* Push end task last */
227 xbt_dynar_push(result, &end_task);
229 /* Free previous copy of the files */
230 xbt_dict_free(&files);
231 if(acyclic_graph_detail(result))
233 acyclic_graph_detail(result);
238 /* dot_add_task create a sd_task and all transfers required for this
239 * task. The execution time of the task is given by the attribute size.
240 * The unit of size is the Flop.*/
241 void dot_add_task(Agnode_t * dag_node)
243 char *name = agnameof(dag_node);
244 SD_task_t current_job;
245 double runtime = dot_parse_double(agget(dag_node, (char *) "size"));
247 DEBUG3("See <job id=%s runtime=%s %.0f>", name,
248 agget(dag_node, (char *) "size"), runtime);
249 current_job = xbt_dict_get_or_null(jobs, name);
250 if (current_job == NULL) {
252 SD_task_create_comp_seq(name, NULL , runtime);
254 TRACE_sd_dotloader (current_job, agget (dag_node, (char*)"category"));
256 xbt_dict_set(jobs, name, current_job, NULL);
257 xbt_dynar_push(result, ¤t_job);
263 for (e = agfstin(dag_dot, dag_node); e; e = agnxtin(dag_dot, e))
265 for (e = agfstin(dag_node); e; e = agnxtin(e))
268 dot_add_input_dependencies(current_job, e);
271 if (count == 0 && current_job != root_task) {
272 SD_task_dependency_add(NULL, NULL, root_task, current_job);
276 for (e = agfstout(dag_dot, dag_node); e; e = agnxtout(dag_dot, e))
278 for (e = agfstout(dag_node); e; e = agnxtout(e))
282 dot_add_output_dependencies(current_job, e);
285 if (count == 0 && current_job != end_task) {
286 SD_task_dependency_add(NULL, NULL, current_job, end_task);
289 if(schedule || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose)){
290 /* try to take the information to schedule the task only if all is
292 // performer is the computer which execute the task
293 unsigned long performer = -1;
294 char * char_performer = agget(dag_node, (char *) "performer");
295 if (char_performer != NULL)
296 performer = (long) dot_parse_int(char_performer);
298 // order is giving the task order on one computer
299 unsigned long order = -1;
300 char * char_order = agget(dag_node, (char *) "order");
301 if (char_order != NULL)
302 order = (long) dot_parse_int(char_order);
303 xbt_dynar_t computer = NULL;
304 //INFO2("performer = %d, order=%d",performer,order);
305 if(performer != -1 && order != -1){
306 //necessary parameters are given
307 computer = xbt_dict_get_or_null(computers, char_performer);
308 if(computer == NULL){
309 computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
310 xbt_dict_set(computers, char_performer, computer, NULL);
312 if(performer < sd_global->workstation_count){
313 // the wanted computer is available
314 SD_task_t *task_test = NULL;
315 if(order < computer->used)
316 task_test = xbt_dynar_get_ptr(computer,order);
317 if(task_test != NULL && *task_test != NULL && *task_test != current_job){
318 /*the user gives the same order to several tasks*/
320 VERB4("The task %s starts on the computer %s at the position : %s like the task %s",
321 (*task_test)->name, char_performer, char_order, current_job->name);
323 //the parameter seems to be ok
324 xbt_dynar_set_as(computer, order, SD_task_t, current_job);
327 /*the platform has not enough processors to schedule the DAG like
330 VERB0("The schedule is ignored, there are not enough computers");
334 //one of necessary parameters are not given
336 VERB1("The schedule is ignored, the task %s is not correctly schedule", current_job->name);
341 /* dot_add_output_dependencies create the dependencies between a task
342 * and a transfers. This is given by the edges in the dot file.
343 * The amount of data transfers is given by the attribute size on the
345 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge)
350 sprintf(name, "%s->%s", agnameof(agtail(edge)), agnameof(aghead(edge)));
351 double size = dot_parse_double(agget(edge, (char *) "size"));
352 DEBUG2("size : %e, get size : %s", size, agget(edge, (char *) "size"));
355 file = xbt_dict_get_or_null(files, name);
357 file = SD_task_create_comm_e2e(name, NULL, size);
359 TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
361 xbt_dict_set(files, name, file, &dot_task_free);
363 if (SD_task_get_amount(file) != size) {
364 WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
365 name, SD_task_get_amount(file), size);
368 SD_task_dependency_add(NULL, NULL, file, current_job);
370 file = xbt_dict_get_or_null(jobs, agnameof(agtail(edge)));
372 SD_task_dependency_add(NULL, NULL, file, current_job);
377 /* dot_add_output_dependencies create the dependencies between a
378 * transfers and a task. This is given by the edges in the dot file.
379 * The amount of data transfers is given by the attribute size on the
381 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge)
385 sprintf(name, "%s->%s", agnameof(agtail(edge)), agnameof(aghead(edge)));
386 double size = dot_parse_double(agget(edge, (char *) "size"));
387 DEBUG2("size : %e, get size : %s", size, agget(edge, (char *) "size"));
390 file = xbt_dict_get_or_null(files, name);
392 file = SD_task_create_comm_e2e(name, NULL, size);
394 TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
396 xbt_dict_set(files, name, file, &dot_task_free);
398 if (SD_task_get_amount(file) != size) {
399 WARN3("Ignoring file %s size redefinition from %.0f to %.0f",
400 name, SD_task_get_amount(file), size);
403 SD_task_dependency_add(NULL, NULL, current_job, file);
404 if (xbt_dynar_length(file->tasks_before) > 1) {
405 WARN1("File %s created at more than one location...", file->name);
408 file = xbt_dict_get_or_null(jobs, agnameof(aghead(edge)));
410 SD_task_dependency_add(NULL, NULL, current_job, file);