Logo AND Algorithmique Numérique Distribuée

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