Logo AND Algorithmique Numérique Distribuée

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