1 /* Copyright (c) 2009-2016. 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 "src/internal_config.h"
8 #include "simgrid/simdag.h"
11 #include "simdag_private.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_dotparse, sd, "Parsing DOT files");
16 #include <graphviz/cgraph.h>
24 xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par, bool schedule);
26 static void dot_task_p_free(void *task) {
27 SD_task_destroy(*(SD_task_t *)task);
30 /** @brief loads a DOT file describing a DAG
32 * See http://www.graphviz.org/doc/info/lang.html for more details.
33 * The size attribute of a node describes:
34 * - for a compute task: the amount of flops to execute
35 * - for a communication task : the amount of bytes to transfer
36 * If this attribute is ommited, the default value is zero.
38 xbt_dynar_t SD_dotload(const char *filename) {
39 return SD_dotload_generic(filename, sequential, false);
42 xbt_dynar_t SD_PTG_dotload(const char * filename) {
43 return SD_dotload_generic(filename, parallel, false);
46 xbt_dynar_t SD_dotload_with_sched(const char *filename) {
47 return SD_dotload_generic(filename, sequential, true);
50 static int edge_compare(const void *a, const void *b)
52 unsigned va = AGSEQ(*(Agedge_t **)a);
53 unsigned vb = AGSEQ(*(Agedge_t **)b);
54 return va == vb ? 0 : (va < vb ? -1 : 1);
57 xbt_dynar_t SD_dotload_generic(const char * filename, seq_par_t seq_or_par, bool schedule){
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);
67 xbt_dynar_t computer = nullptr;
68 xbt_dict_cursor_t dict_cursor;
69 bool schedule_success = true;
71 xbt_dict_t jobs = xbt_dict_new_homogeneous(nullptr);
72 xbt_dynar_t result = xbt_dynar_new(sizeof(SD_task_t), dot_task_p_free);
74 Agraph_t * dag_dot = agread(in_file, NIL(Agdisc_t *));
77 computers = xbt_dict_new_homogeneous(nullptr);
79 /* Create all the nodes */
80 Agnode_t *node = nullptr;
81 for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
82 char *name = agnameof(node);
83 double amount = atof(agget(node, (char*)"size"));
84 task = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, name));
85 if (task == nullptr) {
86 if (seq_or_par == sequential){
87 XBT_DEBUG("See <job id=%s amount =%.0f>", name, amount);
88 task = SD_task_create_comp_seq(name, nullptr , amount);
90 double alpha = atof(agget(node, (char *) "alpha"));
91 XBT_DEBUG("See <job id=%s amount =%.0f alpha = %.3f>", name, amount, alpha);
92 task = SD_task_create_comp_par_amdahl(name, nullptr , amount, alpha);
95 xbt_dict_set(jobs, name, task, nullptr);
97 if (strcmp(name,"root") && strcmp(name,"end"))
98 xbt_dynar_push(result, &task);
100 if((seq_or_par == sequential) &&
101 ((schedule && schedule_success) || XBT_LOG_ISENABLED(sd_dotparse, xbt_log_priority_verbose))){
102 /* try to take the information to schedule the task only if all is right*/
103 char *char_performer = agget(node, (char *) "performer");
104 char *char_order = agget(node, (char *) "order");
105 /* Tasks will execute on in a given "order" on a given set of "performer" hosts */
106 int performer = ((!char_performer || !strcmp(char_performer,"")) ? -1:atoi(char_performer));
107 int order = ((!char_order || !strcmp(char_order, ""))? -1:atoi(char_order));
109 if((performer != -1 && order != -1) && performer < (int) sg_host_count()){
110 /* required parameters are given and less performers than hosts are required */
111 XBT_DEBUG ("Task '%s' is scheduled on workstation '%d' in position '%d'", task->name, performer, order);
112 computer = static_cast<xbt_dynar_t> (xbt_dict_get_or_null(computers, char_performer));
113 if(computer == nullptr){
114 computer = xbt_dynar_new(sizeof(SD_task_t), nullptr);
115 xbt_dict_set(computers, char_performer, computer, nullptr);
118 if(static_cast<unsigned int>(order) < xbt_dynar_length(computer)){
119 SD_task_t *task_test = (SD_task_t *)xbt_dynar_get_ptr(computer,order);
120 if(*task_test && *task_test != task){
121 /* the user gave the same order to several tasks */
122 schedule_success = false;
123 XBT_VERB("Task '%s' wants to start on performer '%s' at the same position '%s' as task '%s'",
124 (*task_test)->name, char_performer, char_order, task->name);
128 /* the parameter seems to be ok */
129 xbt_dynar_set_as(computer, order, SD_task_t, task);
131 /* one of required parameters is not given */
132 schedule_success = false;
133 XBT_VERB("The schedule is ignored, task '%s' can not be scheduled on %d hosts", task->name, performer);
137 XBT_WARN("Task '%s' is defined more than once", name);
141 /*Check if 'root' and 'end' nodes have been explicitly declared. If not, create them. */
142 root = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, "root"));
144 root = (seq_or_par == sequential?SD_task_create_comp_seq("root", nullptr, 0):
145 SD_task_create_comp_par_amdahl("root", nullptr, 0, 0));
147 SD_task_set_state(root, SD_SCHEDULABLE); /* by design the root task is always SCHEDULABLE */
148 xbt_dynar_insert_at(result, 0, &root); /* Put it at the beginning of the dynar */
150 end = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, "end"));
152 end = (seq_or_par == sequential?SD_task_create_comp_seq("end", nullptr, 0):
153 SD_task_create_comp_par_amdahl("end", nullptr, 0, 0));
156 xbt_dynar_t edges = xbt_dynar_new(sizeof(Agedge_t*), nullptr);
157 for (node = agfstnode(dag_dot); node; node = agnxtnode(dag_dot, node)) {
159 xbt_dynar_reset(edges);
160 for (edge = agfstout(dag_dot, node); edge; edge = agnxtout(dag_dot, edge))
161 xbt_dynar_push_as(edges, Agedge_t *, edge);
163 /* Be sure edges are sorted */
164 xbt_dynar_sort(edges, edge_compare);
166 xbt_dynar_foreach(edges, i, edge) {
167 char *src_name=agnameof(agtail(edge));
168 char *dst_name=agnameof(aghead(edge));
169 double size = atof(agget(edge, (char *) "size"));
171 SD_task_t src = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, src_name));
172 SD_task_t dst = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, dst_name));
175 char *name = bprintf("%s->%s", src_name, dst_name);
176 XBT_DEBUG("See <transfer id=%s amount = %.0f>", name, size);
177 task = static_cast<SD_task_t>(xbt_dict_get_or_null(jobs, name));
178 if (task == nullptr) {
179 if (seq_or_par == sequential)
180 task = SD_task_create_comm_e2e(name, nullptr , size);
182 task = SD_task_create_comm_par_mxn_1d_block(name, nullptr , size);
183 SD_task_dependency_add(nullptr, nullptr, src, task);
184 SD_task_dependency_add(nullptr, nullptr, task, dst);
185 xbt_dict_set(jobs, name, task, nullptr);
186 xbt_dynar_push(result, &task);
188 XBT_WARN("Task '%s' is defined more than once", name);
192 SD_task_dependency_add(nullptr, nullptr, src, dst);
196 xbt_dynar_free(&edges);
198 XBT_DEBUG("All tasks have been created, put %s at the end of the dynar", end->name);
199 xbt_dynar_push(result, &end);
201 /* Connect entry tasks to 'root', and exit tasks to 'end'*/
202 xbt_dynar_foreach (result, i, task){
203 if (task->predecessors->empty() && task->inputs->empty() && task != root) {
204 XBT_DEBUG("Task '%s' has no source. Add dependency from 'root'", task->name);
205 SD_task_dependency_add(nullptr, nullptr, root, task);
208 if (task->successors->empty() && task->outputs->empty() && task != end) {
209 XBT_DEBUG("Task '%s' has no destination. Add dependency to 'end'", task->name);
210 SD_task_dependency_add(nullptr, nullptr, task, end);
215 xbt_dict_free(&jobs);
220 if (schedule_success) {
221 const sg_host_t *workstations = sg_host_list ();
222 xbt_dict_foreach(computers,dict_cursor,computer_name,computer){
223 SD_task_t previous_task = nullptr;
224 xbt_dynar_foreach(computer, i, task){
225 /* add dependency between the previous and the task to avoid parallel execution */
227 if(previous_task && !SD_task_dependency_exists(previous_task, task))
228 SD_task_dependency_add(nullptr, nullptr, previous_task, task);
230 SD_task_schedulel(task, 1, workstations[atoi(computer_name)]);
231 previous_task = task;
234 xbt_dynar_free(&computer);
236 xbt_dict_free(&computers);
238 XBT_WARN("The scheduling is ignored");
239 xbt_dict_foreach(computers,dict_cursor,computer_name,computer)
240 xbt_dynar_free(&computer);
241 xbt_dict_free(&computers);
242 xbt_dynar_free(&result);
247 if (result && !acyclic_graph_detail(result)) {
248 char* base = xbt_basename(filename);
249 XBT_ERROR("The DOT described in %s is not a DAG. It contains a cycle.", base);
251 xbt_dynar_free(&result);