Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into hypervisor
[simgrid.git] / src / simdag / sd_dotloader.c
1 /* Copyright (c) 2009-2013. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "private.h"
8 #include "simdag/simdag.h"
9 #include "xbt/misc.h"
10 #include "xbt/log.h"
11 #include <stdbool.h>
12 #include <string.h>
13 #include <libgen.h>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_dotparse, sd, "Parsing DOT files");
16
17 #undef CLEANUP
18
19 #ifdef HAVE_CGRAPH_H
20 #include <graphviz/cgraph.h>
21 #elif HAVE_AGRAPH_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)
28 #endif
29
30 typedef enum {
31   sequential =0,
32   parallel
33 } seq_par_t;
34
35 void dot_add_task(Agnode_t * dag_node);
36 void dot_add_parallel_task(Agnode_t * dag_node);
37 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge,
38                                 seq_par_t seq_or_par);
39 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge,
40                                  seq_par_t seq_or_par);
41 xbt_dynar_t SD_dotload_generic(const char * filename);
42
43 static double dot_parse_double(const char *string)
44 {
45   if (string == NULL)
46     return -1;
47   double value = -1;
48   char *err;
49
50   //ret = sscanf(string, "%lg", &value);
51   errno = 0;
52   value = strtod(string,&err);
53   if(errno)
54   {
55     XBT_WARN("Failed to convert string to double: %s\n",strerror(errno));
56     return -1;
57   }
58   return value;
59 }
60
61
62 static int dot_parse_int(const char *string)
63 {
64   if (string == NULL)
65     return -10;
66   int ret = 0;
67   int value = -1;
68
69   ret = sscanf(string, "%d", &value);
70   if (ret != 1)
71     XBT_WARN("%s is not an integer", string);
72   return value;
73 }
74
75 static xbt_dynar_t result;
76 static xbt_dict_t jobs;
77 static xbt_dict_t files;
78 static xbt_dict_t computers;
79 static SD_task_t root_task, end_task;
80 static Agraph_t *dag_dot;
81 static bool schedule = true;
82
83 static void dump_res()
84 {
85   unsigned int cursor;
86   SD_task_t task;
87   xbt_dynar_foreach(result, cursor, task) {
88     XBT_INFO("Task %d", cursor);
89     SD_task_dump(task);
90   }
91 }
92
93
94 static void dot_task_free(void *task)
95 {
96   SD_task_t t = task;
97   SD_task_destroy(t);
98 }
99
100 static void dot_task_p_free(void *task)
101 {
102   SD_task_t *t = task;
103   SD_task_destroy(*t);
104 }
105
106 #ifdef HAVE_TRACING
107 static void TRACE_sd_dotloader (SD_task_t task, const char *category)
108 {
109   if (category && strlen (category)){
110     if (task->category)
111       XBT_DEBUG("Change the category of %s from %s to %s",
112           task->name, task->category, category);
113     else
114       XBT_DEBUG("Set the category of %s to %s",task->name, category);
115     TRACE_category (category);
116     TRACE_sd_set_task_category(task, category);
117   }
118 }
119 #endif
120
121 /** @brief loads a DOT file describing a DAG
122  * 
123  * See http://www.graphviz.org/doc/info/lang.html
124  * for more details.
125  * To obtain information about transfers and tasks, two attributes are
126  * required : size on task (execution time in Flop) and size on edge
127  * (the amount of data transfer in bit).
128  * if they aren't here, there choose to be equal to zero.
129  */
130 xbt_dynar_t SD_dotload(const char *filename)
131 {
132   SD_dotload_generic(filename);
133   xbt_dynar_t computer = NULL;
134   xbt_dict_cursor_t dict_cursor;
135   char *computer_name;
136   xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
137     xbt_dynar_free(&computer);
138   }
139   xbt_dict_free(&computers);
140   return result;
141 }
142
143 xbt_dynar_t SD_dotload_with_sched(const char *filename){
144   SD_dotload_generic(filename);
145
146   if(schedule == true){
147     xbt_dynar_t computer = NULL;
148     xbt_dict_cursor_t dict_cursor;
149     char *computer_name;
150     const SD_workstation_t *workstations = SD_workstation_get_list ();
151     xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
152       int count_computer = dot_parse_int(computer_name);
153       unsigned int count=0;
154       SD_task_t task;
155       SD_task_t task_previous = NULL;
156       xbt_dynar_foreach(computer,count,task){
157         // add dependency between the previous and the task to avoid
158         // parallel execution
159         if(task != NULL ){
160           if(task_previous != NULL &&
161              !SD_task_dependency_exists(task_previous, task))
162             SD_task_dependency_add(NULL, NULL, task_previous, task);
163           SD_task_schedulel(task, 1, workstations[count_computer]);
164           task_previous = task;
165         }
166       }
167       xbt_dynar_free(&computer);
168     }
169     xbt_dict_free(&computers);
170     if(acyclic_graph_detail(result))
171       return result;
172     else
173       XBT_WARN("There is at least one cycle in the provided task graph");
174   }else{
175     XBT_WARN("The scheduling is ignored");
176   }
177   SD_task_t task;
178   unsigned int count;
179   xbt_dynar_t computer = NULL;
180   xbt_dict_cursor_t dict_cursor;
181   char *computer_name;
182   xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
183     xbt_dynar_free(&computer);
184   }
185   xbt_dict_free(&computers);
186   xbt_dynar_foreach(result,count,task){
187      SD_task_destroy(task);
188   }
189   return NULL;
190 }
191
192 xbt_dynar_t SD_PTG_dotload(const char * filename)
193 {
194   xbt_assert(filename, "Unable to use a null file descriptor\n");
195   FILE *in_file = fopen(filename, "r");
196   dag_dot = agread(in_file, NIL(Agdisc_t *));
197
198   result = xbt_dynar_new(sizeof(SD_task_t), dot_task_p_free);
199   files = xbt_dict_new_homogeneous(&dot_task_free);
200   jobs = xbt_dict_new_homogeneous(NULL);
201   computers = xbt_dict_new_homogeneous(NULL);
202   root_task = SD_task_create_comp_par_amdahl("root", NULL, 0., 0.);
203   /* by design the root task is always SCHEDULABLE */
204   __SD_task_set_state(root_task, SD_SCHEDULABLE);
205
206   xbt_dict_set(jobs, "root", root_task, NULL);
207   xbt_dynar_push(result, &root_task);
208   end_task = SD_task_create_comp_par_amdahl("end", NULL, 0., 0.);
209   xbt_dict_set(jobs, "end", end_task, NULL);
210
211   Agnode_t *dag_node = NULL;
212   for (dag_node = agfstnode(dag_dot); dag_node; dag_node = agnxtnode(dag_dot, dag_node)) {
213     dot_add_parallel_task(dag_node);
214   }
215   agclose(dag_dot);
216   xbt_dict_free(&jobs);
217
218   /* And now, post-process the files.
219    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
220    * Files not produced in the system are said to be produced by root task (top of DAG).
221    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
222    */
223   xbt_dict_cursor_t cursor;
224   SD_task_t file;
225   char *name;
226   xbt_dict_foreach(files, cursor, name, file) {
227     unsigned int cpt1, cpt2;
228     SD_task_t newfile = NULL;
229     SD_dependency_t depbefore, depafter;
230     if (xbt_dynar_is_empty(file->tasks_before)) {
231       xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
232         SD_task_t newfile =
233             SD_task_create_comm_par_mxn_1d_block(file->name, NULL, file->amount);
234         SD_task_dependency_add(NULL, NULL, root_task, newfile);
235         SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
236         xbt_dynar_push(result, &newfile);
237       }
238     } else if (xbt_dynar_is_empty(file->tasks_after)) {
239       xbt_dynar_foreach(file->tasks_before, cpt2, depbefore) {
240         SD_task_t newfile =
241             SD_task_create_comm_par_mxn_1d_block(file->name, NULL, file->amount);
242         SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
243         SD_task_dependency_add(NULL, NULL, newfile, end_task);
244         xbt_dynar_push(result, &newfile);
245       }
246     } else {
247       xbt_dynar_foreach(file->tasks_before, cpt1, depbefore) {
248         xbt_dynar_foreach(file->tasks_after, cpt2, depafter) {
249           if (depbefore->src == depafter->dst) {
250             XBT_WARN
251                 ("File %s is produced and consumed by task %s. This loop dependency will prevent the execution of the task.",
252                  file->name, depbefore->src->name);
253           }
254           newfile =
255               SD_task_create_comm_par_mxn_1d_block(file->name, NULL, file->amount);
256           SD_task_dependency_add(NULL, NULL, depbefore->src, newfile);
257           SD_task_dependency_add(NULL, NULL, newfile, depafter->dst);
258           xbt_dynar_push(result, &newfile);
259         }
260       }
261     }
262   }
263
264   /* Push end task last */
265   xbt_dynar_push(result, &end_task);
266
267   /* Free previous copy of the files */
268   xbt_dict_free(&files);
269   xbt_dict_free(&computers);
270   fclose(in_file);
271   if (!acyclic_graph_detail(result)) {
272     XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.",
273               basename((char*)filename));
274     xbt_dynar_free(&result);
275     /* (result == NULL) here */
276   }
277   return result;
278 }
279
280
281 xbt_dynar_t SD_dotload_generic(const char * filename)
282 {
283   xbt_assert(filename, "Unable to use a null file descriptor\n");
284   //dag_dot =  agopen((char*)filename,Agstrictdirected,0);
285   FILE *in_file = fopen(filename, "r");
286   dag_dot = agread(in_file, NIL(Agdisc_t *));
287
288   result = xbt_dynar_new(sizeof(SD_task_t), dot_task_p_free);
289   files = xbt_dict_new_homogeneous(NULL);
290   jobs = xbt_dict_new_homogeneous(NULL);
291   computers = xbt_dict_new_homogeneous(NULL);
292   root_task = SD_task_create_comp_seq("root", NULL, 0);
293   /* by design the root task is always SCHEDULABLE */
294   __SD_task_set_state(root_task, SD_SCHEDULABLE);
295
296   xbt_dict_set(jobs, "root", root_task, NULL);
297   xbt_dynar_push(result, &root_task);
298   end_task = SD_task_create_comp_seq("end", NULL, 0);
299   xbt_dict_set(jobs, "end", end_task, NULL);
300
301   Agnode_t *dag_node = NULL;
302   for (dag_node = agfstnode(dag_dot); dag_node; dag_node = agnxtnode(dag_dot, dag_node)) {
303     dot_add_task(dag_node);
304   }
305   agclose(dag_dot);
306   xbt_dict_free(&jobs);
307
308   /* And now, post-process the files.
309    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
310    * Files not produced in the system are said to be produced by root task (top of DAG).
311    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
312    */
313   xbt_dict_cursor_t cursor;
314   SD_task_t file;
315   char *name;
316   xbt_dict_foreach(files, cursor, name, file) {
317     XBT_DEBUG("Considering file '%s' stored in the dictionnary",
318         file->name);
319     if (xbt_dynar_is_empty(file->tasks_before)) {
320       XBT_DEBUG("file '%s' has no source. Add dependency from 'root'",
321           file->name);
322       SD_task_dependency_add(NULL, NULL, root_task, file);
323     } else if (xbt_dynar_is_empty(file->tasks_after)) {
324       XBT_DEBUG("file '%s' has no destination. Add dependency to 'end'",
325           file->name);
326       SD_task_dependency_add(NULL, NULL, file, end_task);
327     }
328     xbt_dynar_push(result, &file);
329   }
330
331   /* Push end task last */
332   xbt_dynar_push(result, &end_task);
333
334   xbt_dict_free(&files);
335   fclose(in_file);
336   if (!acyclic_graph_detail(result)) {
337     XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.",
338               basename((char*)filename));
339     xbt_dynar_free(&result);
340     /* (result == NULL) here */
341   }
342   return result;
343 }
344
345 /* dot_add_parallel_task create a sd_task of SD_TASK_COMP_PAR_AMDHAL type and
346  * all transfers required for this task. The execution time of the task is
347  * given by the attribute size. The unit of size is the Flop.*/
348 void dot_add_parallel_task(Agnode_t * dag_node)
349 {
350   char *name = agnameof(dag_node);
351   SD_task_t current_job;
352   double amount = dot_parse_double(agget(dag_node, (char *) "size"));
353   double alpha = dot_parse_double(agget(dag_node, (char *) "alpha"));
354
355   if (alpha == -1.)
356     alpha = 0.0;
357
358   XBT_DEBUG("See <job id=%s amount=%s %.0f alpha=%.2f>", name,
359         agget(dag_node, (char *) "size"), amount, alpha);
360   if (!strcmp(name, "root")){
361     XBT_WARN("'root' node is explicitly declared in the DOT file. Ignore it");
362     return;
363   }
364   if (!strcmp(name, "end")){
365     XBT_WARN("'end' node is explicitly declared in the DOT file. Ignore it");
366     return;
367   }
368
369   current_job = xbt_dict_get_or_null(jobs, name);
370   if (current_job == NULL) {
371     current_job =
372         SD_task_create_comp_par_amdahl(name, NULL , amount, alpha);
373 #ifdef HAVE_TRACING
374    TRACE_sd_dotloader (current_job, agget (dag_node, (char*)"category"));
375 #endif
376     xbt_dict_set(jobs, name, current_job, NULL);
377     xbt_dynar_push(result, &current_job);
378   }
379   Agedge_t *e;
380   int count = 0;
381
382   for (e = agfstin(dag_dot, dag_node); e; e = agnxtin(dag_dot, e)) {
383     dot_add_input_dependencies(current_job, e, parallel);
384     count++;
385   }
386   if (count == 0 && current_job != root_task) {
387     SD_task_dependency_add(NULL, NULL, root_task, current_job);
388   }
389   count = 0;
390   for (e = agfstout(dag_dot, dag_node); e; e = agnxtout(dag_dot, e)) {
391     dot_add_output_dependencies(current_job, e, parallel);
392     count++;
393   }
394   if (count == 0 && current_job != end_task) {
395     SD_task_dependency_add(NULL, NULL, current_job, end_task);
396   }
397 }
398
399 /* dot_add_task create a sd_task and all transfers required for this
400  * task. The execution time of the task is given by the attribute size.
401  * The unit of size is the Flop.*/
402 void dot_add_task(Agnode_t * dag_node)
403 {
404   char *name = agnameof(dag_node);
405   SD_task_t current_job;
406   double runtime = dot_parse_double(agget(dag_node, (char *) "size"));
407
408   XBT_DEBUG("See <job id=%s runtime=%s %.0f>", name,
409         agget(dag_node, (char *) "size"), runtime);
410
411   if (!strcmp(name, "root")){
412     XBT_WARN("'root' node is explicitly declared in the DOT file. Update it");
413     root_task->amount = runtime;
414 #ifdef HAVE_TRACING
415     TRACE_sd_dotloader (root_task, agget (dag_node, (char*)"category"));
416 #endif
417   }
418
419   if (!strcmp(name, "end")){
420     XBT_WARN("'end' node is explicitly declared in the DOT file. Update it");
421     end_task->amount = runtime;
422 #ifdef HAVE_TRACING
423     TRACE_sd_dotloader (end_task, agget (dag_node, (char*)"category"));
424 #endif
425   }
426
427   current_job = xbt_dict_get_or_null(jobs, name);
428   if (!current_job) {
429     current_job =
430         SD_task_create_comp_seq(name, NULL , runtime);
431 #ifdef HAVE_TRACING
432     TRACE_sd_dotloader (current_job, agget (dag_node, (char*)"category"));
433 #endif
434     xbt_dict_set(jobs, name, current_job, NULL);
435     xbt_dynar_push(result, &current_job);
436   }
437   Agedge_t *e;
438   int count = 0;
439
440   for (e = agfstin(dag_dot, dag_node); e; e = agnxtin(dag_dot, e)) {
441     dot_add_input_dependencies(current_job, e, sequential);
442     count++;
443   }
444   if (count == 0 && current_job != root_task) {
445     SD_task_dependency_add(NULL, NULL, root_task, current_job);
446   }
447   count = 0;
448   for (e = agfstout(dag_dot, dag_node); e; e = agnxtout(dag_dot, e)) {
449     dot_add_output_dependencies(current_job, e, sequential);
450     count++;
451   }
452   if (count == 0 && current_job != end_task) {
453     SD_task_dependency_add(NULL, NULL, current_job, end_task);
454   }
455
456   if(schedule || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose)){
457     /* try to take the information to schedule the task only if all is
458      * right*/
459     // performer is the computer which execute the task
460     unsigned long performer = -1;
461     char * char_performer = agget(dag_node, (char *) "performer");
462     if (char_performer != NULL)
463       performer = (long) dot_parse_int(char_performer);
464
465     // order is giving the task order on one computer
466     unsigned long order = -1;
467     char * char_order = agget(dag_node, (char *) "order");
468     if (char_order != NULL)
469       order = (long) dot_parse_int(char_order);
470     xbt_dynar_t computer = NULL;
471     //XBT_INFO("performer = %d, order=%d",performer,order);
472     if(performer != -1 && order != -1){
473       //necessary parameters are given
474       computer = xbt_dict_get_or_null(computers, char_performer);
475       if(computer == NULL){
476         computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
477         xbt_dict_set(computers, char_performer, computer, NULL);
478       }
479       if(performer < xbt_lib_length(host_lib)){
480         // the  wanted computer is available
481         SD_task_t *task_test = NULL;
482         if(order < computer->used)
483           task_test = xbt_dynar_get_ptr(computer,order);
484         if(task_test != NULL && *task_test != NULL && *task_test != current_job){
485           /*the user gives the same order to several tasks*/
486           schedule = false;
487           XBT_VERB("The task %s starts on the computer %s at the position : %s like the task %s",
488                  (*task_test)->name, char_performer, char_order, current_job->name);
489         }else{
490           //the parameter seems to be ok
491           xbt_dynar_set_as(computer, order, SD_task_t, current_job);
492         }
493       }else{
494         /*the platform has not enough processors to schedule the DAG like
495         *the user wants*/
496         schedule = false;
497         XBT_VERB("The schedule is ignored, there are not enough computers");
498       }
499     }
500     else {
501       //one of necessary parameters are not given
502       schedule = false;
503       XBT_VERB("The schedule is ignored, the task %s is not correctly scheduled", current_job->name);
504     }
505   }
506 }
507
508 /* dot_add_output_dependencies create the dependencies between a task
509  * and a transfers. This is given by the edges in the dot file. 
510  * The amount of data transfers is given by the attribute size on the
511  * edge. */
512 void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge,
513                                 seq_par_t seq_or_par)
514 {
515   SD_task_t file = NULL;
516   char *name_tail=agnameof(agtail(edge));
517   char *name_head=agnameof(aghead(edge));
518   char *name = xbt_malloc((strlen(name_head)+strlen(name_tail)+6)*sizeof(char));
519   sprintf(name, "%s->%s", name_tail, name_head);
520   double size = dot_parse_double(agget(edge, (char *) "size"));
521   XBT_DEBUG("add input -- edge: %s, size : %e, get size : %s",
522       name, size, agget(edge, (char *) "size"));
523
524   if (size > 0) {
525     file = xbt_dict_get_or_null(files, name);
526     if (file == NULL) {
527       if (seq_or_par == sequential){
528           file = SD_task_create_comm_e2e(name, NULL, size);
529       } else {
530           file = SD_task_create_comm_par_mxn_1d_block(name, NULL, size);
531       }
532 #ifdef HAVE_TRACING
533       TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
534 #endif
535       XBT_DEBUG("add input -- adding %s to the dict as new file", name);
536       xbt_dict_set(files, name, file, NULL);
537     } else {
538       XBT_WARN("%s already exists", name);
539       if (SD_task_get_amount(file) != size) {
540         XBT_WARN("Ignoring file %s size redefinition from %.0f to %.0f",
541               name, SD_task_get_amount(file), size);
542       }
543     }
544     SD_task_dependency_add(NULL, NULL, file, current_job);
545   } else {
546     file = xbt_dict_get_or_null(jobs, name_tail);
547     if (file != NULL) {
548       SD_task_dependency_add(NULL, NULL, file, current_job);
549     }
550   }
551   free(name);
552 }
553
554 /* dot_add_output_dependencies create the dependencies between a
555  * transfers and a task. This is given by the edges in the dot file.
556  * The amount of data transfers is given by the attribute size on the
557  * edge. */
558 void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge,
559                                  seq_par_t seq_or_par){
560   SD_task_t file;
561   char *name_tail=agnameof(agtail(edge));
562   char *name_head=agnameof(aghead(edge));
563   char *name = xbt_malloc((strlen(name_head)+strlen(name_tail)+6)*sizeof(char));
564   sprintf(name, "%s->%s", name_tail, name_head);
565   double size = dot_parse_double(agget(edge, (char *) "size"));
566   XBT_DEBUG("add_output -- edge: %s, size : %e, get size : %s",
567       name, size, agget(edge, (char *) "size"));
568
569   if (size > 0) {
570     file = xbt_dict_get_or_null(files, name);
571     if (file == NULL) {
572       if (seq_or_par == sequential){
573           file = SD_task_create_comm_e2e(name, NULL, size);
574       } else {
575           file = SD_task_create_comm_par_mxn_1d_block(name, NULL, size);
576       }
577 #ifdef HAVE_TRACING
578       TRACE_sd_dotloader (file, agget (edge, (char*)"category"));
579 #endif
580       XBT_DEBUG("add output -- adding %s to the dict as new file", name);
581       xbt_dict_set(files, name, file, NULL);
582     } else {
583       XBT_WARN("%s already exists", name);
584       if (SD_task_get_amount(file) != size) {
585         XBT_WARN("Ignoring file %s size redefinition from %.0f to %.0f",
586               name, SD_task_get_amount(file), size);
587       }
588     }
589     SD_task_dependency_add(NULL, NULL, current_job, file);
590     if (xbt_dynar_length(file->tasks_before) > 1) {
591       XBT_WARN("File %s created at more than one location...", file->name);
592     }
593   } else {
594     file = xbt_dict_get_or_null(jobs, name_head);
595     if (file != NULL) {
596       SD_task_dependency_add(NULL, NULL, current_job, file);
597     }
598   }
599   free(name);
600 }