Logo AND Algorithmique Numérique Distribuée

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