Logo AND Algorithmique Numérique Distribuée

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