Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
first attempt to a DAG loader for s4u. To be improved and polished before release
[simgrid.git] / src / simdag / sd_dotloader.cpp
1 /* Copyright (c) 2009-2021. 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/Activity.hpp"
9 #include "simgrid/s4u/Comm.hpp"
10 #include "simgrid/s4u/Engine.hpp"
11 #include "simgrid/s4u/Exec.hpp"
12 #include "simgrid/simdag.h"
13 #include "src/internal_config.h"
14 #include "xbt/file.hpp"
15 #include <algorithm>
16 #include <cstring>
17 #include <unordered_map>
18 #include <vector>
19
20 #if HAVE_GRAPHVIZ
21 #include <graphviz/cgraph.h>
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_dotparse, sd, "Parsing DOT files");
24
25 namespace simgrid {
26 namespace s4u {
27
28 std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename)
29 {
30   FILE* in_file = fopen(filename.c_str(), "r");
31   xbt_assert(in_file != nullptr, "Failed to open file: %s", filename.c_str());
32
33   Agraph_t* dag_dot = agread(in_file, NIL(Agdisc_t*));
34
35   std::unordered_map<std::string, ActivityPtr> activities;
36   std::vector<ActivityPtr> dag;
37
38   ActivityPtr root;
39   ActivityPtr end;
40   ActivityPtr act;
41   /* Create all the nodes */
42   Agnode_t* node = nullptr;
43   for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
44     char* name    = agnameof(node);
45     double amount = atof(agget(node, (char*)"size"));
46
47     if (activities.find(name) == activities.end()) {
48       XBT_DEBUG("See <Exec id = %s amount = %.0f>", name, amount);
49       act = Exec::init()->set_name(name)->set_flops_amount(amount)->vetoable_start();
50       activities.insert({std::string(name), act});
51       if (strcmp(name, "root") && strcmp(name, "end"))
52         dag.push_back(act);
53     } else {
54       XBT_WARN("Exec '%s' is defined more than once", name);
55     }
56   }
57   /*Check if 'root' and 'end' nodes have been explicitly declared.  If not, create them. */
58   if (activities.find("root") == activities.end())
59     root = Exec::init()->set_name("root")->set_flops_amount(0)->vetoable_start();
60   else
61     root = activities.at("root");
62
63   if (activities.find("end") == activities.end())
64     end = Exec::init()->set_name("end")->set_flops_amount(0)->vetoable_start();
65   else
66     end = activities.at("end");
67
68   /* Create edges */
69   std::vector<Agedge_t*> edges;
70   for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
71     edges.clear();
72     for (Agedge_t* edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge))
73       edges.push_back(edge);
74
75     /* Be sure edges are sorted */
76     std::sort(edges.begin(), edges.end(), [](const Agedge_t* a, const Agedge_t* b) { return AGSEQ(a) < AGSEQ(b); });
77
78     for (Agedge_t* edge : edges) {
79       const char* src_name = agnameof(agtail(edge));
80       const char* dst_name = agnameof(aghead(edge));
81       double size          = atof(agget(edge, (char*)"size"));
82
83       ActivityPtr src = activities.at(src_name);
84       ActivityPtr dst = activities.at(dst_name);
85       if (size > 0) {
86         std::string name = std::string(src_name) + "->" + dst_name;
87         XBT_DEBUG("See <Comm id=%s amount = %.0f>", name.c_str(), size);
88         if (activities.find(name) == activities.end()) {
89           act = Comm::sendto_init()->set_name(name)->set_payload_size(size)->vetoable_start();
90           src->add_successor(act);
91           act->add_successor(dst);
92           activities.insert({name, act});
93           dag.push_back(act);
94         } else {
95           XBT_WARN("Comm '%s' is defined more than once", name.c_str());
96         }
97       } else {
98         src->add_successor(dst);
99       }
100     }
101   }
102
103   XBT_DEBUG("All activities have been created, put %s at the beginning and %s at the end", root->get_cname(),
104             end->get_cname());
105   dag.insert(dag.begin(), root);
106   dag.push_back(end);
107
108   /* Connect entry tasks to 'root', and exit tasks to 'end'*/
109   for (const auto& a : dag) {
110     if (a->dependencies_solved() && a != root) {
111       XBT_DEBUG("Activity '%s' has no dependencies. Add dependency from 'root'", a->get_cname());
112       root->add_successor(a);
113     }
114
115     if (a->is_waited_by() == 0 && a != end) {
116       XBT_DEBUG("Activity '%s' has no successors. Add dependency to 'end'", a->get_cname());
117       a->add_successor(end);
118     }
119   }
120   agclose(dag_dot);
121   fclose(in_file);
122
123   return dag;
124 }
125
126 } // namespace s4u
127 } // namespace simgrid
128
129 xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool schedule);
130
131 static void dot_task_p_free(void *task) {
132   (*(SD_task_t*)task)->destroy();
133 }
134
135 /** @brief loads a DOT file describing a DAG
136  *
137  * See http://www.graphviz.org/doc/info/lang.html  for more details.
138  * The size attribute of a node describes:
139  *   - for a compute task: the amount of flops to execute
140  *   - for a communication task : the amount of bytes to transfer
141  * If this attribute is omitted, the default value is zero.
142  */
143 xbt_dynar_t SD_dotload(const char *filename) {
144   return SD_dotload_generic(filename, true, false);
145 }
146
147 xbt_dynar_t SD_PTG_dotload(const char * filename) {
148   return SD_dotload_generic(filename, false, false);
149 }
150
151 xbt_dynar_t SD_dotload_with_sched(const char *filename) {
152   return SD_dotload_generic(filename, true, true);
153 }
154
155 xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool schedule)
156 {
157   xbt_assert(filename, "Unable to use a null file descriptor\n");
158   FILE *in_file = fopen(filename, "r");
159   xbt_assert(in_file != nullptr, "Failed to open file: %s", filename);
160
161   SD_task_t root;
162   SD_task_t end;
163   SD_task_t task;
164   std::vector<SD_task_t>* computer;
165   std::unordered_map<std::string, std::vector<SD_task_t>*> computers;
166   bool schedule_success = true;
167
168   std::unordered_map<std::string, SD_task_t> jobs;
169   xbt_dynar_t result = xbt_dynar_new(sizeof(SD_task_t), dot_task_p_free);
170
171   Agraph_t * dag_dot = agread(in_file, NIL(Agdisc_t *));
172
173   /* Create all the nodes */
174   Agnode_t *node = nullptr;
175   for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
176     char *name = agnameof(node);
177     double amount = atof(agget(node, (char*)"size"));
178     if (jobs.find(name) == jobs.end()) {
179       if (sequential) {
180         XBT_DEBUG("See <job id=%s amount =%.0f>", name, amount);
181         task = simgrid::sd::Task::create_comp_seq(name, amount, nullptr);
182       } else {
183         double alpha = atof(agget(node, (char *) "alpha"));
184         XBT_DEBUG("See <job id=%s amount =%.0f alpha = %.3f>", name, amount, alpha);
185         task = simgrid::sd::Task::create_comp_par_amdahl(name, amount, nullptr, alpha);
186       }
187
188       jobs.insert({std::string(name), task});
189
190       if (strcmp(name,"root") && strcmp(name,"end"))
191         xbt_dynar_push(result, &task);
192
193       if (sequential && ((schedule && schedule_success) || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose))) {
194         /* try to take the information to schedule the task only if all is right*/
195         char *char_performer = agget(node, (char *) "performer");
196         char *char_order = agget(node, (char *) "order");
197         /* Tasks will execute on in a given "order" on a given set of "performer" hosts */
198         int performer = ((not char_performer || not strcmp(char_performer, "")) ? -1 : atoi(char_performer));
199         int order     = ((not char_order || not strcmp(char_order, "")) ? -1 : atoi(char_order));
200
201         if ((performer != -1 && order != -1) && performer < static_cast<int>(sg_host_count())) {
202           /* required parameters are given and less performers than hosts are required */
203           XBT_DEBUG("Task '%s' is scheduled on workstation '%d' in position '%d'", task->get_cname(), performer, order);
204           auto comp = computers.find(char_performer);
205           if (comp != computers.end()) {
206             computer = comp->second;
207           } else {
208             computer = new std::vector<SD_task_t>();
209             computers.insert({char_performer, computer});
210           }
211           if (static_cast<unsigned int>(order) < computer->size()) {
212             const_SD_task_t task_test = computer->at(order);
213             if (task_test && task_test != task) {
214               /* the user gave the same order to several tasks */
215               schedule_success = false;
216               XBT_VERB("Task '%s' wants to start on performer '%s' at the same position '%s' as task '%s'",
217                        task_test->get_cname(), char_performer, char_order, task->get_cname());
218               continue;
219             }
220           } else
221             computer->resize(order);
222
223           computer->insert(computer->begin() + order, task);
224         } else {
225           /* one of required parameters is not given */
226           schedule_success = false;
227           XBT_VERB("The schedule is ignored, task '%s' can not be scheduled on %d hosts", task->get_cname(), performer);
228         }
229       }
230     } else {
231       XBT_WARN("Task '%s' is defined more than once", name);
232     }
233   }
234
235   /*Check if 'root' and 'end' nodes have been explicitly declared.  If not, create them. */
236   if (jobs.find("root") == jobs.end())
237     root = (sequential ? simgrid::sd::Task::create_comp_seq("root", 0, nullptr)
238                        : simgrid::sd::Task::create_comp_par_amdahl("root", 0, nullptr, 0));
239   else
240     root = jobs.at("root");
241
242   root->set_state(SD_SCHEDULABLE);           /* by design the root task is always SCHEDULABLE */
243   xbt_dynar_insert_at(result, 0, &root);     /* Put it at the beginning of the dynar */
244
245   if (jobs.find("end") == jobs.end())
246     end = (sequential ? simgrid::sd::Task::create_comp_seq("end", 0, nullptr)
247                       : simgrid::sd::Task::create_comp_par_amdahl("end", 0, nullptr, 0));
248   else
249     end = jobs.at("end");
250
251   /* Create edges */
252   std::vector<Agedge_t*> edges;
253   for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
254     edges.clear();
255     for (Agedge_t* edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge))
256       edges.push_back(edge);
257
258     /* Be sure edges are sorted */
259     std::sort(edges.begin(), edges.end(), [](const Agedge_t* a, const Agedge_t* b) { return AGSEQ(a) < AGSEQ(b); });
260
261     for (Agedge_t* edge : edges) {
262       const char* src_name = agnameof(agtail(edge));
263       const char* dst_name = agnameof(aghead(edge));
264       double size = atof(agget(edge, (char *) "size"));
265
266       SD_task_t src = jobs.at(src_name);
267       SD_task_t dst = jobs.at(dst_name);
268
269       if (size > 0) {
270         std::string name = std::string(src_name) + "->" + dst_name;
271         XBT_DEBUG("See <transfer id=%s amount = %.0f>", name.c_str(), size);
272         if (jobs.find(name) == jobs.end()) {
273           if (sequential)
274             task = simgrid::sd::Task::create_comm_e2e(name.c_str(), size, nullptr);
275           else
276             task = simgrid::sd::Task::create_comm_par_mxn_1d_block(name.c_str(), size, nullptr);
277           SD_task_dependency_add(src, task);
278           SD_task_dependency_add(task, dst);
279           jobs.insert({name, task});
280           xbt_dynar_push(result, &task);
281         } else {
282           XBT_WARN("Task '%s' is defined more than once", name.c_str());
283         }
284       } else {
285         SD_task_dependency_add(src, dst);
286       }
287     }
288   }
289
290   XBT_DEBUG("All tasks have been created, put %s at the end of the dynar", end->get_cname());
291   xbt_dynar_push(result, &end);
292
293   /* Connect entry tasks to 'root', and exit tasks to 'end'*/
294   unsigned i;
295   xbt_dynar_foreach (result, i, task){
296     if (task->has_unsolved_dependencies() == 0 && task != root) {
297       XBT_DEBUG("Task '%s' has no source. Add dependency from 'root'", task->get_cname());
298       SD_task_dependency_add(root, task);
299     }
300
301     if (task->is_waited_by() == 0 && task != end) {
302       XBT_DEBUG("Task '%s' has no destination. Add dependency to 'end'", task->get_cname());
303       SD_task_dependency_add(task, end);
304     }
305   }
306
307   agclose(dag_dot);
308   fclose(in_file);
309
310   if(schedule){
311     if (schedule_success) {
312       std::vector<simgrid::s4u::Host*> hosts = simgrid::s4u::Engine::get_instance()->get_all_hosts();
313
314       for (auto const& elm : computers) {
315         SD_task_t previous_task = nullptr;
316         for (auto const& cur_task : *elm.second) {
317           /* add dependency between the previous and the task to avoid parallel execution */
318           if (cur_task) {
319             if (previous_task && not SD_task_dependency_exists(previous_task, cur_task))
320               SD_task_dependency_add(previous_task, cur_task);
321
322             cur_task->schedulev({hosts[std::stoi(elm.first)]});
323             previous_task = cur_task;
324           }
325         }
326         delete elm.second;
327       }
328     } else {
329       XBT_WARN("The scheduling is ignored");
330       for (auto const& elm : computers)
331         delete elm.second;
332       xbt_dynar_free(&result);
333       result = nullptr;
334     }
335   }
336
337   if (result && not acyclic_graph_detail(result)) {
338     std::string base = simgrid::xbt::Path(filename).get_base_name();
339     XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.", base.c_str());
340     xbt_dynar_free(&result);
341     result = nullptr;
342   }
343   return result;
344 }
345 #else
346 namespace simgrid {
347 namespace s4u {
348 std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename)
349 {
350   xbt_die("create_DAG_from_dot() is not usable because graphviz was not found.\n"
351           "Please install graphviz, graphviz-dev, and libgraphviz-dev (and erase CMakeCache.txt) before recompiling.");
352 }
353 // namespace s4u
354 // namespace simgrid
355
356 xbt_dynar_t SD_dotload(const char *filename) {
357   xbt_die("SD_dotload_generic() is not usable because graphviz was not found.\n"
358       "Please install graphviz, graphviz-dev, and libgraphviz-dev (and erase CMakeCache.txt) before recompiling.");
359 }
360 xbt_dynar_t SD_dotload_with_sched(const char *filename) {
361   return SD_dotload(filename);
362 }
363 xbt_dynar_t SD_PTG_dotload(const char * filename) {
364   return SD_dotload(filename);
365 }
366 #endif