Logo AND Algorithmique Numérique Distribuée

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