1 /* Copyright (c) 2009-2021. The SimGrid Team.
2 * All rights reserved. */
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. */
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"
17 #include <unordered_map>
21 #include <graphviz/cgraph.h>
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_dotparse, sd, "Parsing DOT files");
28 std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename)
30 FILE* in_file = fopen(filename.c_str(), "r");
31 xbt_assert(in_file != nullptr, "Failed to open file: %s", filename.c_str());
33 Agraph_t* dag_dot = agread(in_file, NIL(Agdisc_t*));
35 std::unordered_map<std::string, ActivityPtr> activities;
36 std::vector<ActivityPtr> dag;
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"));
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"))
54 XBT_WARN("Exec '%s' is defined more than once", name);
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();
61 root = activities.at("root");
63 if (activities.find("end") == activities.end())
64 end = Exec::init()->set_name("end")->set_flops_amount(0)->vetoable_start();
66 end = activities.at("end");
69 std::vector<Agedge_t*> edges;
70 for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
72 for (Agedge_t* edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge))
73 edges.push_back(edge);
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); });
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"));
83 ActivityPtr src = activities.at(src_name);
84 ActivityPtr dst = activities.at(dst_name);
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});
95 XBT_WARN("Comm '%s' is defined more than once", name.c_str());
98 src->add_successor(dst);
103 XBT_DEBUG("All activities have been created, put %s at the beginning and %s at the end", root->get_cname(),
105 dag.insert(dag.begin(), root);
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);
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);
127 } // namespace simgrid
129 xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool schedule);
131 static void dot_task_p_free(void *task) {
132 (*(SD_task_t*)task)->destroy();
135 /** @brief loads a DOT file describing a DAG
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.
143 xbt_dynar_t SD_dotload(const char *filename) {
144 return SD_dotload_generic(filename, true, false);
147 xbt_dynar_t SD_PTG_dotload(const char * filename) {
148 return SD_dotload_generic(filename, false, false);
151 xbt_dynar_t SD_dotload_with_sched(const char *filename) {
152 return SD_dotload_generic(filename, true, true);
155 xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool schedule)
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);
164 std::vector<SD_task_t>* computer;
165 std::unordered_map<std::string, std::vector<SD_task_t>*> computers;
166 bool schedule_success = true;
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);
171 Agraph_t * dag_dot = agread(in_file, NIL(Agdisc_t *));
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()) {
180 XBT_DEBUG("See <job id=%s amount =%.0f>", name, amount);
181 task = simgrid::sd::Task::create_comp_seq(name, amount, nullptr);
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);
188 jobs.insert({std::string(name), task});
190 if (strcmp(name,"root") && strcmp(name,"end"))
191 xbt_dynar_push(result, &task);
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));
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;
208 computer = new std::vector<SD_task_t>();
209 computers.insert({char_performer, computer});
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());
221 computer->resize(order);
223 computer->insert(computer->begin() + order, task);
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);
231 XBT_WARN("Task '%s' is defined more than once", name);
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));
240 root = jobs.at("root");
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 */
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));
249 end = jobs.at("end");
252 std::vector<Agedge_t*> edges;
253 for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
255 for (Agedge_t* edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge))
256 edges.push_back(edge);
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); });
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"));
266 SD_task_t src = jobs.at(src_name);
267 SD_task_t dst = jobs.at(dst_name);
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()) {
274 task = simgrid::sd::Task::create_comm_e2e(name.c_str(), size, nullptr);
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);
282 XBT_WARN("Task '%s' is defined more than once", name.c_str());
285 SD_task_dependency_add(src, dst);
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);
293 /* Connect entry tasks to 'root', and exit tasks to 'end'*/
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);
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);
311 if (schedule_success) {
312 std::vector<simgrid::s4u::Host*> hosts = simgrid::s4u::Engine::get_instance()->get_all_hosts();
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 */
319 if (previous_task && not SD_task_dependency_exists(previous_task, cur_task))
320 SD_task_dependency_add(previous_task, cur_task);
322 cur_task->schedulev({hosts[std::stoi(elm.first)]});
323 previous_task = cur_task;
329 XBT_WARN("The scheduling is ignored");
330 for (auto const& elm : computers)
332 xbt_dynar_free(&result);
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);
348 std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename)
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.");
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.");
360 xbt_dynar_t SD_dotload_with_sched(const char *filename) {
361 return SD_dotload(filename);
363 xbt_dynar_t SD_PTG_dotload(const char * filename) {
364 return SD_dotload(filename);