Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'oldstyle_element_set'
[simgrid.git] / src / simdag / sd_dotloader.cpp
1 /* Copyright (c) 2009-2016. 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 "src/simdag/simdag_private.h"
8 #include "simgrid/simdag.h"
9 #include "xbt/log.h"
10 #include <stdbool.h>
11 #include <string.h>
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_dotparse, sd, "Parsing DOT files");
14
15 #undef CLEANUP
16
17 #ifdef HAVE_CGRAPH_H
18 #include <graphviz/cgraph.h>
19 #elif HAVE_AGRAPH_H
20 #include <graphviz/agraph.h>
21 #define agnxtnode(dot, node)    agnxtnode(node)
22 #define agfstout(dot, node)     agfstout(node)
23 #define agnxtout(dot, edge)     agnxtout(edge)
24 #endif
25
26 typedef enum {
27   sequential =0,
28   parallel
29 } seq_par_t;
30
31 xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par, bool schedule);
32
33
34 static void dot_task_p_free(void *task) {
35   SD_task_destroy(*(SD_task_t *)task);
36 }
37
38 /** @brief loads a DOT file describing a DAG
39  * 
40  * See http://www.graphviz.org/doc/info/lang.html  for more details.
41  * The size attribute of a node describes:
42  *   - for a compute task: the amount of flops to execute
43  *   - for a communication task : the amount of bytes to transfer
44  * If this attribute is ommited, the default value is zero.
45  */
46 xbt_dynar_t SD_dotload(const char *filename) {
47   return SD_dotload_generic(filename, sequential, false);
48 }
49
50 xbt_dynar_t SD_PTG_dotload(const char * filename) {
51   return SD_dotload_generic(filename, parallel, false);
52 }
53
54 xbt_dynar_t SD_dotload_with_sched(const char *filename) {
55   return SD_dotload_generic(filename, sequential, true);
56 }
57
58 static int edge_compare(const void *a, const void *b)
59 {
60   unsigned va = AGSEQ(*(Agedge_t **)a);
61   unsigned vb = AGSEQ(*(Agedge_t **)b);
62   return va == vb ? 0 : (va < vb ? -1 : 1);
63 }
64
65 xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par, bool schedule){
66   xbt_assert(filename, "Unable to use a null file descriptor\n");
67   FILE *in_file = fopen(filename, "r");
68   xbt_assert(in_file != NULL, "Failed to open file: %s", filename);
69
70   unsigned int i;
71   SD_task_t root, end, task;
72   xbt_dict_t computers;
73   xbt_dynar_t computer = NULL;
74   xbt_dict_cursor_t dict_cursor;
75   bool schedule_success = true;
76
77   xbt_dict_t jobs = xbt_dict_new_homogeneous(NULL);
78   xbt_dynar_t result = xbt_dynar_new(sizeof(SD_task_t), dot_task_p_free);
79
80   Agraph_t * dag_dot = agread(in_file, NIL(Agdisc_t *));
81
82   if (schedule)
83     computers = xbt_dict_new_homogeneous(NULL);
84
85   /* Create all the nodes */
86   Agnode_t *node = NULL;
87   for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
88     char *name = agnameof(node);
89     double amount = atof(agget(node, (char *) "size"));
90
91     if (!(task = (SD_task_t)xbt_dict_get_or_null(jobs, name))) {
92       if (seq_or_par == sequential){
93         XBT_DEBUG("See <job id=%s amount =%.0f>", name, amount);
94         task = SD_task_create_comp_seq(name, NULL , amount);
95       } else {
96         double alpha = atof(agget(node, (char *) "alpha"));
97         XBT_DEBUG("See <job id=%s amount =%.0f alpha = %.3f>", name, amount, alpha);
98         task = SD_task_create_comp_par_amdahl(name, NULL , amount, alpha);
99       }
100
101       xbt_dict_set(jobs, name, task, NULL);
102
103       if (strcmp(name,"root") && strcmp(name,"end"))
104         xbt_dynar_push(result, &task);
105
106       if((seq_or_par == sequential) &&
107         ((schedule && schedule_success) || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose))){
108         /* try to take the information to schedule the task only if all is right*/
109         char *char_performer = agget(node, (char *) "performer");
110         char *char_order = agget(node, (char *) "order");
111         /* Tasks will execute on in a given "order" on a given set of "performer" hosts */
112         int performer = ((!char_performer || !strcmp(char_performer,"")) ? -1:atoi(char_performer));
113         int order = ((!char_order || !strcmp(char_order, ""))? -1:atoi(char_order));
114
115         if((performer != -1 && order != -1) && performer < (int) sg_host_count()){
116           /* required parameters are given and less performers than hosts are required */
117           XBT_DEBUG ("Task '%s' is scheduled on workstation '%d' in position '%d'", task->name, performer, order);
118           if(!(computer = (xbt_dynar_t) xbt_dict_get_or_null(computers, char_performer))){
119             computer = xbt_dynar_new(sizeof(SD_task_t), NULL);
120             xbt_dict_set(computers, char_performer, computer, NULL);
121           }
122
123           if((unsigned int)order < xbt_dynar_length(computer)){
124             SD_task_t *task_test = (SD_task_t *)xbt_dynar_get_ptr(computer,order);
125             if(*task_test && *task_test != task){
126               /* the user gave the same order to several tasks */
127               schedule_success = false;
128               XBT_VERB("Task '%s' wants to start on performer '%s' at the same position '%s' as task '%s'",
129                        (*task_test)->name, char_performer, char_order, task->name);
130               continue;
131             }
132           }
133           /* the parameter seems to be ok */
134           xbt_dynar_set_as(computer, order, SD_task_t, task);
135         } else {
136           /* one of required parameters is not given */
137           schedule_success = false;
138           XBT_VERB("The schedule is ignored, task '%s' can not be scheduled on %d hosts", task->name, performer);
139         }
140       }
141     } else {
142       XBT_WARN("Task '%s' is defined more than once", name);
143     }
144   }
145
146   /*Check if 'root' and 'end' nodes have been explicitly declared.  If not, create them. */
147   if (!(root = (SD_task_t)xbt_dict_get_or_null(jobs, "root")))
148     root = (seq_or_par == sequential?SD_task_create_comp_seq("root", NULL, 0):
149                                      SD_task_create_comp_par_amdahl("root", NULL, 0, 0));
150
151   SD_task_set_state(root, SD_SCHEDULABLE);   /* by design the root task is always SCHEDULABLE */
152   xbt_dynar_insert_at(result, 0, &root);     /* Put it at the beginning of the dynar */
153
154   if (!(end = (SD_task_t)xbt_dict_get_or_null(jobs, "end")))
155     end = (seq_or_par == sequential?SD_task_create_comp_seq("end", NULL, 0):
156                                     SD_task_create_comp_par_amdahl("end", NULL, 0, 0));
157
158   /* Create edges */
159   xbt_dynar_t edges = xbt_dynar_new(sizeof(Agedge_t*), NULL);
160   for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
161     Agedge_t * edge;
162     xbt_dynar_reset(edges);
163     for (edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge))
164       xbt_dynar_push_as(edges, Agedge_t *, edge);
165
166     /* Be sure edges are sorted */
167     xbt_dynar_sort(edges, edge_compare);
168
169     xbt_dynar_foreach(edges, i, edge) {
170       SD_task_t src, dst;
171       char *src_name=agnameof(agtail(edge)), *dst_name=agnameof(aghead(edge));
172       double size = atof(agget(edge, (char *) "size"));
173
174       src = (SD_task_t)xbt_dict_get_or_null(jobs, src_name);
175       dst = (SD_task_t)xbt_dict_get_or_null(jobs, dst_name);
176
177       if (size > 0) {
178         char *name = (char*)xbt_malloc((strlen(src_name)+strlen(dst_name)+6)*sizeof(char));
179         sprintf(name, "%s->%s", src_name, dst_name);
180         XBT_DEBUG("See <transfer id=%s amount = %.0f>", name, size);
181         if (!(task = (SD_task_t)xbt_dict_get_or_null(jobs, name))) {
182           if (seq_or_par == sequential)
183             task = SD_task_create_comm_e2e(name, NULL , size);
184           else
185             task = SD_task_create_comm_par_mxn_1d_block(name, NULL , size);
186           SD_task_dependency_add(NULL, NULL, src, task);
187           SD_task_dependency_add(NULL, NULL, task, dst);
188           xbt_dict_set(jobs, name, task, NULL);
189           xbt_dynar_push(result, &task);
190         } else {
191           XBT_WARN("Task '%s' is defined more than once", name);
192         }
193         xbt_free(name);
194       } else {
195         SD_task_dependency_add(NULL, NULL, src, dst);
196       }
197     }
198   }
199   xbt_dynar_free(&edges);
200
201   XBT_DEBUG("All tasks have been created, put %s at the end of the dynar", end->name);
202   xbt_dynar_push(result, &end);
203
204   /* Connect entry tasks to 'root', and exit tasks to 'end'*/
205   xbt_dynar_foreach (result, i, task){
206     if (xbt_dynar_is_empty(task->tasks_before) && task != root) {
207       XBT_DEBUG("Task '%s' has no source. Add dependency from 'root'", task->name);
208       SD_task_dependency_add(NULL, NULL, root, task);
209     }
210
211     if (xbt_dynar_is_empty(task->tasks_after) && task != end) {
212       XBT_DEBUG("Task '%s' has no destination. Add dependency to 'end'", task->name);
213       SD_task_dependency_add(NULL, NULL, task, end);
214     }
215   }
216
217   agclose(dag_dot);
218   xbt_dict_free(&jobs);
219   fclose(in_file);
220
221   if(schedule){
222     char *computer_name;
223     if (schedule_success) {
224       const sg_host_t *workstations = sg_host_list ();
225       xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
226         SD_task_t previous_task = NULL;
227         xbt_dynar_foreach(computer, i, task){
228           /* add dependency between the previous and the task to avoid parallel execution */
229           if(task){
230             if(previous_task && !SD_task_dependency_exists(previous_task, task))
231               SD_task_dependency_add(NULL, NULL, previous_task, task);
232
233             SD_task_schedulel(task, 1, workstations[atoi(computer_name)]);
234             previous_task = task;
235           }
236         }
237         xbt_dynar_free(&computer);
238       }
239       xbt_dict_free(&computers);
240     } else {
241       XBT_WARN("The scheduling is ignored");
242       xbt_dict_foreach(computers,dict_cursor,computer_name,computer)
243         xbt_dynar_free(&computer);
244       xbt_dict_free(&computers);
245       xbt_dynar_free(&result);
246       result = NULL;
247     }
248   }
249
250   if (result && !acyclic_graph_detail(result)) {
251     XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.", basename((char*)filename));
252     xbt_dynar_free(&result);
253     result = NULL;
254   }
255   return result;
256 }