Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix build with graphviz >= 3.
[simgrid.git] / src / dag / loaders.cpp
1 /* Copyright (c) 2009-2022. 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/internal_config.h"
8 #include <algorithm>
9 #include <map>
10 #include <simgrid/s4u/Comm.hpp>
11 #include <simgrid/s4u/Engine.hpp>
12 #include <simgrid/s4u/Exec.hpp>
13 #include <stdexcept>
14 #include <xbt/asserts.h>
15 #include <xbt/file.hpp>
16 #include <xbt/log.h>
17 #include <xbt/misc.h>
18
19 #include "dax_dtd.h"
20 #include "dax_dtd.c"
21
22 #if HAVE_GRAPHVIZ
23 #include <graphviz/cgraph.h>
24 #endif
25
26 XBT_LOG_NEW_DEFAULT_CATEGORY(dag_parsing, "Generation DAGs from files");
27
28 /* Ensure that transfer tasks have unique names even though a file is used several times */
29 static void uniq_transfer_task_name(simgrid::s4u::Comm* comm)
30 {
31   const auto& child  = comm->get_successors().front();
32   const auto& parent = *(comm->get_dependencies().begin());
33
34   std::string new_name = parent->get_name() + "_" + comm->get_name() + "_" + child->get_name();
35
36   comm->set_name(new_name)->vetoable_start();
37 }
38
39 static bool check_for_cycle(const std::vector<simgrid::s4u::ActivityPtr>& dag)
40 {
41   std::vector<simgrid::s4u::ActivityPtr> current;
42
43   for (const auto& a : dag)
44     if (dynamic_cast<simgrid::s4u::Exec*>(a.get()) != nullptr && a->has_no_successor())
45       current.push_back(a);
46
47   while (not current.empty()) {
48     std::vector<simgrid::s4u::ActivityPtr> next;
49     for (auto const& a : current) {
50       a->mark();
51       for (auto const& pred : a->get_dependencies()) {
52         if (dynamic_cast<simgrid::s4u::Comm*>(pred.get()) != nullptr) {
53           pred->mark();
54           // Comms have only one predecessor
55           auto pred_pred = *(pred->get_dependencies().begin());
56           if (std::none_of(pred_pred->get_successors().begin(), pred_pred->get_successors().end(),
57                            [](const simgrid::s4u::ActivityPtr& act) { return not act->is_marked(); }))
58             next.push_back(pred_pred);
59         } else {
60           if (std::none_of(pred->get_successors().begin(), pred->get_successors().end(),
61                            [](const simgrid::s4u::ActivityPtr& act) { return not act->is_marked(); }))
62             next.push_back(pred);
63         }
64       }
65     }
66     current.clear();
67     current = next;
68   }
69
70   return not std::any_of(dag.begin(), dag.end(), [](const simgrid::s4u::ActivityPtr& a) { return not a->is_marked(); });
71 }
72
73 static YY_BUFFER_STATE input_buffer;
74
75 namespace simgrid::s4u {
76
77 static std::vector<ActivityPtr> result;
78 static std::map<std::string, ExecPtr, std::less<>> jobs;
79 static std::map<std::string, Comm*, std::less<>> files;
80 static ExecPtr current_job;
81
82 /** @brief loads a DAX file describing a DAG
83  *
84  * See https://confluence.pegasus.isi.edu/display/pegasus/WorkflowGenerator for more details.
85  */
86 std::vector<ActivityPtr> create_DAG_from_DAX(const std::string& filename)
87 {
88   FILE* in_file = fopen(filename.c_str(), "r");
89   xbt_assert(in_file, "Unable to open \"%s\"\n", filename.c_str());
90   input_buffer = dax__create_buffer(in_file, 10);
91   dax__switch_to_buffer(input_buffer);
92   dax_lineno = 1;
93
94   auto root_task = Exec::init()->set_name("root")->set_flops_amount(0);
95   root_task->vetoable_start();
96
97   result.push_back(root_task);
98
99   auto end_task = Exec::init()->set_name("end")->set_flops_amount(0);
100   end_task->vetoable_start();
101
102   xbt_assert(dax_lex() == 0, "Parse error in %s: %s", filename.c_str(), dax__parse_err_msg());
103   dax__delete_buffer(input_buffer);
104   fclose(in_file);
105   dax_lex_destroy();
106
107   /* And now, post-process the files.
108    * We want a file task per pair of computation tasks exchanging the file. Duplicate on need
109    * Files not produced in the system are said to be produced by root task (top of DAG).
110    * Files not consumed in the system are said to be consumed by end task (bottom of DAG).
111    */
112   for (auto const& [_, elm] : files) {
113     CommPtr file = elm;
114     CommPtr newfile;
115     if (file->dependencies_solved()) {
116       for (auto const& it : file->get_successors()) {
117         newfile = Comm::sendto_init()->set_name(file->get_name())->set_payload_size(file->get_remaining());
118         root_task->add_successor(newfile);
119         newfile->add_successor(it);
120         result.push_back(newfile);
121       }
122     }
123     if (file->has_no_successor()) {
124       for (auto const& it : file->get_dependencies()) {
125         newfile = Comm::sendto_init()->set_name(file->get_name())->set_payload_size(file->get_remaining());
126         it->add_successor(newfile);
127         newfile->add_successor(end_task);
128         result.push_back(newfile);
129       }
130     }
131     for (auto const& it : file->get_dependencies()) {
132       for (auto const& it2 : file->get_successors()) {
133         if (it == it2) {
134           XBT_WARN("File %s is produced and consumed by task %s."
135                    "This loop dependency will prevent the execution of the task.",
136                    file->get_cname(), it->get_cname());
137         }
138         newfile = Comm::sendto_init()->set_name(file->get_name())->set_payload_size(file->get_remaining());
139         it->add_successor(newfile);
140         newfile->add_successor(it2);
141         result.push_back(newfile);
142       }
143     }
144     /* Free previous copy of the files */
145     file->destroy();
146   }
147
148   /* Push end task last */
149   result.push_back(end_task);
150
151   for (const auto& a : result) {
152     auto* comm = dynamic_cast<Comm*>(a.get());
153     if (comm != nullptr) {
154       uniq_transfer_task_name(comm);
155     } else {
156       /* If some tasks do not take files as input, connect them to the root
157        * if they don't produce files, connect them to the end node.
158        */
159       if ((a != root_task) && (a != end_task)) {
160         if (a->dependencies_solved())
161           root_task->add_successor(a);
162         if (a->has_no_successor())
163           a->add_successor(end_task);
164       }
165     }
166   }
167
168   if (not check_for_cycle(result)) {
169     XBT_ERROR("The DAX described in %s is not a DAG. It contains a cycle.",
170               simgrid::xbt::Path(filename).get_base_name().c_str());
171     for (const auto& a : result)
172       a->destroy();
173     result.clear();
174   }
175
176   return result;
177 }
178
179 #if HAVE_GRAPHVIZ
180 std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename)
181 {
182   FILE* in_file = fopen(filename.c_str(), "r");
183   xbt_assert(in_file != nullptr, "Failed to open file: %s", filename.c_str());
184
185   Agraph_t* dag_dot = agread(in_file, NULL);
186
187   std::unordered_map<std::string, ActivityPtr> activities;
188   std::vector<ActivityPtr> dag;
189
190   ActivityPtr root;
191   ActivityPtr end;
192   ActivityPtr act;
193   /* Create all the nodes */
194   Agnode_t* node = nullptr;
195   for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
196     const std::string name = agnameof(node);
197     double amount = atof(agget(node, (char*)"size"));
198
199     if (activities.find(name) == activities.end()) {
200       XBT_DEBUG("See <Exec id = %s amount = %.0f>", name.c_str(), amount);
201       act = Exec::init()->set_name(name)->set_flops_amount(amount)->vetoable_start();
202       activities.try_emplace(name, act);
203       if (name != "root" && name != "end")
204         dag.push_back(act);
205     } else {
206       XBT_WARN("Exec '%s' is defined more than once", name.c_str());
207     }
208   }
209   /*Check if 'root' and 'end' nodes have been explicitly declared.  If not, create them. */
210   if (activities.find("root") == activities.end())
211     root = Exec::init()->set_name("root")->set_flops_amount(0)->vetoable_start();
212   else
213     root = activities.at("root");
214
215   if (activities.find("end") == activities.end())
216     end = Exec::init()->set_name("end")->set_flops_amount(0)->vetoable_start();
217   else
218     end = activities.at("end");
219
220   /* Create edges */
221   std::vector<Agedge_t*> edges;
222   for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
223     edges.clear();
224     for (Agedge_t* edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge))
225       edges.push_back(edge);
226
227     /* Be sure edges are sorted */
228     std::sort(edges.begin(), edges.end(), [](const Agedge_t* a, const Agedge_t* b) { return AGSEQ(a) < AGSEQ(b); });
229
230     for (Agedge_t* edge : edges) {
231       const char* src_name = agnameof(agtail(edge));
232       const char* dst_name = agnameof(aghead(edge));
233       double size          = atof(agget(edge, (char*)"size"));
234
235       ActivityPtr src = activities.at(src_name);
236       ActivityPtr dst = activities.at(dst_name);
237       if (size > 0) {
238         std::string name = std::string(src_name) + "->" + dst_name;
239         XBT_DEBUG("See <Comm id=%s amount = %.0f>", name.c_str(), size);
240         if (activities.find(name) == activities.end()) {
241           act = Comm::sendto_init()->set_name(name)->set_payload_size(size)->vetoable_start();
242           src->add_successor(act);
243           act->add_successor(dst);
244           activities.try_emplace(name, act);
245           dag.push_back(act);
246         } else {
247           XBT_WARN("Comm '%s' is defined more than once", name.c_str());
248         }
249       } else {
250         src->add_successor(dst);
251       }
252     }
253   }
254
255   XBT_DEBUG("All activities have been created, put %s at the beginning and %s at the end", root->get_cname(),
256             end->get_cname());
257   dag.insert(dag.begin(), root);
258   dag.push_back(end);
259
260   /* Connect entry tasks to 'root', and exit tasks to 'end'*/
261   for (const auto& a : dag) {
262     if (a->dependencies_solved() && a != root) {
263       XBT_DEBUG("Activity '%s' has no dependencies. Add dependency from 'root'", a->get_cname());
264       root->add_successor(a);
265     }
266
267     if (a->has_no_successor() && a != end) {
268       XBT_DEBUG("Activity '%s' has no successors. Add dependency to 'end'", a->get_cname());
269       a->add_successor(end);
270     }
271   }
272   agclose(dag_dot);
273   fclose(in_file);
274
275   if (not check_for_cycle(dag)) {
276     std::string base = simgrid::xbt::Path(filename).get_base_name();
277     XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.", base.c_str());
278     for (const auto& a : dag)
279       a->destroy();
280     dag.clear();
281     dag.shrink_to_fit();
282   }
283
284   return dag;
285 }
286 #else
287 std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename)
288 {
289   xbt_die("create_DAG_from_dot() is not usable because graphviz was not found.\n"
290           "Please install graphviz, graphviz-dev, and libgraphviz-dev (and erase CMakeCache.txt) before recompiling.");
291 }
292 #endif
293 } // namespace simgrid::s4u
294
295 void STag_dax__adag()
296 {
297   try {
298     double version = std::stod(std::string(A_dax__adag_version));
299     xbt_assert(version == 2.1, "Expected version 2.1 in <adag> tag, got %f. Fix the parser or your file", version);
300   } catch (const std::invalid_argument&) {
301     throw std::invalid_argument(std::string("Parse error: ") + A_dax__adag_version + " is not a double");
302   }
303 }
304
305 void STag_dax__job()
306 {
307   try {
308     double runtime = std::stod(std::string(A_dax__job_runtime));
309
310     std::string name = std::string(A_dax__job_id) + "@" + A_dax__job_name;
311     runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
312     XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
313     simgrid::s4u::current_job = simgrid::s4u::Exec::init()->set_name(name)->set_flops_amount(runtime)->vetoable_start();
314     simgrid::s4u::jobs.try_emplace(A_dax__job_id, simgrid::s4u::current_job);
315     simgrid::s4u::result.push_back(simgrid::s4u::current_job);
316   } catch (const std::invalid_argument&) {
317     throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
318   }
319 }
320
321 void STag_dax__uses()
322 {
323   double size;
324   try {
325     size = std::stod(std::string(A_dax__uses_size));
326   } catch (const std::invalid_argument&) {
327     throw std::invalid_argument(std::string("Parse error: ") + A_dax__uses_size + " is not a double");
328   }
329   bool is_input = (A_dax__uses_link == A_dax__uses_link_input);
330
331   XBT_DEBUG("See <uses file=%s %s>", A_dax__uses_file, (is_input ? "in" : "out"));
332   auto it = simgrid::s4u::files.find(A_dax__uses_file);
333   simgrid::s4u::CommPtr file;
334   if (it == simgrid::s4u::files.end()) {
335     file = simgrid::s4u::Comm::sendto_init()->set_name(A_dax__uses_file)->set_payload_size(size);
336     simgrid::s4u::files[A_dax__uses_file] = file.get();
337   } else {
338     file = it->second;
339     if (file->get_remaining() < size || file->get_remaining() > size) {
340       XBT_WARN("Ignore file %s size redefinition from %.0f to %.0f", A_dax__uses_file, file->get_remaining(), size);
341     }
342   }
343   if (is_input) {
344     file->add_successor(simgrid::s4u::current_job);
345   } else {
346     simgrid::s4u::current_job->add_successor(file);
347     if (file->get_dependencies().size() > 1) {
348       XBT_WARN("File %s created at more than one location...", file->get_cname());
349     }
350   }
351 }
352
353 static simgrid::s4u::ExecPtr current_child;
354 void STag_dax__child()
355 {
356   auto job = simgrid::s4u::jobs.find(A_dax__child_ref);
357   if (job != simgrid::s4u::jobs.end()) {
358     current_child = job->second;
359   } else {
360     throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
361                             ": Asked to add dependencies to the non-existent " + A_dax__child_ref + "task");
362   }
363 }
364
365 void ETag_dax__child()
366 {
367   current_child = nullptr;
368 }
369
370 void STag_dax__parent()
371 {
372   auto job = simgrid::s4u::jobs.find(A_dax__parent_ref);
373   if (job != simgrid::s4u::jobs.end()) {
374     auto parent = job->second;
375     parent->add_successor(current_child);
376     XBT_DEBUG("Control-flow dependency from %s to %s", current_child->get_cname(), parent->get_cname());
377   } else {
378     throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) +
379                             ": Asked to add a dependency from " + current_child->get_name() + " to " +
380                             A_dax__parent_ref + ", but " + A_dax__parent_ref + " does not exist");
381   }
382 }
383
384 void ETag_dax__adag()
385 {
386   XBT_DEBUG("See </adag>");
387 }
388
389 void ETag_dax__job()
390 {
391   simgrid::s4u::current_job = nullptr;
392   XBT_DEBUG("See </job>");
393 }
394
395 void ETag_dax__parent()
396 {
397   XBT_DEBUG("See </parent>");
398 }
399
400 void ETag_dax__uses()
401 {
402   XBT_DEBUG("See </uses>");
403 }